<style>
    body{font-family: sans-serif;}
</style>

<div class="wrapper" style="color:#000 !important">
            <div class="container">
                <!-- Page-Title -->
                
                <div class="row">
                    <div class="col-md-12">
                        <div class="panel panel-default">
                            <div class="panel-body" style="padding:10px !important;">
                                <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>Invoices 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>
                                <?php if(sizeof($invoice) > 0){?>                               
                                <div class="row" style="color:#000 !important">
                                    <div class="col-md-12">                                        
                                        <div class="pull-left m-t-10 " >
                                            <label>Enrollment #: <?php echo $invoice[0]['id_program_enrollment']; ?></label>    
                                            <input name="customerid" id="customerid" type="hidden" value="<?php echo $invoice[0]['customer_id']; ?>"/>
                                            <input name="txtinvoiceid" id="txtinvoiceid" type="hidden" value="<?php echo $invoice[0]['id_program_invoices']; ?>"/>
                                            <address>
                                              <strong><?php 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"){ 
                                                        echo $invoice[0]['customer_cell']; 
                                                    } 
                                              ?></span><br>
                                              <span class="hidden-sm hidden-xs"><?php echo $invoice[0]['customer_address']; ?></span><br>
                                              <span class="hidden-sm hidden-xs"><?php 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 echo $invoice[0]['payment_mode']; ?></span></p>                                            
                                            <p class="m-t-0"><strong>Agent:</strong><span> <?php 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 class="">Service Name</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">Invoiced Amount</th>
                                                        <th class="text-right">Other Charges</th>
                                                        <th class="text-right">CC Fee</th>
                                                        <th class="text-right">Discount</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></th>
                                                </tr>
                                                </thead>
                                                <tbody style="color:#000 !important">
                                                    <?php $invoicetotal=0; $grosstotal=0; $paidtotal=0; $balancetotal=0; $x=1; if(isset($invoice)){   
                                                        foreach($invoice as $invoicedetail){?>
                                                        <tr>
                                                            <td><?php echo $invoicedetail['id_program_invoices'];?></td>
                                                            <td class="hidden"><?php echo $invoicedetail['service_id'] == '' ? 0 : $invoicedetail['service_id']; ?></td>
                                                            <td><?php if(null != $invoicedetail['service_name']){ echo  $invoicedetail['service_name'] ;} else {echo "Registration Fee";} ?></td>
                                                            <td><?php echo  $invoicedetail['program_invoice_remarks'] ; ?></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"><?php echo $invoicedetail['invoiced_amount']; ?></td>   
                                                            <td class="text-right"><?php echo $invoicedetail['invoice_other_charges']; ?></td>
                                                            <td class="text-right"><?php echo $invoicedetail['cc_charge']; ?></td>     
                                                            <td class="text-right"><?php echo $invoicedetail['invoice_discount']; ?></td>     
                                                            <td class="text-right"><?php echo $invoicedetail['salestax']; ?></td>     
                                                            <td class="text-right"><?php 
                                                                if($invoicedetail["invoice_remarks"]=="Enrolled Charge"){
                                                                    $invoicetotal = ($invoicedetail['invoiced_amount']+ $invoicedetail['cc_charge'] + $invoicedetail['salestax']); 
                                                                    echo $invoicetotal; 
                                                                    $grosstotal=$grosstotal + $invoicetotal; 
                                                                }
                                                                ?>
                                                            </td>
                                                            <td class="text-right">
                                                                <?php echo (($invoicedetail['paid']+ $invoicedetail['cc_charge'])); $paidtotal=$paidtotal+(($invoicedetail['paid']+ $invoicedetail['cc_charge'])); 
                                                            ?></td>
                                                            <td class="text-right">
                                                                <?php $balance = $invoicedetail['balance']; echo $invoicedetail['balance'] ; $balancetotal = ($balancetotal+$balance); ?>
                                                            </td>
                                                            <td class="hidden-print">
                                                                <?php $remark=$invoicedetail['program_invoice_remarks']; if($invoicedetail['paid'] > 0){ ?>
                                                                    <a href="<?php echo base_url();?>/invoice_payments_on_each_invoice/<?php echo $invoicedetail['id_program_invoices']."/".$invoicedetail['id_program_enrollment'];?> " class="m-r-10 btn btn-default btn-xs" target="_blank"  ><i class="zmdi zmdi-collection-text"></i> Payments</a>
                                                                <?php } else if(substr($remark,0,8) =='Recovery'){ ?>
                                                                    <a href="<?php echo base_url();?>/invoice_payments_on_each_invoice/<?php echo $invoicedetail['id_program_invoices']."/".$invoicedetail['id_program_enrollment'];?>" class="m-r-10 btn  btn-default btn-xs"  target="_blank"><i class="zmdi zmdi-collection-text"></i> Payments</a>
                                                                <?php } ?>
                                                            </td>
                                                        </tr>
                                                        <?php $x++;}} ?>

                                                </tbody>
                                                <tfoot>
                                                    <tr>
                                                        <th colspan="11"></th>
                                                        <th class="text-right"><?php echo $grosstotal;?></th>
                                                        <th class="text-right"><?php echo $paidtotal;?></th>
                                                        <th class="text-right"><?php echo $balancetotal;?></th>
                                                        <th></th>
                                                    </tr>
                                                </tfoot>
                                            </table>
                                        </div>
                                    </div>
                                </div>
                                <div class="row" style="color:#000 !important">
                                    <div class="col-md-6 col-lg-6 hidden-sm hidden-xs">
