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

<?php
$logged = (int)get_staff_user_id();
$canAll = staff_can('view', 'coi_disclosure');
$canOwn = staff_can('view_own', 'coi_disclosure') && ((int)$decl['staff_id'] === $logged);

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

<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('coi_disclosure_view'); ?> #<?php echo (int)$decl['id']; ?>
              </h4>
              <div class="pull-right">
                 <!-- <a class="btn btn-default" href="<?php echo admin_url('coi_disclosure/pdf/'.$decl['id']); ?>">
                  <i class="fa fa-file-pdf-o"></i> <?php echo _l('coi_disclosure_pdf'); ?>
                </a> -->
                <?php if ($canEdit) { ?>
                  <a class="btn btn-info" href="<?php echo admin_url('coi_disclosure/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('coi_disclosure/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('coi_disclosure/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="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['signature'] ?? ''); ?></p>
                <p><strong>Date:</strong> <?php echo htmlspecialchars($decl['signed_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>

            <hr>

            <h5><strong>Part B Responses</strong></h5>

            <div class="table-responsive">
              <table class="table table-bordered">
                <thead>
                  <tr>
                    <th style="width:50px">No.</th>
                    <th>Question</th>
                    <th style="width:110px">Answer</th>
                    <th>Details</th>
                  </tr>
                </thead>
                <tbody>
                  <?php foreach ($items as $it) { ?>
                    <tr>
                      <td><?php echo (int)$it['question_no']; ?></td>
                      <td><?php echo htmlspecialchars($it['question'] ?? ''); ?></td>
                      <td>
                        <?php if (($it['answer'] ?? 'No') === 'Yes') { ?>
                          <span class="label label-danger">Yes</span>
                        <?php } else { ?>
                          <span class="label label-default">No</span>
                        <?php } ?>
                      </td>
                      <td><?php echo nl2br(htmlspecialchars($it['details'] ?? '')); ?></td>
                    </tr>
                  <?php } ?>
                </tbody>
              </table>
            </div>

            <?php if (!empty($files)) { ?>
              <h5 class="mtop20"><strong>Attachments</strong></h5>
              <ul>
                <?php foreach ($files as $f) { ?>
                  <li>
                    <a href="<?php echo admin_url('coi_disclosure/download_file/'.$decl['id'].'/'.$f['id']); ?>">
                      <?php echo htmlspecialchars($f['original_name'] ?: $f['file_name']); ?>
                    </a>
                  </li>
                <?php } ?>
              </ul>
            <?php } ?>

            <?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('coi_disclosure'); ?>">
                <i class="fa fa-arrow-left"></i> <?php echo _l('back'); ?>
              </a>
            </div>

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

<?php init_tail(); ?>
