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

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

$project_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($project_id <= 0) {
    die("<h2 style='color:#ef4444;text-align:center;padding:100px;'>No Project Selected</h2>");
}

// Fetch Project Details
$stmt = $conn->prepare("
    SELECT * FROM project_master WHERE id = ?
");
$stmt->bind_param("i", $project_id);
$stmt->execute();
$project = $stmt->get_result()->fetch_assoc();

if (!$project) {
    die("<h2 style='color:#ef4444;text-align:center;padding:100px;'>Project Not Found</h2>");
}

// Fetch Assigned Managers
$managers = $conn->query("
    SELECT u.full_name 
    FROM project_managers pm 
    JOIN users u ON pm.user_id = u.id 
    WHERE pm.project_id = $project_id
")->fetch_all(MYSQLI_ASSOC);

// Fetch Payment Milestones
$milestones = $conn->query("
    SELECT * FROM project_milestones 
    WHERE project_id = $project_id 
    ORDER BY due_date ASC
")->fetch_all(MYSQLI_ASSOC);

// Calculate Milestone Progress
$total_milestone_amount = 0;
$total_paid_amount = 0;
foreach ($milestones as $m) {
    $total_milestone_amount += $m['amount'];
    $total_paid_amount += $m['paid_amount'] ?? 0;
}
$progress_percentage = $total_milestone_amount > 0 ? round(($total_paid_amount / $total_milestone_amount) * 100) : 0;

// Fetch Attachments
$attachments = $conn->query("
    SELECT * FROM project_attachments 
    WHERE project_id = $project_id 
    ORDER BY created_at DESC
")->fetch_all(MYSQLI_ASSOC);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>View Project - <?= htmlspecialchars($project['project_name']) ?></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;
            min-height: 100vh;
        }
        .container { max-width: 1200px; 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: 50px 40px;
            text-align: center;
        }
        .header h1 { 
            font-size: clamp(28px, 5.5vw, 42px); 
            font-weight: 700; 
            margin-bottom: 8px;
        }

        .form-area { padding: 45px 40px; }

        .info-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
            gap: 20px;
            margin-bottom: 40px;
        }
        .info-card {
            background: rgba(255,255,255,0.08);
            border: 1px solid rgba(255,255,255,0.18);
            border-radius: 20px;
            padding: 25px;
        }
        .info-label {
            font-size: 0.95rem;
            color: #c4b5fd;
            margin-bottom: 6px;
        }
        .info-value {
            font-size: 1.15rem;
            color: #e0e7ff;
        }

        .status-badge {
            display: inline-block;
            padding: 8px 22px;
            border-radius: 50px;
            font-weight: 700;
            font-size: 1rem;
        }
        .status-Planning { background: #8b5cf6; color: white; }
        .status-In Progress, .status-Active { background: #22c55e; color: white; }
        .status-On Hold { background: #f59e0b; color: white; }
        .status-Completed { background: #15803d; color: white; }
        .status-Cancelled { background: #ef4444; color: white; }

        .section-title {
            font-size: 1.45rem;
            margin: 45px 0 20px;
            color: #e0e7ff;
            display: flex;
            align-items: center;
            gap: 12px;
        }

        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: 18px 16px;
            text-align: left;
        }
        td {
            padding: 16px;
            border-bottom: 1px solid rgba(255,255,255,0.1);
        }

        .milestone-status {
            padding: 6px 14px;
            border-radius: 30px;
            font-size: 0.9rem;
            font-weight: 600;
        }
        .status-Pending { background: #f59e0b; color: white; }
        .status-Paid { background: #22c55e; color: white; }
        .status-Overdue { background: #ef4444; color: white; }

        .attachment-list .file-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            background: rgba(255,255,255,0.06);
            padding: 16px;
            border-radius: 14px;
            margin-bottom: 12px;
        }

        .btn {
            padding: 14px 28px;
            border-radius: 14px;
            text-decoration: none;
            font-weight: 700;
            display: inline-flex;
            align-items: center;
            gap: 10px;
            margin-right: 12px;
        }
        .btn-edit {
            background: linear-gradient(135deg, var(--primary), var(--accent));
            color: white;
        }
        .btn-back {
            background: rgba(255,255,255,0.1);
            color: #e0e7ff;
            border: 1px solid rgba(255,255,255,0.3);
        }

        .progress-bar {
            height: 12px;
            background: rgba(255,255,255,0.1);
            border-radius: 20px;
            overflow: hidden;
            margin: 15px 0;
        }
        .progress-fill {
            height: 100%;
            background: linear-gradient(90deg, #a855f7, #c084fc);
            transition: width 0.6s ease;
        }

        @media (max-width: 768px) {
            .form-area { padding: 30px 20px; }
            .info-grid { grid-template-columns: 1fr; }
        }
    </style>
</head>
<body>
<?php include '../config/header.php'; ?>

<div class="container">
    <div class="glass-container">
        <div class="header">
            <a href="project-management.php" style="color:#c4b5fd; text-decoration:none; position:absolute; left:40px;">
                ← Back to Projects
            </a>
            <h1><i class="fas fa-folder-open"></i> Project Details</h1>
            <p style="margin-top:10px; opacity:0.9;"><?= htmlspecialchars($project['project_code']) ?></p>
        </div>

        <div class="form-area">

            <!-- Status -->
            <div style="text-align:center; margin-bottom:30px;">
                <span class="status-badge status-<?= str_replace(' ', '', htmlspecialchars($project['project_status'])) ?>">
                    <?= htmlspecialchars($project['project_status']) ?>
                </span>
            </div>

            <!-- Basic Info -->
            <div class="info-grid">
                <div class="info-card">
                    <div class="info-label">Project Name</div>
                    <div class="info-value"><?= htmlspecialchars($project['project_name']) ?></div>
                </div>
                <div class="info-card">
                    <div class="info-label">Client</div>
                    <div class="info-value"><?= htmlspecialchars($project['client_name'] ?? '—') ?></div>
                </div>
                <div class="info-card">
                    <div class="info-label">Project Managers</div>
                    <div class="info-value">
                        <?= !empty($managers) ? implode(', ', array_column($managers, 'full_name')) : 'Not Assigned' ?>
                    </div>
                </div>
                <div class="info-card">
                    <div class="info-label">Contract Value</div>
                    <div class="info-value">₹ <?= number_format($project['project_contract_value'] ?? 0, 2) ?></div>
                </div>
            </div>

            <!-- Dates & Address -->
            <div class="info-grid">
                <div class="info-card">
                    <div class="info-label">Start Date</div>
                    <div class="info-value"><?= $project['start_date'] ? date('d M Y', strtotime($project['start_date'])) : '—' ?></div>
                </div>
                <div class="info-card">
                    <div class="info-label">End Date</div>
                    <div class="info-value"><?= $project['end_date'] ? date('d M Y', strtotime($project['end_date'])) : '—' ?></div>
                </div>
                <div class="info-card">
                    <div class="info-label">PO Number</div>
                    <div class="info-value"><?= htmlspecialchars($project['po_number'] ?? '—') ?></div>
                </div>
                <div class="info-card">
                    <div class="info-label">GST Number</div>
                    <div class="info-value"><?= htmlspecialchars($project['gst_number'] ?? '—') ?></div>
                </div>
            </div>

            <?php if (!empty($project['project_address'])): ?>
            <div class="info-card" style="margin-bottom:40px;">
                <div class="info-label">Project Address</div>
                <div class="info-value"><?= nl2br(htmlspecialchars($project['project_address'])) ?></div>
            </div>
            <?php endif; ?>

            <!-- Payment Milestones -->
            <div class="section-title">
                <i class="fas fa-chart-line"></i> Payment Milestones
            </div>

            <?php if (!empty($milestones)): ?>
                <div class="progress-bar">
                    <div class="progress-fill" style="width: <?= $progress_percentage ?>%;"></div>
                </div>
                <p style="text-align:center; margin-bottom:20px;">
                    <strong><?= $progress_percentage ?>%</strong> of total milestones paid 
                    (₹ <?= number_format($total_paid_amount, 2) ?> / ₹ <?= number_format($total_milestone_amount, 2) ?>)
                </p>

                <table>
                    <thead>
                        <tr>
                            <th>Milestone</th>
                            <th>Amount (₹)</th>
                            <th>Due Date</th>
                            <th>Status</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($milestones as $ms): ?>
                        <tr>
                            <td><?= htmlspecialchars($ms['milestone_name']) ?></td>
                            <td style="font-weight:600;">₹ <?= number_format($ms['amount'], 2) ?></td>
                            <td><?= $ms['due_date'] ? date('d M Y', strtotime($ms['due_date'])) : '—' ?></td>
                            <td>
                                <span class="milestone-status status-<?= htmlspecialchars($ms['status']) ?>">
                                    <?= htmlspecialchars($ms['status']) ?>
                                </span>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            <?php else: ?>
                <p style="text-align:center; color:#94a3b8; padding:40px;">No payment milestones added yet.</p>
            <?php endif; ?>

            <!-- Attachments -->
            <div class="section-title">
                <i class="fas fa-paperclip"></i> Project Attachments
            </div>

            <?php if (!empty($attachments)): ?>
                <div class="attachment-list">
                    <?php foreach ($attachments as $att): ?>
                    <div class="file-item">
                        <div>
                            <i class="fas fa-file-pdf"></i> 
                            <?= htmlspecialchars($att['original_name']) ?>
                        </div>
                        <a href="../<?= htmlspecialchars($att['file_path']) ?>" 
                           target="_blank" 
                           class="btn" 
                           style="background:rgba(255,255,255,0.15); padding:10px 20px; font-size:0.95rem;">
                            <i class="fas fa-download"></i> Download
                        </a>
                    </div>
                    <?php endforeach; ?>
                </div>
            <?php else: ?>
                <p style="text-align:center; color:#94a3b8; padding:40px;">No attachments uploaded yet.</p>
            <?php endif; ?>

            <!-- Action Buttons -->
            <div style="text-align:center; margin-top:50px;">
                <a href="edit-project.php?id=<?= $project_id ?>" class="btn btn-edit">
                    <i class="fas fa-edit"></i> Edit Project
                </a>
                <a href="project-management.php" class="btn btn-back">
                    <i class="fas fa-arrow-left"></i> Back to Project List
                </a>
            </div>
        </div>
    </div>
</div>
</body>
</html>

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