<?php

session_start();

// Cek apakah user sudah login
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {
    // Jika belum login, redirect ke halaman login
    header("Location: login.php");
    exit();
}
// koneksi ke database
include 'connection.php';

// Mendapatkan data dealer untuk dropdown
$dealer_query = "SELECT name_dealer, code_dealer FROM dealer";
$dealer_result = $conn->query($dealer_query);

// Mendapatkan data bulan untuk dropdown
$bulan_query = "SELECT bulan FROM api_service_ach_vw2 GROUP BY bulan";
$bulan_result = $conn->query($bulan_query);

// Mendapatkan data tahun untuk dropdown
$tahun_query = "SELECT tahun FROM api_service_ach_vw2 GROUP BY tahun";
$tahun_result = $conn->query($tahun_query);
?>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta content="width=device-width, initial-scale=1.0" name="viewport">

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

  <!-- Bootstrap CSS -->
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
  <!-- Select2 CSS -->
  <link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
  <!-- Select2 Bootstrap Theme CSS -->
  <link href="https://cdn.jsdelivr.net/npm/select2-bootstrap-5-theme@1.1.1/dist/select2-bootstrap-5-theme.min.css" rel="stylesheet" />
  <!-- jQuery -->
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  <!-- Select2 JS -->
  <script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>




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

  <!-- UNICOS -->
  <link rel="stylesheet" href="https://unicons.iconscout.com/release/v4.0.8/css/line.css">

  <!-- 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">





  <style>
    thead th {
      border: 1px solid rgb(230, 230, 230) !important;
      /* Custom border for table header */
    }

    table {
      overflow-y: auto;
      overflow-x: auto !important;
      min-height: 60px;

    }

    .top {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
    .bottom {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
		.table-secondary {
			background-color: #F4F4F4 !important; /* Warna latar yang kamu inginkan */
			border: 1px solid #dee2e6 !important; /* Menambahkan border */
		}

		.table-bordered th, .table-bordered td {
			border: 1px solid #dee2e6 !important; /* Border untuk semua sel dalam tabel */
		}
    .tooltip-inner {
      background-color: #6c757d !important; /* Bootstrap secondary color */
      color: #fff; /* Text color for better contrast */
    }
    .tooltip-arrow::before {
      border-top-color: #6c757d !important; /* Change arrow color to match the tooltip background */
    }
  </style>
</head>
<body>

<!-- HEADER -->
<?php
include 'header.php';
?>



<?php
$activeMenu = 'productivity'; // Sesuaikan dengan halaman yang sedang dibuka
include 'sidebar.php';
?>



  <main id="main" class="main">

    <div class="pagetitle">
      <h1>Productivity</h1>
			
      <nav>
        <ol class="breadcrumb">
          <li class="breadcrumb-item"><a href="index.html">Home</a></li>
          <li class="breadcrumb-item active">Productivity</li>
        </ol>
      </nav>
    </div><!-- End Page Title -->

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

        <!-- Left side columns -->
        <div class="col-lg-12">
          <div class="row">

            <!-- Filter Tujuan Dealer -->
            <div class="col-12">
              <div class="card recent-sales overflow-auto">

                <div class="card-body">
                  <h5 class="card-title">Filter Tujuan Dealer</h5>

                  <form method="POST" action="" class="row g-3 align-items-end">
                    <div class="col-md-6">
                      <label for="dealer" class="form-label">Dealer</label>
                      <select name="dealer_code" id="dealer" class="form-control select2">
                        <option value="" disabled selected hidden>Pilih Dealer</option>
                        <?php
                        if ($dealer_result->num_rows > 0) {
                          while ($dealer = $dealer_result->fetch_assoc()) {
                        ?>
                            <option value="<?php echo "$dealer[code_dealer]" ?>"
                              <?php if (isset($_POST['dealer_code']) && $_POST['dealer_code'] == $dealer['code_dealer']) { ?>selected="" <?php } ?>>
                              <?php echo htmlspecialchars($dealer['name_dealer']) ?></option>
                        <?php
                          }
                        }
                        ?>
                      </select>
                    </div>

                    <div class="col-md-2">
                      <label for="bulan" class="form-label">Bulan</label>
                      <select name="bulan" id="bulan" class="form-control">
                        <option value="" disabled selected hidden>Pilih Bulan</option>
                        <?php
                        if ($bulan_result->num_rows > 0) {
                          while ($bulan = $bulan_result->fetch_assoc()) {
                        ?>
                            <option value="<?php echo "$bulan[bulan]" ?>"
                              <?php if (isset($_POST['bulan']) && $_POST['bulan'] == $bulan['bulan']) { ?>selected="" <?php } ?>>
                              <?php echo htmlspecialchars($bulan['bulan']) ?></option>
                        <?php
                          }
                        }
                        ?>
                      </select>
                    </div>

                    <div class="col-md-2">
                      <label for="tahun" class="form-label">Tahun</label>
                      <select name="tahun" id="tahun" class="form-control">
                        <option value="" disabled selected hidden>Pilih Tahun</option>
                        <?php
                        if ($tahun_result->num_rows > 0) {
                          while ($tahun = $tahun_result->fetch_assoc()) {
                        ?>
                            <option value="<?php echo "$tahun[tahun]" ?>"
                              <?php if (isset($_POST['tahun']) && $_POST['tahun'] == $tahun['tahun']) { ?>selected="" <?php } ?>>
                              <?php echo htmlspecialchars($tahun['tahun']) ?></option>
                        <?php
                          }
                        }
                        ?>
                      </select>
                    </div>

                    <div class="col-md-2 d-flex justify-content-end">
                      <button type="submit" class="btn btn-primary mt-3 w-100">Tampilkan</button>
                    </div>
                  </form><!-- End Custom Styled Validation with Tooltips -->

                </div>

              </div>
            </div><!-- End Filter Tujuan Dealer -->

            <!-- Table -->
            <div class="col-12">
              <div class="card recent-sales overflow-auto">

                <div class="card-body">



                  <?php
                  // Menangani form submission
                  if ($_SERVER["REQUEST_METHOD"] == "POST") {
                    $site_id = $_POST['dealer_code'];
                    $bulan = $_POST['bulan'];
                    $tahun = $_POST['tahun'];

                    // Validasi input
                    if (empty($site_id) || empty($bulan) || empty($tahun)) {
                      echo "<br><div class='alert alert-danger'>Pastikan anda memilih Dealer, Bulan, dan Tahun.</div>";
                    } else {

                      // Cek apakah kombinasi bulan & tahun sudah ada di target_service_dealer
                      $check_query = "SELECT * FROM target_service_dealer WHERE bulan = '$bulan' AND tahun = '$tahun'";
                      $check_result = $conn->QUERY($check_query);

                      IF ($check_result->num_rows == 0) {
                          // Jika belum ada, isi otomatis target_service dengan DATA dari api_service_ach_vw2
                          $insert_query = "INSERT INTO target_service (site_id, name_dealer, bulan, tahun)
                                          SELECT site_id, name_dealer, '$bulan', '$tahun' FROM api_service_ach_vw2
                                          WHERE bulan = '$bulan' AND tahun = '$tahun'
                                          GROUP BY site_id, bulan, tahun";
                          IF ($conn->QUERY($insert_query) === TRUE) {
                              echo "<br><div class='alert alert-success'>Data Target bulan ini telah terisi Otomatis.</div>";
                          } ELSE {
                              echo "<br><div class='alert alert-danger'>Gagal mengisi Data Target bulan ini: " . $conn->error . "</div>";
                          }
                      }



                      // Query untuk mengambil data berdasarkan site_id, bulan, dan tahun
                      $sql = "SELECT *
                          FROM api_service_ach_vw2
                          WHERE site_id = '$site_id' AND bulan = '$bulan' AND tahun = '$tahun'";

                      // echo $sql; // Debugging: menampilkan query

                      $result = $conn->query($sql);

                      if ($result->num_rows > 0) {
                        // Query untuk mengambil nama dealer secara terpisah
                        $sql_name = "SELECT *
                              FROM api_service_ach_vw2
                              WHERE site_id = '$site_id' AND bulan = '$bulan' AND tahun = '$tahun' LIMIT 1";
                        $result_name = $conn->query($sql_name);

                        if ($result_name->num_rows > 0) {
                          $row_name = $result_name->fetch_assoc();
                          $name_dealer = $row_name['name_dealer'];
                          $bulan = $row_name['bulan'];
                          $tahun = $row_name['tahun'];
                        } else {
                          $name_dealer = "Nama dealer tidak ditemukan";
                        }
                  ?>
                        <!-- Display Dealer and Period -->

                        <div class="row">
                          <div class="col-md-12 text-end">

                            <!-- <div class="col-3 w-100">
                              <button type="submit" class="btn btn-outline-success mt-3 col-3"><i class="uil uil-file-export"></i> Export</button>
                            </div> -->
                          </div>
                        </div>
                        <h3 class="card-title text-center mt-3 mb-1"><strong>LAPORAN PERFORMANCE BENGKEL</strong> </h3>
                        <h3 class="card-title text-center mb-3"><strong>Periode : <?php echo " $bulan/$tahun"; ?><br><br><?php echo $name_dealer; ?></strong> </h3>

                        <table class='table table-hover table-bordered align-middle text-center'>
                          <thead class="thead-light">
                            <tr>
                              <th class="table-secondary">Item</th>
                              <th class="table-secondary">Target</th>
                              <th class="table-secondary">Result</th>
                              <th class="table-secondary">Pencapaian</th>
                              <th class="table-secondary">GAP(-/+)</th>
                            </tr>
                          </thead>
                          <tbody>
                          <?php 
                          $sql_target_detail = "SELECT *
                            FROM `target_service`
                            WHERE site_id = '$site_id' AND bulan = '$bulan' AND tahun = '$tahun'";
                          $result_detail = $conn->query($sql_target_detail); // Eksekusi query

                          if ($result_detail->num_rows > 0) {
                            $row_detail = $result_detail->fetch_assoc(); // Ambil hasil
                            $jml_tech = $row_detail['jml_tech'];
                            $unit_ksb = $row_detail['unit_ksb'];
                            $unit_ksg = $row_detail['unit_ksg'];
                            $jasa_ksb = $row_detail['jasa_ksb'];
                            $jasa_ksg = $row_detail['jasa_ksg'];
                            $part_ws = $row_detail['part_ws'];
                            $oli_ws = $row_detail['oli_ws'];
                            $hari_kerja = $row_detail['hari_kerja'];
                          } else {
                            $jml_tech = 0;
                            $unit_ksb = 0;
                            $unit_ksg = 0;
                            $jasa_ksb = 0;
                            $jasa_ksg = 0;
                            $part_ws = 0;
                            $oli_ws = 0;
                            $hari_kerja = 0;
                          }

                          $sql_target_dealer = "SELECT *
                            FROM `target_service_per_unit`
                            WHERE site_id = '$site_id' AND bulan = '$bulan' AND tahun = '$tahun'";
                          $result_dealer = $conn->query($sql_target_dealer); // Eksekusi query

                          if ($result_dealer->num_rows > 0) {
                            $row_detail = $result_dealer->fetch_assoc(); // Ambil hasil
                            $rut = $row_detail['rut'];
                            $jasa_per_unit = $row_detail['jasa_per_unit'];
                            $part_per_unit = $row_detail['part_per_unit'];
                            $oli_per_unit = $row_detail['oli_per_unit'];
                          } else {
                            $rut = 0;
                            $jasa_per_unit = 0;
                            $part_per_unit = 0;
                            $oli_per_unit = 0;
                          }
                          ?>
														<tr>
															<td scope="row" class="text-start table-secondary">Unit KSG</td>
															<td><?php 
                                $target_ksg = $unit_ksg;
                                echo $target_ksg;
                                ?></td>
															<td>
                                <?php 
                                $result_ksg = $conn->query("SELECT SUM(jumlah_category) AS jml_ksg FROM api_service_performance WHERE site_id = '$site_id' AND bulan = '$bulan' AND tahun = '$tahun' AND service_category = 'KSG'");
                                $jml_ksg = $result_ksg->fetch_assoc()['jml_ksg'] ?? 0;
                                echo $jml_ksg;
                                ?>
                              </td>
															<td><?php 
                                if($target_ksg == 0){$capai_jml_ksg = 0;}
                                else{$capai_jml_ksg = $jml_ksg / $target_ksg;}
                                echo number_format($capai_jml_ksg * 100, 1, ',', '.')
                                ?>%</td>
															<td><?php
                                $gap_ksg = $jml_ksg-$target_ksg;
                                if ($gap_ksg >= 0) {
                                  echo '+' . $gap_ksg;
                                } else {
                                  echo $gap_ksg;
                                }
                                ?></td>
														</tr>
														<tr>
															<td scope="row" class="text-start table-secondary">Unit KSB</td>
															<td><?php 
                                $target_ksb = $unit_ksb;
                                echo $target_ksb;
                                ?></td>
															<td>
                                <?php 
                                $result_ksb = $conn->query("SELECT SUM(jumlah_category) AS jml_ksb FROM api_service_performance WHERE site_id = '$site_id' AND bulan = '$bulan' AND tahun = '$tahun' AND service_category != 'KSG'");
                                $jml_ksb = $result_ksb->fetch_assoc()['jml_ksb'] ?? 0;
                                echo $jml_ksb;
                                ?>
                              </td>
															<td><?php 
                                if($target_ksb == 0){$capai_jml_ksb = 0;}
                                else{$capai_jml_ksb = $jml_ksb / $target_ksb;}
                                echo number_format($capai_jml_ksb * 100, 1, ',', '.')
                                ?>%</td>
															<td><?php
                                $gap_ksb = $jml_ksb - $target_ksb;
                                if ($gap_ksb >= 0) {
                                  echo '+' . $gap_ksb;
                                } else {
                                  echo $gap_ksb;
                                }
                                ?></td>
														</tr>
														<tr>
															<th scope="row" class="table-secondary">Total Unit</th>
															<td>
                                <?php 
                                $total_target = $target_ksg + $target_ksb;
                                echo $total_target;
                                ?>
                              </td>
															<td>
                                <?php 
                                $total_unit = $jml_ksg + $jml_ksb;
                                echo $total_unit;
                                ?>
                              </td>
															<td><?php 
                                if($total_target == 0){$capai_total_unit = 0;}
                                else{$capai_total_unit = $total_unit / $total_target;}
                                echo number_format($capai_total_unit * 100, 1, ',', '.')
                                ?>%</td>
															<td><?php
                                $gap_total_unit = $total_unit - $total_target;
                                if ($gap_total_unit >= 0) {
                                  echo '+' . $gap_total_unit;
                                } else {
                                  echo $gap_total_unit;
                                }
                                ?></td>
														</tr>
														<tr>
															<th colspan="5"> </th>
														</tr>
														<tr>
															<td scope="row" class="text-start table-secondary">Jasa KSG</td>
															<td>Rp<?php 
                                $target_jasa_ksg = $jasa_ksg;
                                echo number_format($target_jasa_ksg, 0, ',', '.');
                                ?></td>
															<td>RP<?php 
                                $result_ksg = $conn->query("SELECT SUM(total_jasa) AS jasa_ksg FROM api_service_performance WHERE site_id = '$site_id' AND bulan = '$bulan' AND tahun = '$tahun' AND service_category = 'KSG'");
                                $jasa_ksg = $result_ksg->fetch_assoc()['jasa_ksg'] ?? 0;
                                echo number_format($jasa_ksg, 0, ',', '.');
                                ?>
                              </td>
															<td><?php 
                                if($target_jasa_ksg == 0){$capai_jasa_ksg = 0;}
                                else{$capai_jasa_ksg = $jasa_ksg / $target_jasa_ksg;}
                                echo number_format($capai_jasa_ksg * 100, 1, ',', '.')
                                ?>%</td>
															<td><?php
                                $gap_jasa_ksg = $jasa_ksg - $target_jasa_ksg;
                                if ($gap_jasa_ksg >= 0) {
                                  echo '+RP' . number_format($gap_jasa_ksg, 0, ',', '.');
                                } else {
                                  echo '-RP' . number_format(abs($gap_jasa_ksg), 0, ',', '.');
                                }
                                ?></td>
														</tr>
														<tr>
															<td scope="row" class="text-start table-secondary">Jasa KSB</td>
															<td>Rp<?php 
                                $target_jasa_ksb = $jasa_ksb;
                                echo number_format($target_jasa_ksb, 0, ',', '.');
                                ?></td>
															<td>RP<?php 
                                $result_ksb = $conn->query("SELECT SUM(total_jasa) AS jasa_ksb FROM api_service_performance WHERE site_id = '$site_id' AND bulan = '$bulan' AND tahun = '$tahun' AND service_category != 'KSG'");
                                $jasa_ksb = $result_ksb->fetch_assoc()['jasa_ksb'] ?? 0;
                                echo number_format($jasa_ksb, 0, ',', '.');
                                ?>
                              </td>
															<td><?php 
                                if($target_jasa_ksb == 0){$capai_jasa_ksb = 0;}
                                else{$capai_jasa_ksb = $jasa_ksb / $target_jasa_ksb;}
                                echo number_format($capai_jasa_ksb * 100, 1, ',', '.')
                                ?>%</td>
															<td><?php
                                $gap_jasa_ksb = $jasa_ksb - $target_jasa_ksb;
                                if ($gap_jasa_ksb >= 0) {
                                  echo '+RP' . number_format($gap_jasa_ksb, 0, ',', '.');
                                } else {
                                  echo '-RP' . number_format(abs($gap_jasa_ksb), 0, ',', '.');
                                }
                                ?></td>
														</tr>
														<tr>
															<th scope="row" class="table-secondary">Total Jasa</th>
															<td>Rp<?php 
                                $total_target_jasa = $target_jasa_ksg + $target_jasa_ksb;
                                echo number_format($total_target_jasa, 0, ',', '.');
                                ?>
                              </td>
															<td>RP<?php 
                                $total_jasa = $jasa_ksg + $jasa_ksb;
                                echo number_format($total_jasa, 0, ',', '.');
                                ?></td>
															<td><?php 
                                if($total_target_jasa == 0){$total_capai_jasa = 0;}
                                else{$total_capai_jasa = $total_jasa / $total_target_jasa;}
                                echo number_format($total_capai_jasa * 100, 1, ',', '.')
                                ?>%</td>
															<td><?php
                                $gap_total_jasa = $total_jasa - $total_target_jasa;
                                if ($gap_total_jasa >= 0) {
                                  echo '+RP' . number_format($gap_total_jasa, 0, ',', '.');
                                } else {
                                  echo '-RP' . number_format(abs($gap_total_jasa), 0, ',', '.');
                                }
                                ?></td>
														</tr>
														</tr>
														<tr>
															<th colspan="5"> </th>
														</tr>
														<tr>
                              <td scope="row" class="text-start table-secondary">
                                <span data-bs-toggle="tooltip" data-bs-placement="top" title="Part termasuk Busi">Sparepart w/s</span>
                              </td>
															<td>Rp<?php 
                                $target_ws_part = $part_ws;
                                echo number_format($target_ws_part, 0, ',', '.');
                                ?></td>
															<td>RP<?php 
                                $result_part = $conn->query("SELECT SUM(total_part_amount) AS tot_part FROM `api_service_ach_vw3` WHERE site_id = '$site_id' AND bulan = '$bulan' AND tahun = '$tahun' GROUP BY site_id");
                                $ws_part = $result_part->fetch_assoc()['tot_part'] ?? 0;
                                echo number_format($ws_part, 0, ',', '.');
                                ?>
                              </td>
															<td><?php 
                                if($target_ws_part == 0){$total_capai_ws_part = 0;}
                                else{$total_capai_ws_part = $ws_part / $target_ws_part;}
                                echo number_format($total_capai_ws_part * 100, 1, ',', '.')
                                ?>%</td>
															<td><?php
                                $gap_total_ws_part = $ws_part - $target_ws_part;
                                if ($gap_total_ws_part >= 0) {
                                  echo '+RP' . number_format($gap_total_ws_part, 0, ',', '.');
                                } else {
                                  echo '-RP' . number_format(abs($gap_total_ws_part), 0, ',', '.');
                                }
                                ?></td>
														</tr>
														<tr>
															<td scope="row" class="text-start table-secondary">Oli w/s</td>
															<td>Rp<?php 
                                $target_ws_oli = $oli_ws;
                                echo number_format($target_ws_oli, 0, ',', '.');
                                ?></td>
															<td>RP<?php 
                                $result_oli = $conn->query("SELECT SUM(jumlah) AS tot_oli FROM `api_service_ach_vw5` WHERE site_id = '$site_id' AND bulan = '$bulan' AND tahun = '$tahun' GROUP BY site_id");
                                $ws_oli = $result_oli->fetch_assoc()['tot_oli'] ?? 0;
                                echo number_format($ws_oli, 0, ',', '.');
                                ?>
															<td><?php 
                                if($target_ws_oli == 0){$total_capai_ws_oli = 0;}
                                else{$total_capai_ws_oli = $ws_oli / $target_ws_oli;}
                                echo number_format($total_capai_ws_oli * 100, 1, ',', '.')
                                ?>%</td>
															<td><?php
                                $gap_total_ws_oli = $ws_oli - $target_ws_oli;
                                if ($gap_total_ws_oli >= 0) {
                                  echo '+RP' . number_format($gap_total_ws_oli, 0, ',', '.');
                                } else {
                                  echo '-RP' . number_format(abs($gap_total_ws_oli), 0, ',', '.');
                                }
                                ?></td>
														</tr>
														<tr>
															<th scope="row" class="table-secondary">Total Workshop</th>
															<td>Rp<?php 
                                $total_target_ws = $target_ws_part + $target_ws_oli;
                                echo number_format($total_target_ws, 0, ',', '.');
                                ?>
                              </td>
															<td>RP<?php 
                                $total_ws = $ws_part + $ws_oli;
                                echo number_format($total_ws, 0, ',', '.');
                                ?></td>
															<td><?php 
                                if($total_target_ws == 0){$total_capai_ws = 0;}
                                else{$total_capai_ws = $total_ws / $total_target_ws;}
                                echo number_format($total_capai_ws * 100, 1, ',', '.')
                                ?>%</td>
															<td><?php
                                $gap_total_ws = $total_ws - $total_target_ws;
                                if ($gap_total_ws >= 0) {
                                  echo '+RP' . number_format($gap_total_ws, 0, ',', '.');
                                } else {
                                  echo '-RP' . number_format(abs($gap_total_ws), 0, ',', '.');
                                }
                                ?></td>
														</tr>
														</tr>
														<!-- <tr>
															<th colspan="5"> </th>
														</tr>
														<tr>
															<th scope="row" class="table-secondary">Retail</th>
															<td>RP0</td>
															<td>RP0</td>
															<td>0%</td>
															<td>Rp0</td>
														</tr>
														<tr>
															<th scope="row" class="table-secondary">Total Sparepart</th>
															<td>RP0</td>
															<td>RP0</td>
															<td>0%</td>
															<td>Rp0</td>
														</tr> -->
														<tr>
															<th colspan="5"> </th>
														</tr>
														<tr>
															<th scope="row" class="table-secondary">Total Omset</th>
															<td>Rp<?php 
                                $total_target_omset = $total_target_jasa + $total_target_ws;
                                echo number_format($total_target_omset, 0, ',', '.');
                                ?>
                              </td>
															<td>RP<?php 
                                $total_omset = $total_jasa + $total_ws;
                                echo number_format($total_omset, 0, ',', '.');
                                ?></td>
															<td><?php 
                                if($total_target_omset == 0){$total_capai_omset = 0;}
                                else{$total_capai_omset = $total_omset / $total_target_omset;}
                                echo number_format($total_capai_omset * 100, 1, ',', '.')
                                ?>%</td>
															<td><?php
                                $gap_total_omset = $total_omset - $total_target_omset;
                                if ($gap_total_omset >= 0) {
                                  echo '+RP' . number_format($gap_total_omset, 0, ',', '.');
                                } else {
                                  echo '-RP' . number_format(abs($gap_total_omset), 0, ',', '.');
                                }
                                ?></td>
														</tr>
													</tbody>
                        </table>

                        <table class='table table-hover table-bordered align-middle text-center'>
                          <thead class="thead-light">
                            <tr>
                              <th colspan="5" class="table-secondary">Parameter</th>
                            </tr>
                            <tr>
                              <th class="table-secondary">Item</th>
                              <th class="table-secondary">Target</th>
                              <th class="table-secondary">Result</th>
                              <th class="table-secondary">GAP(-/+)</th>
                            </tr>
                          </thead>
                          <tbody>
                            <tr>
                              <th scope="row" class="text-start table-secondary">RUT</th>
                              <td><?php 
                                echo number_format($rut, 1);
                              ?></td>
                              <td>
                                <?php 
                                $rut_dealer = $total_unit / $jml_tech / $hari_kerja;
                                echo number_format($rut_dealer, 1);
                                ?>
                              </td>
                              <td><?php
                                $gap_rut = $rut_dealer - $rut;
                                if ($gap_rut >= 0) {
                                  echo '+' . number_format($gap_rut, 1);
                                } else {
                                  echo number_format($gap_rut, 1);
                                }
                                ?></td>
                            </tr>
														<tr>
															<th scope="row" class="text-start table-secondary">Jasa Per Unit</th>
															<td>Rp<?php 
                                $jasa_per_unit_target = $jasa_per_unit;
                                echo number_format($jasa_per_unit_target, 0, ',', '.');
                                ?></td>
															<td>
                                <?php 
                                $jasa_per_unit_param = $total_jasa / $total_unit;
                                echo number_format($jasa_per_unit_param, 1);
                                ?>
                              </td>
															<td><?php
                                $gap_jasa_per_unit_param = $jasa_per_unit_param - $jasa_per_unit_target;
                                if ($gap_jasa_per_unit_param >= 0) {
                                  echo '+RP' . number_format($gap_jasa_per_unit_param, 0, ',', '.');
                                } else {
                                  echo '-RP' . number_format(abs($gap_jasa_per_unit_param), 0, ',', '.');
                                }
                                ?></td>
														</tr>
														<tr>
															<th scope="row" class="text-start table-secondary">Part w/s Per Unit</th>
															<td>Rp<?php 
                                $ws_part_target = $part_per_unit;
                                echo number_format($ws_part_target, 0, ',', '.');
                                ?></td>
															<td>
                                <?php 
                                $ws_part_param = $ws_part / $total_unit;
                                echo number_format($ws_part_param, 1);
                                ?>
                              </td>
															<td><?php
                                $gap_ws_part_param = $ws_part_param - $ws_part_target;
                                if ($gap_ws_part_param >= 0) {
                                  echo '+RP' . number_format($gap_ws_part_param, 0, ',', '.');
                                } else {
                                  echo '-RP' . number_format(abs($gap_ws_part_param), 0, ',', '.');
                                }
                                ?></td>
														</tr>
														<tr>
															<th scope="row" class="text-start table-secondary">Oli w/s Per Unit</th>
															<td>Rp<?php 
                                $oli_per_unit = $oli_per_unit;
                                echo number_format($oli_per_unit, 0, ',', '.');
                                ?></td>
															<td>
                                <?php 
                                $ws_oli_param = $ws_oli / $total_unit;
                                echo number_format($ws_oli_param, 1);
                                ?>
                              </td>
															<td><?php
                                $gap_ws_oli_param = $ws_oli_param - $oli_per_unit;
                                if ($gap_ws_oli_param >= 0) {
                                  echo '+RP' . number_format($gap_ws_oli_param, 0, ',', '.');
                                } else {
                                  echo '-RP' . number_format(abs($gap_ws_oli_param), 0, ',', '.');
                                }
                                ?></td>
														</tr>
													</tbody>
                        </table>

                    <?php
                      } else {
                        echo "<div class='alert alert-danger mt-4 mb-1'>Tidak ada data yang ditemukan untuk Dealer, Bulan, dan Tahun yang dipilih.</div>";
                      }
                    }
                  } else { ?>
                    <!-- Default Title -->
                    <div class="alert alert-warning text-center mt-4 mb-1" role="alert">
                      <h3 class="card-title">Silahkan Pilih Dealer, Bulan dan Tahun</h3>
                    </div>
                  <?php } ?>

                </div>

              </div>
            </div><!-- End Table -->


          </div>
        </div><!-- End Left side columns -->


      </div>
    </section>
		
  </main><!-- End #main -->

  <?php
  include 'footer.php';
  ?>

  <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>

	<script>
		$(document).ready(function() {
      $('#dealer').select2({
        theme: 'bootstrap-5', // Menggunakan tema Bootstrap 5
        placeholder: "Pilih Dealer",
        allowClear: true
      });
    });
	</script>

</body>
</html>
