 <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <div class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h1 class="m-0 text-dark">All Salary <b><?php echo $cur_date;?></b></h1>
          </div><!-- /.col -->
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item"><a href="<?php echo base_url() ?>admin/admin/dashboard">Dashboard</a></li>
              <li class="breadcrumb-item"><a href="#">Payout</a></li>
              <li class="breadcrumb-item active">All Salary</li>

            </ol>
          </div><!-- /.col -->
        </div><!-- /.row -->
      </div><!-- /.container-fluid -->
    </div>
    <!-- /.content-header -->
    <!-- Main content -->
    <section class="content">
      <div class="container-fluid">
        <div class="card card-default">
          <div class="card-header">
            <h3 class="card-title">
              <?php echo form_open(base_url().'admin/payout/view_all_salary'); ?>
                <div class="d-inline-block">
                  <input type="month" name="date" required class="form-control" value="<?php echo date('Y-m')?>">
                </div>
                <div class="d-inline-block">
                  <button class="btn btn-info" type="submit" name="search">Search</button>
                </div>
              <?php echo form_close(); ?>
            </h3>
          </div>
            <!-- /.card-header -->
            <div class="card-body">
              <table id="example1" class="table table-bordered table-striped">
                <thead>
                <tr>
                  <th>ID</th>
                  <th>Employee Name</th>
                  <th>Employee Role</th>
                  <th>Payment Details</th>
                  <th>Pending Payment</th>
                  <th></th>
                </tr>
                </thead>
                <tbody>
                <?php $i=1; foreach ($all_salary_adv as $salary):
                $ttl_amt = 0;
                foreach ($salary->payout as $payout) 
                {
                  $ttl_amt += $payout->pay_amount;
                }
                ?>
                <tr>
                  <td><?php echo $i ?></td>
                  <td><?php echo $salary->user_name ?></td>
                  <td>
                    <span class="badge badge-success"><?php echo $salary->user_role ?></span>
                    <?php if($salary->is_instructor == 1): ?>
                      <br/>
                      <span class="badge badge-warning">Instructor</span>
                    <?php endif; ?>
                  </td>
                  <td>
                    <button data-toggle="modal" data-target="#pay_details<?php echo $salary->user_id; ?>" class="btn btn-warning btn-xs"><i class="fa fa-eye"></i> View Details</button>
                  </td>
                  <td><b>&#8377; <?php echo $tlt_atm = $salary->employee_pay_value - $ttl_amt ; ?></b></td>
                  <td>
                    <?php if($tlt_atm != 0):?>
                      <button data-toggle="modal" data-target="#pay_modal<?php echo $salary->user_id; ?>" class="btn btn-info btn-xs">Pay Now</button>
                    <?php else: ?>
                      <span class="badge badge-success">Complete</span>
                    <?php endif; ?>
                  </td>
                </tr>
                <?php $i++; ?>
                <?php endforeach; ?>
                </tbody>
              </table>
            </div>
          </div>
      </div>
    </section>
  </div>
<?php $i=1; foreach ($all_salary_adv as $salary):
$ttl_amt = 0;
foreach ($salary->payout as $payout) 
{
  $ttl_amt += $payout->pay_amount;
}
?>
<?php $main_amt =  $salary->employee_pay_value - $ttl_amt ; ?>
<div class="modal fade" id="pay_modal<?php echo $salary->user_id; ?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Emp Name: <b><?php echo $salary->user_name; ?></b></h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <?php echo form_open(base_url().'admin/payout/add_salary_details');?>
      <div class="modal-body">
        <div class="form-group">
          <label for="message-amount" class="col-form-label">Deduction Amount</label>
          <input class="form-control" onkeyup="check_main_amt('<?php echo $salary->user_id; ?>', '<?php echo $main_amt; ?>')" name="deduction_amount" required type="number" id="ded_amt_<?php echo $salary->user_id; ?>" placeholder="Enter Deduction Amount">
        </div>
        <div class="form-group">
          <label for="message-text" class="col-form-label">Deduction Note:</label>
          <textarea class="form-control" id="message-text" placeholder="Enter Deduction Note Here" name="ded_pay_note"></textarea>
        </div>
        <div class="form-group">
          <label for="message-amount" class="col-form-label">Payble Amount <span class="text-danger">*</span></label>
          <input class="form-control" name="pay_amount" value="<?php echo $main_amt; ?>" required type="number" id="pay_amt_<?php echo $salary->user_id; ?>">
        </div>
        <div class="form-group">
          <label for="message-text" class="col-form-label">Note:</label>
          <textarea class="form-control" id="message-text" placeholder="Enter Note Here" name="pay_note"></textarea>
        </div>
        <input type="hidden" name="pay_emp_id" value="<?php echo $salary->user_id; ?>">
      </div>
      <div class="modal-footer">
        <button onclick="return confirm('Are you sure?')" type="submit" class="btn btn-primary">Save</button>
      </div>
      <?php echo form_close();?>
    </div>
  </div>
