<?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 target_service1 untuk isi
$target_service1 = "SELECT * FROM target_service1";
$target_result = $conn->query($target_service1);

// Mendapatkan data claim untuk isi
$claim = "SELECT * FROM claim";
$result_claim = $conn->query($claim);

// Mendapatkan data dealer untuk dropdown
$dealer_query = "SELECT name_dealer, code_dealer FROM dealer";
$dealer_result = $conn->query($dealer_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 | Target</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 Bootstrap Theme -->
  <link href="https://cdn.jsdelivr.net/npm/select2-bootstrap-5-theme@1.3.0/dist/select2-bootstrap-5-theme.min.css" rel="stylesheet" />

  <!-- DataTables CSS -->
  <link rel="stylesheet" href="https://cdn.datatables.net/1.13.6/css/dataTables.bootstrap5.min.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-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">

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

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

  <style>
    thead th {
      border: 1px solid rgb(230, 230, 230) !important;
    }

    table {
      overflow-y: auto;
      overflow-x: auto !important;
      min-height: 40px;
    }
    .top {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
    .bottom {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
  </style>
</head>


<body>

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

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


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

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

    <section class="section dashboard">
      
      <div class="card">
        <div class="card-body">

          <!-- Default Tabs -->
          <ul class="nav mt-4 nav-tabs d-flex" id="myTabjustified" role="tablist">
            
            <li class="nav-item flex-fill" role="presentation">
              <button class="nav-link w-100 active" id="data-claim-tab" data-bs-toggle="tab" data-bs-target="#data-claim-justified" type="button" role="tab" aria-controls="data-claim" aria-selected="false">Data Claim</button>
            </li>
            <li class="nav-item flex-fill" role="presentation">
              <button class="nav-link w-100" id="input-claim-tab" data-bs-toggle="tab" data-bs-target="#input-claim-justified" type="button" role="tab" aria-controls="input-claim" aria-selected="true">Input Claim</button>
            </li>
          </ul>
          <div class="tab-content pt-2" id="myTabjustifiedContent">
            
            <div class="tab-pane fade show active" id="data-claim-justified" role="tabpanel" aria-labelledby="data-claim-tab">
                  <h5 class="card-title">Data Claim</h5>
                  <p>Data claim semua dealer</p>

                  <!-- Table with stripped rows -->
                  <table id="targetTable" class="table table-hover table-bordered text-center">
                    <thead>
                      <tr>
                        <th></th>
                        <th>Nama Dealer</th>
                        <th>Nama Teknisi</th>
                        <th>Frame No</th>
                        <th>Jasa</th>
                        <th>Part</th>
                        <th>Oli</th>
                        <th>Tanggal</th>
                      </tr>
                    </thead>
                    <tbody>
                      <?php
                      if ($result_claim->num_rows > 0) {
                        while ($target = $result_claim->fetch_assoc()) {
                      ?>
                          <tr>
                            <td><form action="#" method="GET">
                                  <input type="hidden" name="dealer_code" value="<?php echo $target['site_id']; ?>">
                                  <input type="hidden" name="technician_name" value="<?php echo $target['technician_name']; ?>">
                                  <input type="hidden" name="frame_number" value="<?php echo $target['frame_number']; ?>">
                                  <input type="hidden" name="date" value="<?php echo $target['date']; ?>">
                                  <button type="submit" class="btn btn-outline-warning" style="--bs-btn-padding-y: .1rem; --bs-btn-padding-x: .5rem; --bs-btn-font-size: .80rem;"><i class="uil uil-pen"></i></button>
                                </form></td>
                            
                            <td class="text-start align-middle"><?php echo $target['name_dealer']; ?></td>
                            <td class="align-middle"><?php echo $target['technician_name']; ?></td>
                            <td class="align-middle"><?php echo $target['frame_number']; ?></td>
                            <td class="text-end align-middle">Rp<?php echo number_format($target['total_labor_amount'], 0, ',', '.'); ?></td>
                            <td class="text-end align-middle">Rp<?php echo number_format($target['total_part_amount'], 0, ',', '.'); ?></td>
                            <td class="text-end align-middle">Rp<?php echo number_format($target['total_oil_amount'], 0, ',', '.'); ?></td>
                            <td class="text-end align-middle"><?php echo $target['date']; ?></td>
                          </tr>
                      <?php
                        }
                      }
                      ?>
                    </tbody>
                  </table>
                  <!-- End Table with stripped rows -->


            </div>

            <div class="tab-pane fade" id="input-claim-justified" role="tabpanel" aria-labelledby="input-claim-tab">
              <h5 class="card-title">Pilih Dealer</h5>
              
              <!-- Form untuk menambahkan data -->
              <form method="POST" onsubmit="return confirmSubmit()">
                <div class="row mb-3">
                  <label class="col-sm-2 col-form-label">Dealer</label>
                  <div class="col-sm-10">
                    <select name="dealer_code" id="dealer" class="form-control select2" required>
                      <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']; ?>"
                            data-name="<?php echo htmlspecialchars($dealer['name_dealer']); ?>">
                            <?php echo htmlspecialchars($dealer['name_dealer']); ?>
                          </option>
                      <?php
                        }
                      }
                      ?>
                    </select>
                  </div>
                </div>

                <div class="col-12 d-flex justify-content-start">
                  <form action="claim_tambah.php" method="GET">
                    <input type="hidden" name="dealer_code" value="<?php echo $target['site_id']; ?>">
                    <input type="hidden" name="name_dealer" value="<?php echo $target['name_dealer']; ?>">
                    <button type="submit" class="btn btn-primary mt-3 me-2">Tambah Data</button>
                  </form>
                </div>
              </form><!-- END General Form Elements -->
            </div>

          </div><!-- End Default Tabs -->

        </div>
      </div>

    </section>

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


  <!-- ======= Footer ======= -->
  <footer id="footer" class="footer">
    <div class="copyright">
      IT Lautan Teduh Interniaga <strong><span>&copy; 2024</span></strong>
    </div>
    <div class="credits">
      Created by <a href="https://github.com/Rizky-Boolean/"><strong>Fadhil IT</strong></a><strong></strong>
    </div>
  </footer><!-- End Footer -->

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

<!-- jQuery (harus sebelum DataTables) -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

<!-- DataTables JS -->
<script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/dataTables.bootstrap5.min.js"></script>


  
  <!-- Main JS File -->
  <script src="assets/js/main.js"></script>

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

      // Event listener ketika dealer dipilih
      $('#dealer').on('change', function() {
        var selectedDealer = $(this).find('option:selected');
        var dealerCode = selectedDealer.val();
        var dealerName = selectedDealer.data('name');

        // Set value ke input site_id dan name_dealer
        $('#site_id').val(dealerCode);
        $('#name_dealer').val(dealerName);
      });
    });

    $(document).ready(function() {
      $('#targetTable').DataTable({
        "lengthMenu": [
          [10, 25, 50, -1],
          [10, 25, 50, "All"]
        ], // Mengubah teks lengthMenu
        "paging": true,  // Enable pagination
        "searching": true, // Enable searching
        "ordering": true,  // Enable column ordering
        "language": {
          "info": "Menampilkan _START_ sampai _END_ dari _TOTAL_ entri",
          "infoEmpty": "Menampilkan 0 sampai 0 dari 0 entri",
          "search": "Cari:", // Mengubah teks pencarian
          "lengthMenu": "Tampilkan _MENU_ data",
          "paginate": {
            "previous": "<span aria-hidden='true'>&laquo;</span>",
            "next": "<span aria-hidden='true'>&raquo;</span>"
          }
        },
        "pagingType": "full_numbers",  // Gaya pagination full dengan Bootstrap
        "dom": '<"top"lf>rt<"bottom"ip><"clear">'  // Menempatkan lengthMenu (l) dan searching (f) di atas
      });
    });
  </script>


</body>

</html>