<?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 dari URL
$dealer_code = $_GET['dealer_code'];
$name_dealer = $_GET['name_dealer'];
$bulan = $_GET['bulan'];
$tahun = $_GET['tahun'];

// Query untuk mengambil data dari target_service berdasarkan site_id, bulan, dan tahun
$query = "SELECT *
          FROM target_service 
          WHERE site_id = '$dealer_code' 
          AND name_dealer = '$name_dealer' 
          AND bulan = '$bulan' 
          AND tahun = '$tahun'";
$result = $conn->query($query);

// Cek apakah data ditemukan
if ($result->num_rows > 0) {
  // Mengambil data dari hasil query
  $row = $result->fetch_assoc();
  $jml_tech = $row['jml_tech'];
  $unit_ksg = $row['unit_ksg'];
  $unit_ksb = $row['unit_ksb'];
  $jasa_ksg = $row['jasa_ksg'];
  $jasa_ksb = $row['jasa_ksb'];
  $part_ws = $row['part_ws'];
  $oli_ws = $row['oli_ws'];
  $hari_kerja = $row['hari_kerja'];
} else {
  // Jika data tidak ditemukan, set nilai default
  $jml_tech = 0;
  $unit_ksg = 0;
  $unit_ksb = 0;
  $jasa_ksg = 0;
  $jasa_ksb = 0;
  $part_ws = 0;
  $oli_ws = 0;
  $hari_kerja = 0;
}

// Cek jika form disubmit
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  // Mengambil data dari form
  $jml_tech = $_POST['jml_tech'];
  $unit_ksg = $_POST['unit_ksg'];
  $unit_ksb = $_POST['unit_ksb'];
  $jasa_ksg = $_POST['jasa_ksg'];
  $jasa_ksb = $_POST['jasa_ksb'];
  $part_ws = $_POST['part_ws'];
  $oli_ws = $_POST['oli_ws'];
  $hari_kerja = $_POST['hari_kerja'];

  // Update database
  $update_query = "UPDATE target_service SET
        jml_tech = '$jml_tech',
        unit_ksg = '$unit_ksg',
        unit_ksb = '$unit_ksb',
        jasa_ksg = '$jasa_ksg',
        jasa_ksb = '$jasa_ksb',
        part_ws = '$part_ws',
        oli_ws = '$oli_ws',
        hari_kerja = '$hari_kerja'
        WHERE site_id = '$dealer_code'
        AND bulan = '$bulan'
        AND tahun = '$tahun'";

  if ($conn->query($update_query) === TRUE) {
    // Redirect ke halaman target.php setelah update berhasil
    header("Location: target.php");
    exit(); // pastikan script berhenti setelah redirect
  } else {
    echo "Error updating data: " . $conn->error;
  }
}
?>

<!DOCTYPE html>
<html lang="en">
<?php
include 'head.php';
?>

