<?php 
require_once '../db_connect.php';
include 'includes/header.php'; 
include 'includes/sidebar.php'; 

$id = $_GET['id'] ?? null;
if (!$id) { echo "<script>window.location.href='view_admissions.php';</script>"; exit; }

// Fetch current data
$stmt = $pdo->prepare("SELECT * FROM admissions WHERE id = ?");
$stmt->execute([$id]);
$data = $stmt->fetch();

if (!$data) { die("<div class='p-5 text-center fw-bold text-danger'>Admission request not found!</div>"); }

// Fetch active courses for dropdown
$courses = $pdo->query("SELECT id, title FROM courses WHERE status='active'")->fetchAll(PDO::FETCH_ASSOC);

$msg = ""; $msgType = "";

// Data Update Logic
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    try {
        $sql = "UPDATE admissions SET 
                student_name = ?, phone = ?, guardian_phone = ?, email = ?, 
                course_id = ?, admission_type = ?, father_husband_name = ?, 
                mother_name = ?, permanent_address = ?, present_address = ?, 
                edu_ssc = ?, edu_hsc = ?, payment_status = ?, status = ?,
                payable_fee = ?, payment_method = ?, transaction_id = ?, referred_by = ?
                WHERE id = ?";
        
        $pdo->prepare($sql)->execute([
            $_POST['student_name'], $_POST['phone'], $_POST['guardian_phone'], $_POST['email'],
            (int)$_POST['course_id'], $_POST['admission_type'], $_POST['father_husband_name'],
            $_POST['mother_name'], $_POST['permanent_address'], $_POST['present_address'],
            $_POST['edu_ssc'], $_POST['edu_hsc'], $_POST['payment_status'], $_POST['status'],
            $_POST['payable_fee'], $_POST['payment_method'], $_POST['transaction_id'], $_POST['referred_by'], $id
        ]);
        
        $msg = "Student information updated successfully!"; 
        $msgType = "success";
        
        // Reload updated data
        $stmt->execute([$id]); 
        $data = $stmt->fetch();
    } catch (PDOException $e) { 
        $msg = "Error: " . $e->getMessage(); 
        $msgType = "danger"; 
    }
}
?>

