<?php defined('BASEPATH') or exit('No direct script access allowed');

$aColumns = [
  db_prefix().'coi_special_approvals.id as id',
  db_prefix().'coi_special_approvals.staff_name as staff_name',
  db_prefix().'coi_special_approvals.department as department',
  db_prefix().'coi_special_approvals.activity_name as activity_name',
  db_prefix().'coi_special_approvals.status as status',
  db_prefix().'coi_special_approvals.created_at as created_at',
];

$sIndexColumn = 'id';
$sTable       = db_prefix().'coi_special_approvals';
$where        = [];

if (staff_cant('view', 'coi_special_approval') && staff_can('view_own', 'coi_special_approval')) {
  $where[] = 'AND '.db_prefix().'coi_special_approvals.staff_id = '.(int)get_staff_user_id();
}

$additionalSelect = [db_prefix().'coi_special_approvals.staff_id as owner_id'];

$result  = data_tables_init($aColumns, $sIndexColumn, $sTable, [], $where, $additionalSelect);
$output  = $result['output'];
$rResult = $result['rResult'];

$logged = (int)get_staff_user_id();

foreach ($rResult as $aRow) {
  $row = [];

  $id = (int)$aRow['id'];
  $ownerId = (int)$aRow['owner_id'];

  $canAll = staff_can('view', 'coi_special_approval');
  $canOwn = staff_can('view_own', 'coi_special_approval') && ($ownerId === $logged);

  $canEdit   = staff_can('edit', 'coi_special_approval') && ($canAll || $canOwn);
  $canDelete = staff_can('delete', 'coi_special_approval') && ($canAll || $canOwn);

  // Approver quick action (CEO)
  $isApprover = (is_admin() || staff_can('approve', 'coi_special_approval')); // final check is done server-side in controller approve()

  // Staff name column with row options (like your GPS module)
  $viewUrl = admin_url('coi_special_approval/view/'.$id);

  $_data = '<a href="'.$viewUrl.'" class="tw-font-medium">'.e($aRow['staff_name']).'</a>';
  $_data .= '<div class="row-options">';
  $_data .= '<a href="'.$viewUrl.'">'._l('view').'</a>';

  if ($canEdit)   $_data .= ' | <a href="'.admin_url('coi_special_approval/edit/'.$id).'">'._l('edit').'</a>';
  if ($canDelete) $_data .= ' | <a href="'.admin_url('coi_special_approval/delete/'.$id).'" class="_delete text-danger">'._l('delete').'</a>';

  // show quick approve link only if pending
  if ($isApprover && ($aRow['status'] === 'pending')) {
    $_data .= ' | <a href="#" class="text-success csa-quick-approve" data-id="'.$id.'">Approve</a>';
    $_data .= ' | <a href="#" class="text-danger csa-quick-reject" data-id="'.$id.'">Reject</a>';
  }

  $_data .= '</div>';

  $row[] = '#'.$id;
  $row[] = $_data;
  $row[] = e($aRow['department'] ?? '-');
  $row[] = e($aRow['activity_name'] ?? '-');

  $st = $aRow['status'] ?? 'pending';
  if ($st === 'approved') {
    $row[] = '<span class="label label-success">Approved</span>';
  } elseif ($st === 'rejected') {
    $row[] = '<span class="label label-danger">Rejected</span>';
  } else {
    $row[] = '<span class="label label-warning">Pending</span>';
  }

  $row[] = !empty($aRow['created_at']) ? e(_dt($aRow['created_at'])) : '-';

  $row['DT_RowClass'] = 'has-row-options';
  $output['aaData'][] = $row;
}