<body>

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




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



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

    <div class="pagetitle">
      <h1>Edit Target Dealer</h1>
      <nav>
        <ol class="breadcrumb">
          <li class="breadcrumb-item"><a href="index.html">Home</a></li>
          <li class="breadcrumb-item"><a href="target.php">Target</a></li>
          <li class="breadcrumb-item active">Edit Target Dealer</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 Laporan -->
            <div class="col-12">
              <div class="card recent-sales overflow-auto">

                <div class="card-body">
                  <h5 class="card-title">Target Bulanan</h5>
                  <p>Setelah data <b>Target Bulanan terisi LENGKAP</b>, sistem secara otomatis akan memperbarui <b>Target Per Unit</b> dan <b>Target Per Dealer</b>.</p>

                  <!-- General Form Elements -->
                  <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">
                        <input type="text" class="form-control" value="<?php echo $name_dealer; ?>" disabled>
                        <input type="hidden" name="dealer_code" value="<?php echo $dealer_code; ?>">
                      </div>
                    </div>

                    <div class="row mb-3">
                      <label class="col-sm-2 col-form-label">Tahun</label>
                      <div class="col-sm-10">
                        <input type="number" class="form-control" value="<?php echo $tahun; ?>" disabled>
                      </div>
                    </div>

                    <div class="row mb-3">
                      <label class="col-sm-2 col-form-label">Bulan</label>
                      <div class="col-sm-10">
                        <input type="number" class="form-control" value="<?php echo $bulan; ?>" disabled>
                      </div>
                    </div>

                    <div class="row mb-3">
                      <label class="col-sm-2 col-form-label">Jumlah Teknisi</label>
                      <div class="col-sm-10">
                        <input type="number" class="form-control" name="jml_tech" placeholder="jumlah teknisi" value="<?php echo $jml_tech; ?>" required>
                      </div>
                    </div>

                    <div class="row mb-3">
                      <label class="col-sm-2 col-form-label">Unit KSG</label>
                      <div class="col-sm-10">
                        <input type="number" class="form-control" name="unit_ksg" placeholder="unit ksg" value="<?php echo $unit_ksg; ?>" required>
                      </div>
                    </div>

                    <div class="row mb-3">
                      <label class="col-sm-2 col-form-label">Unit KSB</label>
                      <div class="col-sm-10">
                        <input type="number" class="form-control" name="unit_ksb" placeholder="unit ksb" value="<?php echo $unit_ksb; ?>" required>
                      </div>
                    </div>

                    <div class="row mb-3">
                      <label class="col-sm-2 col-form-label">Jasa KSG</label>
                      <div class="col-sm-10">
                        <div class="input-group">
                          <span class="input-group-text">Rp</span>
                          <input type="number" class="form-control" name="jasa_ksg" placeholder="jasa ksg" value="<?php echo $jasa_ksg; ?>" required>
                        </div>
                      </div>
                    </div>

                    <div class="row mb-3">
                      <label class="col-sm-2 col-form-label">Jasa KSB</label>
                      <div class="col-sm-10">
                        <div class="input-group">
                          <span class="input-group-text">Rp</span>
                          <input type="number" class="form-control" name="jasa_ksb" placeholder="jasa ksb" value="<?php echo $jasa_ksb; ?>" required>
                        </div>
                      </div>
                    </div>

                    <div class="row mb-3">
                      <label class="col-sm-2 col-form-label">Part w/s</label>
                      <div class="col-sm-10">
                        <div class="input-group">
                          <span class="input-group-text">Rp</span>
                          <input type="number" class="form-control" name="part_ws" placeholder="part ws" value="<?php echo $part_ws; ?>" required>
                        </div>
                      </div>
                    </div>

                    <div class="row mb-3">
                      <label class="col-sm-2 col-form-label">Oli w/s</label>
                      <div class="col-sm-10">
                        <div class="input-group">
                          <span class="input-group-text">Rp</span>
                          <input type="number" class="form-control" name="oli_ws" placeholder="oli ws" value="<?php echo $oli_ws; ?>" required>
                        </div>
                      </div>
                    </div>

                    <div class="row mb-3">
                      <label class="col-sm-2 col-form-label">Hari Kerja</label>
                      <div class="col-sm-10">
                        <input type="number" class="form-control" name="hari_kerja" placeholder="hari kerja" value="<?php echo $hari_kerja; ?>" required>
                      </div>
                    </div>

                    <div class="col-12 d-flex justify-content-start">
                      <button class="btn btn-primary mt-3 me-2" type="submit">Simpan</button>
                      <button class="btn btn-secondary mt-3" type="button" onclick="window.history.back()">Batal</button>
                    </div>
                  </form><!-- END General Form Elements -->


                </div>

              </div>
            </div><!-- End Filter Laporan -->


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

  <script>
    function confirmSubmit() {
      return confirm("Apakah yakin ingin menyimpan perubahan?");
    }
  </script>

  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>


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

</body>

</html>