<?php

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

allow_roles(['admin']);

$page_title = "View Staff Attendance";
$current_session_id = get_current_session_id($pdo);
$school_id = $_SESSION['school_id'];

/*
|--------------------------------------------------------------------------
| FILTER DATE
|--------------------------------------------------------------------------
*/

$filter_date = $_GET['attendance_date'] ?? date('Y-m-d');

if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $filter_date)) {
    $filter_date = date('Y-m-d');
}

/*
|--------------------------------------------------------------------------
| FETCH ATTENDANCE RECORDS
|--------------------------------------------------------------------------
*/

$stmt = $pdo->prepare("
    SELECT
        sa.id,
        sa.attendance_date,
        sa.status,
        sa.remarks,

        t.name,
        t.salary

    FROM staff_attendance sa

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

    WHERE sa.school_id = ?
    AND sa.attendance_date = ?
	AND sa.session_id = ?

    ORDER BY t.name ASC
");

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

$attendance_records = $stmt->fetchAll(PDO::FETCH_ASSOC);

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

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

?>
<div class="content-wrapper">

    <!-- PAGE HEADER -->

    <section class="content-header">
        <div class="container-fluid">
            <div class="row mb-3">
                <div class="col-sm-6">
                    <h1 class="m-0">
                        Staff Attendance Report
                    </h1>
                </div>
                <div class="col-sm-6 text-right">
                    <a href="staff_attendance.php" class="btn btn-primary mr-2">
                        <i class="fas fa-plus-circle"></i>
                        Mark Attendance
                    </a>
                    <a href="staff_attendance_report.php" class="btn btn-success">
                        <i class="fas fa-money-bill-wave"></i>
                        Staff Attendance Report
                    </a>
                </div>
            </div>
        </div>
    </section>

    <!-- MAIN CONTENT -->

    <section class="content">

        <div class="container-fluid">

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

                <!-- CARD HEADER -->

                <div class="card-header">

                    <h3 class="card-title">

                        View Attendance

                    </h3>

                </div>

                <!-- CARD BODY -->

                <div class="card-body">

                    <!-- FILTER -->

                    <form method="GET" class="mb-4">

                        <div class="row">

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

                                <label>

                                    Attendance Date

                                </label>

                                <input
                                    type="date"
                                    name="attendance_date"
                                    class="form-control"
                                    value="<?php echo htmlspecialchars($filter_date); ?>"
                                >

                            </div>

                            <div class="col-md-2 d-flex align-items-end">

                                <button
                                    type="submit"
                                    class="btn btn-success w-100"
                                >

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

                                    Filter

                                </button>

                            </div>

                        </div>

                    </form>
					<?php

$day_name = date(

    'l',

    strtotime($filter_date)

);


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

$stmtHoliday = $pdo->prepare("

    SELECT title

    FROM holidays

    WHERE school_id = ?
    AND holiday_date = ?

    LIMIT 1

");

$stmtHoliday->execute([

    $school_id,
    $filter_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>

        Holiday:
        <strong>

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

        </strong>

    </div>

<?php elseif ($weeklyData): ?>

    <div class="alert alert-dark">

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

        Weekly Off:
        <strong>

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

        </strong>

    </div>

<?php endif; ?>

                    <!-- TABLE -->

                    <div class="table-responsive">

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

                            <thead class="thead-dark">

                                <tr>

                                    <th width="60">#</th>

                                    <th>Teacher Name</th>

                                    <th>Monthly Salary</th>

                                    <th>Date</th>

                                    <th>Status</th>

                                    <th>Remarks</th>

                                    <th width="140">Actions</th>

                                </tr>

                            </thead>

                            <tbody>

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

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

                                        <tr>

                                            <!-- SERIAL -->

                                            <td>

                                                <?php echo $index + 1; ?>

                                            </td>

                                            <!-- NAME -->

                                            <td>

                                                <strong>

                                                    <?php echo htmlspecialchars($row['name']); ?>

                                                </strong>

                                            </td>

                                            <!-- SALARY -->

                                            <td>

                                                <span class="badge badge-success p-2">

                                                    ₹<?php echo number_format($row['salary'], 2); ?>

                                                </span>

                                            </td>

                                            <!-- DATE -->

                                            <td>

                                                <?php echo date(
                                                    'd-m-Y',
                                                    strtotime($row['attendance_date'])
                                                ); ?>

                                            </td>

                                            <!-- STATUS -->

                                            <td>

                                                <?php

                                                $badge = 'secondary';

                                                if ($row['status'] === 'Present') {
                                                    $badge = 'success';
                                                }

                                                elseif ($row['status'] === 'Absent') {
                                                    $badge = 'danger';
                                                }

                                                elseif ($row['status'] === 'Leave') {
                                                    $badge = 'warning';
                                                }

                                                elseif ($row['status'] === 'Half Day') {
                                                    $badge = 'info';
                                                }
												elseif ($row['status'] === 'Holiday') {

													$badge = 'primary';
												}

												elseif ($row['status'] === 'Weekly Off') {

													$badge = 'dark';
												}

                                                ?>

                                                <span class="badge badge-<?php echo $badge; ?> p-2">

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

                                                </span>

                                            </td>

                                            <!-- REMARKS -->

                                            <td>

                                                <?php echo !empty($row['remarks'])
                                                    ? htmlspecialchars($row['remarks'])
                                                    : '-'; ?>

                                            </td>

                                            <!-- ACTIONS -->

                                            <td>

                                                <a href="edit_staff_attendance.php?id=<?php echo $row['id']; ?>"
                                                   class="btn btn-sm btn-info">

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

                                                </a>

                                                <form method="POST"
													  action="delete_staff_attendance.php"
													  class="d-inline"
													  onsubmit="return confirm('Delete this attendance record?')">

													<input type="hidden"
														   name="id"
														   value="<?php echo (int)$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="7"
                                            class="text-center text-muted">

                                            No staff attendance found for selected date

                                        </td>

                                    </tr>

                                <?php endif; ?>

                            </tbody>

                        </table>
						<div class="mt-3">

    <strong>

        Legend:

    </strong>

    <span class="badge badge-success p-2 ml-2">

        Present

    </span>

    <span class="badge badge-danger p-2 ml-2">

        Absent

    </span>

    <span class="badge badge-warning p-2 ml-2">

        Leave

    </span>

    <span class="badge badge-info p-2 ml-2">

        Half Day

    </span>

    <span class="badge badge-primary p-2 ml-2">

        Holiday

    </span>

    <span class="badge badge-dark p-2 ml-2">

        Weekly Off

    </span>

</div>

                    </div>

                </div>

            </div>

        </div>

    </section>

</div>

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