<?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");

$success = "";
$error = "";
$wbs = null;

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

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

// Fetch existing WBS task
$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();

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $task = trim($_POST["task"]);
    $wbs_start_date = $_POST["wbs_start_date"] ?: null;
    $wbs_end_date = $_POST["wbs_end_date"] ?: null;
    $wbs_action = trim($_POST["wbs_action"]);
    $wbs_percentage_done = trim($_POST["wbs_percentage_done"]);
    $wbs_status = $_POST["wbs_status"];
    

    // Validation
    if (empty($task)) {
        $error = "Task name is required.";
    } elseif (!empty($wbs_percentage_done) && (!is_numeric($wbs_percentage_done) || $wbs_percentage_done < 0 || $wbs_percentage_done > 100)) {
        $error = "Percentage Done must be between 0 and 100.";
    } else {
        $percentage = $wbs_percentage_done === '' ? null : (float)$wbs_percentage_done;

        try {
            $query = "UPDATE wbs_master SET 
                        task = ?, 
                        wbs_start_date = ?, 
                        wbs_end_date = ?, 
                        wbs_action = ?, 
                        wbs_percentage_done = ?, 
                        wbs_status = ?
                      WHERE id = ?";
                        

            $stmt = $conn->prepare($query);
            $stmt->bind_param(
                "ssssdsi",
                $task,
                $wbs_start_date,
                $wbs_end_date,
                $wbs_action,
                $percentage,
                $wbs_status,
                $id
                
            );

            if ($stmt->execute()) {
                $success = "WBS task updated successfully!";
                // Refresh data
                $wbs['task'] = $task;
                $wbs['wbs_start_date'] = $wbs_start_date;
                $wbs['wbs_end_date'] = $wbs_end_date;
                $wbs['wbs_action'] = $wbs_action;
                $wbs['wbs_percentage_done'] = $percentage;
                $wbs['wbs_status'] = $wbs_status;
                
            } else {
                $error = "Failed to update WBS task.";
            }
            $stmt->close();
        } catch (mysqli_sql_exception $e) {
            $error = "Database Error: " . $e->getMessage();
        }
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Edit 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;
            --success: #22c55e;
            --error: #ef4444;
            --text: #ffffff;
            --glass-bg: rgba(255, 255, 255, 0.12);
            --glass-border: rgba(255, 255, 255, 0.25);
        }

        * { 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: 600px;
            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: rgba(255, 255, 255, 0.15);
            padding: 40px 30px;
            text-align: center;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
        }

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

        .header p {
            opacity: 0.9;
            font-size: 16px;
        }

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

        .message {
            padding: 16px 20px;
            border-radius: 14px;
            margin-bottom: 30px;
            display: flex;
            align-items: center;
            font-weight: 600;
            backdrop-filter: blur(8px);
        }

        .success { background: rgba(34, 197, 94, 0.25); border: 1px solid rgba(74, 222, 128, 0.5); color: #86efac; }
        .error { background: rgba(239, 68, 68, 0.25); border: 1px solid rgba(252, 165, 165, 0.5); color: #fca5a5; }

        .message i { margin-right: 12px; font-size: 20px; }

        .form-group { margin-bottom: 24px; }

        label {
            display: block;
            margin-bottom: 10px;
            font-weight: 600;
            font-size: 15px;
        }

        .required { color: #fca5a5; }

        input, select {
            width: 100%;
            padding: 16px 18px;
            background: rgba(255, 255, 255, 0.15);
            border: 1px solid rgba(255, 255, 255, 0.3);
            border-radius: 14px;
            color: white;
            font-size: 16px;
            backdrop-filter: blur(8px);
            transition: all 0.3s ease;
        }

        input::placeholder { color: rgba(255, 255, 255, 0.7); }

        input:focus, select:focus {
            outline: none;
            border-color: var(--primary);
            background: rgba(255, 255, 255, 0.25);
            box-shadow: 0 0 0 4px rgba(139, 92, 246, 0.3);
        }

        input[readonly] {
            background: rgba(255, 255, 255, 0.08);
            cursor: not-allowed;
        }

        .btn-group {
            display: flex;
            gap: 15px;
            margin-top: 30px;
        }

        .submit-btn, .cancel-btn {
            flex: 1;
            padding: 18px;
            border: none;
            border-radius: 14px;
            font-size: 18px;
            font-weight: 700;
            cursor: pointer;
            transition: all 0.4s ease;
            display: flex;
            align-items: center;
            justify-content: center;
            box-shadow: 0 10px 30px rgba(139, 92, 246, 0.4);
        }

        .submit-btn {
            background: linear-gradient(135deg, var(--primary), #a78bfa);
            color: white;
        }

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

        .cancel-btn {
            background: rgba(255, 255, 255, 0.15);
            color: white;
            text-decoration: none;
        }

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

        .submit-btn i, .cancel-btn i { margin-right: 10px; }

        @media (max-width: 480px) {
            .glass-container { margin: 10px; border-radius: 20px; }
            .header, .form-area { padding: 30px 25px; }
            .header h1 { font-size: 28px; }
            .btn-group { flex-direction: column; }
        }
    </style>
</head>
<body>
    <div class="glass-container">
        <div class="header">
            <h1>Edit WBS Task</h1>
            <p>Updating <strong><?= htmlspecialchars($wbs['wbs_code']) ?> - <?= htmlspecialchars($wbs['task']) ?></strong></p>
        </div>

        <div class="form-area">
            <?php if ($success): ?>
                <div class="message success">
                    <i class="fas fa-check-circle"></i>
                    <?= htmlspecialchars($success) ?>
                </div>
            <?php endif; ?>

            <?php if ($error): ?>
                <div class="message error">
                    <i class="fas fa-exclamation-triangle"></i>
                    <?= htmlspecialchars($error) ?>
                </div>
            <?php endif; ?>

            <form action="" method="post">
                <div class="form-group">
                    <label for="wbs_code">WBS Code</label>
                    <input type="text" 
                           value="<?= htmlspecialchars($wbs['wbs_code']) ?>" 
                           readonly>
                </div>

                <div class="form-group">
                    <label for="task">Task Name <span class="required">*</span></label>
                    <input type="text" 
                           name="task" 
                           id="task" 
                           value="<?= htmlspecialchars($wbs['task']) ?>" 
                           required 
                           placeholder="Enter task name">
                </div>

                <div class="form-group">
                    <label for="wbs_start_date">Start Date</label>
                    <input type="date" 
                           name="wbs_start_date" 
                           id="wbs_start_date" 
                           value="<?= htmlspecialchars($wbs['wbs_start_date'] ?? '') ?>">
                </div>

                <div class="form-group">
                    <label for="wbs_end_date">End Date</label>
                    <input type="date" 
                           name="wbs_end_date" 
                           id="wbs_end_date" 
                           value="<?= htmlspecialchars($wbs['wbs_end_date'] ?? '') ?>">
                </div>

                <div class="form-group">
                    <label for="wbs_action">Action / Description</label>
                    <input type="text" 
                           name="wbs_action" 
                           id="wbs_action" 
                           value="<?= htmlspecialchars($wbs['wbs_action'] ?? '') ?>" 
                           placeholder="Optional details">
                </div>

                <div class="form-group">
                    <label for="wbs_percentage_done">Percentage Done (%)</label>
                    <input type="number" 
                           step="0.01" 
                           min="0" 
                           max="100" 
                           name="wbs_percentage_done" 
                           id="wbs_percentage_done" 
                           value="<?= $wbs['wbs_percentage_done'] !== null ? $wbs['wbs_percentage_done'] : '' ?>" 
                           placeholder="e.g. 75.5">
                </div>


                <div class="form-group">
                    <label for="wbs_status">Status <span class="required">*</span></label>
                    <select name="wbs_status" id="wbs_status" required>
                        <option value="Pending" <?= $wbs['wbs_status'] == 'Pending' ? 'selected' : '' ?>>Pending</option>
                        <option value="Completed" <?= $wbs['wbs_status'] == 'Completed' ? 'selected' : '' ?>>Completed</option>
                    </select>
                </div>

                <div class="btn-group">
                    <button type="submit" class="submit-btn">
                        <i class="fas fa-save"></i>
                        Save Changes
                    </button>
                    <a href="work-breakdown-management.php" class="cancel-btn">
                        <i class="fas fa-times"></i>
                        Cancel
                    </a>
                </div>
            </form>
        </div>
    </div>
</body>
</html>