<?php
include "../config/koneksi.php";
include "../middleware/auth.php";
include "../config/komplain_workflow.php";

sikompro_require_role('teknisi');

$teknisi_id = (int) $_SESSION['user_id'];

$data = mysqli_query($conn,"
SELECT * FROM komplain 
WHERE teknisi_id='$teknisi_id'
AND status IN ('dikirim_ke_teknisi','diproses_teknisi','selesai_teknisi')
ORDER BY id DESC
");

$html = "";

while($d = mysqli_fetch_assoc($data)){

$html .= '
<div class="card mb-3">
<div class="card-body">

<p><b>Lokasi:</b> '.htmlspecialchars($d['lokasi']).'</p>
<span class="badge badge-'.komplain_status_badge($d['status']).'">'.komplain_status_label($d['status']).'</span><br>

<img src="../uploads/before/'.rawurlencode($d['foto_before']).'" width="120"><br>';

if($d['status'] == 'dikirim_ke_teknisi'){
$html .= '
<form method="post" action="terima_teknisi.php">
<input type="hidden" name="id" value="'.$d['id'].'">
<button class="btn btn-primary btn-sm mt-2">Terima</button>
</form>';
}

if($d['status'] == 'diproses_teknisi'){
$html .= '
<form method="post" action="teknisi.php" enctype="multipart/form-data">
<input type="hidden" name="id" value="'.$d['id'].'">
<input type="hidden" name="selesaikan" value="1">
<input type="file" name="foto_after" class="form-control mt-2">
<textarea name="keterangan" class="form-control mt-2"></textarea>
<button class="btn btn-success mt-2 btn-sm">Selesai</button>
</form>';
}

if($d['status'] == 'selesai_teknisi'){
$html .= '
<div class="alert alert-info mt-2 mb-0">Hasil teknisi sudah menunggu pengecekan pelaksana.</div>';
}

$html .= '</div></div>';
}

echo $html;