-                                    <div class="clearfix m-t-40">
                                            <h5 class="small text-inverse font-600">PAYMENT TERMS AND POLICIES</h5>
                                            <small>
                                                <?php echo $business[0]['payment_terms'];?>
                                            </small>
                                        </div>
                                    </div>
                                    <div class="col-md-3 col-sm-12 col-xs-12 col-md-offset-3" style="color:#000 !important">
                                        <p class="text-right m-t-0"><b>Total Invoiced Amount :  <?php echo CURRENCY_CODE;?>. <?php echo $grosstotal;?></b> </p>                                        
                                        <p class="text-right m-t-0"><b>Paid Amount :  <?php echo CURRENCY_CODE;?>. <?php echo $paidtotal;?></b> </p>
                                        
                                        <hr>
                                        <p class="text-right m-t-0"><b>Balance :  <?php echo CURRENCY_CODE;?>. <?php echo $balancetotal;?></b></p>
                                        
                                        <hr>
                                        
                                    </div>
                                    <div style="font-size:14px; text-align: center">
                                        <?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>                                
                                <hr>
                                
                                <div class="hidden-print">
                                   
                                    <div class="pull-right">
                                        <a href="javascript:window.print()" class="btn btn-inverse waves-effect waves-light" id="btnprint" ><i class="fa fa-print"></i></a>
                                    </div>
                                     <div class="pull-right">
                                        <a onclick="sendEmail(<?php echo $invoice[0]['customer_id']; ?>);" data-toggle="tooltip" data-placement="top" data-original-title="Email this" href="javascript:void(0);" class="btn btn-default waves-effect waves-light" id="btnprint"><i class="fa fa-envelope"></i></a>
                                    </div>  
                                     <div class="pull-right">
                                         <?php if($balancetotal>0){?>
                                        <a href="<?php echo base_url();?>/service_invoice/<?php echo $invoice[0]['id_program_enrollment'];?>" class="m-r-10 btn btn-pink waves-effect waves-light" id="btnprint" ><i class="fa fa-dollar"></i> Add Payment</a>
                                         <?php } ?>
                                    </div>
                                </div>
 <?php } ?>
                            </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>
               
<script>


    function remove_payment(invoiceid, paid_amount){

        if(invoiceid !='' && invoiceid>0){

           swal({
                title: 'Are you sure?',
                text: "You will not be able to undo the payment cancellation for invoice "+invoiceid,
                type: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#ff5b5b !important',
                cancelButtonColor: '#d33',
                cancelButtonText: 'No',
                confirmButtonText: 'Yes'

            }, function(isConfirm) {          
                if(isConfirm){
                    remove_payment_query(invoiceid, paid_amount);
                }
            });                            
        }
    } 

    function view_all_payments_against_each_invoice(invoice_id){

    }

    function remove_payment_query(invoiceid, paid_amount){
        
        $.ajax({
                type: 'POST',
                //url: 'Scheduler_controller/staffreporting',
                url: "<?php echo base_url() . 'Coaching_controller/cancel_invoice_payment'; ?>",
                //data: 'staff_id=' +staff_id+ '&calendar_date=' +calendar_date,
                data: {
                    invoice_id: invoiceid,
                    paid_amount: paid_amount
                },
                success: function(response) {
                    console.log(response);
                     var data = response.split('|');
                     
                     if(data[0]=='Success'){
                        toastr.success('Payment Removed for Invoice '+ data[1] +'!', 'Done!');
                        location.reload();
                    }else{
                        toastr.error(data[1], 'Info');
                    }
                    
                }
            });
    }


    function remove_recovery(invoiceid){

        if(invoiceid !='' && invoiceid>0){

           swal({
                title: 'Are you sure?',
                text: "No recovery will be possible for this amount after removal of invoice "+invoiceid,
                type: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#ff5b5b !important',
                cancelButtonColor: '#d33',
                cancelButtonText: 'No',
                confirmButtonText: 'Yes'

            }, function(isConfirm) {          
                if(isConfirm){
                    remove_recovery_query(invoiceid);
                }
            });                            
        }
    }

    function remove_recovery_query(invoiceid){
        
        $.ajax({
                type: 'POST',
                //url: 'Scheduler_controller/staffreporting',
                url: "<?php echo base_url() . 'Coaching_controller/remove_recovery'; ?>",
                //data: 'staff_id=' +staff_id+ '&calendar_date=' +calendar_date,
                data: {
                    invoice_id: invoiceid
                },
                success: function(response) {
                    console.log(response);
                     var data = response.split('|');
                     
                     if(data[0]=='Success'){
                        toastr.success('Recovery Removed for Invoice '+ data[1] +'!', 'Done!');
                       // location.reload();
                    }else{
                        toastr.error(data[1], 'Info');
                    }
                    
                }
            });
    }

    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
                });

            }
        });
    }
</script>