<style>
    body{font-family: sans-serif;}
</style>
<?php //if(sizeof($invoice) > 0){?>
<div class="wrapper" style="color:#000 !important">
            <div class="container">
                <!-- Page-Title -->
                
                <div class="row">
                    <div class="col-md-12 col-sm-12 col-xs-12">
                        <div class="panel panel-default">
                            <div class="panel-body" style="padding:10px !important;">
                                <form id="invoiceform" action="<?php echo base_url();?>add_receivable_invoices_payment" method="Post">
                                <div class="row">
                                    <div class="col-sm-12 col-md-6 pull-left">
                                        <div ><?php if(isset($business)){ echo "<img width='180px' src='".base_url().'assets/images/business/'.$business[0]['business_logo']."' alt='".$business[0]['business_name']."' class='img-responsive' />";} else {echo 'SkedWise';}?>
                                        <span>Payment View</span>
                                            <span ><?php if(isset($business)){ echo $business[0]['business_address'];} ?></span>
                                            <span ><?php if(isset($business)){ echo $business[0]['business_phone1'];} ?></span>
                                        </div>
                                    </div>                                    
                                   
                                </div>
                                
                                <div class="row" style="color:#000 !important">
                                    <div class="col-md-12">
                                        
                                        <div class="pull-left m-t-10 " >
                                            <label>Enrollment #:<?php echo $enrollmentid; ?></label>
                                            <input name="id_program_enrollment" id="id_program_enrollment" type="hidden" value="<?php echo $enrollmentid; ?>"/>
                                            <input name="customerid" id="customerid" type="hidden" value="<?php if(null != $invoice){ echo $invoice[0]['customer_id'];} ?>"/>
                                            
                                            <address>
                                              <strong><?php if(null != $invoice){echo $invoice[0]['customer_name']; }?></strong><br>
                                              <span ><abbr class="hidden-sm hidden-xs" title="Phone">P:</abbr> <?php 
                                                    if( $this->session->userdata('hide_cell') && $this->session->userdata('hide_cell') == "No"){ 
                                                        if(null != $invoice){echo $invoice[0]['customer_cell']; }
                                                    } 
                                              ?></span><br>
                                              <span class="hidden-sm hidden-xs"><?php if(null != $invoice){echo $invoice[0]['customer_address'];} ?></span><br>
                                              <span class="hidden-sm hidden-xs"><?php if(null != $invoice){echo $invoice[0]['customer_email'];} ?></span>
                                              
                                            </address>
                                        </div>
                                        <div class="pull-right m-t-10">                                            
                                            <p class="m-t-0" id="modep"><strong>Payment: </strong> <span id="mode"><?php if(null != $invoice){echo $invoice[0]['payment_mode']; }?></span></p>                                            
                                            <p class="m-t-0"><strong>Agent:</strong><span> <?php if(null != $invoice){echo $invoice[0]['created_by'];} ?></span></p>
                                        </div>
                                    </div><!-- end col -->
                                </div>
                                <!-- end row -->
                                <div class="row" style="color:#000 !important">
                                    <div class="col-md-12">
                                        <div class="">
                                            <table class="table " id="tblservices" style="color:#000 !important">
                                                <thead style="color:#000 !important">
                                                    <tr>
                                                        <th>#</th>
                                                        <th class="hidden">Service ID</th>
                                                        <th>Description</th>
                                                        <th>Invoice Status</th>
                                                        <th class="text-right">Invoiced Sessions</th>                                                        
                                                        <th class="text-right">Session Rate</th>
                                                        <th class="text-right">Other Charges</th>                                                        
                                                        <th class="text-right">Discount</th>
                                                        <th class="text-right">Payment Mode</th>
                                                        <th class="text-right">CC Fee</th>
                                                        <th class="text-right">Sales Tax</th>
                                                        <th class="text-right">Total</th>
                                                        <th class="text-right ">Paid</th>
                                                        <th class="text-right ">Balance</th>
                                                        <th class="text-right text-primary"><strong>Paying Now</strong></th>
                                                </tr>
                                                </thead>
                                                <tbody style="color:#000 !important">
                                                 
                                                    <?php $invoicetotal=0; $grosstotal=0; $x=1; if(isset($invoice)){    
                                                     
                                                        foreach($invoice as $invoicedetail){?>
                                                        <tr>
                                                            <td invoiceamount = <?php echo $invoicedetail['invoiced_amount']; ?>><?php echo $invoicedetail['id_program_invoices'];?></td>
                                                            <td class="hidden"><input name="id_program_invoice[]" value="<?php echo $invoicedetail['id_program_invoices'];?>" type="hidden" ><?php echo $invoicedetail['service_id'] == '' ? 0 : $invoicedetail['service_id']; ?></td>                                                            
                                                            <td><?php echo $invoicedetail['service_name'] =='' ? $invoicedetail['program_invoice_remarks'] : $invoicedetail['service_name']; ?></td>
                                                            <td ><strong><?php echo $invoicedetail['payment_mode']; ?></strong></td>    
                                                            <td class="text-right"><?php echo $invoicedetail['number_of_sessions'] ==0 ? '' : $invoicedetail['number_of_sessions']; ?></td>    
                                                            <td class="text-right"><?php echo $invoicedetail['per_session']; ?></td>   
                                                            <td class="text-right"><input name="invoice_other_charges[]" <?php if($invoicedetail['paid_amount']!=0){echo "readonly='readonly'";}?> onchange="updatetotal();" type="number" min="0" style="text-align: right; border:none; width:80px;" value="<?php echo $invoicedetail['invoice_other_charges']; ?>" readonly/></td>
                                                            
                                                            <td class="text-right"><input name="invoice_discount[]" <?php if($invoicedetail['paid_amount']!=0){echo "readonly='readonly'";}?> onchange="updatetotal();" type="number" min="0" style="text-align: right; border:none; width:80px;" value="<?php echo $invoicedetail['invoice_discount']; ?>" readonly/></td>     
                                                            <td  class="text-right">
                                                                <select name="mode[]" style="text-align: right; border:none; width:90px;">
                                                                    <option  value="Cash">Cash</option>
                                                                    <option value="Card">Card</option>
                                                                    <option value="Check">Check / Online</option>                                                                    
                                                                </select>
                                                            </td>
                                                            <td class="text-right"><input name="cc_charge[]" <?php if($invoicedetail['paid_amount']!=0){echo "readonly='readonly'";}?> onchange="updatetotal();" type="number" min="0" style="text-align: right; border:none; width:80px;" value="0"/></td>     
                                                            <td class="text-right"><input  name="salestax[]" <?php if($invoicedetail['paid_amount']!=0){echo "readonly='readonly'";}else{echo "readonly='readonly'"; }?> onchange="updatetotal();" type="number" min="0" style="text-align: right; border:none; width:80px;" value="<?php echo $invoicedetail['salestax']; ?>"/></td>
                                                            <td class="text-right"><?php $invoicetotal = ($invoicedetail['invoiced_amount'] + $invoicedetail['salestax'] + $invoicedetail['cc_charge']); echo $invoicetotal; ?></td>
                                                            <td class="text-right "><?php echo (($invoicedetail['paid']+$invoicedetail['cc_charge']));?></td>
                                                            <td class="text-right "><?php $invoicepayable = $invoicetotal - (($invoicedetail['paid']+$invoicedetail['cc_charge'])); echo $invoicepayable;  $grosstotal=$grosstotal + $invoicepayable; ?></td> 
                                                            <?php  echo  $invoicepayable; ?>
                                                            <td class="text-right text-primary"><input name="paid_amount[]" onchange="if($(this).val()<0){$(this).val(0);}else if($(this).val() > <?php echo $invoicepayable; ?>){}  updatetotal(); updatepaying(); " min="0" type="number" style="font-weight: bold; text-align: right; border:none; width:80px;" value="0" step="any"/></td>
                                                            <td class="hidden"><input type="hidden"  name="balance[]" value="<?php echo $invoicepayable; ?>"></td>
                                                        </tr>
                                                        <?php $x++;}} ?>
                                                        <tr>
                                                            <td colspan="8">                                                               
                                                            </td>
                                                            <td ></td>
                                                            <td ></td>
                                                            <td ></td>
                                                            <td ></td>                                                                                                                        
                                                        </tr>
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>
                                </div>
                                <div class="row" style="color:#000 !important">
                                    
                                    <div class="col-md-3 col-sm-12 col-xs-12 col-md-offset-2"  >
                                        
    -                                      
                                                <h5 class="small text-inverse font-600">PAYMENT TERMS AND POLICIES</h5>
                                                <small>
                                                    <?php echo $business[0]['payment_terms'];?>
                                                </small>
                                            
                                        
                                        <?php if(isset($business[0]['fineprint']) && $business[0]['fineprint'] == 'Yes'){?>
                                        <p><strong style="color:#000">THANK YOU FOR YOUR VISIT</strong></p>
                                        <?php } ?>
                                    </div>
                                   
                                    
                                    <div class="col-md-3 col-sm-12 col-xs-12 col-md-offset-4" style="color:#000 !important">
                                        <p class="text-right m-t-0"><b>Gross Total:  <?php echo CURRENCY_CODE;?>. <input readonly="readonly" name="grosstotal" id="grosstotal" style="text-align:left; border:none; width:150px;" value="<?php echo $grosstotal;?>"> <input type="hidden" id="gross_total_hidden" value="<?php echo $grosstotal;?>" /></b> </p>                                        
                                        
                                        <p class="text-right m-t-0"><b>Paying Now:  <?php echo CURRENCY_CODE;?>. <input readonly="readonly" name="payingnow" id="payingnow" style="text-align:right; padding-right: 25px; border:none; width:150px;" value="0"></b> </p>
                                        <hr>
                                        <p class="text-right m-t-0"><b class="text-danger">Balance:  <?php echo CURRENCY_CODE;?>. <input class="text-danger" readonly="readonly" name="totalbalance" id="balance" style="text-align:right; padding-right: 25px; border:none; width:150px;" value="<?php echo $grosstotal;?>"></b> </p>
                                        
                                    </div>
                                </div>                                
                                <hr>
                                
                                <div class="hidden-print">

                                    <div class="pull-right">
                                        <button type="submit" class="btn btn-pink waves-effect waves-light" id="btnprint" ><i class="fa fa-save"></i> Submit</button>
                                    </div>
                                    <div class="pull-right">
                                        
                                    </div>                                   
                                </div>
                                </form>
                            </div>
                        
                        </div>

                    </div>

                </div>
                <!-- end row -->
                
                <div class="modal fade none-border" id="eventview" tabindex="-1" role="dialog" aria-labelledby="Event View" aria-hidden="true">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-header">
                                <button type="button" class="close" onclick="$('#eventview').modal('hide');" aria-hidden="true">×</button>
                                <h4 class="modal-title">Send Email</h4>
                            </div>
                            <div class="modal-body">
                                <label class="" for="email">Customer Email</label>
                                <input class="form-control" type="email" id="email" placeholder="Enter Customer Email e.g. example@abc.com">
                                
                                <div class="pull-right">
                                    <label for="default" style="margin-top: 10px;">
                                        <input type="checkbox" id="default" checked="checked"> Default
                                    </label>
                                </div>
                                <div class="clearfix"></div>
                                
                                <hr>
                                
                                <div class="pull-right">
                                    <button onclick="sendEmailDo(<?php echo $invoice[0]['customer_id']; ?>);" class="btn btn-sm btn-primary">Send <i class="fa fa-send"></i></button>
                                </div>
                                
                                <div class="clearfix"></div>
                            </div>
                        </div><!-- /.modal-content -->
                    </div><!-- /.modal-dialog -->
                </div>
