<?php 
$stmt = $pdo->prepare("SELECT * FROM v_tiket_gangguan WHERE $SqlSvData id = '$page[1]'");
$stmt->execute();
$data = $stmt->fetchAll();

// echo "<pre>";
// print_r($data);
// echo "</pre>";

?>
<!-- Main content -->
<section class="content">
  <div class="container-fluid">
    <div class="row">
      <div class="col-12">
        <div class="card">
          <div class="card-header bg-secondary">
            <div class="row">
              <div class="col-sm-6">
                <h3 class="card-title">Proses Tiket Gangguan</h3>
              </div>
              <div class="col-sm-6">
              </div>
            </div>
          </div>
          <!-- /.card-header -->
          <form method="post" enctype="multipart/form-data">
            <div class="card-body">
              <div class="row">
                <div class="col-md-6">
                  <table class="table table-borderless">
                    <tbody>
                      <tr>
                        <td class="p-1">Status</td>
                        <td class="p-1">:</td>
                        <td class="p-1">
                          <?php 
                            if($data[0]['status'] == '1') {
                              echo '<div class="badge badge-warning">Dalam Antrian</div>';
                            } elseif($data[0]['status'] == '2') {
                              echo '<div class="badge badge-primary">Akan di Proses</div>';
                            } elseif($data[0]['status'] == '3') {
                              echo '<div class="badge badge-primary">Sedang di Proses</div>';
                            } elseif($data[0]['status'] == '4') {
                              echo '<div class="badge badge-secondary">Sedang di Verifikasi</div>';
                            } else {
                              echo '<div class="badge badge-success">Tiket Selesai</div>';
                            }
                          ?>
                        </td>
                      </tr>
                      <tr>
                        <td class="p-1">Dibuat Oleh</td>
                        <td class="p-1">:</td>
                        <td class="p-1"><?php echo $data[0]['nama_pembuat']; ?></td>
                      </tr>
                      <tr>
                        <td class="p-1">Waktu Dibuat Tiket</td>
                        <td class="p-1">:</td>
                        <td class="p-1"><?php echo $data[0]['start_time']; ?></td>
                      </tr>
                      <tr>
                        <td class="p-1">Jenis Gangguan</td>
                        <td class="p-1">:</td>
                        <td class="p-1"><?php echo $data[0]['jenis_tiket']; ?></td>
                      </tr>
                      <?php if($data[0]['jenis_tiket'] == "Pelanggan") { ?>
                      <tr>
                        <td class="p-1">Nama Pelanggan</td>
                        <td class="p-1">:</td>
                        <td class="p-1"><?php echo $data[0]['nama_pelanggan']; ?></td>
                      </tr>
                      <?php } ?>
                      <tr>
                        <td class="p-1">Lokasi Gangguan</td>
                        <td class="p-1">:</td>
                        <td class="p-1"><?php echo $data[0]['lokasi']; ?></td>
                      </tr>
                      <tr>
                        <td class="p-1">Titik Koordinat</td>
                        <td class="p-1">:</td>
                        <td class="p-1"><a href="https://www.google.com/maps/search/?api=1&query=<?php echo $data[0]['latitude'].','.$data[0]['longitude']; ?>" target="_blank"><?php echo $data[0]['latitude'].', '.$data[0]['longitude']; ?></a></td>
                      </tr>
                      <tr>
                        <td class="p-1">Indikasi Masalah</td>
                        <td class="p-1">:</td>
                        <td class="p-1"><?php echo $data[0]['permasalahan']; ?></td>
                      </tr>
                      <tr style="border-top: 1px solid #d3d3d3;">
                        <td class="p-1">Mulai di Proses</td>
                        <td class="p-1">:</td>
                        <td class="p-1"><?php echo $data[0]['sebelum_proses_time']; ?></td>
                      </tr>
                      <tr>
                        <td class="p-1">Di Proses Oleh</td>
                        <td class="p-1">:</td>
                        <td class="p-1"><?php echo $data[0]['nama_penyelesai']; ?></td>
                      </tr>
                      <?php if($data[0]['status'] == 4 OR $data[0]['status'] == 5) { ?>
                      <tr style="border-top: 1px solid #d3d3d3;">
                        <td class="p-1">Selesai di Proses</td>
                        <td class="p-1">:</td>
                        <td class="p-1"><?php echo $data[0]['telah_proses_time']; ?></td>
                      </tr>
                      <tr>
                        <td class="p-1">Catatan Penyelesaian</td>
                        <td class="p-1">:</td>
                        <td class="p-1"><?php echo $data[0]['penyelesaian']; ?></td>
                      </tr>
                      <?php } ?>
                    </tbody>
                  </table>
                </div>
                <div class="col-md-6">
                  <center>
                    <div class="row">
                      <div class="col-md-6">
                        <?php if($data[0]['status'] == 4 OR $data[0]['status'] == 5) { ?>
                        <h6>Foto Sebelum Perbaikan</h6><br>
                        <img src="img/user/<?php echo $data[0]['img_sebelum']; ?>" width="100%">
                      </div>
                      <div class="col-md-6">
                        <h6>Foto Setelah Perbaikan</h6><br>
                        <img src="img/user/<?php echo $data[0]['img_selesai']; ?>" width="100%">
                        <!-- Sebelum di perbaikai -->
                        <?php } if($data[0]['status'] == 2) { ?>
                        <label>Foto sebelum di perbaikan</label>
                        <div class="uploadImage" data-id="fsb<?php echo $data[0]['id']; ?>">
                          <span>Click to upload image</span>
                          <img src="#" alt="Image preview">
                          <div class="progressContainer">
                            <div class="progressBar"></div>
                          </div>
                        </div>
                        <div class="form-group m-1">
                            <label>Pilih Lokasi Maps</label><br>
                            <input type="hidden" id="latitude" value="<?php echo $data[0]['latitude']; ?>" name="latitude">
                            <input type="hidden" id="longitude" value="<?php echo $data[0]['longitude']; ?>" name="longitude">
                            <div id="map" style="height:250px; margin-top: 10px;"></div>
                        </div>
                        <!-- Setelah di perbaiki -->
                        <?php } if($data[0]['status'] == 3) { ?>
                        <label>Foto setelah di perbaiki</label>
                        <div class="uploadImage" data-id="fst<?php echo $data[0]['id']; ?>">
                          <span>Click to upload image</span>
                          <img src="#" alt="Image preview">
                          <div class="progressContainer">
                            <div class="progressBar"></div>
                          </div>
                        </div>
                      </div>
                      <div class="col-md-6">
                        <div class="form-group">
                          <label>Penyelesaian Masalah</label>
                          <textarea name="penyelesaian" rows="5" class="form-control" required="required"></textarea>
                        </div>
                        <?php } ?>
                      </div>
                    </div>
                  </center>
                </div>
              </div>
            </div>
            <div class="card-footer">
              <?php if($data[0]['status'] == 2 && in_array("Ubah", $json_array[36]['sub_menu'])) { ?>
              <input type="submit" class="btn btn-sm btn-primary m-1" name="lanjut" value="Lanjutkan Proses">
              <?php } if($data[0]['status'] == 3 && in_array("Ubah", $json_array[36]['sub_menu'])) { ?>
              <input type="submit" class="btn btn-sm btn-primary m-1" name="selesai" value="Selesaikan Proses">
              <?php } if($data[0]['status'] == 4 && in_array("Verifikasi", $json_array[36]['sub_menu'])) { ?>
              <input type="submit" class="btn btn-sm btn-secondary m-1" name="verifikasi" value="Verifikasi">
              <input type="submit" class="btn btn-sm btn-danger m-1" name="kembali" value="Kembalikan ke Proses">
              <input type="submit" class="btn btn-sm btn-warning m-1" name="sebelum" value="Kembalikan ke Sebelum Proses">
              <?php } if($data[0]['status'] == 2 && in_array("Verifikasi", $json_array[36]['sub_menu'])) { ?>
              <input type="submit" class="btn btn-sm btn-warning m-1" name="sebelum" value="Kembalikan ke Sebelum Proses">
              <?php } if($data[0]['status'] == 3 && in_array("Verifikasi", $json_array[36]['sub_menu'])) { ?>
              <input type="submit" class="btn btn-sm btn-warning m-1" name="sebelum" value="Kembalikan ke Sebelum Proses">
              <?php } ?>
            </div>
          </div>
        </form>
      </div>
    </div>
  </div>
