<!-- Extend area where template is defined -->
<?= $this->extend('templates/adminlte/generalcontent') ?>
<!-- .Extend -->

<!-- Title of the page -->
<?= $this->section('title') ?>Payslip View<?= $this->endSection() ?>
<!-- .Title -->

<!-- CSS of the page -->
<?= $this->section('css') ?>

<!-- Select2 -->
<link rel="stylesheet" href="<?= base_url() ?>adminlte/plugins/select2/css/select2.min.css">
<link rel="stylesheet" href="<?= base_url() ?>adminlte/plugins/select2-bootstrap4-theme/select2-bootstrap4.min.css">

<?= $this->endSection() ?>
<!-- .CSS -->

<!-- body attribute - class definition -->
<?= $this->section('bodyclass') ?>sidebar-mini<?= $this->endSection() ?>
<!-- .body attribute -->

<!-- Container title -->
<?= $this->section('containertitle') ?>Payslip View<?= $this->endSection() ?>
<!-- .Container title -->

<!-- Active breadcrumb -->
<?= $this->section('breadcrumbs') ?>
<li class="breadcrumb-item active">Payslip View</li>
<?= $this->endSection() ?>
<!-- .Active breadcrumb -->

<!-- Main content -->
<?= $this->section('main') ?>

<!-- Modal Add Branch -->
<div class="modal fade" id="mdlAddBranch">
  <div class="modal-dialog">
    <div class="modal-content">
      <form action="<?= url_to('payroll/addpaygroup') ?>" method="post">
        <div class="modal-header">
          <h4 class="modal-title" >New Group</h4>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>
        <div class="modal-body">
          <div class="row">
            <div class="col-12">
              <p class="lead">Select Payroll Period</p>
              <div class="row">
                <div class="col-12">
                  <div class="form-group">
                    <label for="txtGroupCode">Payroll Group Code</label>
                    <input class="form-control" type="text" id="txtGroupCode" name="pay_group_code" value="<?= old('pay_group_code') ?>">
                  </div>
                  <div class="form-group">
                    <label for="txtGroupName">Payroll Group Name</label>
                    <input class="form-control" type="text" id="txtGroupName" name="pay_group_name" values="<?= old('pay_group_name') ?>">
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="modal-footer">
            <button type="submit" class="btn btn-primary">Save changes</button>
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        </div>
      </form>
    </div>
  </div>
</div>


<div class="row">
    <div class="col-12">
        <div class="card">
            <div class="card-header">
                <h3 class="card-title">Select Payroll Period</h3>
            </div>
            <div class="card-body">
              <form action="/remp/payview" method="get">
                <div class="form-group">
                  <label>Payroll Period</label>
                  <div class="input-group mb-3">  
                    <select class="form-control select2 rounded-0" name="paytransid">
                      <option value="-1">-- Select --</option>
                      <?php foreach($payTransactions as $payTrans): ?>
                        <?php $selected = ($paytransid != null && $paytransid == $payTrans->paytrans_id) ? 'selected' : ''; ?>
                        <option value="<?= $payTrans->paytrans_id ?>" <?= $selected ?>><?= '['.($payTrans->is_open ? 'Open' : 'Closed').'] Payroll from '.$payTrans->payroll_from.' to '.$payTrans->payroll_to ?></option>
                      <?php endforeach; ?>
                    </select>
                    <span class="input-group-append">
                      <button type="submit" class="btn btn-info btn-flat">View Payslip</button>
                    </span>
                  </div>
                </div>
              </form>
            </div>
        </div>
    </div>
</div>

<?php if($paytransid != null): ?>
  <?php if($empPayTrans != null): ?>
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title">Payslip View</h3>
                </div>
                <div class="card-body">
                  <table id="tblEmpPayslipView" class="table table-bordered">
                      <tr>
                          <td>Employee Name:</td>
                          <td colspan="3" class="text-bold"><?= $empPayTrans->last_name.", ".$empPayTrans->first_name ?></td>
                      </tr>
                      <tr class="bg-light text-bold">
                          <td>Description</td>
                          <td>Deduction</td>
                          <td>Income</td>
                          <td>Total</td>
                      </tr>
  
                      <tr>
                          <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Basic Pay @<?= number_format($empPayTrans->basic_daily_pay, 2, '.', ',') ?> x <?= number_format($empPayTrans->actual_work_days, 2, '.', ',') ?> days</td>
                          <td>&nbsp;</td>
                          <td><?= number_format($empPayTrans->basic_pay, 2, '.', ','); ?></td>
                          <td>&nbsp;</td>
                      </tr>

                      <?php 
                        $grossIncome = $empPayTrans->basic_pay;
                        $totalDeduction = 0;
                        foreach($empPayTransIncomes as $empPayTransIncome): 
                      ?>
                        <tr>
                          <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?= $empPayTransIncome->payslip_display ?></td>
                          <td>&nbsp;</td>
                          <td><?= number_format($empPayTransIncome->amount, 2, '.', ','); ?></td>
                          <td>&nbsp;</td>
                        </tr>
                      <?php 
                        $grossIncome += $empPayTransIncome->amount;
                        endforeach; 
                      ?>

                      <tr>
                        <td><strong>Gross Income</strong></td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td class="text-right"><strong><?= number_format($grossIncome, 2, ".", ",") ?></strong></td>
                      </tr>

                      <tr>
                          <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Income Tax</td>
                          <td>-<?= number_format($empPayTrans->income_tax, 2, '.', ','); ?></td>
                          <td>&nbsp;</td>
                          <td>&nbsp;</td>
                      </tr>

                      <?php foreach($empPayTransDeductions as $empPayTransDeduction): ?>
                        <tr>
                          <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?= $empPayTransDeduction->payslip_display ?></td>
                          <td>-<?= number_format($empPayTransDeduction->amount, 2, '.', ','); ?></td>
                          <td>&nbsp;</td>
                          <td>&nbsp;</td>
                        </tr>
                      <?php 
                        $totalDeduction += $empPayTransDeduction->amount;
                        endforeach; 
                      ?>

                      <tr>
                        <td><strong>Total Deduction</strong></td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td class="text-right">-<strong><?= number_format($totalDeduction, 2, ".", ",") ?></strong></td>
                      </tr>
                      <tr>
                        <td><strong>Net Income</strong></td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td class="text-right"><strong><?= number_format($empPayTrans->net_pay, 2, ".", ",") ?></strong></td>
                      </tr>
                  </table>
                </div>
                <div class="card-footer">
                <a href="/remp/payprint/<?= $paytransid ?>" target="_blank" class="btn btn-primary">Print Payslip</a>
                </div>
            </div>
        </div>
    </div>
  <?php else: ?>
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title">Payslip View</h3>
                </div>
                <div class="card-body">
                    <p>No payslip for this cut-off.</p>
                </div>
            </div>
        </div>
    </div>
  <?php endif; ?>
<?php endif; ?>

<?= $this->endSection() ?>
<!-- .Main content -->

<!-- Javascript -->

<?= $this->section('js') ?>

<!-- Select2 -->
<script src="<?= base_url() ?>adminlte/plugins/select2/js/select2.full.min.js"></script>

<script>
$(document).ready(function() {
  //Initialize Select2 Elements
  $('.select2').select2();
});


</script>

<?= $this->endSection() ?>

<!-- .Javascript -->