<style>
        .table1{}
        .table1 td{
            border-top: none !important;
            padding:1px 8px 1px 8px !important;
        }
        
        .table .top {
            border-top: 1px solid #000 !important;
        }
</style>  
<div class="wrapper">
    <div class="container">
        <!-- Page-Title -->
        <div class="row">
            <div class="col-sm-12">
                <h4 class="page-title">New Invoice for <?php echo $details->customer_name; ?> </h4>
            </div>
        </div>
        <?php if ($this->session->flashdata('Success') && $this->session->flashdata('Success') != "") { ?>
            <div class="row hidden-print">
                <div class="col-sm-12">
                    <div class="alert alert-success">
                        <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                        <strong>Success!</strong> <?php echo $this->session->flashdata('Success'); ?>
                    </div>
                </div>
            </div>
        <?php } ?>
        <?php if ($this->session->flashdata('Error') && $this->session->flashdata('Error') != "") { ?>
            <div class="row hidden-print">
                <div class="col-sm-12">
                    <div class="alert alert-danger">
                        <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                        <strong>Error!</strong> <?php echo $this->session->flashdata('Error'); ?>
                    </div>
                </div>
            </div>
        <?php } ?>
        
        <div class="row">
            <div class="col-md-12">
                <div class="panel panel-default">
                    <div class="panel-body">
                        <div class="row">
                            <div class="pull-left">
                                <h3 class="logo invoice-logo"><?php if(isset($business)){ if(isset($business[0]['business_logo1'])){echo "<img src='".base_url().'assets/images/business/'.$business[0]['business_logo1']."' alt='".$business[0]['business_name']."' class='img-responsive' />";}else{echo "<img src='".base_url().'assets/images/business/'.$business[0]['business_logo']."' alt='".$business[0]['business_name']."' class='img-responsive' />"; }} else {echo 'SalonPK';}?></h3>
                            </div>
                            <div class="pull-right">
                                <table class="table table1" style="border:none !important;">
                                    <tr>
                                        <td style="border-right:1px solid #f7f7f7; color:#797979; text-align: right; vertical-align: bottom;">Date</td>
                                        <td style="color: #000; font: normal normal 16px/16px arial, serif; vertical-align: middle;"><?php echo date('d-m-Y') ?></td>
                                    </tr>
                                    <tr>
                                        <td style="border-right:1px solid #f7f7f7; color:#797979; text-align: right">Student</td>
                                        <td style="color:#000;"><?php if(isset($details)){ echo sprintf("%04s",$details->id_program_enrollment);}else{echo '00001';} ?></td>
                                    </tr>
                                    <tr>
                                        <td style="border-right:1px solid #f7f7f7; color:#797979; text-align: right">Contact</td>
                                        <td style="color:#000;">Academy Manager</td>
                                    </tr>
                                    <tr>
                                        <td style="border-right:1px solid #f7f7f7; color:#797979; text-align: right"></td>
                                        <td style="color:#000;"><?php if(isset($business)){ echo $business[0]['business_email'];}?></td>
                                    </tr>
                                    <tr>
                                        <td style="border-right:1px solid #f7f7f7; color:#797979; text-align: right"></td>
                                        <td style="color:#000;"><?php if(isset($business)){ echo $business[0]['business_phone2'];}?></td>
                                    </tr>
                                </table>
                            </div>
                        </div>
                        
                        <div class="row m-t-40">
                                   
                                <div class="col-xs-6">

                                </div>
                                <div class="col-xs-6">
                                    <span style="color: #000; font: normal normal 16px/40px Roboto, serif;  text-transform: uppercase;"><?php echo $details->customer_name; ?></span><br>
                                    <span style="color: #000; font: normal normal 16px/20px Roboto, serif;  "><?php echo $details->customer_address; ?></span><br>
                                    <span style="color: #000; font: normal normal 12px/40px Roboto, serif;  text-transform: uppercase;"><?php echo $details->customer_cell; ?></span><br>


                                    <br>
                                </div>

                        </div>
                        
                        <div class="row">
                            <div class="col-md-12">
                               
                           </div>
                        </div>
                        <form action="<?php echo base_url();?>programs_controller/add_invoice_payment" method="post">
                        <input type="hidden" name="enrollment_id" value="<?php echo $details->id_program_enrollment;?>">
                        <div class="row">
                            <div class="col-md-4 hidden-sm hidden-xs hidden-print">
                                <span style="font: normal bold 22px/30px arial, serif; color:#000; " >Details</span>
                                <table class="table m-t-40">
                                    
                                    <tbody>
                                        <tr>
                                            <td>Program: </td>
                                            <td><?php echo $details->program;?></td>
                                        </tr>
                                        <tr>
                                            <td>Price: </td>
                                            <td><?php echo $details->program_price;?></td>
                                        </tr>
                                        <tr>
                                            <td>Session: </td>
                                            <td><?php echo $details->session_name;?></td>
                                        </tr>
                                        <tr>
                                            <td></td>
                                            <td><?php echo $details->program_session_start.' - '.$details->program_session_end;?></td>
                                        </tr>
                                        <tr>
                                            <td>Total Months</td>
                                            <td><?php echo $details->month_diff;?> months (<?php echo CURRENCY_CODE;?>. <span id="monthly" style="font-weight: bold;"><?php if($details->month_diff>0){ echo $details->program_price/$details->month_diff;} else { echo $details->program_price;}?></span>/month)</td>
                                        </tr>
                                    </tbody>
                                </table>
                                
                                
                                <div class="form-group hidden-sm hidden-xs hidden-print">
                                    <label for="remarks">Remarks</label>
                                    <textarea name="remarks" id="remarks" class="form-control" ></textarea>
                                </div>
                            </div>
                            <div class="col-md-2"></div>
                            <div class="col-md-6">
                                <span style="font: normal bold 22px/30px arial, serif; color:#000">RECEIPT</span>
                                <table class="table">
                                    <thead>
                                        <tr>
                                            <th>Item</th>
                                            <th style="text-align: right;">Amount</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr>
                                            <td><strong>Invoice Date</strong></td>
                                            <td><input class="form-control" id="invoicedate" name="invoicedate"></td>
                                        </tr>
                                        <tr>
                                             
                                            <td><strong>Payment Mode</strong></td>
                                            <td style="text-align: right;">
                                                <select name="paymentmode" style="width: 120px; float: right;" class="form-control">
                                                    <option value="cash">Cash</option>
                                                    <option value="card">Card</option>
                                                    <option value="check">Check</option>
                                                </select>
                                                
                                                
                                            </td>
                                        </tr>
                                    <td><strong>For the Months</strong></td>
                                            <td style="text-align: left;">
                                                <select id="frommonth" name="frommonth" style="width: 120px; float: right;  margin-left:20px;" class="form-control ">
                                                    <?php 
                                                    if(sizeof($invoices)>0){
                                                        if($invoices[0]['to_month']!==null and $invoices[0]['to_month']==''){
                                                        $start = substr($invoices[0]['to_month'],-4);
                                                        } else { $start=date("Y")+1;}
                                                    } else {$start=date("Y")+1; }
                                                    $years=[]; 
                                                    for($x=0; $x<=3; $x++){
                                                        $years[$x]= $start - $x;
                                                    }
                                                    foreach($years as $year){
                                                        $i_months = $months;//array_reverse($months);
                                                        $x=1;
                                                        $selected=false;
                                                        foreach($i_months as $month){?>
                                                        <option <?php echo "month_int='".$x."'"; echo "year_int='".$year."'"; if(sizeof($invoices)>0){if($selected==true){ echo "selected='selected'"; $selected=false; } } else if($month['month']==date('m') && $year==date('Y')){echo "selected='selected'";} ?> value="<?php echo $month['month_name'].' '.$year;?>"><?php echo $month['month_name'].' '.$year;?></option>
                                                        <?php if($month['month_name'].' '.$year == trim(trim(substr($invoices[0]['to_month'],0,3)).' '.substr($invoices[0]['to_month'],-4))){ 
                                                            $selected=true;
                                                        }?>  
                                                        <?php $x++;} ?>
                                                    <?php } ?>
                                                </select>
                                                <select onchange="update_months();" id="tomonth" name="tomonth" style="width: 120px; float: right;" class="form-control">
                                                    <?php 
                                                    if(sizeof($invoices)>0){
                                                        if($invoices[0]['to_month']!==null and $invoices[0]['to_month']==''){
                                                        $start = substr($invoices[0]['to_month'],-4);
                                                        } else { $start=date("Y")+1;}
                                                    } else {$start=date("Y")+1; }
                                                    $years=[]; 
                                                    for($x=0; $x<=3; $x++){
                                                        $years[$x]= $start - $x;
                                                    }
                                                    foreach($years as $year){
                                                        $i_months = $months;// array_reverse($months);
                                                        $x=1;
                                                        $selected=false;
                                                        foreach($i_months as $month){?>     
                                                        
                                                        <option <?php echo "month_int='".$x."'"; echo "year_int='".$year."'"; if(sizeof($invoices)>0){if($selected==true){ echo "selected='selected'"; $selected=false; } } else if($month['month']==date('m') && $year==date('Y')){echo "selected='selected'";} ?> value="<?php echo $month['month_name'].' '.$year;?>"><?php echo $month['month_name'].' '.$year;?></option>
                                                         <?php if($month['month_name'].' '.$year == trim(trim(substr($invoices[0]['to_month'],0,3)).' '.substr($invoices[0]['to_month'],-4))){ 
                                                            $selected=true;
                                                        }?>   
                                                        <?php $x++; } ?>
                                                    <?php } ?>
                                                </select>
                                                
                                                
                                                
                                            </td>
                                        </tr>
                                        <tr>
                                            <td><strong>Program Price</strong></td>
                                             <td style="text-align: right;"><strong><?php echo $details->program_price;?></strong></td>
                                        </tr>
                                        <tr>
                                            <td>Agreed Discount</td>
                                             <td style="text-align: right;"><?php echo $details->agreeddiscount;?></td>
                                        </tr>
                                        <tr >
                                            <td class="top">Total Due</td>
                                             <td class="top" style="text-align: right;"><?php echo $details->program_price - $details->agreeddiscount;?></td>
                                        </tr>
                                        <tr>
                                             <td>Already Paid</td>
                                             <td style="text-align: right;"><?php echo $details->paid_till_date;?></td>
                                        </tr>
                                        
                                        <tr>
                                            <td class="top"><strong>Remaining Amount</strong></td>
                                            <td class="top" style="text-align: right;"><strong><span id="remaining"><?php echo $details->program_price - $details->agreeddiscount - $details->paid_till_date;?></span></strong></td>
                                        </tr>
                                        
                                        <tr class="hidden">
                                            <td><strong>Sales Tax </strong><input onchange="javascript:update_tax();" style="width:100px; display: inline-block; text-align:right;" class="form-control" id="taxrate" name="taxrate" value="0"> <strong>%</strong></td>
                                             <td style="text-align: right;"><input onchange="javascript:update_total();" style="text-align: right; width:250px; float:right;" class="form-control" name="salestax" id="salestax" value="0" /></td>
                                        </tr>
                                         <tr>
                                             <td class="top"><strong>Due After Taxes </strong></td>
                                             <td class="top" style="text-align: right;"><input style="text-align: right; width:250px; border: none; background: transparent; font-weight: bold;"  name="aftertax" id="aftertax" value="<?php echo $details->program_price - $details->agreeddiscount - $details->paid_till_date;?>" /></td>
                                        </tr>
                                         <tr>
                                            <td><strong>Paying Now</strong></td>
                                            <td style="text-align: right;"><input  required="required" style="text-align: right; width:250px; float:right;" class="form-control numeric" name="payingnow" id="payingnow" value="<?php echo $details->program_price - $details->agreeddiscount - $details->paid_till_date;?>" /></td>
                                        </tr>
                                       
                                    </tbody>
                                    <tfoot>
                                        <tr>
                                            <th></th>
                                            <th><button style="float:right"  class="btn btn-primary" >Submit</button></th>
                                        </tr>
                                    </tfoot>
                                </table>
                                
                            </div>
                           
                        </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>

    </div>
    
    
    <script>
        
        $(document).ready(function(){
            
            $('#invoicedate').datepicker({
                default: today,
                autoclose: true,
                todayHighlight: true,
                format: 'yyyy-mm-dd'
            }).datepicker('setDate', new Date());
            
            
            
        });
        
        $(".numeric").keypress(function(e) {
            //if the letter is not digit then display error and don't type anything
            if (e.which != 13 && e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
                return false;
            }
        });

        
        $(".decimal").keypress(function(e) {
            //if the letter is not digit then display error and don't type anything
            if (e.which != 13 && e.which != 8 && e.which != 0  && e.which != 46 && (e.which < 48 || e.which > 57) ) {
                
                return false;
            }
        });
        
        function update_tax(){
            //console.log(parseInt($("#remaining").html()));
            //console.log(parseInt($("#taxrate").val()));
            var tax = (parseInt($("#remaining").html()) * parseInt($("#taxrate").val()))/100;
            $("#salestax").val(tax);
            update_total();
            
        }
        
        function update_total(){
            var totalpayable = parseInt($("#remaining").html()) + parseInt($("#salestax").val());
            $("#aftertax").val(totalpayable);
        }
        
       function update_months(){
           
           var d1 = $('option:selected', '#frommonth').attr('year_int')+'-'+$('option:selected', '#frommonth').attr('month_int')+'-'+'1';
           var d2 = $('option:selected', '#tomonth').attr('year_int')+'-'+$('option:selected', '#tomonth').attr('month_int')+'-'+'1';
           
           var fromdate = new Date(d1);
           var todate = new Date(d2);
          
        var diff = Math.floor(todate.getTime() - fromdate.getTime());
        var day = 1000 * 60 * 60 * 24;
           
        var days = Math.floor(diff/day);
        var months = Math.floor(days/31)+1;
        var years = Math.floor(months/12);
          
        var price = $("#monthly").html();
        price = parseFloat(price)*months;
        $("#payingnow").val(price);
        $("#payingnow").css("readonly","readonly");
           
       }
        
    </script>