<?php

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

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

$page_title = "Edit Salary Payout";

$school_id = $_SESSION['school_id'];

$message = '';

/*
|--------------------------------------------------------------------------
| VALIDATE ID
|--------------------------------------------------------------------------
*/

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

if ($salary_id <= 0) {

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

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

$stmt = $pdo->prepare("
    SELECT
        sp.*,
        t.name AS teacher_name
    FROM salary_payments sp

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

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

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

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

/*
|--------------------------------------------------------------------------
| RECORD NOT FOUND
|--------------------------------------------------------------------------
*/

if (!$salary) {

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

/*
|--------------------------------------------------------------------------
| FETCH ACTIVE TEACHERS
|--------------------------------------------------------------------------
*/

$teacherStmt = $pdo->prepare("
    SELECT
        id,
        name
    FROM teachers
    WHERE school_id = ?
    AND status = 'Active'
    ORDER BY name ASC
");

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

$teachers = $teacherStmt->fetchAll(PDO::FETCH_ASSOC);

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

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

    $teacher_id = (int) ($_POST['teacher_id'] ?? 0);

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

    $amount = (float) ($_POST['amount'] ?? 0);

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

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

    if (
        $teacher_id > 0 &&
        !empty($month_year) &&
        $amount > 0 &&
        !empty($payment_date)
    ) {

        $updateStmt = $pdo->prepare("
            UPDATE salary_payments
            SET
                teacher_id = ?,
                amount = ?,
                payment_date = ?,
                month_year = ?,
                remarks = ?
            WHERE id = ?
            AND school_id = ?
        ");

        $updateStmt->execute([
            $teacher_id,
            $amount,
            $payment_date,
            $month_year,
            $remarks,
            $salary_id,
            $school_id
        ]);

        $message = "Salary Payout Updated Successfully";

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

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

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

                        Edit Salary Payout

                    </h1>

                </div>

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

                    <a href="view_payout.php"
                       class="btn btn-info">

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

                        Back to Payouts

                    </a>

                </div>

            </div>

        </div>

    </section>

    <!-- MAIN CONTENT -->

    <section class="content">

        <div class="container-fluid">

            <!-- SUCCESS MESSAGE -->

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

                <div class="alert alert-success alert-dismissible fade show">

                    <button type="button"
                            class="close"
                            data-dismiss="alert">

                        &times;

                    </button>

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

                </div>

            <?php endif; ?>

            <!-- CARD -->

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

                <div class="card-header">

                    <h3 class="card-title">

                        Update Salary Details

                    </h3>

                </div>

                <!-- FORM -->

                <form method="POST">

                    <div class="card-body">

                        <div class="row">

                            <!-- TEACHER -->

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

                                <label>

                                    Select Teacher

                                </label>

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

                                    <?php foreach ($teachers as $teacher): ?>

                                        <option
                                            value="<?php echo $teacher['id']; ?>"
                                            <?php echo ($salary['teacher_id'] == $teacher['id'])
                                                ? 'selected'
                                                : ''; ?>
                                        >

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

                                        </option>

                                    <?php endforeach; ?>

                                </select>

                            </div>

                            <!-- MONTH -->

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

                                <label>

                                    Month-Year

                                </label>

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

                            </div>

                            <!-- AMOUNT -->

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

                                <label>

                                    Amount

                                </label>

                                <input
                                    type="number"
                                    name="amount"
                                    class="form-control"
                                    value="<?php echo htmlspecialchars($salary['amount']); ?>"
                                    min="0"
                                    step="0.01"
                                    required
                                >

                            </div>

                            <!-- PAYMENT DATE -->

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

                                <label>

                                    Payment Date

                                </label>

                                <input
                                    type="date"
                                    name="payment_date"
                                    class="form-control"
                                    value="<?php echo htmlspecialchars($salary['payment_date']); ?>"
                                    required
                                >

                            </div>

                            <!-- RECEIPT -->

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

                                <label>

                                    Receipt Number

                                </label>

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

                            </div>

                            <!-- REMARKS -->

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

                                <label>

                                    Remarks

                                </label>

                                <input
                                    type="text"
                                    name="remarks"
                                    class="form-control"
                                    value="<?php echo htmlspecialchars($salary['remarks']); ?>"
                                    placeholder="Optional Remarks"
                                >

                            </div>

                        </div>

                    </div>

                    <!-- FOOTER -->

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

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

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

                            Update Payout

                        </button>

                    </div>

                </form>

            </div>

        </div>

    </section>

</div>

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