<?php  require_once('../auth/auth.php'); ?>
<?php if(isset($_GET['pack_id'])){
  $pack_id       = $_GET['pack_id'];
  $pack = mysqli_query($conn,"SELECT package_name,package_price,branch_charge,package_duration FROM package WHERE pack_id='$pack_id' AND status=1 ORDER BY pack_id") or die(mysqli_error($conn));
  $pcr= mysqli_fetch_assoc($pack);

    $bridq = mysqli_query($conn,"SELECT brid FROM branch WHERE coid='$coid'")or die(mysqli_error($conn));
    $n_brid = mysqli_num_rows($bridq);

  $package_name  = $pcr['package_name'];
  $package_price = $pcr['package_price'];
  $package_duration = $pcr['package_duration'];
}
?>
<section class="content">
  <div class="container-fluid">
    <div class="row">
      <div class="col-md-2"></div>
      <div class="col-md-8">
        <div class="card card-primary card-outline direct-chat direct-chat-primary shadow-none" style="border-left:1px solid #969c9d;border-right:1px solid #969c9d;border-bottom:1px solid #969c9d;">
          <div class="card-header" style="padding: 3px 8px;">
            <h3 class="card-title"><i class="fa fa-plus"></i> Package Update</h3>
            <div class="card-tools m-0">
              <span class="mark1" onclick="copy(this);" title="Copy Invoice"><?php echo $invoice = substr(str_shuffle('1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ'), 0, 10);?></span>
              <button type="button" class="btn btn-tool" data-card-widget="remove">
              <i class="fas fa-times"></i>
              </button>
            </div>
          </div>
          <div class="card-body">
            <div class="col-md-12 my-2">
              <table border="1" style="width: 100%;">
               <thead>
                <tr>
                  <th colspan="5" class="text-lg text-center p-1">Package Information</th>
                </tr>
                <tr>
                  <th class="text-center w-35 p-1">Package Name</th>
                  <th class="text-center w-15 p-1">Price</th>
                  <th class="text-center w-15 p-1">Branch Charge</th>
                  <th class="text-center w-15 p-1">Total Charge</th>
                  <th class="text-center w-20 p-1">Your Balance</th>
                </tr> 
              </thead>
              <tbody>
                <tr>
                  <td class="text-center w-30 p-1"><?php echo $package_name;?></td>
                  <td class="text-center w-15 p-1"><?php echo $package_price;?></td>
                  <td class="text-center w-15 p-1">(<?php echo ($n_brid-1);?>*<?php echo $pcr['branch_charge'];?>) = <?php echo (($n_brid-1)*$pcr['branch_charge']);?></td>
                  <td class="text-center w-15 p-1">(<?php echo $package_price;?> + <?php echo ($pcr['branch_charge']*($n_brid-1));?>) = <?php echo ($package_price+($pcr['branch_charge']*($n_brid-1)));?></td>
                  <td class="text-center w-25 p-1">(<?php echo $company_balance;?> - <?php echo ($package_price+($pcr['branch_charge']*($n_brid-1)));?>) = <?php echo ($company_balance-($package_price+($pcr['branch_charge']*($n_brid-1))));?></td>
                </tr> 
              </tbody>               
              </table>
            </div>
          </div>
          <center>
            <div class="card-footer" style="padding: 8px 5px;">
            <?php if(($company_balance-($package_price+($pcr['branch_charge']*($n_brid-1))))>=0){ ?>
              <button id="save" class="btn btn-success btn-xs" onclick="renew(<?php echo $pack_id;?>)">Confirm</button>
            <?php } else { ?>
              <span class="text-lg text-danger text-bold blink-soft">Insuficient Balance Please Recharge </span> <button class="btn btn-primary btn-xs" onclick="pagecall('../bill_package/balance_add.php','#packagegview');">Rechage Now</button>
            <?php } ?>
            </div>
          </center>          
        </div>
      </div>
      <div class="col-md-2"></div>
    </div>
  </div>
</section>

<style type="text/css">
  .blink-hard {
    animation: blinker 1s step-end infinite;
  }
  .blink-soft {
    animation: blinker 1.5s linear infinite;
  }
  @keyframes blinker {
    50% {
      opacity: 0;
    }
  }
</style>

<script>
    function renew(pack_id){
     $('#save').prop('disabled', true);
     $('#save').html('<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> '+'Loading...'); 
     $.post("../bill_package/balance_db.php",
      {
        invoice      : '<?php echo $invoice;?>',
        pack_id      : pack_id,
        total_charge : "<?php echo ($package_price+($pcr['branch_charge']*($n_brid-1)));?>",
        package_duration : '<?php echo $package_duration;?>'
      },
      function(data) {
      if(data==101){
        toastr.warning('Something Is Wrong', 'Error');
      } else {
        toastr.success(Successfully, yourdata);
        window.location.reload();
    }
   });
  }  
</script>