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

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

if (!isset($_GET['id']) || empty($_GET['id'])) {
    header("Location: dpr_list.php");
    exit();
}

$dpr_id = (int)$_GET['id'];

// Fetch DPR Master
$dpr = $conn->query("
    SELECT d.*, p.project_code, p.project_name, u.full_name as created_by_name
    FROM dpr_master d
    LEFT JOIN project_master p ON d.project_id = p.id
    LEFT JOIN users u ON d.created_by = u.id
    WHERE d.id = $dpr_id
")->fetch_assoc();

if (!$dpr) {
    die("<h2 style='color:red;text-align:center;margin-top:50px;'>DPR Not Found!</h2>");
}

// Fetch All Sections
$progress = $conn->query("SELECT * FROM dpr_progress WHERE dpr_id = $dpr_id ORDER BY sr_no ASC");
$materials = $conn->query("SELECT * FROM dpr_major_material WHERE dpr_id = $dpr_id ORDER BY sr_no ASC");
$issues = $conn->query("SELECT * FROM dpr_critical_issues WHERE dpr_id = $dpr_id ORDER BY sr_no ASC");
$manpower = $conn->query("SELECT * FROM dpr_manpower WHERE dpr_id = $dpr_id ORDER BY sr_no ASC");
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>DPR #<?= $dpr['id'] ?> - <?= date('d M Y', strtotime($dpr['report_date'])) ?></title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
    
    <style>
        :root {
            --primary: #a855f7;
            --accent: #c084fc;
            --glass: rgba(255, 255, 255, 0.10);
            --glass-border: rgba(255, 255, 255, 0.20);
        }
        * { margin:0; padding:0; box-sizing:border-box; }
        body {
            font-family: 'Inter', system-ui, sans-serif;
            background: linear-gradient(135deg, rgba(0,0,0,0.78), rgba(0,0,0,0.92)), url('../background.jpeg') center/cover fixed;
            color: #f1f5f9;
            padding: 20px 15px;
        }
        .container { max-width: 1300px; margin: auto; }
        .glass-container {
            background: var(--glass);
            backdrop-filter: blur(24px);
            border-radius: 32px;
            border: 1px solid var(--glass-border);
            box-shadow: 0 25px 80px rgba(0,0,0,0.65);
            overflow: hidden;
        }
        .header {
            background: linear-gradient(90deg, rgba(168,85,247,0.35), rgba(147,51,234,0.25));
            padding: 40px;
            text-align: center;
        }
        .header h1 { font-size: 2.4rem; font-weight: 700; }

        .content { padding: 40px; }
        .info-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
            gap: 20px;
            margin-bottom: 35px;
        }
        .info-box {
            background: rgba(255,255,255,0.08);
            padding: 18px 22px;
            border-radius: 16px;
            border: 1px solid rgba(255,255,255,0.15);
        }
        .info-box label { display: block; font-size: 0.9rem; opacity: 0.8; margin-bottom: 4px; }

        table {
            width: 100%;
            border-collapse: collapse;
            background: rgba(0,0,0,0.25);
            border-radius: 16px;
            overflow: hidden;
            margin-bottom: 30px;
        }
        th {
            background: rgba(168,85,247,0.3);
            padding: 16px;
            text-align: left;
        }
        td {
            padding: 14px 16px;
            border-bottom: 1px solid rgba(255,255,255,0.1);
        }
        .section-title {
            color: #c4b5fd;
            margin: 30px 0 15px;
            font-size: 1.4rem;
        }

        .btn {
            padding: 12px 24px;
            border-radius: 12px;
            text-decoration: none;
            font-weight: 600;
            display: inline-flex;
            align-items: center;
            gap: 8px;
        }
        .btn-back { background: rgba(255,255,255,0.15); color: white; }
        .btn-print { background: linear-gradient(135deg, #22c55e, #86efac); color: black; }

        @media (max-width: 768px) {
            .header h1 { font-size: 2rem; }
            .content { padding: 25px 18px; }
        }
    </style>
</head>
<body>
<?php include('../config/header.php'); ?>
<div class="container">
    <div class="glass-container">
        <div class="header">
            <h1><i class="fas fa-file-alt"></i> Daily Progress Report</h1>
        </div>

        <div class="content">
            <div style="margin-bottom: 25px;">
                <a href="dpr_list.php" class="btn btn-back">
                    <i class="fas fa-arrow-left"></i> Back to List
                </a>
                <!-- <button onclick="window.print()" class="btn btn-print">
                    <i class="fas fa-print"></i> Print DPR
                </button> -->
            </div>

            <!-- DPR Info -->
            <div class="info-grid">
                <div class="info-box">
                    <label>Report Date</label>
                    <strong><?= date('d M Y', strtotime($dpr['report_date'])) ?></strong>
                </div>
                <div class="info-box">
                    <label>Project</label>
                    <strong><?= htmlspecialchars($dpr['project_code'] ?? '') ?> - <?= htmlspecialchars($dpr['project_name'] ?? 'No Project') ?></strong>
                </div>
                <div class="info-box">
                    <label>Created By</label>
                    <strong><?= htmlspecialchars($dpr['created_by_name'] ?? 'Unknown') ?></strong>
                </div>
                <div class="info-box">
                    <label>Created At</label>
                    <strong><?= date('d M Y H:i', strtotime($dpr['created_at'])) ?></strong>
                </div>
            </div>

            <!-- 1. Progress Report -->
            <h2 class="section-title">1. Progress Report</h2>
            <table>
                <thead>
                    <tr>
                        <th>Sr No</th>
                        <th>Activities</th>
                        <th>UOM</th>
                        <th>Scope</th>
                        <th>Clear Scope</th>
                        <th>Prev Day</th>
                        <th>Today</th>
                        <th>Upto Date</th>
                        <th>Balance</th>
                        <th>Remarks</th>
                    </tr>
                </thead>
                <tbody>
                    <?php while ($row = $progress->fetch_assoc()): ?>
                    <tr>
                        <td><?= $row['sr_no'] ?></td>
                        <td><?= nl2br(htmlspecialchars($row['activities'])) ?></td>
                        <td><?= $row['uom_id'] ? $row['uom_id'] : '-' ?></td>
                        <td><?= number_format($row['scope_qty'], 2) ?></td>
                        <td><?= number_format($row['clear_scope'], 2) ?></td>
                        <td><?= number_format($row['completed_till_previous_day'], 2) ?></td>
                        <td><?= number_format($row['achieved_for_the_day'], 2) ?></td>
                        <td><?= number_format($row['achieved_upto_date'], 2) ?></td>
                        <td><?= number_format($row['balance'], 2) ?></td>
                        <td><?= htmlspecialchars($row['remarks']) ?></td>
                    </tr>
                    <?php endwhile; ?>
                </tbody>
            </table>

            <!-- 2. Major Materials -->
            <h2 class="section-title">2. Major Materials Used</h2>
            <table>
                <thead>
                    <tr>
                        <th>Sr No</th>
                        <th>Description</th>
                        <th>UOM</th>
                        <th>Quantity</th>
                    </tr>
                </thead>
                <tbody>
                    <?php while ($row = $materials->fetch_assoc()): ?>
                    <tr>
                        <td><?= $row['sr_no'] ?></td>
                        <td><?= nl2br(htmlspecialchars($row['description'])) ?></td>
                        <td><?= $row['uom_id'] ?></td>
                        <td><?= number_format($row['quantity'], 2) ?></td>
                    </tr>
                    <?php endwhile; ?>
                </tbody>
            </table>

            <!-- 3. Critical Issues -->
            <h2 class="section-title">3. Critical Issues / Delays</h2>
            <table>
                <thead>
                    <tr>
                        <th>Sr No</th>
                        <th>Description</th>
                    </tr>
                </thead>
                <tbody>
                    <?php while ($row = $issues->fetch_assoc()): ?>
                    <tr>
                        <td><?= $row['sr_no'] ?></td>
                        <td><?= nl2br(htmlspecialchars($row['description'])) ?></td>
                    </tr>
                    <?php endwhile; ?>
                </tbody>
            </table>

            <!-- 4. Manpower -->
            <h2 class="section-title">4. Manpower Deployed</h2>
            <table>
                <thead>
                    <tr>
                        <th>Sr No</th>
                        <th>Description</th>
                        <th>No. of Manpower</th>
                    </tr>
                </thead>
                <tbody>
                    <?php while ($row = $manpower->fetch_assoc()): ?>
                    <tr>
                        <td><?= $row['sr_no'] ?></td>
                        <td><?= nl2br(htmlspecialchars($row['description'])) ?></td>
                        <td><?= $row['no_of_manpower'] ?></td>
                    </tr>
                    <?php endwhile; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

</body>
</html>

<?php $conn->close(); ?>