<?php

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

allow_roles(['admin']);

$page_title = "Edit Staff Attendance";

$school_id = $_SESSION['school_id'];
$current_session_id = get_current_session_id($pdo);

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

if ($id <= 0) {

    header("Location: view_staff_attendance.php");
    exit;
}

/*
|--------------------------------------------------------------------------
| FETCH RECORD
|--------------------------------------------------------------------------
*/

$stmt = $pdo->prepare("
    SELECT
        sa.*,
        u.name
    FROM staff_attendance sa

    INNER JOIN teachers u
        ON u.id = sa.staff_id
        AND u.school_id = sa.school_id

    WHERE sa.id = ?
    AND sa.school_id = ?
    AND sa.session_id = ?
    LIMIT 1
");

$stmt->execute([$id, $school_id, $current_session_id]);

$record = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$record) {

    die('Attendance record not found');
}

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

$message = '';

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

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

    'Present',
    'Absent',
    'Leave',
    'Half Day',
    'Holiday',
    'Weekly Off'

];

if (!in_array($status, $allowed_status, true)) {
    die('Invalid status');
}

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

    $updateStmt = $pdo->prepare("
        UPDATE staff_attendance
SET
    status = ?,
    remarks = ?
WHERE id = ?
AND school_id = ?
AND session_id = ?
    ");

    $updateStmt->execute([
        $status,
        $remarks,
        $id,
        $school_id,
		$current_session_id
    ]);

    $message = "Attendance Updated Successfully";

    /*
    |--------------------------------------------------------------------------
    | REFRESH RECORD
    |--------------------------------------------------------------------------
    */

    $stmt->execute([$id, $school_id, $current_session_id]);

    $record = $stmt->fetch(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">

            <div class="row mb-3">

                <div class="col-sm-6">

                    <h1>Edit Staff Attendance</h1>

                </div>

                <div class="col-sm-6 text-right">

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

                        <i class="fas fa-arrow-left"></i>

                        Back

                    </a>

                </div>

            </div>

        </div>

    </section>

    <section class="content">

        <div class="container-fluid">

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

                <div class="alert alert-success">

                    <?php echo htmlspecialchars($message); ?>

                </div>

            <?php endif; ?>
			<?php

$day_name = date(

    'l',

    strtotime($record['attendance_date'])

);


/*
|--------------------------------------------------------------------------
| CHECK HOLIDAY
|--------------------------------------------------------------------------
*/

$stmtHoliday = $pdo->prepare("

    SELECT title

    FROM holidays

    WHERE school_id = ?
    AND holiday_date = ?

    LIMIT 1

");

$stmtHoliday->execute([

    $school_id,
    $record['attendance_date']

]);

$holidayData =
    $stmtHoliday->fetch(PDO::FETCH_ASSOC);


/*
|--------------------------------------------------------------------------
| CHECK WEEKLY OFF
|--------------------------------------------------------------------------
*/

$stmtWeekly = $pdo->prepare("

    SELECT id

    FROM weekly_offs

    WHERE school_id = ?
    AND day_name = ?

    LIMIT 1

");

$stmtWeekly->execute([

    $school_id,
    $day_name

]);

$weeklyData =
    $stmtWeekly->fetch(PDO::FETCH_ASSOC);

?>


<?php if ($holidayData): ?>

    <div class="alert alert-primary">

        <i class="fas fa-calendar-alt"></i>

        This date is marked as Holiday:
        <strong>

            <?php echo htmlspecialchars($holidayData['title']); ?>

        </strong>

    </div>

<?php elseif ($weeklyData): ?>

    <div class="alert alert-dark">

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

        This date is Weekly Off:
        <strong>

            <?php echo htmlspecialchars($day_name); ?>

        </strong>

    </div>

<?php endif; ?>

            <div class="card card-outline card-primary shadow-sm">

                <div class="card-header">

                    <h3 class="card-title">

                        Edit Attendance Record

                    </h3>

                </div>

                <form method="POST">

                    <div class="card-body">

                        <div class="row">

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

                                <label>Staff Name</label>

                                <input
                                    type="text"
                                    class="form-control"
                                    value="<?php echo htmlspecialchars($record['name']); ?>"
                                    readonly
                                >

                            </div>

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

                                <label>Attendance Date</label>

                                <input
                                    type="text"
                                    class="form-control"
                                    value="<?php echo date('d-m-Y', strtotime($record['attendance_date'])); ?>"
                                    readonly
                                >

                            </div>

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

                                <label>Status</label>

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

                                    <option value="Present"
                                        <?php if ($record['status'] === 'Present') echo 'selected'; ?>>

                                        Present

                                    </option>

                                    <option value="Absent"
                                        <?php if ($record['status'] === 'Absent') echo 'selected'; ?>>

                                        Absent

                                    </option>

                                    <option value="Leave"
                                        <?php if ($record['status'] === 'Leave') echo 'selected'; ?>>

                                        Leave

                                    </option>

                                    <option value="Half Day"
                                        <?php if ($record['status'] === 'Half Day') echo 'selected'; ?>>

                                        Half Day

                                    </option>
									<option value="Holiday"
										<?php if ($record['status'] === 'Holiday') echo 'selected'; ?>>

										Holiday

									</option>

									<option value="Weekly Off"
										<?php if ($record['status'] === 'Weekly Off') echo 'selected'; ?>>

										Weekly Off

									</option>

                                </select>

                            </div>

                            <div class="col-md-12">

                                <label>Remarks</label>

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

                            </div>

                        </div>

                    </div>

                    <div class="card-footer text-right">

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

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

                            Update Attendance

                        </button>

                    </div>

                </form>

            </div>

        </div>

    </section>

</div>

<?php
require_once __DIR__ . '/../include/footer.php';
?>