<?php
require_once '../includes/functions.php';
requireLogin();
requireAdmin();

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

$id = $_GET['id'];
$stmt = $pdo->prepare("SELECT * FROM certificates WHERE id = ?");
$stmt->execute([$id]);
$cert = $stmt->fetch();

if (!$cert) {
    die("Certificate not found.");
}

$msg = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Collect Data
    $reg_no = $_POST['reg_no'];
    $name = $_POST['name'];
    $gender = $_POST['gender'];
    $dob = $_POST['dob'];
    $place = $_POST['place'];
    $date_reg = $_POST['date_reg'];
    $mother = $_POST['mother'];
    $father = $_POST['father'];
    $present_address = $_POST['present_address'];
    $permanent_address = $_POST['permanent_address'];
    $issue_date = $_POST['issue_date'];
    $status = $_POST['status'];

    $sql = "UPDATE certificates SET registration_no=?, name=?, gender=?, dob=?, place_of_birth=?, date_of_registration=?, mother_name=?, father_name=?, present_address=?, permanent_address=?, date_of_issue=?, status=? WHERE id=?";
    $update = $pdo->prepare($sql);
    
    try {
        $update->execute([$reg_no, $name, $gender, $dob, $place, $date_reg, $mother, $father, $present_address, $permanent_address, $issue_date, $status, $id]);
        $msg = "<div class='alert alert-success'>Certificate updated successfully.</div>";
        // Refresh data
        $stmt->execute([$id]);
        $cert = $stmt->fetch();
    } catch (Exception $e) {
        $msg = "<div class='alert alert-danger'>Error updating certificate: " . $e->getMessage() . "</div>";
    }
}

include '../includes/header.php';
?>

<div class="row justify-content-center">
    <div class="col-md-10">
        <div class="card p-4">
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h4>Edit Certificate</h4>
                <a href="manage_certificates.php" class="btn btn-secondary btn-sm">Back</a>
            </div>
            
            <?php echo $msg; ?>

            <form method="POST">
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Registration No</label>
                        <input type="text" name="reg_no" class="form-control" value="<?php echo htmlspecialchars($cert['registration_no']); ?>" required>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Date of Registration</label>
                        <input type="date" name="date_reg" class="form-control" value="<?php echo $cert['date_of_registration']; ?>" required>
                    </div>
                </div>

                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Name</label>
                        <input type="text" name="name" class="form-control" value="<?php echo htmlspecialchars($cert['name']); ?>" required>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Gender</label>
                        <select name="gender" class="form-select" required>
                            <option value="Male / पुस्र्ष" <?php echo $cert['gender'] == 'Male / पुस्र्ष' ? 'selected' : ''; ?>>Male / पुस्र्ष</option>
                            <option value="Female / महिला" <?php echo $cert['gender'] == 'Female / महिला' ? 'selected' : ''; ?>>Female / महिला</option>
                        </select>
                    </div>
                </div>

                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Date of Birth</label>
                        <input type="date" name="dob" class="form-control" value="<?php echo $cert['dob']; ?>" required>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Place of Birth</label>
                        <input type="text" name="place" class="form-control" value="<?php echo htmlspecialchars($cert['place_of_birth']); ?>" required>
                    </div>
                </div>

                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Mother's Name</label>
                        <input type="text" name="mother" class="form-control" value="<?php echo htmlspecialchars($cert['mother_name']); ?>" required>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Father's Name</label>
                        <input type="text" name="father" class="form-control" value="<?php echo htmlspecialchars($cert['father_name']); ?>" required>
                    </div>
                </div>

                <div class="mb-3">
                    <label class="form-label">Present Address</label>
                    <textarea name="present_address" class="form-control" rows="2" required><?php echo htmlspecialchars($cert['present_address']); ?></textarea>
                </div>

                <div class="mb-3">
                    <label class="form-label">Permanent Address</label>
                    <textarea name="permanent_address" class="form-control" rows="2" required><?php echo htmlspecialchars($cert['permanent_address']); ?></textarea>
                </div>

                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Date of Issue</label>
                        <input type="datetime-local" name="issue_date" class="form-control" value="<?php echo date('Y-m-d\TH:i:s', strtotime($cert['date_of_issue'])); ?>" required>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Status</label>
                        <select name="status" class="form-select">
                            <option value="active" <?php echo $cert['status'] == 'active' ? 'selected' : ''; ?>>Active</option>
                            <option value="deleted" <?php echo $cert['status'] == 'deleted' ? 'selected' : ''; ?>>Deleted</option>
                        </select>
                    </div>
                </div>

                <button type="submit" class="btn btn-primary w-100">Update Certificate</button>
            </form>
        </div>
    </div>
</div>

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