<?php
include '../../includes/header.php';
require_once __DIR__ . '/_module.php';

payrollRequireAdmin($auth);

$payroll = new Payroll($conn);
$loanId = isset($_GET['id']) ? (int)$_GET['id'] : 0;
$loan = $loanId > 0 ? $payroll->getLoansByID($loanId) : null;

if (!$loan) {
    echo '<div class="container mt-4"><div class="alert alert-danger">Loan record not found.</div></div>';
    include '../../includes/footer.php';
    exit();
}

$principal = (float)($loan['loan_amount'] ?? 0);
$paid = (float)($loan['loan_total_paid'] ?? 0);
$balance = max(0, $principal - $paid);
$monthlyDeduction = ((int)($loan['loan_months'] ?? 0) > 0) ? $principal / (int)$loan['loan_months'] : 0;

payrollModuleStyles();
?>

<div class="container-fluid px-4">
    <?php payrollModuleNav('loans'); ?>

    <div class="payroll-page-head">
        <div>
            <h1>Loan #<?php echo (int)$loan['record_id']; ?></h1>
            <p><?php echo payrollText($loan['employee_fullname'] ?? 'Employee'); ?> loan details and deduction summary.</p>
        </div>
        <div class="no-print">
            <a href="loans.php" class="btn btn-outline-secondary"><i class="fas fa-arrow-left"></i> Back to Loans</a>
            <button type="button" onclick="window.print()" class="btn btn-outline-secondary"><i class="fas fa-print"></i> Print</button>
        </div>
    </div>

    <div class="payroll-metrics">
        <div class="payroll-metric"><strong>Principal</strong><span><?php echo payrollMoney($principal); ?></span></div>
        <div class="payroll-metric"><strong>Paid</strong><span><?php echo payrollMoney($paid); ?></span></div>
        <div class="payroll-metric"><strong>Balance</strong><span><?php echo payrollMoney($balance); ?></span></div>
        <div class="payroll-metric"><strong>Monthly Deduction</strong><span><?php echo payrollMoney($monthlyDeduction); ?></span></div>
    </div>

    <div class="payroll-panel">
        <div class="payroll-panel-title"><h2>Loan Details</h2></div>
        <div class="row g-3">
            <div class="col-md-6"><strong>Employee</strong><br><?php echo payrollText($loan['employee_fullname'] ?? ''); ?></div>
            <div class="col-md-6"><strong>Phone</strong><br><?php echo payrollText($loan['employee_phone'] ?? ''); ?></div>
            <div class="col-md-6"><strong>Branch</strong><br><?php echo payrollText($loan['branch'] ?? ''); ?></div>
            <div class="col-md-6"><strong>Position</strong><br><?php echo payrollText($loan['employee_position'] ?? ''); ?></div>
            <div class="col-md-6"><strong>Loan Type</strong><br><?php echo payrollText(str_replace('_', ' ', $loan['loan_type'] ?? '')); ?></div>
            <div class="col-md-6"><strong>Status</strong><br><?php echo payrollStatusBadge($loan['loan_status'] ?? ''); ?></div>
            <div class="col-md-6"><strong>Loan Date</strong><br><?php echo payrollText($loan['loan_date'] ?? ''); ?></div>
            <div class="col-md-6"><strong>Deduction Starts</strong><br><?php echo payrollText($loan['loans_starts'] ?? ''); ?></div>
            <div class="col-md-6"><strong>Interest Rate</strong><br><?php echo payrollText($loan['interest_rate'] ?? 0); ?>%</div>
            <div class="col-md-6"><strong>Repayment Period</strong><br><?php echo number_format((int)($loan['loan_months'] ?? 0)); ?> months</div>
        </div>
    </div>

    <?php payrollModuleEnd(); ?>
</div>

<?php include '../../includes/footer.php'; ?>