</div>
<?php endforeach; ?>
<script>
function check_main_amt(id, amt) 
{
  var main_amount = amt;
  var ded_amt = $('#ded_amt_'+id).val();
  var pay_amt = main_amount - ded_amt;
  $('#pay_amt_'+id).val(pay_amt);
}
</script>
<?php $i=1; foreach ($all_salary_adv as $salary):?>
  <div class="modal fade" id="pay_details<?php echo $salary->user_id; ?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title" id="exampleModalLabel">Payment Details for <b><?php echo $cur_date;?></b></h5>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>
        <div class="modal-body">
          <table class="table table-bordered table-striped">
            <tr>
              <th>No</th>
              <th>Emp. Name</th>
              <th>Note</th>
              <th>Date</th>
              <th>Mode</th>
              <th>Amount</th>
            </tr>
            <?php 
            if($salary->payout)
            {
              $ttl_pay = 0;
              $ttl_ded = 0;
              $ttl_salary =0;
              $ttl_adv =0;
              $a =1; foreach ($salary->payout as $payout): 
              if($payout->pay_is_advance == 1)
              {
                $ttl_adv += $payout->pay_amount;
              }

              if($payout->pay_is_advance == 2)
              {
                $ttl_ded += $payout->pay_amount;
              }
              
              if($payout->pay_is_advance == 0)
              {
                $ttl_salary += $payout->pay_amount;
              }
              ?>
                <tr>
                  <td><?php echo $a; ?></td>
                  <td><?php echo $salary->user_name; ?></td>
                  <td width="30%"><small><?php echo $payout->pay_note; ?></small></td>
                  <td><?php echo date('d-m-Y', strtotime($payout->pay_date)); ?></td>
                  <td>
                    <?php if($payout->pay_is_advance == 1)
                      {
                        echo '<b class="text-primary">Advance</b>';
                      }
                      elseif($payout->pay_is_advance == 2)
                      {
                        echo '<b class="text-danger">Deduction</b>'; 
                      }
                      else
                      {
                        echo '<b class="text-success">Salary</b>';
                      }
                    ?>
                  </td>
                  <td>&#8377;<?php echo $payout->pay_amount; ?></td>
                </tr>
              <?php $a++; endforeach ?>
              <tr>
                <th colspan="4"></th>
                <th>Main Salary</th>
                <th>&#8377;<?php echo $salary->employee_pay_value; ?></th>
              </tr>
              <tr>
                <th colspan="4"></th>
                <th>Total Adv.</th>
                <th>&#8377;<?php echo $ttl_adv; ?></th>
              </tr>
              
              <tr>
                <th colspan="4"></th>
                <th>Deduction</th>
                <th>-&#8377;<?php echo $ttl_ded; ?></th>
              </tr>
              <tr>
                <th colspan="4"></th>
                <th>Final Payment</th>
                <th>&#8377;<?php echo $ttl_salary; ?></th>
              </tr>
            <?php }else{?>
              <tr>
                <td colspan="6" class="text-center">No Data Found</td>
              </tr>
            <?php }?>
          </table>
        </div>
      </div>
    </div>
  </div>
<?php endforeach; ?>


