<?php $__template->layout('Layout.main'); ?>
<?php $mn = [1=>'يناير',2=>'فبراير',3=>'مارس',4=>'أبريل',5=>'مايو',6=>'يونيو',7=>'يوليو',8=>'أغسطس',9=>'سبتمبر',10=>'أكتوبر',11=>'نوفمبر',12=>'ديسمبر']; ?>
<?php $__template->section('title'); ?>فترة الرواتب: <?= e($mn[(int) $period->month] ?? '') ?> <?= e($period->year) ?><?php $__template->endSection(); ?>
<?php $__template->section('page_actions'); ?><a href="/hr/payroll/periods" class="btn btn-secondary">رجوع</a><?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>
<?php $statusLabels = ['open'=>'مفتوح','processing'=>'جاري','calculated'=>'محسوب','approved'=>'معتمد','paid'=>'مدفوع','closed'=>'مغلق']; ?>
<?php $statusColors = ['open'=>'#DBEAFE;color:#1E40AF','calculated'=>'#E0E7FF;color:#3730A3','approved'=>'#DEF7EC;color:#03543F','paid'=>'#D1FAE5;color:#065F46','closed'=>'#E5E7EB;color:#374151']; ?>

<div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(160px,1fr));gap:12px;margin-bottom:20px;">
    <div class="card" style="padding:16px;text-align:center;"><div style="font-size:13px;color:#6B7280;">الحالة</div><div><span style="display:inline-block;padding:4px 12px;border-radius:9999px;font-size:14px;font-weight:600;background:<?= $statusColors[$period->status] ?? '#E5E7EB;color:#374151' ?>;"><?= e($statusLabels[$period->status] ?? $period->status) ?></span></div></div>
    <div class="card" style="padding:16px;text-align:center;"><div style="font-size:13px;color:#6B7280;">عدد الموظفين</div><div style="font-size:24px;font-weight:700;"><?= (int) ($totals['employee_count'] ?? 0) ?></div></div>
    <?php if (can('hr.employee.view_salary')): ?>
    <div class="card" style="padding:16px;text-align:center;"><div style="font-size:13px;color:#6B7280;">إجمالي المستحقات</div><div style="font-size:18px;font-weight:700;color:#059669;"><?= number_format((float) ($totals['total_gross'] ?? 0), 2) ?></div></div>
    <div class="card" style="padding:16px;text-align:center;"><div style="font-size:13px;color:#6B7280;">إجمالي الخصومات</div><div style="font-size:18px;font-weight:700;color:#DC2626;"><?= number_format((float) ($totals['total_deductions'] ?? 0), 2) ?></div></div>
    <div class="card" style="padding:16px;text-align:center;"><div style="font-size:13px;color:#6B7280;">صافي الرواتب</div><div style="font-size:24px;font-weight:700;color:#2563EB;"><?= number_format((float) ($totals['total_net'] ?? 0), 2) ?></div></div>
    <?php endif; ?>
</div>

<div style="display:flex;gap:10px;margin-bottom:20px;flex-wrap:wrap;">
    <?php if (in_array($period->status, ['open', 'calculated']) && can('hr.payroll.process')): ?>
    <form method="POST" action="/hr/payroll/periods/<?= (int) $period->id ?>/calculate"><?= csrf_field() ?><button type="submit" class="btn btn-primary" onclick="this.disabled=true;this.form.submit();">حساب الرواتب</button></form>
    <?php endif; ?>
    <?php if ($period->status === 'calculated' && can('hr.payroll.approve')): ?>
    <form method="POST" action="/hr/payroll/periods/<?= (int) $period->id ?>/approve"><?= csrf_field() ?><button type="submit" class="btn btn-primary">اعتماد</button></form>
    <?php endif; ?>
    <?php if ($period->status === 'approved' && can('hr.payroll.process')): ?>
    <form method="POST" action="/hr/payroll/periods/<?= (int) $period->id ?>/pay" style="display:flex;gap:8px;align-items:end;">
        <?= csrf_field() ?>
        <input type="date" name="payment_date" value="<?= date('Y-m-d') ?>" class="form-control" style="max-width:160px;">
        <button type="submit" class="btn btn-primary">صرف</button>
    </form>
    <?php endif; ?>
    <?php if ($period->status === 'paid' && can('hr.payroll.process')): ?>
    <form method="POST" action="/hr/payroll/periods/<?= (int) $period->id ?>/close" onsubmit="return confirm('إغلاق الفترة نهائياً؟');"><?= csrf_field() ?><button type="submit" class="btn btn-secondary">إغلاق الفترة</button></form>
    <?php endif; ?>
</div>

<div class="card">
    <div style="padding:16px;border-bottom:1px solid #E5E7EB;"><h3 style="margin:0;font-size:16px;">تفاصيل الرواتب</h3></div>
    <div class="table-responsive">
        <table class="data-table">
            <thead><tr><th>الموظف</th><?php if (can('hr.employee.view_salary')): ?><th>الأساسي</th><th>إجمالي المستحقات</th><th>الخصومات</th><th>الصافي</th><?php endif; ?><th>إجراءات</th></tr></thead>
            <tbody>
                <?php if (empty($runs)): ?>
                <tr><td colspan="<?= can('hr.employee.view_salary') ? '6' : '2' ?>" style="text-align:center;padding:40px;color:#9CA3AF;">لم يتم حساب الرواتب بعد</td></tr>
                <?php else: ?>
                <?php foreach ($runs as $r): ?>
                <tr>
                    <td><a href="/hr/employees/<?= (int) $r['employee_profile_id'] ?>"><?= e(($r['first_name_ar'] ?? '') . ' ' . ($r['last_name_ar'] ?? '')) ?></a></td>
                    <?php if (can('hr.employee.view_salary')): ?>
                    <td><?= number_format((float) $r['basic_salary'], 2) ?></td>
                    <td style="color:#059669;font-weight:600;"><?= number_format((float) $r['gross_earnings'], 2) ?></td>
                    <td style="color:#DC2626;"><?= number_format((float) $r['total_deductions'], 2) ?></td>
                    <td style="font-weight:700;"><?= number_format((float) $r['net_salary'], 2) ?></td>
                    <?php endif; ?>
                    <td style="white-space:nowrap;">
                        <a href="/hr/payroll/runs/<?= (int) $r['id'] ?>" style="color:#2563EB;margin-inline-end:8px;" title="التفاصيل"><i data-lucide="eye" style="width:16px;height:16px;"></i></a>
                        <a href="/hr/payroll/runs/<?= (int) $r['id'] ?>/slip" style="color:#059669;" title="قسيمة الراتب"><i data-lucide="file-text" style="width:16px;height:16px;"></i></a>
                    </td>
                </tr>
                <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
<script>if(typeof lucide!=='undefined')lucide.createIcons();</script>
<?php $__template->endSection(); ?>
