<?php

include('../config/config.php');
include('../config/session_check.php'); 
$branch_id = $_SESSION['branch_id'];
$today = date('Y-m-d');
$currentMonth = date('m');
$currentYear = date('Y');

//Orders
$completedQuery = $conn->query("SELECT COUNT(*) as total FROM orders WHERE status='completed' AND MONTH(created_at) = $currentMonth AND YEAR(created_at) = $currentYear AND branch_id = '$branch_id'");
$completed = $completedQuery->fetch_assoc()['total'];

$pendingQuery = $conn->query("SELECT COUNT(*) as total FROM orders WHERE status='pending' AND MONTH(created_at) = $currentMonth AND YEAR(created_at) = $currentYear AND branch_id = '$branch_id'");
$pending = $pendingQuery->fetch_assoc()['total'];

// Vehicle, Driver, Customer
$vehicleQuery = $conn->query("SELECT COUNT(*) as total FROM vehicles WHERE status='active' AND branch_id = '$branch_id'");
$vehicleCount = $vehicleQuery->fetch_assoc()['total'];

$driverQuery = $conn->query("SELECT COUNT(*) as total FROM drivers WHERE status='active' AND branch_id = '$branch_id'");
$driverCount = $driverQuery->fetch_assoc()['total'];

$customerQuery = $conn->query("SELECT COUNT(*) as total FROM customers WHERE status='active' AND branch_id = '$branch_id'");
$customerCount = $customerQuery->fetch_assoc()['total'];


$month_completed = isset($month_completed) && is_numeric($month_completed) ? $month_completed : 0;
$year_completed = isset($year_completed) && is_numeric($year_completed) ? $year_completed : 0;



?>
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta content="width=device-width, initial-scale=1.0" name="viewport">

  <title>Customer Master</title>
  <meta content="" name="description">
  <meta content="" name="keywords">

  <!-- Favicons -->
  <link href="../assets/img/favicon.png" rel="icon">
  <link href="../assets/img/apple-touch-icon.png" rel="apple-touch-icon">

  <!-- Google Fonts -->
  <link href="https://fonts.gstatic.com" rel="preconnect">
  <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,600,600i,700,700i|Nunito:300,300i,400,400i,600,600i,700,700i|Poppins:300,300i,400,400i,500,500i,600,600i,700,700i" rel="stylesheet">

  <!-- Vendor CSS Files -->
  <link href="../assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
  <link href="../assets/vendor/bootstrap-icons/bootstrap-icons.css" rel="stylesheet">
  <link href="../assets/vendor/boxicons/css/boxicons.min.css" rel="stylesheet">
  <link href="../assets/vendor/quill/quill.snow.css" rel="stylesheet">
  <link href="../assets/vendor/quill/quill.bubble.css" rel="stylesheet">
  <link href="../assets/vendor/remixicon/remixicon.css" rel="stylesheet">
  <link href="../assets/vendor/simple-datatables/style.css" rel="stylesheet">

  <!-- Template Main CSS File -->
  <link href="../assets/css/style.css" rel="stylesheet">


</head>

<body>

  <!-- ======= Header ======= -->
<?php include('../config/header.php'); ?> // Include the header

  <!-- ======= Sidebar ======= -->

<?php include('../config/sidebar.php'); ?>
<main id="main" class="main">
  <div class="pagetitle">
    <h1>Branch Dashboard</h1>
  </div>

  <section class="section dashboard">
    <div class="row">

      <!-- Completed Orders -->
      <div class="col-lg-3 col-md-6">
        <div class="card info-card sales-card">
          <div class="card-body">
            <h5 class="card-title">Completed Orders <span>| This Month</span></h5>
            <div class="d-flex align-items-center">
              <div class="ps-3">
                <h6><?= $completed ?></h6>
              </div>
            </div>
          </div>
        </div>
      </div>

      <!-- Pending Orders -->
      <div class="col-lg-3 col-md-6">
        <div class="card info-card revenue-card">
          <div class="card-body">
            <h5 class="card-title">Pending Orders <span>| This Month</span></h5>
            <div class="d-flex align-items-center">
              <div class="ps-3">
                <h6><?= $pending ?></h6>
              </div>
            </div>
          </div>
        </div>
      </div>

      <!-- Vehicles -->
      <div class="col-lg-2 col-md-4">
        <div class="card info-card customers-card">
          <div class="card-body">
            <h5 class="card-title">Vehicles</h5>
            <h6><?= $vehicleCount ?></h6>
          </div>
        </div>
      </div>

      <!-- Drivers -->
      <div class="col-lg-2 col-md-4">
        <div class="card info-card customers-card">
          <div class="card-body">
            <h5 class="card-title">Drivers</h5>
            <h6><?= $driverCount ?></h6>
          </div>
        </div>
      </div>

      <!-- Customers -->
      <div class="col-lg-2 col-md-4">
        <div class="card info-card customers-card">
          <div class="card-body">
            <h5 class="card-title">Customers</h5>
            <h6><?= $customerCount ?></h6>
          </div>
        </div>
      </div>
   <!-- Circle Chart for Monthly Orders -->
            <div class="col-lg-6">
                <div class="card">
                    <div class="card-body">
                        <h5 class="card-title">Monthly Orders (Completed)</h5>
                        <canvas id="monthlyChart" width="400" height="400"></canvas>
                    </div>
                </div>
            </div>

            <!-- Circle Chart for Yearly Orders -->
            <div class="col-lg-6">
                <div class="card">
                    <div class="card-body">
                        <h5 class="card-title">Yearly Orders (Completed)</h5>
                        <canvas id="yearlyChart" width="400" height="400"></canvas>
                    </div>
                </div>
            </div>

        </div>
    </div>
  </section>
</main>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script>
    var monthCompleted = <?= json_encode((int)$month_completed) ?>;
    var yearCompleted = <?= json_encode((int)$year_completed) ?>;

    var monthlyCtx = document.getElementById('monthlyChart').getContext('2d');
    new Chart(monthlyCtx, {
        type: 'doughnut',
        data: {
            labels: ['Completed This Month', 'Remaining'],
            datasets: [{
                data: [monthCompleted, 100 - monthCompleted],
                backgroundColor: ['#36A2EB', '#E5E5E5']
            }]
        }
    });

    var yearlyCtx = document.getElementById('yearlyChart').getContext('2d');
    new Chart(yearlyCtx, {
        type: 'doughnut',
        data: {
            labels: ['Completed This Year', 'Remaining'],
            datasets: [{
                data: [yearCompleted, 100 - yearCompleted],
                backgroundColor: ['#4BC0C0', '#E5E5E5']
            }]
        }
    });
</script>

  <a href="#" class="back-to-top d-flex align-items-center justify-content-center"><i class="bi bi-arrow-up-short"></i></a>

  <!-- Vendor JS Files -->
  <script src="../assets/vendor/apexcharts/apexcharts.min.js"></script>
  <script src="../assets/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
  <script src="../assets/vendor/chart.js/chart.umd.js"></script>
  <script src="../assets/vendor/echarts/echarts.min.js"></script>
  <script src="../assets/vendor/quill/quill.js"></script>
  <script src="../assets/vendor/simple-datatables/simple-datatables.js"></script>
  <script src="../assets/vendor/tinymce/tinymce.min.js"></script>
  <script src="../assets/vendor/php-email-form/validate.js"></script>
  <!-- Template Main JS File -->
  <script src="../assets/js/main.js"></script>


<?php include('../config/footer.php'); ?>