<style>
    /* Premium Theme Colors */
    :root { --theme-green: #064e3b; --theme-dark: #022c22; --theme-gold: #d4af37; --bg-soft: #f8fafc; }
    
    .main-content { background-color: var(--bg-soft); min-height: 100vh; }
    .dashboard-header { background: #fff; padding: 20px 30px; border-radius: 15px; box-shadow: 0 5px 20px rgba(0,0,0,0.03); margin-bottom: 35px; border-left: 6px solid var(--theme-green); }
    .custom-card { background: #fff; border: 1px solid #e2e8f0; border-radius: 18px; box-shadow: 0 10px 40px rgba(0,0,0,0.05); padding: 35px; border-top: 6px solid var(--theme-green); }
    
    .section-title { font-weight: 800; color: var(--theme-dark); font-size: 15px; margin-top: 30px; border-bottom: 2px dashed #e2e8f0; padding-bottom: 10px; margin-bottom: 25px; display: flex; align-items: center; text-transform: uppercase; letter-spacing: 1px; }
    .section-title i { color: var(--theme-gold); margin-right: 12px; font-size: 18px; }
    
    .form-label { font-weight: 700; color: #64748b; font-size: 11px; text-transform: uppercase; letter-spacing: 0.5px; margin-bottom: 8px; }
    .form-control, .form-select { border-radius: 10px; padding: 12px 18px; border: 2px solid #f1f5f9; background: var(--bg-soft); transition: 0.3s; font-size: 14px; color: var(--theme-dark); font-weight: 500;}
    .form-control:focus, .form-select:focus { border-color: var(--theme-green); background: #fff; box-shadow: 0 0 0 4px rgba(6, 78, 59, 0.1); outline: none; }
    
    .btn-update { background: var(--theme-green); color: #fff; padding: 15px 45px; border-radius: 50px; font-weight: 800; font-size: 16px; border: none; transition: 0.3s; box-shadow: 0 8px 20px rgba(6, 78, 59, 0.2); }
    .btn-update:hover { background: var(--theme-dark); color: var(--theme-gold); transform: translateY(-3px); }

    .fade-up { animation: fadeInUp 0.5s ease-out forwards; }
    @keyframes fadeInUp { from { opacity: 0; transform: translateY(20px); } to { opacity: 1; transform: translateY(0); } }

    /* --- 📱 Mobile Responsive Fixes --- */
    @media (max-width: 768px) {
        .main-content { padding: 15px !important; }
        .dashboard-header { flex-direction: column; align-items: flex-start !important; gap: 15px; padding: 15px 20px; }
        .dashboard-header .d-flex { width: 100%; align-items: center; }
        .dashboard-header > a.btn { width: 100%; justify-content: center; } 
        
        /* Fixed Square Menu Button */
        #sidebarToggle { width: 42px !important; height: 42px !important; padding: 0; display: inline-flex !important; align-items: center; justify-content: center; flex-shrink: 0; border-radius: 8px;}
        .custom-card { padding: 25px 20px !important; }
        .btn-update { width: 100%; }
    }
</style>

<div class="main-content p-4">
    <div class="dashboard-header d-flex justify-content-between align-items-center fade-up">
        <div class="d-flex align-items-center">
            <button class="btn btn-light border d-md-none me-3 shadow-sm" id="sidebarToggle">
                <i class="fas fa-bars text-dark"></i>
            </button>
            <h4 class="mb-0 fw-bold text-dark">Edit Admission (ID: <?= $id ?>)</h4>
        </div>
        <a href="view_admissions.php" class="btn btn-light border px-4 py-2 rounded-pill fw-bold shadow-sm text-dark text-decoration-none">
            <i class="fas fa-list me-2"></i> Back to List
        </a>
    </div>

    <?php if($msg): ?> 
        <div class="alert alert-<?= $msgType ?> border-0 border-start border-4 border-<?= $msgType ?> shadow-sm mb-4 fw-bold p-3 fade-up bg-white">
            <i class="fas fa-info-circle me-2"></i> <?= $msg ?>
        </div> 
    <?php endif; ?>

    <div class="custom-card fade-up">
        <form method="POST">
            
            <div class="section-title mt-0"><i class="fas fa-user-circle"></i> Personal Information</div>
            <div class="row g-4">
                <div class="col-md-4">
                    <label class="form-label">Student Full Name *</label>
                    <input type="text" name="student_name" class="form-control" value="<?= htmlspecialchars($data['student_name']) ?>" required>
                </div>
                <div class="col-md-4">
                    <label class="form-label">Phone Number *</label>
                    <input type="text" name="phone" class="form-control" value="<?= htmlspecialchars($data['phone']) ?>" required>
                </div>
                <div class="col-md-4">
                    <label class="form-label">Guardian Phone</label>
                    <input type="text" name="guardian_phone" class="form-control" value="<?= htmlspecialchars($data['guardian_phone'] ?? '') ?>">
                </div>
                <div class="col-md-6">
                    <label class="form-label">Father / Husband Name</label>
                    <input type="text" name="father_husband_name" class="form-control" value="<?= htmlspecialchars($data['father_husband_name']) ?>">
                </div>
                <div class="col-md-6">
                    <label class="form-label">Mother's Name</label>
                    <input type="text" name="mother_name" class="form-control" value="<?= htmlspecialchars($data['mother_name']) ?>">
                </div>
                <div class="col-md-12">
                    <label class="form-label">Email Address</label>
                    <input type="email" name="email" class="form-control" value="<?= htmlspecialchars($data['email']) ?>">
                </div>
            </div>

            <div class="section-title"><i class="fas fa-map-marked-alt"></i> Address & Academic Info</div>
            <div class="row g-4">
                <div class="col-md-6">
                    <label class="form-label">SSC / Equivalent Info</label>
                    <input type="text" name="edu_ssc" class="form-control" value="<?= htmlspecialchars($data['edu_ssc']) ?>">
                </div>
                <div class="col-md-6">
                    <label class="form-label">HSC / Equivalent Info</label>
                    <input type="text" name="edu_hsc" class="form-control" value="<?= htmlspecialchars($data['edu_hsc']) ?>">
                </div>
                <div class="col-md-6">
                    <label class="form-label">Permanent Address</label>
                    <textarea name="permanent_address" class="form-control" rows="2"><?= htmlspecialchars($data['permanent_address']) ?></textarea>
                </div>
                <div class="col-md-6">
                    <label class="form-label">Present Address</label>
                    <textarea name="present_address" class="form-control" rows="2"><?= htmlspecialchars($data['present_address'] ?? '') ?></textarea>
                </div>
            </div>

            <div class="section-title"><i class="fas fa-graduation-cap"></i> Course & Application Settings</div>
            <div class="row g-4">
                <div class="col-md-3">
                    <label class="form-label">Assigned Course</label>
                    <select name="course_id" class="form-select fw-bold" style="color: var(--theme-green);">
                        <?php foreach($courses as $c): ?>
                            <option value="<?= $c['id'] ?>" <?= $c['id'] == $data['course_id'] ? 'selected' : '' ?>><?= htmlspecialchars($c['title']) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="col-md-3">
                    <label class="form-label">Admission Type</label>
                    <select name="admission_type" class="form-select">
                        <option value="Online" <?= $data['admission_type'] == 'Online' ? 'selected' : '' ?>>Online</option>
                        <option value="Offline" <?= $data['admission_type'] == 'Offline' ? 'selected' : '' ?>>Offline</option>
                    </select>
                </div>
                <div class="col-md-3">
                    <label class="form-label">Payable Fee (BDT)</label>
                    <input type="number" name="payable_fee" class="form-control" value="<?= $data['payable_fee'] ?>">
                </div>
                <div class="col-md-3">
                    <label class="form-label">Referred By</label>
                    <input type="text" name="referred_by" class="form-control" value="<?= htmlspecialchars($data['referred_by'] ?? '') ?>">
                </div>

                <div class="col-md-4">
                    <label class="form-label">Payment Method</label>
                    <input type="text" name="payment_method" class="form-control" value="<?= htmlspecialchars($data['payment_method'] ?? '') ?>">
                </div>
                <div class="col-md-4">
                    <label class="form-label">Transaction ID</label>
                    <input type="text" name="transaction_id" class="form-control" value="<?= htmlspecialchars($data['transaction_id'] ?? '') ?>">
                </div>
                <div class="col-md-4">
                    <label class="form-label">Payment Status</label>
                    <select name="payment_status" class="form-select fw-bold">
                        <option value="unpaid" <?= $data['payment_status'] == 'unpaid' ? 'selected' : '' ?>>Unpaid</option>
                        <option value="pending" <?= $data['payment_status'] == 'pending' ? 'selected' : '' ?>>Pending</option>
                        <option value="verified" <?= $data['payment_status'] == 'verified' ? 'selected' : '' ?> class="text-success">Verified</option>
                    </select>
                </div>
                <div class="col-12">
                    <label class="form-label">Overall Application Status</label>
                    <select name="status" class="form-select fw-bold text-primary">
                        <option value="pending" <?= $data['status'] == 'pending' ? 'selected' : '' ?>>Pending Review</option>
                        <option value="approved" <?= $data['status'] == 'approved' ? 'selected' : '' ?> class="text-success">Approved</option>
                        <option value="rejected" <?= $data['status'] == 'rejected' ? 'selected' : '' ?> class="text-danger">Rejected</option>
                    </select>
                </div>
            </div>

            <div class="mt-5 text-center border-top pt-4">
                <button type="submit" class="btn-update shadow">
                    <i class="fas fa-save me-2"></i> Update Admission Records
                </button>
            </div>
        </form>
    </div>
</div>

<script>
    document.addEventListener("DOMContentLoaded", function() {
        const toggleBtn = document.getElementById('sidebarToggle');
        if (toggleBtn) {
            toggleBtn.addEventListener('click', function(e) {
                e.preventDefault();
                const sidebar = document.querySelector('.sidebar');
                if (sidebar) { sidebar.classList.toggle('active'); }
            });
        }
    });
</script>

<?php include 'includes/footer.php'; ?>