</section>

<?php
  if (isset($_POST['lanjut'])) {
    if(!empty($_SESSION['uploaded_files']['fsb'.$data[0]['id']])) {
      $stmt = $pdo->prepare("UPDATE tiket_gangguan SET img_sebelum = :imgsebelum, status = '3', latitude = :latitude, longitude = :longitude WHERE id = :idtik");
      $stmt->bindParam(':latitude', $_POST['latitude']);
      $stmt->bindParam(':longitude', $_POST['longitude']);
      $stmt->bindParam(':imgsebelum', $_SESSION['uploaded_files']['fsb'.$data[0]['id']]);
      $stmt->bindParam(':idtik', $data[0]['id'], PDO::PARAM_INT);
      $stmt->execute();
      sweetAlert("Proses di lanjutkan", "success");
    }else{
      sweetAlert("Gambar belum di upload", "error");
    }
  }
  if (isset($_POST['selesai'])) {
    $waktu = date('Y-m-d H:i:s');
    if(!empty($_SESSION['uploaded_files']['fst'.$data[0]['id']])) {
      $stmt = $pdo->prepare("UPDATE tiket_gangguan SET penyelesaian = :penyelesaian, telah_proses_time = :waktu, img_selesai = :imgsebelum, status = '4' WHERE id = :idtik");
      $stmt->bindParam(':waktu', $waktu);
      $stmt->bindParam(':penyelesaian', $_POST['penyelesaian']);
      $stmt->bindParam(':imgsebelum', $_SESSION['uploaded_files']['fst'.$data[0]['id']]);
      $stmt->bindParam(':idtik', $data[0]['id'], PDO::PARAM_INT);
      $stmt->execute();
      sweetAlert("Tiket akan di verifikasi", "success");
    }else{
      sweetAlert("Gambar belum di upload", "error");
    }
  }
  if (isset($_POST['verifikasi'])) {
    $waktu = date('Y-m-d H:i:s');
    $stmt = $pdo->prepare("UPDATE tiket_gangguan SET status = '5', verifikasi_time = :verifikasi_time WHERE id = :idtik");
    $stmt->bindParam(':verifikasi_time', $waktu);
    $stmt->bindParam(':idtik', $data[0]['id'], PDO::PARAM_INT);
    $stmt->execute();
    sweetAlert("Tiket selesai di verifikasi", "success");
  }
  if (isset($_POST['kembali'])) {
    $stmt = $pdo->prepare("UPDATE tiket_gangguan SET status = '2' WHERE id = :idtik");
    $stmt->bindParam(':idtik', $data[0]['id'], PDO::PARAM_INT);
    $stmt->execute();
    sweetAlert("Tiket telah di kembalikan ke proses", "success");
  }
  if (isset($_POST['sebelum'])) {
    $stmt = $pdo->prepare("UPDATE tiket_gangguan SET status = '1' WHERE id = :idtik");
    $stmt->bindParam(':idtik', $data[0]['id'], PDO::PARAM_INT);
    $stmt->execute();
    sweetAlert("Tiket telah di kembalikan ke sebeum proses", "success", "?".encrypt_url('tiket_buat'));
  }

?>