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

sikompro_require_role('teknisi');

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    header("Location: teknisi.php");
    exit();
}

$id = isset($_POST['id']) ? (int) $_POST['id'] : 0;
$teknisiId = (int) $_SESSION['user_id'];

if ($id <= 0) {
    header("Location: teknisi.php?pesan=invalid");
    exit();
}

if (isset($_POST['terima_komplain'])) {
    mysqli_query($conn, "
        UPDATE komplain
        SET status='diproses_teknisi',
            tanggal_update=NOW()
        WHERE id='$id' AND teknisi_id='$teknisiId'
    ");

    header("Location: teknisi.php?pesan=komplain_diterima");
    exit();
}

if (isset($_POST['tolak_komplain'])) {
    $alasan = isset($_POST['alasan_tolak']) ? komplain_escape($conn, $_POST['alasan_tolak']) : 'Tidak ada alasan';

    mysqli_query($conn, "
        UPDATE komplain
        SET status='dikirim_ke_teknisi',
            catatan_teknisi='$alasan',
            tanggal_update=NOW()
        WHERE id='$id' AND teknisi_id='$teknisiId'
    ");

    header("Location: teknisi.php?pesan=komplain_ditolak");
    exit();
}

header("Location: teknisi.php");
?>
