<?php /* payroll/employee_view.php */
$sym = setting('currency_symbol','Rs.');
$stColors=['active'=>['bg-success','success'],'inactive'=>['bg-secondary','secondary'],'terminated'=>['bg-danger','danger']];
$stBadge=$stColors[$employee['status']]??['bg-secondary','secondary'];
$initials=strtoupper(substr($employee['full_name'],0,1).(strpos($employee['full_name'],' ')!==false?substr(strrchr($employee['full_name'],' '),1,1):''));
?>

<div class="d-flex justify-content-between align-items-center mb-3 flex-wrap gap-2">
  <h5 class="fw-bold mb-0">
    <i class="bi bi-person-vcard me-2 text-primary"></i>Employee Profile
  </h5>
  <div class="d-flex gap-2">
    <a href="<?=APP_URL?>/payroll/employees" class="btn btn-sm btn-outline-secondary">← Employees</a>
    <a href="<?=APP_URL?>/payroll/employees/edit/<?=$employee['id']?>" class="btn btn-sm btn-primary">
      <i class="bi bi-pencil me-1"></i>Edit
    </a>
  </div>
</div>

<?= flashMessages() ?>

<div class="row g-3">
  <!-- Profile card -->
  <div class="col-lg-4">
    <div class="card mb-3">
      <div class="card-body text-center py-4">
        <div class="rounded-circle d-inline-flex align-items-center justify-content-center fw-bold mb-3"
             style="width:80px;height:80px;background:#eff6ff;color:#1d4ed8;font-size:1.5rem">
          <?= e($initials) ?>
        </div>
        <div class="fw-bold fs-6"><?= e($employee['full_name']) ?></div>
        <div class="text-muted small"><?= e($employee['designation']??'—') ?></div>
        <code class="d-block mt-1" style="font-size:.75rem"><?= e($employee['emp_code']) ?></code>
        <span class="badge <?=$stBadge[0]?> mt-2"><?=ucfirst($employee['status'])?></span>
      </div>
      <div class="card-body border-top pt-3">
        <table class="table table-sm table-borderless mb-0">
          <?php foreach([
            ['<i class="bi bi-telephone text-success me-1"></i>Phone', $employee['phone']??'—'],
            ['<i class="bi bi-envelope text-primary me-1"></i>Email', $employee['email']??'—'],
            ['<i class="bi bi-building text-info me-1"></i>Branch', $employee['branch_name']??'Main'],
            ['<i class="bi bi-diagram-3 text-warning me-1"></i>Dept', $employee['department']??'—'],
            ['<i class="bi bi-calendar3 text-secondary me-1"></i>Join Date', fDate($employee['join_date'])],
            ['<i class="bi bi-cash text-success me-1"></i>Basic', currency($employee['basic_salary'])],
          ] as[$l,$v]):?>
          <tr>
            <td class="text-muted small ps-0"><?= $l ?></td>
            <td class="fw-semibold small"><?= e($v) ?></td>
          </tr>
          <?php endforeach;?>
        </table>
      </div>
      <div class="card-footer d-flex gap-2">
        <a href="<?=APP_URL?>/payroll/employees/edit/<?=$employee['id']?>"
           class="btn btn-sm btn-outline-primary w-50">
          <i class="bi bi-pencil me-1"></i>Edit
        </a>
        <form method="POST"
              action="<?=APP_URL?>/payroll/employees/toggle/<?=$employee['id']?>"
              class="w-50"
              onsubmit="return confirm('<?=$employee['status']==='active'?'Deactivate':'Activate'?> this employee?')">
          <?=Auth::csrfField()?>
          <button class="btn btn-sm <?=$employee['status']==='active'?'btn-outline-warning':'btn-outline-success'?> w-100">
            <i class="bi bi-<?=$employee['status']==='active'?'pause-circle':'play-circle'?> me-1"></i>
            <?=$employee['status']==='active'?'Deactivate':'Activate'?>
          </button>
        </form>
      </div>
    </div>

    <!-- Salary Components -->
    <div class="card">
      <div class="card-header fw-semibold small bg-transparent">
        <i class="bi bi-sliders me-1 text-warning"></i>Salary Components
      </div>
      <div class="card-body p-0">
        <table class="table table-sm mb-0">
          <tr style="background:#f0fdf4"><td colspan="2" class="ps-3 fw-bold text-success" style="font-size:.68rem">ALLOWANCES</td></tr>
          <?php foreach(array_filter($components,fn($c)=>$c['type']==='allowance') as $c):?>
          <tr><td class="ps-3 small"><?=e($c['name'])?></td><td class="text-end pe-3 text-success small fw-semibold"><?=currency($c['amount'])?></td></tr>
          <?php endforeach;?>
          <tr style="background:#fef2f2"><td colspan="2" class="ps-3 fw-bold text-danger" style="font-size:.68rem">DEDUCTIONS</td></tr>
          <?php foreach(array_filter($components,fn($c)=>$c['type']==='deduction') as $c):?>
          <tr><td class="ps-3 small"><?=e($c['name'])?></td><td class="text-end pe-3 text-danger small fw-semibold">(<?=currency($c['amount'])?>)</td></tr>
          <?php endforeach;?>
          <tr style="background:#eff6ff"><td class="ps-3 small text-muted">EPF Emp 8%</td><td class="text-end pe-3 text-danger small fw-semibold">(<?=currency($employee['basic_salary']*0.08)?>)</td></tr>
          <tr class="table-primary fw-bold"><td class="ps-3 small">Est. Net Salary</td><td class="text-end pe-3 text-success"><?=currency($employee['basic_salary']-$employee['basic_salary']*0.08)?></td></tr>
        </table>
      </div>
    </div>
  </div>

  <!-- Payroll history -->
  <div class="col-lg-8">
    <div class="card mb-3">
      <div class="card-header fw-semibold small bg-dark text-white d-flex justify-content-between">
        <span><i class="bi bi-receipt me-1"></i>Payroll History</span>
        <span class="badge bg-secondary"><?=count($payrollHistory)?> months</span>
      </div>
      <?php if(!empty($payrollHistory)):?>
      <div class="table-responsive">
        <table class="table table-sm table-hover mb-0">
          <thead class="table-secondary"><tr><th>Period</th><th class="text-end">Basic</th><th class="text-end">Gross</th><th class="text-end">Deductions</th><th class="text-end text-success">Net</th><th class="text-center">Status</th><th>Payslip</th></tr></thead>
          <tbody>
            <?php foreach($payrollHistory as $p):
              $sc=$p['payment_status']==='paid'?'bg-success':'bg-warning text-dark';
            ?>
            <tr>
              <td class="small fw-semibold"><?=e($p['period_name'])?></td>
              <td class="text-end small"><?=currency($p['basic_salary'])?></td>
              <td class="text-end small"><?=currency($p['gross_salary'])?></td>
              <td class="text-end small text-danger">(<?=currency($p['total_deductions']+$p['epf_employee'])?>)</td>
              <td class="text-end fw-bold text-success"><?=currency($p['net_salary'])?></td>
              <td class="text-center"><span class="badge <?=$sc?>" style="font-size:.6rem"><?=ucfirst($p['payment_status'])?></span></td>
              <td><a href="<?=APP_URL?>/payroll/payslip/<?=$p['id']?>" class="btn btn-sm btn-outline-secondary py-0 px-2"><i class="bi bi-file-earmark-person"></i></a></td>
            </tr>
            <?php endforeach;?>
          </tbody>
          <tfoot class="table-secondary fw-bold">
            <tr>
              <td colspan="4">TOTAL (<?=count($payrollHistory)?> months)</td>
              <td class="text-end text-success"><?=currency(array_sum(array_column($payrollHistory,'net_salary')))?></td>
              <td colspan="2"></td>
            </tr>
          </tfoot>
        </table>
      </div>
      <?php else:?>
      <div class="text-center text-muted py-4 small">
        <i class="bi bi-receipt d-block fs-3 mb-2 opacity-25"></i>No payroll records yet.
      </div>
      <?php endif;?>
    </div>

    <!-- Advances -->
    <?php if(!empty($advances)):?>
    <div class="card">
      <div class="card-header fw-semibold small bg-transparent">
        <i class="bi bi-wallet2 me-1 text-warning"></i>Recent Salary Advances
      </div>
      <table class="table table-sm mb-0">
        <thead class="table-secondary"><tr><th>Date</th><th class="text-end">Amount</th><th>Reason</th><th>Deduct</th><th>Status</th></tr></thead>
        <tbody>
          <?php foreach($advances as $a):
            $sc=['pending'=>'bg-warning text-dark','deducted'=>'bg-success','cancelled'=>'bg-secondary'][$a['status']]??'bg-secondary';
          ?>
          <tr>
            <td class="small"><?=fDate($a['advance_date'])?></td>
            <td class="text-end fw-bold text-warning"><?=currency($a['amount'])?></td>
            <td class="small text-muted"><?=e($a['reason']??'—')?></td>
            <td class="small"><?=date('M Y',mktime(0,0,0,$a['deduct_month'],1,$a['deduct_year']))?></td>
            <td><span class="badge <?=$sc?>" style="font-size:.6rem"><?=ucfirst($a['status'])?></span></td>
          </tr>
          <?php endforeach;?>
        </tbody>
      </table>
    </div>
    <?php endif;?>
  </div>
</div>