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

sikompro_require_role('pelaksana');

$pelaksana_id = (int) $_SESSION['user_id'];
$data = mysqli_query($conn,"
SELECT k.*
FROM komplain k
WHERE k.pelaksana_id='{$pelaksana_id}'
ORDER BY k.id DESC
");

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

<div class="card mb-3">
<div class="card-body">

<p><b>Lokasi:</b> <?= htmlspecialchars((string) $d['lokasi']) ?></p>

<img src="../uploads/before/<?= rawurlencode((string) $d['foto_before']) ?>" width="150">

<p><b>Status:</b> <?= htmlspecialchars(komplain_status_label($d['status'])) ?></p>

<p><b>Keterangan Pelaksana:</b> <?= htmlspecialchars((string) ($d['pelaksana_keterangan'] ?? '-')) ?></p>
<p><b>Keterangan Teknisi:</b> <?= htmlspecialchars((string) ($d['hasil_teknisi'] ?: $d['keterangan'] ?: '-')) ?></p>

<form method="post" action="dashboard.php">
<input type="hidden" name="id" value="<?= $d['id'] ?>">

<?php if($d['status']=='diproses_pelaksana'){ ?>

<select name="teknisi_id" class="form-control mt-2" required>
<option value="">Pilih Teknisi</option>
<?php
$tek = mysqli_query($conn,"SELECT * FROM users WHERE role='teknisi'");
while($t=mysqli_fetch_assoc($tek)){
?>
<option value="<?= $t['id'] ?>"><?= $t['username'] ?></option>
<?php } ?>
</select>

<textarea name="keterangan" class="form-control mt-2" placeholder="Keterangan"></textarea>

<button name="kirim" class="btn btn-primary mt-2">Kirim ke Teknisi</button>

<?php } ?>

<?php if(in_array($d['status'], ['selesai_teknisi', 'menunggu_pelaksana'])){ ?>
<button name="terima" class="btn btn-success mt-2">Terima</button>
<button name="tolak" class="btn btn-danger mt-2">Tolak</button>
<?php } ?>

</form>

</div>
</div>

<?php } ?>
