<?php
session_start();
include_once('../includes/config.php');
if (!isset($_SESSION['uid'])) {
    header('location:../logout.php');
    exit();
}

$currentUserId = $_SESSION['uid'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>Sales Reports Dashboard - UZRS Inventory</title>

    <!-- Custom fonts for this template-->
    <link href="../vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css">
    <link href="https://fonts.googleapis.com/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i" rel="stylesheet">

    <!-- Custom styles for this template-->
    <link href="../css/sb-admin-2.min.css" rel="stylesheet">
    <link href="css/sales-reports-dashboard.css" rel="stylesheet">
</head>

<body id="page-top">

    <!-- Page Wrapper -->
    <div id="wrapper">

        <?php include_once('../includes/sidebar.php'); ?>

        <!-- Content Wrapper -->
        <div id="content-wrapper" class="d-flex flex-column">

            <!-- Main Content -->
            <div id="content">

                <?php include_once('../includes/topbar.php'); ?>

                <!-- Begin Page Content -->
                <div class="container-fluid">

                    <!-- Page Heading -->
                    <div class="d-sm-flex align-items-center justify-content-between mb-2">
                        <h1 class="h5 mb-0 text-gray-800 font-weight-bold">
                            <i class="fas fa-chart-line text-success"></i> Sales Reports Dashboard
                        </h1>
                        <a href="../dashboard.php" class="d-none d-sm-inline-block btn btn-sm btn-primary shadow-sm" style="font-size: 0.75rem; padding: 0.25rem 0.75rem;">
                            <i class="fas fa-home fa-sm text-white-50"></i> Back to Home
                        </a>
                    </div>

                    <!-- Quick Filters Section -->
                    <div class="row mb-3">
                        <div class="col-12">
                            <div class="card shadow-sm border-left-primary">
                                <div class="card-header py-2 d-flex flex-row align-items-center justify-content-between" style="background: #f8f9fc;">
                                    <h6 class="m-0 font-weight-bold text-primary" style="font-size: 0.85rem;">
                                        <i class="fas fa-filter"></i> Quick Filters
                                    </h6>
                                    <button class="btn btn-sm btn-outline-primary" id="resetFilters" style="font-size: 0.7rem; padding: 0.25rem 0.5rem;">
                                        <i class="fas fa-redo"></i> Reset
                                    </button>
                                </div>
                                <div class="card-body" style="padding: 0.75rem;">
                                    <form id="quickFiltersForm" class="row g-2">
                                        <div class="col-md-3 mb-2">
                                            <label for="filterDateRange" class="form-label small font-weight-bold mb-1" style="font-size: 0.75rem;">Date Range</label>
                                            <select class="form-control form-control-sm" id="filterDateRange" style="font-size: 0.8rem; padding: 0.35rem 0.5rem;">
                                                <option value="">All Time</option>
                                                <option value="today">Today</option>
                                                <option value="yesterday">Yesterday</option>
                                                <option value="this_week">This Week</option>
                                                <option value="last_week">Last Week</option>
                                                <option value="this_month" selected>This Month</option>
                                                <option value="last_month">Last Month</option>
                                                <option value="this_year">This Year</option>
                                                <option value="custom">Custom Range</option>
                                            </select>
                                        </div>
                                        <div class="col-md-3 mb-2" id="customDateRangeContainer" style="display: none;">
                                            <label for="customDateFrom" class="form-label small font-weight-bold mb-1" style="font-size: 0.75rem;">From Date</label>
                                            <input type="date" class="form-control form-control-sm" id="customDateFrom" style="font-size: 0.8rem; padding: 0.35rem 0.5rem;">
                                        </div>
                                        <div class="col-md-3 mb-2" id="customDateToContainer" style="display: none;">
                                            <label for="customDateTo" class="form-label small font-weight-bold mb-1" style="font-size: 0.75rem;">To Date</label>
                                            <input type="date" class="form-control form-control-sm" id="customDateTo" style="font-size: 0.8rem; padding: 0.35rem 0.5rem;">
                                        </div>
                                        <div class="col-md-3 mb-2">
                                            <label for="filterSalesType" class="form-label small font-weight-bold mb-1" style="font-size: 0.75rem;">Sales Type</label>
                                            <select class="form-control form-control-sm" id="filterSalesType" style="font-size: 0.8rem; padding: 0.35rem 0.5rem;">
                                                <option value="">All Sales Types</option>
                                            </select>
                                        </div>
                                        <div class="col-md-3 mb-2">
                                            <label for="filterCustomer" class="form-label small font-weight-bold mb-1" style="font-size: 0.75rem;">Customer</label>
                                            <select class="form-control form-control-sm" id="filterCustomer" style="font-size: 0.8rem; padding: 0.35rem 0.5rem;">
                                                <option value="">All Customers</option>
                                            </select>
                                        </div>
                                        <div class="col-md-3 mb-2">
                                            <label for="filterCategory" class="form-label small font-weight-bold mb-1" style="font-size: 0.75rem;">Category</label>
                                            <select class="form-control form-control-sm" id="filterCategory" style="font-size: 0.8rem; padding: 0.35rem 0.5rem;">
                                                <option value="">All Categories</option>
                                            </select>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Summary Statistics -->
                    <div class="row" id="summaryStats">
                        <!-- Stats will be loaded here dynamically -->
                        <div class="col-xl-3 col-md-6 mb-4">
                            <div class="card border-left-primary shadow h-100 py-2">
                                <div class="card-body">
                                    <div class="row no-gutters align-items-center">
                                        <div class="col mr-2">
                                            <div class="text-xs font-weight-bold text-primary text-uppercase mb-1">
                                                Total Sales Amount
                                            </div>
                                            <div class="h5 mb-0 font-weight-bold text-gray-800" id="totalSalesAmount">
                                                <i class="fas fa-spinner fa-spin"></i>
                                            </div>
                                        </div>
                                        <div class="col-auto">
                                            <i class="fas fa-rupee-sign fa-2x text-gray-300"></i>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="col-xl-3 col-md-6 mb-4">
                            <div class="card border-left-success shadow h-100 py-2">
                                <div class="card-body">
                                    <div class="row no-gutters align-items-center">
                                        <div class="col mr-2">
                                            <div class="text-xs font-weight-bold text-success text-uppercase mb-1">
                                                Total Invoices
                                            </div>
                                            <div class="h5 mb-0 font-weight-bold text-gray-800" id="totalInvoices">
                                                <i class="fas fa-spinner fa-spin"></i>
                                            </div>
                                        </div>
                                        <div class="col-auto">
                                            <i class="fas fa-file-invoice fa-2x text-gray-300"></i>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="col-xl-3 col-md-6 mb-4">
                            <div class="card border-left-info shadow h-100 py-2">
                                <div class="card-body">
                                    <div class="row no-gutters align-items-center">
                                        <div class="col mr-2">
                                            <div class="text-xs font-weight-bold text-info text-uppercase mb-1">
                                                Total Items Sold
                                            </div>
                                            <div class="h5 mb-0 font-weight-bold text-gray-800" id="totalItemsSold">
                                                <i class="fas fa-spinner fa-spin"></i>
                                            </div>
                                        </div>
                                        <div class="col-auto">
                                            <i class="fas fa-box fa-2x text-gray-300"></i>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="col-xl-3 col-md-6 mb-4">
                            <div class="card border-left-warning shadow h-100 py-2">
                                <div class="card-body">
                                    <div class="row no-gutters align-items-center">
                                        <div class="col mr-2">
                                            <div class="text-xs font-weight-bold text-warning text-uppercase mb-1">
                                                Average Invoice Value
                                            </div>
                                            <div class="h5 mb-0 font-weight-bold text-gray-800" id="avgInvoiceValue">
                                                <i class="fas fa-spinner fa-spin"></i>
                                            </div>
                                        </div>
                                        <div class="col-auto">
                                            <i class="fas fa-calculator fa-2x text-gray-300"></i>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Reports Grid -->
                    <div class="row">
                        <!-- GST Sales Report -->
                        <div class="col-xl-4 col-lg-6 mb-4">
                            <div class="card report-card shadow h-100">
                                <div class="card-body text-center">
                                    <div class="report-icon mb-3">
                                        <i class="fas fa-receipt fa-3x text-primary"></i>
                                    </div>
                                    <h5 class="card-title font-weight-bold">GST Sales Report</h5>
                                    <p class="card-text text-muted small">
                                        Comprehensive GST-based sales analysis with tax breakdowns
                                    </p>
                                    <a href="gst-sales-report.php" class="btn btn-primary btn-sm">
                                        <i class="fas fa-arrow-right"></i> View Report
                                    </a>
                                </div>
                            </div>
                        </div>

                        <!-- Bill-wise Sales Report -->
                        <div class="col-xl-4 col-lg-6 mb-4">
                            <div class="card report-card shadow h-100">
                                <div class="card-body text-center">
                                    <div class="report-icon mb-3">
                                        <i class="fas fa-file-invoice-dollar fa-3x text-success"></i>
                                    </div>
                                    <h5 class="card-title font-weight-bold">Bill-wise Sales Report</h5>
                                    <p class="card-text text-muted small">
                                        Detailed invoice-by-invoice sales analysis
                                    </p>
                                    <a href="bill-wise-sales-report.php" class="btn btn-success btn-sm">
                                        <i class="fas fa-arrow-right"></i> View Report
                                    </a>
                                </div>
                            </div>
                        </div>

                        <!-- Item-wise Sales Report -->
                        <div class="col-xl-4 col-lg-6 mb-4">
                            <div class="card report-card shadow h-100">
                                <div class="card-body text-center">
                                    <div class="report-icon mb-3">
                                        <i class="fas fa-box-open fa-3x text-info"></i>
                                    </div>
                                    <h5 class="card-title font-weight-bold">Item-wise Sales Report</h5>
                                    <p class="card-text text-muted small">
                                        Product performance and sales by individual items
                                    </p>
                                    <a href="item-wise-sales-report.php" class="btn btn-info btn-sm">
                                        <i class="fas fa-arrow-right"></i> View Report
                                    </a>
                                </div>
                            </div>
                        </div>

                        <!-- HSN Wise Sales Report -->
                        <div class="col-xl-4 col-lg-6 mb-4">
                            <div class="card report-card shadow h-100">
                                <div class="card-body text-center">
                                    <div class="report-icon mb-3">
                                        <i class="fas fa-tags fa-3x" style="color: #6f42c1;"></i>
                                    </div>
                                    <h5 class="card-title font-weight-bold">HSN Wise Sales Report</h5>
                                    <p class="card-text text-muted small">
                                        GST analysis grouped by HSN/SAC codes with tax breakdowns
                                    </p>
                                    <a href="hsn-wise-sales-report.php" class="btn btn-sm" style="background-color: #6f42c1; color: white;">
                                        <i class="fas fa-arrow-right"></i> View Report
                                    </a>
                                </div>
                            </div>
                        </div>

                        <!-- Category-wise Sales Report -->
                        <div class="col-xl-4 col-lg-6 mb-4">
                            <div class="card report-card shadow h-100">
                                <div class="card-body text-center">
                                    <div class="report-icon mb-3">
                                        <i class="fas fa-layer-group fa-3x text-warning"></i>
                                    </div>
                                    <h5 class="card-title font-weight-bold">Category-wise Sales Report</h5>
                                    <p class="card-text text-muted small">
                                        Sales analysis grouped by product categories
                                    </p>
                                    <a href="category-wise-sales-report.php" class="btn btn-warning btn-sm">
                                        <i class="fas fa-arrow-right"></i> View Report
                                    </a>
                                </div>
                            </div>
                        </div>

                        <!-- Customer Accounts Report -->
                        <div class="col-xl-4 col-lg-6 mb-4">
                            <div class="card report-card shadow h-100">
                                <div class="card-body text-center">
                                    <div class="report-icon mb-3">
                                        <i class="fas fa-users fa-3x text-danger"></i>
                                    </div>
                                    <h5 class="card-title font-weight-bold">Customer Accounts Report</h5>
                                    <p class="card-text text-muted small">
                                        Customer-wise sales, payments, and outstanding balances
                                    </p>
                                    <a href="customer-accounts-report.php" class="btn btn-danger btn-sm">
                                        <i class="fas fa-arrow-right"></i> View Report
                                    </a>
                                </div>
                            </div>
                        </div>

                        <!-- Location-wise Sales Report -->
                        <div class="col-xl-4 col-lg-6 mb-4">
                            <div class="card report-card shadow h-100">
                                <div class="card-body text-center">
                                    <div class="report-icon mb-3">
                                        <i class="fas fa-map-marker-alt fa-3x text-secondary"></i>
                                    </div>
                                    <h5 class="card-title font-weight-bold">Location-wise Sales Report</h5>
                                    <p class="card-text text-muted small">
                                        Sales analysis by customer location/city
                                    </p>
                                    <a href="location-wise-sales-report.php" class="btn btn-secondary btn-sm">
                                        <i class="fas fa-arrow-right"></i> View Report
                                    </a>
                                </div>
                            </div>
                        </div>

                        <!-- Master Cash Flow Report (New) -->
                        <div class="col-xl-4 col-lg-6 mb-4">
                            <div class="card report-card shadow h-100 border-left-success">
                                <div class="card-body text-center">
                                    <div class="report-icon mb-3">
                                        <i class="fas fa-money-bill-wave fa-3x text-success"></i>
                                    </div>
                                    <h5 class="card-title font-weight-bold">Master Cash Flow Report</h5>
                                    <p class="card-text text-muted small">
                                        Consolidated Cash & UPI flow: Sales, Payments, Purchases & Expenses
                                    </p>
                                    <a href="cash-flow-report.php" class="btn btn-success btn-sm">
                                        <i class="fas fa-arrow-right"></i> View Report
                                    </a>
                                    <span class="badge badge-success badge-pill ml-2">NEW</span>
                                </div>
                            </div>
                        </div>

                        <!-- Detailed Sales Report (New) -->
                        <div class="col-xl-4 col-lg-6 mb-4">
                            <div class="card report-card shadow h-100">
                                <div class="card-body text-center">
                                    <div class="report-icon mb-3">
                                        <i class="fas fa-list-alt fa-3x text-dark"></i>
                                    </div>
                                    <h5 class="card-title font-weight-bold">Detailed Sales Report</h5>
                                    <p class="card-text text-muted small">
                                        Complete transaction-level sales details
                                    </p>
                                    <a href="#" class="btn btn-dark btn-sm" onclick="alert('Coming Soon!'); return false;">
                                        <i class="fas fa-arrow-right"></i> View Report
                                    </a>
                                </div>
                            </div>
                        </div>

                        <!-- Item Stock Report (New) -->
                        <div class="col-xl-4 col-lg-6 mb-4">
                            <div class="card report-card shadow h-100 border-left-info">
                                <div class="card-body text-center">
                                    <div class="report-icon mb-3">
                                        <i class="fas fa-warehouse fa-3x text-info"></i>
                                    </div>
                                    <h5 class="card-title font-weight-bold">Item Stock Report</h5>
                                    <p class="card-text text-muted small">
                                        Current inventory levels, stock valuation & profit potential
                                    </p>
                                    <a href="item-stock-report.php" class="btn btn-info btn-sm">
                                        <i class="fas fa-arrow-right"></i> View Report
                                    </a>
                                    <span class="badge badge-info badge-pill ml-2">NEW</span>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Quick Stats Charts Row -->
                    <div class="row mt-4">
                        <div class="col-xl-8 col-lg-7">
                            <div class="card shadow mb-4">
                                <div class="card-header py-3">
                                    <h6 class="m-0 font-weight-bold text-primary">
                                        <i class="fas fa-chart-area"></i> Sales Trend (Last 30 Days)
                                    </h6>
                                </div>
                                <div class="card-body">
                                    <canvas id="salesTrendChart" height="100"></canvas>
                                </div>
                            </div>
                        </div>
                        <div class="col-xl-4 col-lg-5">
                            <div class="card shadow mb-4">
                                <div class="card-header py-3">
                                    <h6 class="m-0 font-weight-bold text-primary">
                                        <i class="fas fa-chart-pie"></i> Sales by Type
                                    </h6>
                                </div>
                                <div class="card-body">
                                    <canvas id="salesTypeChart"></canvas>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>
                <!-- /.container-fluid -->

            </div>
            <!-- End of Main Content -->

            <?php include_once('../includes/footer.php'); ?>

        </div>
        <!-- End of Content Wrapper -->

    </div>
    <!-- End of Page Wrapper -->

    <!-- Scroll to Top Button-->
    <a class="scroll-to-top rounded" href="#page-top">
        <i class="fas fa-angle-up"></i>
    </a>

    <!-- Bootstrap core JavaScript-->
    <script src="../vendor/jquery/jquery.min.js"></script>
    <script src="../vendor/bootstrap/js/bootstrap.bundle.min.js"></script>

    <!-- Core plugin JavaScript-->
    <script src="../vendor/jquery-easing/jquery.easing.min.js"></script>

    <!-- Custom scripts for all pages-->
    <script src="../js/sb-admin-2.min.js"></script>

    <!-- Chart.js -->
    <script src="https://cdn.jsdelivr.net/npm/chart.js@3.9.1/dist/chart.min.js"></script>

    <!-- Page level custom scripts -->
    <script src="js/sales-reports-dashboard.js"></script>

</body>
</html>
