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

<?php
// Permission helpers for this single record (view.php gets $decl, $items, $files, $activity_logs)
$loggedStaffId = (int) get_staff_user_id();

$canViewAll = staff_can('view', 'staff_declaration');
$isOwner    = staff_can('view_own', 'staff_declaration') && ((int)($decl['staff_id'] ?? 0) === $loggedStaffId);
$isHod      = ((int)($decl['hod_id'] ?? 0) === $loggedStaffId);

// If view.php is reachable, controller already allowed. But still safe:
$canSee = $canViewAll || $isOwner || $isHod;

$st = strtolower(trim((string)($decl['status'] ?? 'pending_hod')));

$canEdit = staff_can('edit', 'staff_declaration') && ($canViewAll || $isOwner);
$canEditFinal = $canEdit && !in_array($st, ['approved','rejected'], true);

$canDelete = staff_can('delete', 'staff_declaration') && ($canViewAll || $isOwner);




$is_final_allowed = is_admin();
if (!$is_final_allowed && !empty($recipients) && is_array($recipients)) {
    $is_final_allowed = in_array(
        $loggedStaffId,
        array_map('intval', array_column($recipients, 'staffid')),
        true
    );
}


$can_hod_action   = ($decl['status'] === 'pending_hod' && $is_hod);
$can_final_action = ($decl['status'] === 'pending_final' && $is_final_allowed);

