<?php
include "/www/wwwroot/bilspro/config/connect.php"; // Pastikan koneksi PDO disini bernama $pdo
include "/www/wwwroot/bilspro/tracking/kata_kunci.php";

// echo encryptNumberID('627');
// Ambil ID dari URL dan validasi
$id = $_GET['id'];

if ($id) {
    try {
        $iddecrirt = decryptNumberID($id);
        // Query menggunakan prepared statement
        $stmt = $pdo->prepare("SELECT * FROM v_tiket_gangguan WHERE id = :id");
        $stmt->bindParam(":id", $iddecrirt, PDO::PARAM_INT);
        $stmt->execute();

        if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            // Tampilkan hasil
            // echo "<pre>";
            // print_r($row);
            // echo "</pre>";
        } else {
            echo "Data tidak ditemukan.";
            exit;
        }
    } catch (PDOException $e) {
        echo "Terjadi kesalahan: " . $e->getMessage();
        exit;
    }
} else {
    echo "Parameter ID tidak valid.";
    exit;
}
?>


<!DOCTYPE html>
<html lang="id">
<head>
  <meta charset="UTF-8">
  <title>Status Tracking</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <!-- Font & Icon -->
  <link rel="icon" href="../dist/img/logo-sm.png" type="image/png">
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
  <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css" rel="stylesheet">
  <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600&display=swap" rel="stylesheet">

  <style>
    body {
      font-family: 'Inter', sans-serif;
      background: linear-gradient(to right, #fff5ec, #ffe8d0);
      font-size: 13px;
      color: #343a40;
    }

    h4 {
      font-size: 16px;
      font-weight: 600;
      color: #ff8a00;
    }

    .card {
      border: none;
      background: #fff;
      box-shadow: 0 4px 12px rgba(0,0,0,0.05);
      border-radius: 10px;
    }

    .timeline {
      list-style: none;
      padding: 0;
      position: relative;
    }

    .timeline::before {
      content: '';
      position: absolute;
      top: 0;
      bottom: 0;
      width: 3px;
      background: #f5c089;
      left: 28px;
    }

    .timeline-item {
      position: relative;
      padding: 15px 15px 10px 65px;
      margin-bottom: 10px;
      background: rgba(255, 255, 255, 0.9);
      border-radius: 8px;
      transition: all 0.2s ease;
      box-shadow: 0 1px 3px rgba(0,0,0,0.04);
    }

    .timeline-item:hover {
      background: #fff;
      box-shadow: 0 2px 8px rgba(0,0,0,0.06);
    }

    .timeline-item::before {
      content: '';
      position: absolute;
      left: 20px;
      top: 22px;
      width: 14px;
      height: 14px;
      background: #ff8a00;
      border: 2px solid #fff;
      border-radius: 50%;
      box-shadow: 0 0 0 3px #fdd2a5;
    }

    .timeline-item.active::before {
      background: #ff8a00;
      box-shadow: 0 0 0 4px #ffb469;
    }

    .timeline-item h5 {
      font-size: 13px;
      margin: 0 0 3px;
      font-weight: 600;
    }

    .timeline-item p {
      font-size: 12px;
      color: #6c757d;
      margin: 0;
    }

    .detail-box {
      display: none;
      margin-top: 10px;
      background: #fff5ea;
      border-left: 3px solid #ffae42;
      padding: 10px 12px;
      border-radius: 6px;
    }

    .timeline-item.show .detail-box {
      display: block;
    }

    .timeline-item img {
      max-width: 100%;
      border-radius: 6px;
      margin-bottom: 6px;
    }

    .badge-status {
      font-size: 12px;
      border-radius: 20px;
      padding: 4px 10px;
      font-weight: 500;
    }

    .badge-selesai {
      background-color: #28a745;
      color: white;
    }

    .badge-proses {
      background-color: #ffc107;
      color: black;
    }

    .badge-belum {
      background-color: #6c757d;
      color: white;
    }

    .table th i {
      color: #ff8a00;
    }

    @media (max-width: 576px) {
      .timeline::before { left: 18px; }
      .timeline-item::before { left: 10px; }
      .timeline-item { padding-left: 50px; }
    }
  </style>
</head>
<body>

<div class="container py-3">
  <!-- Header -->
  <div class="text-center mb-3">
    <h4 class="mb-1">Status Tracking</h4>
    <span class="badge badge-status badge-selesai" style="font-size: 14px;"><?= $_GET['id'];?></span>
  </div>

  <!-- Detail -->
  <div class="card mb-3">
    <div class="card-body p-3">
      <table class="table table-borderless table-sm mb-0">
        <tr>
            <th><i class="fas fa-tools"></i></th><th>Jenis</th><td>:</td><td>Gangguan <?= $row['jenis_tiket']; ?></td></tr>
            <tr><th><i class="fas fa-phone-alt"></i></th><th>Nama</th><td>:</td><td><?= $row['nama_pelanggan']; ?></td></tr>
            <tr><th><i class="fas fa-home"></i></th><th>Alamat</th><td>:</td><td><?= $row['lokasi']; ?></td></tr>
            <tr><th><i class="fas fa-user-circle"></i></th><th>Dibuat</th><td>:</td><td><?= '('.$row['nama_pembuat'] . ') at ' . $row['start_time']; ?></td></tr>
            <tr><th><i class="fas fa-info-circle"></i></th><th>Status</th><td>:</td>
            <td>
                <?php if($row['status'] == 1) { ?>
                <span class="badge badge-status badge-primary">Tiket dibuat</span>
                <?php } elseif($row['status'] >= 2 && $row['status'] <= 4) { ?>
                <span class="badge badge-status badge-proses">Sedang diprose</span>
                <?php } else { ?>
                <span class="badge badge-status badge-success">Selesai</span>
                <?php } ?>
            </td>
        </tr>
      </table>
    </div>
  </div>

  <!-- Timeline -->
  <ul class="timeline">
    <li class="timeline-item active" onclick="toggleDetail(this)">
      <h5>Tiket Telah Dibuat</h5>
      <p><?= $row['start_time'];?></p>
      <div class="detail-box">
        <p><?= $row['permasalahan'];?></p>
      </div>
    </li>
    
    <?php if($row['status'] >= '2' && $row['status'] <= '10') { ?>
    <li class="timeline-item active" onclick="toggleDetail(this)">
      <h5>Tiket di Proses</h5>
      <p><?= $row['sebelum_proses_time'];?></p>
      <div class="detail-box">
        <p>Teknisi (<?= $row['nama_penyelesai']; ?>) telah memproses dan akan dilakukan pengerjaan</p>
      </div>
    </li>
    
    <?php } 
    $s_time = strtotime($row['sebelum_proses_time']);
    $waktuBatas = $s_time + 360;
    
    if($row['status'] >= '2' && $row['status'] <= '10' && strtotime(date('Y-m-d H:i:s')) >= $waktuBatas) { ?>
    <li class="timeline-item active" onclick="toggleDetail(this)">
      <h5>Dalam Pengerjaan</h5>
      <p><?= date('Y-m-d H:i:s', $waktuBatas); ?></p>
      <div class="detail-box">
        <img src="../img/user/<?= $row['img_sebelum']; ?>" alt="Foto">
        <p>Teknisi sedang melakukan perbaikan</p>
      </div>
    </li>
    
    <?php } if($row['status'] >= '4' && $row['status'] <= '10') { ?>
    <!--<li class="timeline-item active" onclick="toggleDetail(this)">-->
    <!--  <h5>Di Pending</h5>-->
    <!--  <p>2025-06-19 09:00</p>-->
    <!--  <div class="detail-box">-->
    <!--    <img src="https://via.placeholder.com/400x200?text=Jadwal" alt="Foto">-->
    <!--    <p>Pengerjaan di pending karena tidak bisa di selesaikan hari ini.</p>-->
    <!--  </div>-->
    <!--</li>-->
    <?php } if($row['status'] >= '3' && $row['status'] <= '10') { ?>
    <li class="timeline-item active" onclick="toggleDetail(this)">
      <h5>Tiket Selesai Dikerjakan</h5>
      <p><?= $row['telah_proses_time'];?></p>
      <div class="detail-box">
        <img src="../img/user/<?= $row['img_selesai']; ?>" alt="Foto">
        <p>Teknisi telah melakukan perkerjaan dan akan di verifikasi oleh admin</p>
      </div>
    </li>
    <!--<li class="timeline-item active" onclick="toggleDetail(this)">-->
    <!--  <h5>Dalam Pengerjaan</h5>-->
    <!--  <p>2025-06-19 08:20</p>-->
    <!--  <div class="detail-box">-->
    <!--    <img src="../img/user/<?= $row['img_sebelum']; ?>" alt="Foto">-->
    <!--    <p>Teknisi sedang di melakukan perbaikan</p>-->
    <!--  </div>-->
    <!--</li>-->
    <?php } 
    $st_time = strtotime($row['telah_proses_time']);
    $waktub = $st_time + 300;

    if($row['status'] >= '4' && $row['status'] <= '10' && strtotime(date('Y-m-d H:i:s')) >= $waktub ) { ?>
    <li class="timeline-item active" onclick="toggleDetail(this)">
      <h5>Sedang Verifikasi</h5>
      <p><?= date('Y-m-d H:i:s', $waktub); ?></p>
      <div class="detail-box">
        <p>Admin sedang memverikasi perkerjaan di lapangan</p>
      </div>
    </li>
    <?php } if($row['status'] >= '5' && $row['status'] <= '10') { ?>
    <li class="timeline-item active" onclick="toggleDetail(this)">
      <h5>Selesai</h5>
      <p><?= $row['verifikasi_time'];?></p>
      <div class="detail-box">
        <p>Tiket telah di verifikasi dan selesai di dikerjakan</p>
      </div>
    </li>
    <?php } ?>
  </ul>
</div>

<script>
  function toggleDetail(el) {
    document.querySelectorAll('.timeline-item').forEach(item => {
      if (item !== el) item.classList.remove('show');
    });
    el.classList.toggle('show');
  }

  window.addEventListener('DOMContentLoaded', () => {
    document.querySelectorAll('.timeline-item').forEach(item => item.classList.remove('show'));
  });
</script>

</body>
</html>
