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

if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'project manager') {
    header("Location: ../login.php");
    exit();
}

$dpr_id = (int)($_GET['id'] ?? 0);
if ($dpr_id <= 0) die("Invalid DPR");

$user_id = $_SESSION['user_id'];

$stmt = $conn->prepare("
    SELECT d.*, p.project_name, p.project_code 
    FROM daily_progress_report d
    JOIN project_master p ON d.project_id = p.id
    WHERE d.id = ? AND d.project_manager_id = ?
");
$stmt->bind_param("ii", $dpr_id, $user_id);
$stmt->execute();
$dpr = $stmt->get_result()->fetch_assoc();

if (!$dpr) die("DPR not found or access denied");
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>View DPR</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
    <style>
        /* Paste same CSS as dpr_add.php or link external file */
            :root {
            --primary: #8b5cf6;
            --success: #22c55e;
            --danger: #ef4444;
            --glass: rgba(255,255,255,0.08);
            --border: rgba(255,255,255,0.15);
        }
        body {
            font-family: 'Segoe UI', sans-serif;
            background: linear-gradient(rgba(0,0,0,0.8), rgba(0,0,0,0.95)),
                        url('../background.jpeg') center/cover fixed;
            color: white;
            margin: 0;
            padding: 20px;
        }
        .container { max-width: 900px; margin: 0 auto; }
        .back { color: #c4b5fd; text-decoration: none; display: inline-flex; align-items: center; gap: 8px; margin-bottom: 20px; }
        .back:hover { color: white; }
        h1 { margin: 0 0 10px; }
        .form-area {
            background: var(--glass);
            backdrop-filter: blur(12px);
            border: 1px solid var(--border);
            border-radius: 16px;
            padding: 30px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.4);
        }
        .form-group { margin-bottom: 24px; }
        label { display: block; margin-bottom: 8px; font-weight: 600; }
        input, select, textarea {
            width: 100%;
            padding: 14px;
            background: rgba(255,255,255,0.08);
            border: 1px solid rgba(255,255,255,0.2);
            border-radius: 10px;
            color: white;
        }
        textarea { min-height: 140px; }
        .submit-btn {
            background: linear-gradient(135deg, var(--primary), #a78bfa);
            color: white;
            border: none;
            padding: 16px;
            border-radius: 12px;
            font-size: 1.1rem;
            font-weight: 600;
            cursor: pointer;
            width: 100%;
        }
        .message { padding: 16px; border-radius: 10px; margin-bottom: 20px; }
        .success { background: rgba(34,197,94,0.25); color: #86efac; }
        .error { background: rgba(239,68,68,0.25); color: #fca5a5; }
        .detail-row { margin: 15px 0; }
        .detail-label { font-weight: bold; min-width: 180px; display: inline-block; }
        .status { padding: 6px 12px; border-radius: 12px; }
        .status-Pending { background: #f59e0b66; }
        .status-Submitted { background: #22c55e66; }
        .status-Approved { background: #16a34a66; }
    </style>
</head>
<body>
    <div class="container">
        <a href="dpr_list.php?project_id=<?= $dpr['project_id'] ?>" class="back">
            <i class="fas fa-arrow-left"></i> Back to DPR List
        </a>

        <h1>DPR Details</h1>
        <p>Project: <?= htmlspecialchars($dpr['project_name']) ?> (<?= htmlspecialchars($dpr['project_code']) ?>)</p>

        <div class="detail-card">
            <div class="detail-row">
                <span class="detail-label">Date:</span> <?= date('d M Y', strtotime($dpr['report_date'])) ?>
            </div>
            <div class="detail-row">
                <span class="detail-label">Status:</span> 
                <span class="status status-<?= $dpr['status'] ?>"><?= htmlspecialchars($dpr['status']) ?></span>
            </div>
            <div class="detail-row">
                <span class="detail-label">Weather:</span> <?= htmlspecialchars($dpr['weather_condition'] ?: '—') ?>
            </div>
            <div class="detail-row">
                <span class="detail-label">Manpower:</span> <?= $dpr['manpower_count'] ?: '—' ?> workers
            </div>
        </div>

        <div class="section-title">Work Description</div>
        <div class="detail-card">
            <?= nl2br(htmlspecialchars($dpr['work_description'])) ?: '<em>No description</em>' ?>
        </div>

        <div class="section-title">Materials Consumed</div>
        <div class="detail-card">
            <?= nl2br(htmlspecialchars($dpr['material_consumed_summary'])) ?: '<em>No materials recorded</em>' ?>
        </div>

        <div class="section-title">Remarks</div>
        <div class="detail-card">
            <?= nl2br(htmlspecialchars($dpr['remarks'])) ?: '<em>No remarks</em>' ?>
        </div>

        <div style="margin-top:30px;">
            <?php if ($dpr['status'] === 'Pending'): ?>
                <a href="dpr_edit.php?id=<?= $dpr_id ?>" class="btn" style="background:var(--warning);color:white;padding:12px 20px;border-radius:8px;text-decoration:none;">
                    <i class="fas fa-edit"></i> Edit DPR
                </a>
            <?php endif; ?>
        </div>
    </div>
</body>
</html>