$hod_name = !empty($decl['hod_id']) ? get_staff_full_name($decl['hod_id']) : 'HOD (Not Set)';
?>
<style>
  .see-kv{ margin-bottom:8px; }
  .see-kv strong{ display:inline-block; min-width:170px; color:#111827; }
  .see-block-title{
    margin-top:18px; padding:10px 12px;
    border-left:4px solid #00829B; background:#f4fbfc; border-radius:6px;
    font-weight:700; color:#004f5c;
  }
  .see-table th{ background:#f4fbfc !important; color:#004f5c; }
  .see-muted{ color:#6b7280; }
  .approval-step-approved { background:#dcedc8; border:1px solid #8ac34a; border-radius:5px; padding:10px; border-width: 2px;}
  .approval-step-rejected { background:#ffe6e6; border:1px solid #f56565; border-radius:5px; padding:10px; }

   .approval-exec-status > * {
  margin-left: auto;
  margin-right: auto;
}
table {
  border-collapse: collapse;
  border-spacing: 0;
}
td, th {
  padding: 0;
}
.approval-timelimit {
  font-weight: bolder;
}
.approval-status-icon--approved {
  color: #558b3F;
}

.approval-theoreticallimit {
  opacity: 0.4;
}
.approval-step-ongoing {
 
  border-width: 2px;
  border: solid 1px;

  border-radius: 5px;
  padding: 10px;
  background-color: #bee3f8;
    border-color: #429ae1;
    border-width: 2px;

}
.approval-status-icon--rejected {
  color: #9d2c2c;
}
.approval-step-header {
  margin: 5px;
  font-weight: bolder;
}
div.approver-label {
  padding: 10px;
    padding-left: 10px;
  padding-left: 16px;
  margin: 5px;
    margin-right: 5px;
  margin-right: 0;
  background-color: #f8f9fa;
  -moz-border-radius: 6px;
  -webkit-border-radius: 6px;
  border-radius: 6px;
  text-transform: uppercase;

}
.approval-status-icon--ongoing {
  color: #2c5382;
}
.approval-status-icon {
  font-size: 1.17rem;
  margin-right: 5px;
}
.fa, .fas {
  font-weight: 900;
}
.fa-hourglass-half::before {
  content: "\f252";
}

.ibo-button.ibo-is-regular.ibo-is-neutral{
  background-color: #f8f9fa;
  color: #212934;
  box-shadow: 0px 2px 0px #d5dde5;
}
.ibo-button, .ui-dialog .ui-button, .ui-datepicker-current, .ui-datepicker-close {
  position: relative;
  display: inline-block;
  padding: 6px 9px;
  border: 0;
  border-radius: 4px;
  cursor: pointer;
  text-transform: uppercase;
  white-space: nowrap;
}
.approval-idle {
  opacity: 0.4;
}

.fa-arrow-right::before {
  content: "\f061";
}

.approval-step-start {
  background-color: #f8f9fa;
  border: solid 1px #aebecd;
  border-radius: 5px;
  padding: 10px;
}
.approval-step-idle {
  background-color: #f8f9fa;
   border: solid 1px #aebecd;
 border-radius: 5px;
 
  padding: 10px;
}
.not .started {
  background-color: #f5f5f5; border-color: #cccccc; border-width: 2px;
}
         .see-section-h{
    margin:18px 0 10px; padding:10px 12px;
    border-left:4px solid #00829B; background:#f4fbfc; border-radius:6px;
    font-weight:700; color:#004f5c;
  }
</style>
<div id="wrapper">
  <div class="content">
    <div class="row">
      <div class="col-md-12">
        <div class="panel_s">
          <div class="panel-body">

            <div class="clearfix">
              <h4 class="pull-left">
                <?php echo _l('staff_declaration'); ?> #<?php echo (int)$decl['id']; ?>
              </h4>

              <div class="pull-right">
                <!--<a class="btn btn-default" href="<?php echo admin_url('staff_declaration/pdf/'.$decl['id']); ?>">-->
                <!--  <i class="fa fa-file-pdf-o"></i> <?php echo _l('staff_declaration_pdf'); ?>-->
                <!--</a>-->

                <?php if ($canEditFinal) { ?>
                  <a class="btn btn-info" href="<?php echo admin_url('staff_declaration/edit/'.$decl['id']); ?>">
                    <i class="fa fa-pencil"></i> <?php echo _l('edit'); ?>
                  </a>
                <?php } ?>

                <?php if ($canDelete) { ?>
                  <a class="btn btn-danger _delete" href="<?php echo admin_url('staff_declaration/delete/'.$decl['id']); ?>">
                    <i class="fa fa-trash"></i> <?php echo _l('delete'); ?>
                  </a>
                <?php } ?>


                <a class="btn btn-success" href="<?php echo admin_url('staff_declaration/export_excel?id='.$decl['id']); ?>">
  <i class="fa fa-file-excel-o"></i> Export Excel
</a>

              </div>
            </div>

            <hr class="hr-panel-heading" />
<div class="see-section-h">Section A — Staff Information</div>
            <div class="row">  
              <div class="col-md-6">
                <p><strong>Name:</strong> <?php echo htmlspecialchars($decl['staff_name'] ?? ''); ?></p>
                <p><strong>Staff ID:</strong> <?php echo htmlspecialchars($decl['staff_code'] ?? $decl['staff_id']); ?></p>
                <p><strong>Position:</strong> <?php echo htmlspecialchars($decl['position'] ?? ''); ?></p>
                <p><strong>Department:</strong> <?php echo htmlspecialchars($decl['department'] ?? ''); ?></p>
              </div>
              <div class="col-md-6">
                <p><strong>Signature:</strong> <?php echo htmlspecialchars($decl['employee_signature'] ?? ''); ?></p>
                <p><strong>Date:</strong> <?php echo htmlspecialchars($decl['employee_date'] ?? ''); ?></p>
                <p><strong>Created:</strong> <?php echo !empty($decl['created_at']) ? _dt($decl['created_at']) : '-'; ?></p>
                <p><strong>Updated:</strong> <?php echo !empty($decl['updated_at']) ? _dt($decl['updated_at']) : '-'; ?></p>
              </div>
            </div>

 
            <div class="see-section-h">Section B — Gift / Hospitality / Entertainment Details</div>
            <div class="table-responsive">
              <table class="table table-bordered">
                <thead>
                  <tr>
                    <th style="width:50px">No</th>
                    <th style="width:120px">Date</th>
                    <th>Category</th>
                    <th>Description</th>
                    <th style="width:110px">Value (RM)</th>
                    <th>From</th>
                    <th>Relationship</th>
                    <th>Purpose</th>
                    <th>Action</th>
                  </tr>
                </thead>
                <tbody>
                  <?php if (!empty($items)) { ?>
                    <?php foreach ($items as $i => $it) { ?>
                      <tr>
                        <td><?php echo $i + 1; ?></td>
                        <td><?php echo htmlspecialchars ($it['date_received'] ?? ''); ?></td>
                        <td><?php echo htmlspecialchars($it['category'] ?? ''); ?></td>
                        <td><?php echo htmlspecialchars($it['description'] ?? ''); ?></td>
                        <td><?php echo htmlspecialchars($it['estimated_value'] ?? ''); ?></td>
                        <td><?php echo htmlspecialchars($it['gift_from'] ?? ''); ?></td>
                        <td><?php echo htmlspecialchars($it['relationship'] ?? ''); ?></td>
                        <td><?php echo htmlspecialchars($it['purpose'] ?? ''); ?></td>
                        <td><?php echo htmlspecialchars($it['action_taken'] ?? ''); ?></td>
                      </tr>
                    <?php } ?>
                  <?php } else { ?>
                    <tr>
                      <td colspan="8" class="text-center text-muted">No rows recorded.</td>
                    </tr>
                  <?php } ?>
                </tbody>
              </table>
            </div>

            <?php if (!empty($files)) { ?>
              <h5 class="mtop20"><strong>Attachments</strong></h5>

              <div class="table-responsive">
                <table class="table table-striped">
                  <thead>
                    <tr>
                      <th>File</th>
                      <th style="width:140px">Uploaded</th>
                      <th style="width:140px">Actions</th>
                    </tr>
                  </thead>
                  <tbody>
                    <?php foreach ($files as $f) { ?>
                      <tr>
                        <td>
                          <a href="<?php echo admin_url('staff_declaration/download_file/'.$decl['id'].'/'.$f['id']); ?>">
                            <?php echo htmlspecialchars($f['original_name'] ?: $f['file_name']); ?>
                          </a>
                        </td>
                        <td><?php echo !empty($f['dateadded']) ? _dt($f['dateadded']) : '-'; ?></td>
                        <td>
                          <a class="btn btn-xs btn-default"
                             href="<?php echo admin_url('staff_declaration/download_file/'.$decl['id'].'/'.$f['id']); ?>">
                            <i class="fa fa-download"></i> Download
                          </a>

                          <?php if ($canEditFinal) { ?>

                            <a class="btn btn-xs btn-danger _delete"
                               href="<?php echo admin_url('staff_declaration/delete_file/'.$decl['id'].'/'.$f['id']); ?>">
                              <i class="fa fa-trash"></i> Delete
                            </a>
                          <?php } ?>
                        </td>
                      </tr>
                    <?php } ?>
                  </tbody>
                </table>
              </div>
            <?php } ?>

             <!-- APPROVAL PROGRESS -->
            
          
            <!-- APPROVER ACTIONS -->
           
<div class="see-section-h">Section C — Declaration</div>
    
          
           <?php if ((int)($decl['staff_declare'] ?? 0) === 1) { ?>
  <p><strong>Declaration:</strong> I Acknowledge</p>
<?php } ?>
 <p><strong>Signature:</strong> <?php echo e($decl['employee_signature'] ?? ''); ?></p>
            <p><strong>Date:</strong> <?php echo !empty($decl['employee_date']) ? e(_d($decl['employee_date'])) : '-'; ?></p>
              <p><strong>Created:</strong> <?php echo !empty($decl['created_at']) ? e(_d($decl['created_at'])) : '-'; ?></p>
                <p><strong>Updated:</strong> <?php echo !empty($decl['updated_at']) ? e(_d($decl['updated_at'])) : '-'; ?></p>
            <!--Approval-->
             <!-- APPROVAL PROGRESS -->
            <hr>
            <div class="col-md-12 p-0">
              <div class="panel_s">
                <div class="panel-body">
    <div class="card-header d-flex align-items-center">
      <h5 class="card-title">Recommendation</h5>
    </div>

    <div class="card-body">
      <table id="process_status_table" class="table-responsive">
        <tbody>
          <tr style="vertical-align: middle;">
            <td></td>

            <td><div class="approval-step approval-step-start">Start</div></td>

            <td class="approval-idle">
              <i class="fa fa-arrow-right" style="font-size:2rem"></i>
            </td>

            <!-- HOD step -->
            <td>
              <?php
                $hod_box_class = 'approval-step-idle';
                if ($decl['status'] === 'pending_hod') $hod_box_class = 'approval-step-ongoing';
                if (($decl['hod_status'] ?? '') === 'approved') $hod_box_class = 'approval-step-approved';
                if (($decl['hod_status'] ?? '') === 'rejected') $hod_box_class = 'approval-step-rejected';
              ?>
              <div class="approval-step <?php echo $hod_box_class; ?>">
                <div class="approval-step-header">
                  <?php echo ($decl['status'] === 'pending_hod') ? 'To Review' : 'HOD'; ?>
                </div>
                <div class="approver-label">
                  <?php echo strtoupper($hod_name); ?>
                  <div class="muted" style="margin-top:6px">
                    <?php if (!empty($decl['hod_status'])) { ?>
                      Status: <strong><?php echo e($decl['hod_status']); ?></strong><br>
                      Date: <?php echo !empty($decl['hod_acted_at']) ? e(_dt($decl['hod_acted_at'])) : '-'; ?>
                    <?php } else { ?>
                      Status: <strong>pending</strong>
                    <?php } ?>
                  </div>
                </div>

                <?php if ($can_hod_action) { ?>
                 <?php echo form_open(admin_url('staff_declaration/hod_decide/'.$decl['id'])); ?>
  <div class="form-group">
    <label>Comment (optional)</label>
    <textarea name="comment" class="form-control" rows="3"></textarea>
  </div>

  <button type="submit" name="action" value="approved" class="btn btn-success">
    Approve
  </button>
  <button type="submit" name="action" value="rejected" class="btn btn-danger">
    Reject
  </button>
<?php echo form_close(); ?>

                <?php } ?>
              </div>
            </td>

            <td class="approval-idle">
              <i class="fa fa-arrow-right" style="font-size:2rem"></i>
            </td>

            <!-- FINAL step -->
            <td>
              <?php
                $final_box_class = 'approval-step-idle';
                if ($decl['status'] === 'pending_final') $final_box_class = 'approval-step-ongoing';
                if (($decl['final_status'] ?? '') === 'approved') $final_box_class = 'approval-step-approved';
                if (($decl['final_status'] ?? '') === 'rejected') $final_box_class = 'approval-step-rejected';

                $final_label = 'PCEO';
                if ($decl['status'] === 'pending_final') $final_label = 'To Approve';
              ?>
              <div class="approval-step <?php echo $final_box_class; ?>">
                <div class="approval-step-header"><?php echo e($final_label); ?></div>

                <div class="approver-label">
                  <?php echo !empty($decl['final_approver_id']) ? strtoupper(get_staff_full_name($decl['final_approver_id'])) : 'PCEO'; ?>
                  <div class="muted" style="margin-top:6px">
                    <?php if (!empty($decl['final_status'])) { ?>
                      Status: <strong><?php echo e($decl['final_status']); ?></strong><br>
                      Date: <?php echo !empty($decl['final_acted_at']) ? e(_dt($decl['final_acted_at'])) : '-'; ?>
                    <?php } else { ?>
                      Status: <strong><?php echo ($decl['status']==='pending_final'?'pending':'idle'); ?></strong>
                    <?php } ?>
                  </div>
                </div>

                <?php if ($can_final_action) { ?>
                <?php echo form_open(admin_url('staff_declaration/final_decide/'.$decl['id'])); ?>
  <div class="form-group">
    <label>Comment (optional)</label>
    <textarea name="comment" class="form-control" rows="3"></textarea>
  </div>

  <button type="submit" name="action" value="approved" class="btn btn-success">
    Approve
  </button>
  <button type="submit" name="action" value="rejected" class="btn btn-danger">
    Reject
  </button>
<?php echo form_close(); ?>

                <?php } ?>
              </div>
            </td>

            <td class="approval-idle">
              <i class="fa fa-arrow-right" style="font-size:2rem"></i>
            </td>

            <td>
              <div class="approval-step approval-step-idle">
                End<br>
                <span class="muted">Overall: <strong><?php echo _l($decl['status']); ?></strong></span>
              </div>
            </td>

          </tr>
        </tbody>
      </table>

      <!-- Optional: show approval history -->
      <?php if (!empty($approvals)) { ?>
        <hr>
        <h5>Approval History</h5>
        <table class="table table-bordered">
          <thead>
            <tr>
              <th>Step</th>
              <th>Approver</th>
              <th>Status</th>
              <th>Comment</th>
              <th>Date</th>
            </tr>
          </thead>
          <tbody>
            <?php foreach ($approvals as $a) { ?>
              <tr>
                <td><?php echo e(strtoupper($a['step'])); ?></td>
                <td><?php echo e($a['approver_name'] ?? ''); ?></td>
                <td><?php echo e($a['status']); ?></td>
                <td><?php echo e($a['comment'] ?? '-'); ?></td>
                <td><?php echo !empty($a['acted_at']) ? e(_dt($a['acted_at'])) : '-'; ?></td>
              </tr>
            <?php } ?>
          </tbody>
        </table>
      <?php } ?>
    </div>
  </div>
</div>
</div>

            <!--Approval-->


            <hr>

            <?php if (!empty($activity_logs)) { ?>
              <h5 class="mtop20"><strong>Activity Log</strong></h5>
              <div class="table-responsive">
                <table class="table table-striped">
                  <thead>
                    <tr>
                      <th style="width:170px">Date</th>
                      <th style="width:220px">Staff</th>
                      <th>Action</th>
                    </tr>
                  </thead>
                  <tbody>
                    <?php foreach ($activity_logs as $l) {
                      $staffName = trim(($l['firstname'] ?? '') . ' ' . ($l['lastname'] ?? ''));
                    ?>
                      <tr>
                        <td><?php echo _dt($l['date']); ?></td>
                        <td><?php echo htmlspecialchars($staffName ?: ('ID ' . ($l['staffid'] ?? '-'))); ?></td>
                        <td><?php echo htmlspecialchars($l['description'] ?? ''); ?></td>
                      </tr>
                    <?php } ?>
                  </tbody>
                </table>
              </div>
            <?php } ?>


            <div class="mtop20">
              <a class="btn btn-default" href="<?php echo admin_url('staff_declaration'); ?>">
                <i class="fa fa-arrow-left"></i> <?php echo _l('back'); ?>
              </a>
            </div>

          </div>
        </div>
      </div>
    </div>
  </div>
</div>

<?php init_tail(); ?>
