<?php
ini_set('display_errors', 0);
error_reporting(E_ALL);
ini_set('log_errors', 1);
ini_set('error_log', 'php_errors.log');

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

$wbs = null;

if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    header("Location: work-breakdown-management.php");
    exit();
}

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

// Fetch WBS task details
$stmt = $conn->prepare("SELECT * FROM wbs_master WHERE id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows === 0) {
    header("Location: work-breakdown-management.php");
    exit();
}

$wbs = $result->fetch_assoc();
$stmt->close();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>View WBS Task - <?= htmlspecialchars($wbs['wbs_code']) ?></title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
    <style>
        :root {
            --primary: #8b5cf6;
            --primary-dark: #7c3aed;
            --text: #ffffff;
            --glass-bg: rgba(255, 255, 255, 0.12);
            --glass-border: rgba(255, 255, 255, 0.25);
            --label-color: rgba(255, 255, 255, 0.8);
            --value-color: #e9d5ff;
        }

        * { margin: 0; padding: 0; box-sizing: border-box; }

        body {
            font-family: 'Segoe UI', system-ui, sans-serif;
            min-height: 100vh;
            background: linear-gradient(rgba(0,0,0,0.6), rgba(0,0,0,0.7)),
                        url('../background.jpeg') center/cover no-repeat fixed;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
            color: var(--text);
        }

        .glass-container {
            width: 100%;
            max-width: 720px;
            background: var(--glass-bg);
            backdrop-filter: blur(16px);
            -webkit-backdrop-filter: blur(16px);
            border-radius: 24px;
            border: 1px solid var(--glass-border);
            box-shadow: 0 25px 60px rgba(0, 0, 0, 0.5);
            overflow: hidden;
        }

        .header {
            background: linear-gradient(135deg, var(--primary), #a78bfa);
            padding: 50px 30px;
            text-align: center;
            position: relative;
        }

        .header h1 {
            font-size: 36px;
            font-weight: 700;
            margin-bottom: 12px;
            text-shadow: 0 2px 10px rgba(0,0,0,0.3);
        }

        .header .code {
            font-size: 26px;
            font-weight: 600;
            opacity: 0.95;
            margin-bottom: 20px;
        }

        .status-badge {
            position: absolute;
            top: 20px;
            right: 20px;
            padding: 10px 22px;
            border-radius: 50px;
            font-weight: 600;
            font-size: 15px;
            text-transform: uppercase;
            letter-spacing: 0.6px;
        }

        .status-badge.completed {
            background: rgba(34, 197, 94, 0.3);
            color: #86efac;
            border: 1px solid #86efac40;
        }

        .status-badge.pending {
            background: rgba(245, 158, 11, 0.3);
            color: #fcd34d;
            border: 1px solid #fcd34d40;
        }

        .details-area {
            padding: 40px 35px;
        }

        .info-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 28px;
            margin-bottom: 40px;
        }

        .info-card {
            background: rgba(255, 255, 255, 0.08);
            padding: 26px;
            border-radius: 18px;
            border: 1px solid rgba(255, 255, 255, 0.15);
            backdrop-filter: blur(8px);
        }

        .info-card h3 {
            font-size: 19px;
            margin-bottom: 20px;
            opacity: 0.9;
            display: flex;
            align-items: center;
            gap: 12px;
        }

        .info-card h3 i {
            color: var(--primary);
            font-size: 22px;
        }

        .detail-row {
            margin-bottom: 20px;
        }

        .detail-row:last-child { margin-bottom: 0; }

        .label {
            font-size: 14px;
            opacity: 0.7;
            margin-bottom: 8px;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }

        .value {
            font-size: 18px;
            font-weight: 600;
            color: var(--value-color);
            word-break: break-word;
        }

        .value.empty {
            opacity: 0.6;
            font-style: italic;
            font-weight: normal;
        }

        .percentage-display {
            font-size: 32px;
            font-weight: 700;
            color: #c4b5fd;
            text-align: center;
            margin: 20px 0;
        }

        .actions {
            display: flex;
            gap: 15px;
            justify-content: center;
            flex-wrap: wrap;
        }

        .btn {
            padding: 16px 34px;
            border-radius: 14px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.4s ease;
            display: inline-flex;
            align-items: center;
            gap: 10px;
            text-decoration: none;
            min-width: 180px;
            justify-content: center;
        }

        .btn-back {
            background: rgba(255, 255, 255, 0.15);
            color: white;
            border: 1px solid rgba(255, 255, 255, 0.3);
        }

        .btn-back:hover {
            background: rgba(255, 255, 255, 0.25);
            transform: translateY(-4px);
        }

        .btn-edit {
            background: linear-gradient(135deg, var(--primary), #a78bfa);
            color: white;
            box-shadow: 0 10px 30px rgba(139, 92, 246, 0.4);
        }

        .btn-edit:hover {
            transform: translateY(-4px);
            box-shadow: 0 20px 40px rgba(139, 92, 246, 0.5);
            background: linear-gradient(135deg, var(--primary-dark), #9333ea);
        }

        @media (max-width: 480px) {
            .glass-container { margin: 10px; border-radius: 20px; }
            .header { padding: 40px 20px; }
            .header h1 { font-size: 30px; }
            .header .code { font-size: 22px; }
            .status-badge { position: static; display: inline-block; margin-top: 10px; }
            .details-area { padding: 30px 25px; }
            .info-grid { grid-template-columns: 1fr; }
            .actions { flex-direction: column; }
            .btn { width: 100%; }
        }
    </style>
</head>
<body>
    <div class="glass-container">
        <div class="header">
            <h1><?= htmlspecialchars($wbs['task']) ?></h1>
            <div class="code"><?= htmlspecialchars($wbs['wbs_code']) ?></div>
            <div class="status-badge <?= strtolower($wbs['wbs_status']) ?>">
                <?= htmlspecialchars($wbs['wbs_status']) ?>
            </div>
        </div>

        <div class="details-area">
            <div class="info-grid">
                <!-- Progress Card -->
                <div class="info-card">
                    <h3><i class="fas fa-chart-pie"></i> Progress</h3>
                    <div class="percentage-display">
                        <?= $wbs['wbs_percentage_done'] !== null 
                            ? number_format($wbs['wbs_percentage_done'], 1) . '%' 
                            : '—' ?>
                    </div>
                    <div class="detail-row">
                        <div class="label">Current Status</div>
                        <div class="value"><?= htmlspecialchars($wbs['wbs_status']) ?></div>
                    </div>
                </div>

                <!-- Timeline Card -->
                <div class="info-card">
                    <h3><i class="fas fa-calendar-alt"></i> Timeline</h3>
                    <div class="detail-row">
                        <div class="label">Start Date</div>
                        <div class="value">
                            <?= $wbs['wbs_start_date'] 
                                ? date('d M Y', strtotime($wbs['wbs_start_date'])) 
                                : '<span class="empty">Not set</span>' ?>
                        </div>
                    </div>
                    <div class="detail-row">
                        <div class="label">End Date</div>
                        <div class="value">
                            <?= $wbs['wbs_end_date'] 
                                ? date('d M Y', strtotime($wbs['wbs_end_date'])) 
                                : '<span class="empty">Not set</span>' ?>
                        </div>
                    </div>
                    
                </div>

                <!-- Description Card -->
                <div class="info-card">
                    <h3><i class="fas fa-tasks"></i> Task Details</h3>
                    <div class="detail-row">
                        <div class="label">Action / Description</div>
                        <div class="value">
                            <?= !empty($wbs['wbs_action']) 
                                ? nl2br(htmlspecialchars($wbs['wbs_action'])) 
                                : '<span class="empty">No description provided</span>' ?>
                        </div>
                    </div>
                </div>
            </div>

            <div class="actions">
                <a href="work-breakdown-management.php" class="btn btn-back">
                    <i class="fas fa-arrow-left"></i>
                    Back to WBS List
                </a>
                <a href="edit-wbs.php?id=<?= $wbs['id'] ?>" class="btn btn-edit">
                    <i class="fas fa-edit"></i>
                    Edit Task
                </a>
            </div>
        </div>
    </div>
</body>
</html>