<?php

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

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

$page_title = "Salary Payout History";

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

/*
|--------------------------------------------------------------------------
| FETCH SALARY PAYOUT RECORDS
|--------------------------------------------------------------------------
*/

$stmt = $pdo->prepare("
    SELECT
        sp.id,
        sp.amount,
        sp.payment_date,
        sp.month_year,
        sp.remarks,
        sp.receipt_no,

        t.name AS teacher_name

    FROM salary_payments sp

    INNER JOIN teachers t
        ON t.id = sp.teacher_id

    WHERE sp.school_id = ?
	AND sp.session_id = ?

    ORDER BY sp.id DESC
");

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

$salary_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">

                        Salary Payout History

                    </h1>

                </div>

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

                    <a href="generate_salary.php"
                       class="btn btn-primary">

                        <i class="fas fa-plus-circle"></i>

                        Generate Salary

                    </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">

                        Staff Salary Records

                    </h3>

                </div>

                <!-- CARD BODY -->

                <div class="card-body">

                    <div class="table-responsive">

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

                            <thead class="thead-dark">

                                <tr>

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

                                    <th>Receipt No</th>

                                    <th>Teacher Name</th>

                                    <th>Month</th>

                                    <th>Payment Date</th>

                                    <th>Amount</th>

                                    <th>Remarks</th>

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

                                </tr>

                            </thead>

                            <tbody>

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

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

                                        <tr>

                                            <!-- SERIAL -->

                                            <td>

                                                <?php echo $index + 1; ?>

                                            </td>

                                            <!-- RECEIPT -->

                                            <td>

                                                <strong>

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

                                                </strong>

                                            </td>

                                            <!-- TEACHER -->

                                            <td>

                                                <strong>

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

                                                </strong>

                                            </td>

                                            <!-- MONTH -->

                                            <td>

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

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

                                                </span>

                                            </td>

                                            <!-- PAYMENT DATE -->

                                            <td>

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

                                            </td>

                                            <!-- AMOUNT -->

                                            <td>

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

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

                                                </span>

                                            </td>

                                            <!-- REMARKS -->

                                            <td>

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

                                            </td>

                                            <!-- ACTIONS -->

                                            <td>

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

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

                                                </a>

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

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

                                                </a>

                                                <a href="delete_salary.php?id=<?php echo $row['id']; ?>"
                                                   class="btn btn-sm btn-danger"
                                                   onclick="return confirm('Delete this salary record?')">

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

                                                </a>

                                            </td>

                                        </tr>

                                    <?php endforeach; ?>

                                <?php else: ?>

                                    <tr>

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

                                            No Salary Records Found

                                        </td>

                                    </tr>

                                <?php endif; ?>

                            </tbody>

                        </table>

                    </div>

                </div>

            </div>

        </div>

    </section>

</div>

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