<?php

ob_start();

require_once __DIR__ . '/../library/auth.php';

allow_roles(['admin', 'teacher']);

$school_id = $_SESSION['school_id'];

$page_title = 'Student Enquiry Management';

$error = '';
$success = '';

/*
|--------------------------------------------------------------------------
| DELETE ENQUIRY
|--------------------------------------------------------------------------
*/

//if (isset($_GET['delete'])) {
//
  //  $delete_id = (int) $_GET['delete'];

    //$deleteStmt = $pdo->prepare("
      //  DELETE FROM student_enquiries
        //WHERE id = ?
  //      AND school_id = ?
   // ");

  //  $deleteStmt->execute([
    //    $delete_id,
      //  $school_id
//    ]);

  //  header('Location: enquiry.php?msg=deleted');
    //exit;
//}

/*
|--------------------------------------------------------------------------
| EDIT MODE
|--------------------------------------------------------------------------
*/

$edit_mode = false;

$edit_data = [];

if (isset($_GET['edit'])) {

    $edit_id = (int) $_GET['edit'];

    $editStmt = $pdo->prepare("
        SELECT *
        FROM student_enquiries
        WHERE id = ?
        AND school_id = ?
        LIMIT 1
    ");

    $editStmt->execute([
        $edit_id,
        $school_id
    ]);

    $edit_data = $editStmt->fetch(PDO::FETCH_ASSOC);

    if ($edit_data) {

        $edit_mode = true;
    }
}

/*
|--------------------------------------------------------------------------
| INSERT / UPDATE
|--------------------------------------------------------------------------
*/

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $student_name = trim($_POST['student_name'] ?? '');

    $father_name = trim($_POST['father_name'] ?? '');

    $mobile_no = trim($_POST['mobile_no'] ?? '');

    $interested_class = trim($_POST['interested_class'] ?? '');

    $address = trim($_POST['address'] ?? '');

    $enquiry_date = trim($_POST['enquiry_date'] ?? '');

    $status = trim($_POST['status'] ?? 'New');

    $remarks = trim($_POST['remarks'] ?? '');

    if (
        empty($student_name) ||
        empty($mobile_no) ||
        empty($enquiry_date)
    ) {

        $error = 'Please fill all required fields.';
    }

    else {

        /*
        |--------------------------------------------------------------------------
        | UPDATE
        |--------------------------------------------------------------------------
        */

        if (isset($_POST['edit_id']) && !empty($_POST['edit_id'])) {

            $edit_id = (int) $_POST['edit_id'];

            $updateStmt = $pdo->prepare("
                UPDATE student_enquiries
                SET
                    student_name = ?,
                    father_name = ?,
                    mobile_no = ?,
                    interested_class = ?,
                    address = ?,
                    enquiry_date = ?,
                    status = ?,
                    remarks = ?
                WHERE id = ?
                AND school_id = ?
            ");

            $updateStmt->execute([

                $student_name,
                $father_name,
                $mobile_no,
                $interested_class,
                $address,
                $enquiry_date,
                $status,
                $remarks,
                $edit_id,
                $school_id
            ]);

            header('Location: enquiry.php?msg=updated');
            exit;
        }

        /*
        |--------------------------------------------------------------------------
        | INSERT
        |--------------------------------------------------------------------------
        */

        else {

            $insertStmt = $pdo->prepare("
                INSERT INTO student_enquiries
                (
                    school_id,
                    student_name,
                    father_name,
                    mobile_no,
                    interested_class,
                    address,
                    enquiry_date,
                    status,
                    remarks
                )
                VALUES
                (
                    ?, ?, ?, ?, ?, ?, ?, ?, ?
                )
            ");

            $insertStmt->execute([

                $school_id,
                $student_name,
                $father_name,
                $mobile_no,
                $interested_class,
                $address,
                $enquiry_date,
                $status,
                $remarks
            ]);

            header('Location: enquiry.php?msg=added');
            exit;
        }
    }
}

/*
|--------------------------------------------------------------------------
| DELETE ENQUIRY
|--------------------------------------------------------------------------
*/

if (
    $_SERVER['REQUEST_METHOD'] === 'POST' &&
    isset($_POST['delete_id'])
) {

    $delete_id = (int) $_POST['delete_id'];

    $deleteStmt = $pdo->prepare("
        DELETE FROM student_enquiries
        WHERE id = ?
        AND school_id = ?
    ");

    $deleteStmt->execute([
        $delete_id,
        $school_id
    ]);

    header('Location: enquiry.php?msg=deleted');
    exit;
}
/*
|--------------------------------------------------------------------------
| FETCH ENQUIRIES
|--------------------------------------------------------------------------
*/

$listStmt = $pdo->prepare("
    SELECT *
    FROM student_enquiries
    WHERE school_id = ?
    ORDER BY id DESC
");

$listStmt->execute([$school_id]);

$enquiries = $listStmt->fetchAll(PDO::FETCH_ASSOC);

require_once __DIR__ . '/../include/header.php';
require_once __DIR__ . '/../include/sidebar.php';

?>

<div class="content-wrapper">

    <section class="content-header">

        <div class="container-fluid">

            <h1>

                Student Enquiry Management

            </h1>

        </div>

    </section>

    <section class="content">

        <div class="container-fluid">

            <!-- FORM CARD -->

            <div class="card card-primary">

                <div class="card-header">

                    <h3 class="card-title">

                        <?= $edit_mode ? 'Edit Enquiry' : 'Add New Enquiry'; ?>

                    </h3>

                </div>

                <form method="POST">

                    <?php if ($edit_mode): ?>

                        <input
                            type="hidden"
                            name="edit_id"
                            value="<?= $edit_data['id']; ?>"
                        >

                    <?php endif; ?>

                    <div class="card-body">

                        <?php if ($error): ?>

                            <div class="alert alert-danger">

                                <?= htmlspecialchars($error); ?>

                            </div>

                        <?php endif; ?>

                        <?php if (isset($_GET['msg'])): ?>

                            <div class="alert alert-success">

                                <?php

                                if ($_GET['msg'] == 'added') {
                                    echo 'Enquiry added successfully.';
                                }

                                elseif ($_GET['msg'] == 'updated') {
                                    echo 'Enquiry updated successfully.';
                                }

                                elseif ($_GET['msg'] == 'deleted') {
                                    echo 'Enquiry deleted successfully.';
                                }

                                ?>

                            </div>

                        <?php endif; ?>

                        <div class="row">

                            <!-- STUDENT NAME -->

                            <div class="col-md-4 mb-3">

                                <label>

                                    Student Name

                                </label>

                                <input
                                    type="text"
                                    name="student_name"
                                    class="form-control"
                                    required
                                    value="<?= htmlspecialchars($edit_data['student_name'] ?? ''); ?>"
                                >

                            </div>

                            <!-- FATHER NAME -->

                            <div class="col-md-4 mb-3">

                                <label>

                                    Father Name

                                </label>

                                <input
                                    type="text"
                                    name="father_name"
                                    class="form-control"
                                    value="<?= htmlspecialchars($edit_data['father_name'] ?? ''); ?>"
                                >

                            </div>

                            <!-- MOBILE -->

                            <div class="col-md-4 mb-3">

                                <label>

                                    Mobile Number

                                </label>

                                <input
                                    type="text"
                                    name="mobile_no"
                                    class="form-control"
                                    required
                                    value="<?= htmlspecialchars($edit_data['mobile_no'] ?? ''); ?>"
                                >

                            </div>

                            <!-- CLASS -->

                            <div class="col-md-4 mb-3">

                                <label>

                                    Interested Class

                                </label>

                                <input
                                    type="text"
                                    name="interested_class"
                                    class="form-control"
                                    value="<?= htmlspecialchars($edit_data['interested_class'] ?? ''); ?>"
                                >

                            </div>

                            <!-- ENQUIRY DATE -->

                            <div class="col-md-4 mb-3">

                                <label>

                                    Enquiry Date

                                </label>

                                <input
                                    type="date"
                                    name="enquiry_date"
                                    class="form-control"
                                    required
                                    value="<?= htmlspecialchars($edit_data['enquiry_date'] ?? date('Y-m-d')); ?>"
                                >

                            </div>

                            <!-- STATUS -->

                            <div class="col-md-4 mb-3">

                                <label>

                                    Status

                                </label>

                                <select
                                    name="status"
                                    class="form-control"
                                >

                                    <option value="New">

                                        New

                                    </option>

                                    <option value="Follow Up">

                                        Follow Up

                                    </option>

                                    <option value="Converted">

                                        Converted

                                    </option>

                                    <option value="Closed">

                                        Closed

                                    </option>

                                </select>

                            </div>

                            <!-- ADDRESS -->

                            <div class="col-md-6 mb-3">

                                <label>

                                    Address

                                </label>

                                <textarea
                                    name="address"
                                    class="form-control"
                                    rows="3"
                                ><?= htmlspecialchars($edit_data['address'] ?? ''); ?></textarea>

                            </div>

                            <!-- REMARKS -->

                            <div class="col-md-6 mb-3">

                                <label>

                                    Remarks

                                </label>

                                <textarea
                                    name="remarks"
                                    class="form-control"
                                    rows="3"
                                ><?= htmlspecialchars($edit_data['remarks'] ?? ''); ?></textarea>

                            </div>

                        </div>

                    </div>

                    <div class="card-footer">

                        <button
                            type="submit"
                            class="btn btn-primary"
                        >

                            <i class="fas fa-save"></i>

                            <?= $edit_mode ? 'Update Enquiry' : 'Save Enquiry'; ?>

                        </button>

                        <?php if ($edit_mode): ?>

                            <a
                                href="enquiry.php"
                                class="btn btn-secondary"
                            >

                                Cancel

                            </a>

                        <?php endif; ?>

                    </div>

                </form>

            </div>

            <!-- TABLE CARD -->

            <div class="card card-outline card-success">

                <div class="card-header">

                    <h3 class="card-title">

                        All Student Enquiries

                    </h3>

                </div>

                <div class="card-body table-responsive">

                    <table class="table table-bordered table-hover table-striped">

                        <thead class="bg-dark text-white">

                            <tr>

                                <th>#</th>

                                <th>Student</th>

                                <th>Father</th>

                                <th>Mobile</th>

                                <th>Class</th>

                                <th>Status</th>

                                <th>Date</th>

                                <th width="140">

                                    Actions

                                </th>

                            </tr>

                        </thead>

                        <tbody>

                            <?php if (!empty($enquiries)): ?>

                                <?php foreach ($enquiries as $index => $row): ?>

                                    <tr>

                                        <td>

                                            <?= $index + 1; ?>

                                        </td>

                                        <td>

                                            <strong>

                                                <?= htmlspecialchars($row['student_name']); ?>

                                            </strong>

                                        </td>

                                        <td>

                                            <?= htmlspecialchars($row['father_name']); ?>

                                        </td>

                                        <td>

                                            <?= htmlspecialchars($row['mobile_no']); ?>

                                        </td>

                                        <td>

                                            <?= htmlspecialchars($row['interested_class']); ?>

                                        </td>

                                        <td>

                                            <span class="badge badge-info">

                                                <?= htmlspecialchars($row['status']); ?>

                                            </span>

                                        </td>

                                        <td>

                                            <?= date(
                                                'd-m-Y',
                                                strtotime($row['enquiry_date'])
                                            ); ?>

                                        </td>

                                        <td>

                                            <a
                                                href="?edit=<?= $row['id']; ?>"
                                                class="btn btn-sm btn-warning"
                                            >

                                                <i class="fas fa-edit"></i>

                                            </a>

                                            <form
    method="POST"
    style="display:inline-block;"
    onsubmit="return confirm('Delete this enquiry?')"
>

    <input
        type="hidden"
        name="delete_id"
        value="<?= $row['id']; ?>"
    >

    <button
        type="submit"
        class="btn btn-sm btn-danger"
    >

        <i class="fas fa-trash"></i>

    </button>

</form>

                                        </td>

                                    </tr>

                                <?php endforeach; ?>

                            <?php else: ?>

                                <tr>

                                    <td
                                        colspan="8"
                                        class="text-center text-muted"
                                    >

                                        No enquiries found.

                                    </td>

                                </tr>

                            <?php endif; ?>

                        </tbody>

                    </table>

                </div>

            </div>

        </div>

    </section>

</div>

<?php

require_once __DIR__ . '/../include/footer.php';

ob_end_flush();

?>