<?php
include 'db.php';

if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $id = $_POST['id'];
    $division = $_POST['division-update'];
    $block = $_POST['block-update'];
    $classification = $_POST['classification-update'];
    $damageType = $_POST['damageType-update'];
    $mitigationPlan = $_POST['mitigationPlan-update'];
    $notes = $_POST['notes-update'];
    $status = $_POST['status-update'];
    $waktu_temuan = $_POST['waktu_temuan'];
    $waktu_update_manual = $_POST['waktu_update_manual'];
    $waktu_perlakuan = isset($_POST['waktu_perlakuan']) ? $_POST['waktu_perlakuan'] : null;
    $waktu_verifikasi = isset($_POST['waktu_verifikasi']) ? $_POST['waktu_verifikasi'] : null;

    // Debug
    error_log("ID: " . $id);
    error_log("Waktu temuan update: " . $waktu_temuan);
    error_log("Waktu update manual: " . $waktu_update_manual);
    error_log("Waktu perlakuan: " . $waktu_perlakuan);
    error_log("Waktu verifikasi: " . $waktu_verifikasi);

    // Set timezone and automatic timestamp for system tracking
    date_default_timezone_set('Asia/Jakarta');
    $timestamp_update = date("Y-m-d H:i:s");

    // Get current photo paths
    $sql = "SELECT initialPhoto, updatedPhoto, photo_perlakuan, photo_verifikasi FROM Report WHERE id = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("s", $id);
    $stmt->execute();
    $result = $stmt->get_result();
    $row = $result->fetch_assoc();

    // Handle initial photo update
    $initialPhotoPath = $row['initialPhoto'];
    if (isset($_FILES['initialPhoto-update']) && $_FILES['initialPhoto-update']['error'] == UPLOAD_ERR_OK) {
        $initialPhotoPath = 'uploads/' . $id . "-initial-" . basename($_FILES['initialPhoto-update']['name']);
        if (move_uploaded_file($_FILES['initialPhoto-update']['tmp_name'], $initialPhotoPath)) {
            echo "File initialPhoto berhasil diunggah.<br>";
        } else {
            echo "Gagal mengunggah file initialPhoto.<br>";
        }
    }

    // Handle updated photo
    $updatedPhotoPath = $row['updatedPhoto'];
    if (isset($_FILES['updatedPhoto-update']) && $_FILES['updatedPhoto-update']['error'] == UPLOAD_ERR_OK) {
        $updatedPhotoPath = 'uploads/' . $id . "-updated-" . basename($_FILES['updatedPhoto-update']['name']);
        if (move_uploaded_file($_FILES['updatedPhoto-update']['tmp_name'], $updatedPhotoPath)) {
            echo "File updatedPhoto berhasil diunggah.<br>";
        } else {
            echo "Gagal mengunggah file updatedPhoto.<br>";
        }
    }

    // Handle photo_perlakuan
    $photoPerlakuanPath = $row['photo_perlakuan'];
    if (isset($_FILES['photo_perlakuan-update']) && $_FILES['photo_perlakuan-update']['error'] == UPLOAD_ERR_OK) {
        $photoPerlakuanPath = 'uploads/' . $id . "-perlakuan-" . basename($_FILES['photo_perlakuan-update']['name']);
        if (move_uploaded_file($_FILES['photo_perlakuan-update']['tmp_name'], $photoPerlakuanPath)) {
            echo "File photo_perlakuan berhasil diunggah.<br>";
        } else {
            echo "Gagal mengunggah file photo_perlakuan.<br>";
        }
    }

    // Handle photo_verifikasi
    $photoVerifikasiPath = $row['photo_verifikasi'];
    if (isset($_FILES['photo_verifikasi-update']) && $_FILES['photo_verifikasi-update']['error'] == UPLOAD_ERR_OK) {
        $photoVerifikasiPath = 'uploads/' . $id . "-verifikasi-" . basename($_FILES['photo_verifikasi-update']['name']);
        if (move_uploaded_file($_FILES['photo_verifikasi-update']['tmp_name'], $photoVerifikasiPath)) {
            echo "File photo_verifikasi berhasil diunggah.<br>";
        } else {
            echo "Gagal mengunggah file photo_verifikasi.<br>";
        }
    }

    // Update query with new fields
    $sql = "UPDATE Report SET 
            division = ?, 
            block = ?, 
            classification = ?, 
            damageType = ?, 
            mitigationPlan = ?, 
            notes = ?, 
            initialPhoto = ?, 
            updatedPhoto = ?, 
            photo_perlakuan = ?, 
            photo_verifikasi = ?, 
            status = ?, 
            timestamp_update = ?,
            waktu_temuan = ?,
            waktu_update_manual = ?,
            waktu_perlakuan = ?,
            waktu_verifikasi = ?
            WHERE id = ?";
    
    $stmt = $conn->prepare($sql);
    
    if (!$stmt) {
        die("Prepare failed: " . $conn->error);
    }
    
    $stmt->bind_param("sssssssssssssssss", 
        $division, 
        $block, 
        $classification, 
        $damageType, 
        $mitigationPlan, 
        $notes, 
        $initialPhotoPath, 
        $updatedPhotoPath, 
        $photoPerlakuanPath,
        $photoVerifikasiPath,
        $status, 
        $timestamp_update,
        $waktu_temuan,
        $waktu_update_manual,
        $waktu_perlakuan,
        $waktu_verifikasi,
        $id
    );

    if ($stmt->execute()) {
        echo "<script>alert('Data berhasil diperbarui!'); window.location.href='/?active=kerusakan';</script>";
    } else {
        echo "Error: " . $stmt->error;
        error_log("Update error: " . $stmt->error);
    }

    $stmt->close();
    $conn->close();
}
?>
