<?php
session_start();
include "../config.php";

if (!isset($_SESSION['id_user']) || $_SESSION['role'] != 'dfo') {
    header("Location: ../index.php");
    exit;
}

$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
$query = $koneksi->query("SELECT * FROM jv_documents WHERE id_jv = '$id'");
$data = $query->fetch_assoc();

if (!$data) { die("Dadus la hetan."); }

// Format Tanggal & USD tetap sama untuk konsistensi [cite: 4, 8]
function formatTanggalPortugal($tanggal) {
    if (empty($tanggal) || $tanggal == '0000-00-00') return '-';
    $bulan = [1 => 'Janeiro', 'Fevereiro', 'Março', 'Abril', 'Maio', 'Junho', 'Julho', 'Agosto', 'Setembro', 'Outubro', 'Novembro', 'Dezembro'];
    return date('d', strtotime($tanggal)) . ' ' . $bulan[(int)date('m', strtotime($tanggal))] . ' ' . date('Y', strtotime($tanggal));
}
?>

<!DOCTYPE html>
<html lang="pt">
<head>
    <meta charset="UTF-8">
    <title>Fo Fila Dokumentu - Finance</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
    <style>
        body { font-family: 'Segoe UI', sans-serif; background: #fff1f2; color: #1f2937; padding: 40px; margin: 0; }
        .container { max-width: 900px; margin: auto; background: white; border-radius: 24px; overflow: hidden; box-shadow: 0 20px 50px rgba(0,0,0,0.1); border: 1px solid #fecdd3; }
        .header { background: linear-gradient(135deg, #9f1239, #e11d48); color: white; padding: 30px; text-align: center; }
        .content { padding: 40px; }
        .row-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 30px; margin-bottom: 30px; }
        .card-info { background: #fffbfb; border: 1px solid #fecdd3; padding: 20px; border-radius: 15px; }
        .label { font-size: 11px; font-weight: 800; color: #be123c; text-transform: uppercase; margin-bottom: 8px; }
        .value { font-size: 16px; font-weight: 600; }
        
        textarea { width: 100%; padding: 15px; border-radius: 12px; border: 1px solid #fecdd3; font-size: 14px; min-height: 120px; outline: none; }
        textarea:focus { border-color: #e11d48; box-shadow: 0 0 0 4px rgba(225,29,72,0.1); }

        .btn-area { display: flex; justify-content: flex-end; gap: 15px; margin-top: 30px; }
        .btn { padding: 12px 25px; border-radius: 12px; font-weight: 700; border: none; cursor: pointer; text-decoration: none; }
        .btn-return { background: #e11d48; color: white; }
       .btn-cancel {
    background: #ef4444;
    color: white;
    border: 1px solid #ef4444;
}
.btn-cancel:hover { background: #dc2626; transform: translateY(-1px); }
 .btn-submit { background: #059669; color: white; }
        .btn-submit:hover { background: #047857; transform: translateY(-1px); }
    </style>
</head>
<body>

<div class="container">
    <div class="header">
        <h2><i class="fas fa-undo"></i> Devolve Dokumentu / Fo Fila</h2>
    </div>

    <form action="proses_return.php" method="POST" class="content">
        <input type="hidden" name="id_jv" value="<?= $id ?>">

        <div class="row-grid">
            <div class="card-info">
                <div class="label">No Pedido</div>
                <div class="value"><?= htmlspecialchars($data['nomor_prt']) ?></div>
                <br>
                <div class="label">Data Simu</div>
                <div class="value"><?= formatTanggalPortugal($data['tgl_terima']) ?></div>
            </div>
            <div class="card-info">
                <div class="label">Diresaun</div>
                <div class="value"><?= htmlspecialchars($data['direksi']) ?></div>
                <br>
                <div class="label">Montante</div>
                <div class="value" style="color:#e11d48; font-size:20px; font-weight:800;">$ <?= number_format($data['total_adiantamento'], 2) ?></div>
            </div>
        </div>

        <div style="margin-bottom:30px;">
            <div class="card-info">
                <div class="label">Assuntu</div>
                <div class="value"><?= nl2br(htmlspecialchars($data['descripsi'])) ?></div>
            </div>
        </div>

        <div class="form-group">
            <div class="label">Razaun / Alasan Dikembalikan</div>
            <textarea name="alasan_dikembalikan" required placeholder="Escreve razaun fofila iha ne'e..."></textarea>
        </div>

        <div class="btn-area">
            <a href="konfirmasidok.php?id=<?= $id ?>" class="btn btn-cancel">
    <i class="fas fa-times"></i> Kansela
</a>
            <button type="submit" name="submit_arsip" class="btn btn-submit">
                <i class="fas fa-save"></i> Submete Fo Fila
            </button>
        </div>
    </form>
</div>

</body>
</html>