<?php //} ?>                
                <script>
                    function sendEmailDo(id){
                        var email = $('#email').val();
                        var invoiceid = $('#txtinvoiceid').val();
                        var senddefault = $('#default:checkbox:checked').val() === 'on' ? 'yes' : 'no';
                        
                        if(email === ""){
                            swal({
                                title: "Empty field found!",
                                text: "Email should not be empty.",
                                type: "error",
                                confirmButtonText: 'OK!'
                            });
                            return false;
                        }
                        $.ajax({
                            url: '<?php echo base_url().'Customer_controller/sendEmailDo'; ?>',
                            type: 'POST',
                            data: {
                                customerid : id,
                                email : email,
                                invoiceid : invoiceid,
                                senddefault : senddefault 
                            },
                            success: function(result){
                                $('#eventview').modal('hide');
                                toastr.success('Email sent!', 'Done!');
                            }
                        });
                    }
                    
                    function sendEmail(id){
                        $.ajax({
                            url: '<?php echo base_url().'Customer_controller/sendEmail'; ?>',
                            type: 'POST',
                            data: { customerid : id },
                            success: function(result){
                                
                                if(result === "empty"){
                                    $('#email').val('');
                                    $('#empty').val('yes');
                                } else{
                                    $('#email').val(result);
                                    $('#empty').val('no');
                                }
                                
                                $('#eventview').modal({
                                    backdrop:'static',
                                    keyboard:false,
                                    show:true
                                });
                                
                            }
                        });
                    }
                    
                    
    function updatetotal(){
            
        var grosstotal = 0;
        $('#tblservices tr').each(function(row, tr) {
            if(parseFloat($(tr).find('td:eq(0)').attr('invoiceamount')) >= 0){
                if(parseFloat($(tr).find('td:eq(14) input').val()) > parseFloat($(tr).find('td:eq(11)').text())){ 
                    $(tr).find('td:eq(14) input').val(0);                    
                }
          

                var invoiced_amount = $(tr).find('td:eq(0)').attr('invoiceamount'); 
   
                var other_charges = $(tr).find('td:eq(6) input').val();
                // var discount = $(tr).find('td:eq(7) input').val();
                var discount = 0;
                var ccfee = $(tr).find('td:eq(9) input').val(); 

                var t_price =  parseFloat( $(tr).find('td:eq(11)').text());  
             
                var tax = $(tr).find('td:eq(10) input').val();
                var paid = $(tr).find('td:eq(14) input').val(); 
                var total = parseFloat(invoiced_amount) + parseFloat(ccfee) +parseFloat(tax); 
                total = total;  
                if( parseFloat(ccfee) > 0 ){ 
                    t_price = Math.round(total*100)/100;
                    console.log(total);                 
                    $(tr).find('td:eq(11)').text(t_price); 
                }else{  
                    t_price = Math.round(t_price*100)/100;
                    console.log(total);                 
                  $(tr).find('td:eq(11)').text(t_price); 

                }              
             

                var half_paid  =  parseFloat($(tr).find('td:eq(12)').text()); 
            
                
                var balance =  parseFloat(parseFloat(half_paid) + parseFloat(paid));
    
                $(tr).find('td:eq(13)').text( parseFloat(t_price) -(parseFloat(half_paid) +  parseFloat(paid))  );
                $(tr).find('td:eq(15) input').val(balance);
                
                grosstotal = grosstotal + balance;
               
                console.log(grosstotal,"grosstotal");
            }
        }); 
            //    var  grosstotal =0;
            var other_charges_plus_cc_charges=0;
            var grand_total = parseFloat( $("#gross_total_hidden").val());  

             $('#tblservices tr').each(function(row, tr) { 
                if(parseFloat($(tr).find('td:eq(0)').attr('invoiceamount')) >= 0){
                   var ccfee = parseFloat($(tr).find('td:eq(9) input').val());  
                   var other_charges = parseFloat($(tr).find('td:eq(6) input').val());  
                   if(ccfee > 0){
                       other_charges_plus_cc_charges+= (ccfee);
                   }
                 }       
                });  
                $("#grosstotal").val((grand_total+other_charges_plus_cc_charges));       

    }
    
    function updatepaying(){
        
        var payingtotal = 0;
        var grosstotal = $("#grosstotal").val();
        $('#tblservices tr').each(function(row, tr) {
            if(parseFloat($(tr).find('td:eq(0)').attr('invoiceamount')) >= 0){
                var paying_amount = $(tr).find('td:eq(14) input').val();
                if(parseFloat(paying_amount) > parseFloat($(tr).find('td:eq(11)').text())){ 
                    paying_amount =  0; 
                }
                paying_amount = Math.round(parseFloat(paying_amount)*100)/100;
                $(tr).find('td:eq(14) input').val(paying_amount);
                payingtotal = payingtotal+paying_amount;               
            }
        });
        
        $("#payingnow").val(payingtotal);
        $("#balance").val(grosstotal-payingtotal);
        
    }
    </script>