  <?php
    $this->load->view('templates/header');
  ?>
    <script>
        var link = "invoice_section";
        var sub_link = "invoice";
    </script>
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <h1 class="content_top"><?php echo empty($inv_data) ? 'ADD INVOICE' : 'UPDATE INVOICE'; ?></h1>
      <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
        <li><a href="#">Invoice</a></li>
        <li class="active"><?php echo empty($inv_data) ? 'Add' : 'Update' ?></li>
      </ol>
    </section>

    <!-- Main content -->
    <section class="content">
        <div class="row"><!-- left column -->
            <div class="col-md-12">
                <div class="box box-primary">
                    <div class="box-body">
                        <div class="alert_msg"></div>
                        <form class="form-horizontal" id="invoice_form">
                            <div class="box-body remove-bottom"><!-- box-body -->
                                <div class="form-group floating-form">
                                    <!-- issue no -->
                                    <div class="col-md-1 floating-label">
                                        <input type="number" class="form-control floating-input" id="inv_no" name="inv_no" value="<?php echo !empty($inv_data) ? $inv_data[0]['inv_entry_no'] : $entry_no; ?>" placeholder=" " autocomplete="off"  />
                                        <label>INVOICE NO</label>
                                    </div>
                                    <!-- issue no -->

                                    <!-- issue date -->
                                    <div class="col-md-1 floating-label">
                                        <input type="text" class="form-control floating-input" id="inv_invoice_date" name="inv_invoice_date" value="<?php echo !empty($inv_data) ? date('d-m-Y', strtotime($inv_data[0]['inv_invoice_date'])) : "" ?>"
                                        placeholder=" " autocomplete="off" required/>
                                        <input type="hidden" class="form-control floating-input" id="inv_entry_date" name="inv_entry_date" value="<?php echo !empty($inv_data) ? date('d-m-Y', strtotime($inv_data[0]['inv_entry_date'])) : date('d-m-Y');?>"
                                        placeholder=" " autocomplete="off" readonly/>
                                        <label>INVOICE DATE</label>
                                    </div>
                                    <!-- issue date -->

                                    <!-- party -->
                                    <div class="col-md-2 floating-label">
                                        <?php echo form_dropdown('', $user, !empty($inv_data) ? $inv_data[0]['inv_customer_id'] : 0 ,'id="cust_id" name="cust_id" class="form-control flexselect floating-select" onchange="get_product_data()" placeholder=" " autocomplete="off" tabindex="2" ');  ?>
                                        <label>CUSTOMER</label>
                                        <span class="text-danger" id="acc_type_name"></span>
                                        <input type="hidden" name="acc_type" id="acc_type" value="<?php echo !empty($inv_data) ? $inv_data[0]['inv_acc_type'] :'';?>">
                                    </div>
                                    <div class="col-md-1 floating-label">
                                        <input type="text" class="form-control floating-input" id="inv_dispatch_by" name="inv_dispatch_by" value="<?php echo !empty($inv_data) ? $inv_data[0]['inv_dispatch_by'] : " " ?>"
                                        placeholder=" " autocomplete="off"/>
                                        <label>DISPATCH BY</label>
                                    </div>
                                    <!-- party -->
                                    
                                    <div class="col-md-1  floating-label">
                                        <input type="text" class="form-control floating-input" id="inv_terms" name="inv_terms" value="<?php echo !empty($inv_data) ? $inv_data[0]['inv_terms'] : " " ?>"
                                        placeholder=" " autocomplete="off"/>
                                        <label>TERMS</label>
                                        <input type="hidden" class="form-control floating-input" id="total_inv_cgst" name="total_inv_cgst" value="<?php echo !empty($inv_data) ? $inv_data[0]['inv_total_cgst'] :'';?>" autocomplete="off" readonly/>
                                        <input type="hidden" class="form-control floating-input" id="total_inv_sgst" name="total_inv_sgst" value="<?php echo !empty($inv_data) ? $inv_data[0]['inv_total_sgst'] :'';?>" autocomplete="off" readonly/>
                                        <input type="hidden" class="form-control floating-input" id="total_inv_igst" name="total_inv_igst" value="<?php echo !empty($inv_data) ? $inv_data[0]['inv_total_igst'] :'';?>" autocomplete="off" readonly/>
                                        <input type="hidden" class="form-control floating-input" id="total_qty" name="total_qty" value="<?php echo !empty($inv_data) ? $inv_data[0]['inv_total_qty'] :'';?>" autocomplete="off" readonly/>
                                        <input type="hidden" class="form-control floating-input" id="sub_total" name="sub_total" value="<?php echo !empty($inv_data) ? $inv_data[0]['inv_sub_total_amt'] :'';?>" autocomplete="off" readonly/>
                                        <input type="hidden" class="form-control floating-input" id="total_rate" name="total_rate" value="<?php echo !empty($inv_data) ? $inv_data[0]['inv_total_rate'] :'';?>" autocomplete="off" readonly/>
                                        <input type="hidden" class="form-control floating-input" id="final_total" name="final_total" value="<?php echo !empty($inv_data) ? $inv_data[0]['inv_total_amt'] :'';?>" autocomplete="off" readonly/>
                                    </div>
                                    <div class="col-md-2 floating-label">
                                        <input type="text" class="form-control floating-input" id="inv_addon" name="inv_addon" value="<?php echo !empty($inv_data) ? $inv_data[0]['inv_addon'] : "GOODS SOLD IN MUMBAI" ?>"
                                        placeholder=" " autocomplete="off"/>
                                        <label>Add On</label>
                                    </div>
                                    <div class="col-md-2 floating-label">
                                        <input type="text" class="form-control floating-input" id="inv_addon_for_self" name="inv_addon_for_self" value="<?php echo !empty($inv_data) ? $inv_data[0]['inv_addon_for_self'] : " " ?>"
                                        placeholder=" " autocomplete="off"/>
                                        <label>Free Text</label>
                                    </div>
                                    <div class="col-md-1 ">
                                        <button type="button" class="btn btn-primary" id="sbt_btn"  onclick="<?php echo !empty($inv_data) ? 'add_invoice('.$inv_data[0]['inv_id'].')' :'add_invoice(0)' ;?>" tabindex="10"><?php echo !empty($inv_data) ? 'Update' :'Submit' ;?></button>
                                        
                                    </div>
                                    <div class="col-md-1">
                                        <a href="<?php echo base_url('invoice/invoicectrl/invoice_master?action=view');?>" type="button" class="btn btn-danger" id="cnl_btn" tabindex="11">Cancel</a>
                                    </div>
                                </div>
                                <div class="form-group floating-form">
                                    
                                </div>
                            </div>
                            <div class="box box-primary">
                                <div class="box-header with-border"><!-- /.box-header -->
                                    <h3 class="box-title">Add Product</h3>
                                    <div class="box-tools pull-right">
                                        <button type="button" class="btn btn-box-tool" data-widget="collapse">
                                            <i class="fa fa-minus"></i>
                                        </button>
                                    </div>
                                </div><!-- /.box-header -->
                                <div class="box-body">
                                    <table class="table">
                                    <br>
                                        <tr class="floating-form">
                                            <!-- diamond code -->
                                            <td class="floating-label" width="6%">
                                                <?php echo form_dropdown('', $product,'','id="pro_id" class="form-control flexselect floating-select" onchange="get_product_data()" placeholder=" " autocomplete="off" tabindex="6"');?>
                                                <label>Product</label>
                                            </td>

                                            <td class="floating-label" width="5%">
                                                <input type="text" class="form-control floating-input" id="pro_hsn_code" placeholder=" " autocomplete="off" readonly/>
                                                <label>HSN Code</label>
                                            </td>

                                            <td class="floating-label" width="5%">
                                                <input type="text" class="form-control floating-input" id="pro_rate" onkeyup="calculate_sub_total_amt()" placeholder=" " autocomplete="off"/>
                                                <label>Rate</label>
                                            </td>

                                            <td class="floating-label" width="5%">
                                                <input type="text" class="form-control floating-input" id="pro_qty" onkeyup="calculate_sub_total_amt()" placeholder=" " autocomplete="off"/>
                                                <label>Qty</label>
                                            </td>

                                            <td class="floating-label" width="5%">
                                                <input type="text" class="form-control floating-input" id="pro_cgst_total" placeholder=" " autocomplete="off" readonly/>
                                                 <input type="text" class="form-control floating-input" id="pro_cgst" placeholder=" " autocomplete="off" readonly/>
                                                <label>CGST</label>
                                            </td>

                                            <td class="floating-label" width="5%">
                                                <input type="text" class="form-control floating-input" id="pro_sgst_total" placeholder=" " autocomplete="off" readonly/>
                                                 <input type="text" class="form-control floating-input" id="pro_sgst" placeholder=" " autocomplete="off" readonly/>
                                                <label>SGST</label>
                                            </td>

                                            <td class="floating-label" width="5%">
                                                <input type="text" class="form-control floating-input" id="pro_igst_total" autocomplete="off" placeholder=" "  readonly/>
                                                <input type="text" class="form-control floating-input" id="pro_igst" autocomplete="off" placeholder=" "  readonly/>
                                                <label>IGST</label>
                                            </td>

                                            <td class="floating-label" width="5%">
                                                <input type="hidden" class="form-control floating-input" id="pro_sub_total" placeholder=" " autocomplete="off" readonly/>
                                                <input type="text" class="form-control floating-input" id="pro_final_total" placeholder=" " autocomplete="off" readonly/>
                                                <label>TOTAL</label>
                                            </td>
                                         
                                            <!-- add trans -->
                                            <td style="text-align: center;" width="5%">
                                                <button  type="button" class="btn btn-success" onclick="<?php echo empty($inv_data) ? 'add_invoice_trans_row(0)' : 'add_invoice_trans_row('.$inv_data[0]['inv_id'].')' ; ?>"  style="padding-top: 5px;padding-bottom: 5px;width: 90%" tabindex="9">Add
                                                </button>
                                            </td>
                                            <!-- add trans -->
                                        </tr>
                                    </table>
                                </div>
                            </div><!-- /.box -->
                            <div class="box box-primary">
                            <div class="box-header with-border">
                                <h3 class="box-title">Product Details</h3>
                                <div class="box-tools pull-right">
                                    <button type="button" class="btn btn-box-tool" data-widget="collapse">
                                        <i class="fa fa-minus"></i></button>
                                </div>
                            </div><!-- /.box-header -->
                            <div class="box-body">
                                <table class="table" id="invoice_wrapper">
                                    <tr>
                                        <th class="floating-label" width="5%">Product</th>
                                        <th class="floating-label" width="5%">HSN Code</th>
                                        <th class="floating-label" width="5%">Rate</th>
                                        <th class="floating-label" width="5%">Qty</th>
                                        <th class="floating-label" width="5%">CGST</th>
                                        <th class="floating-label" width="5%">SGST</th>
                                        <th class="floating-label" width="5%">IGST</th>
                                        <th class="floating-label" width="5%">Total</th>
                                        
                                        <!-- remove trans -->
                                        <th style="text-align: center;" width="5%">REMOVE</th>
                                        <!-- remove trans -->
                                    </tr>
                                    <?php $inv_cnt = 1;
                                    if(!empty($inv_trans)): 
                                        foreach ($inv_trans as $key => $value): ?>
                                    <tr id="rowid_<?php echo $inv_cnt; ?>">
                                        <td>
                                            <input type="hidden" name="inv_id[]" id="inv_id_<?php echo $inv_cnt; ?>" class="form-control floating-input" value="<?php echo $value['invt_id'] ?>"/>
                                            <input type="hidden" name="pro_id[]" id="pro_id_<?php echo $inv_cnt; ?>" class="form-control floating-input" value="<?php echo $value['invt_pro_id'] ?>"/>
                                            <input id="pro_name_<?php echo $inv_cnt; ?>" class="form-control floating-input" value="<?php echo $value['pro_name'] ?>" readonly/>
                                        </td>
                                        <td>
                                            <input name="hsn_code[]" id="hsn_code_<?php echo $inv_cnt; ?>" class="form-control floating-input" value="<?php echo $value['pro_hsn_code'] ?>" readonly/>
                                        </td>
                                        <td>
                                            <input name="inv_rate[]" id="inv_rate_<?php echo $inv_cnt; ?>" class="form-control floating-input" value="<?php echo $value['invt_rate'] ?>" readonly/>
                                        </td>
                                        <td>
                                            <input name="inv_qty[]" id="inv_qty_<?php echo $inv_cnt; ?>" class="form-control floating-input" value="<?php echo $value['invt_qty'] ?>" readonly/>
                                        </td>
                                        <td>
                                            <input name="inv_cgst_total[]" id="inv_cgst_total_<?php echo $inv_cnt; ?>" class="form-control floating-input" value="<?php echo $value['invt_amt_cgst'] ?>" readonly/>
                                            <input name="inv_cgst[]" id="inv_cgst_<?php echo $inv_cnt; ?>" class="form-control floating-input" value="<?php echo $value['invt_per_cgst'] ?>" readonly/>
                                        </td>
                                        <td>
                                            <input name="inv_sgst_total[]" id="inv_sgst_total_<?php echo $inv_cnt; ?>" class="form-control floating-input" value="<?php echo $value['invt_amt_sgst'] ?>" readonly/>
                                            <input name="inv_sgst[]" id="inv_sgst_<?php echo $inv_cnt; ?>" class="form-control floating-input" value="<?php echo $value['invt_per_sgst'] ?>" readonly/>
                                        </td>
                                        <td>
                                            <input name="inv_igst_total[]" id="inv_igst_total_<?php echo $inv_cnt; ?>" class="form-control floating-input" value="<?php echo $value['invt_amt_igst'] ?>" readonly/>
                                            <input name="inv_igst[]" id="inv_igst_<?php echo $inv_cnt; ?>" class="form-control floating-input" value="<?php echo $value['invt_per_igst'] ?>" readonly/>
                                        </td>
                                        <td>
                                            <input type="hidden" name="inv_sub_total[]" id="inv_sub_total_<?php echo $inv_cnt; ?>" class="form-control floating-input" value="<?php echo $value['invt_sub_total_amt'] ?>" readonly/>
                                            <input name="inv_total[]" id="inv_total_<?php echo $inv_cnt; ?>" class="form-control floating-input" value="<?php echo $value['invt_total_amt'] ?>" readonly/>
                                        </td>
                                        <td style="text-align:center;">
                                            <a href="javascript:void(0)" onclick="remove_invoice_row(<?php echo $inv_cnt; ?>)"><i class="glyphicon glyphicon-trash" style="color: #333;padding-top: 9px;"></i></a>
                                        </td>
                                    </tr>
                                    <?php $inv_cnt++; endforeach; endif; ?>
                                   
                                </table>
                            </div><!-- /.box -->
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </section>
    <!-- /.content -->


<?php
    $this->load->view('templates/footer');
?>
<script src="<?php echo assets('js/invoice.js')?>"></script>
<?php
    if(!empty($inv_data))
    {
        echo "<script>";
        echo "inv_cnt = $inv_cnt;";
        echo "</script>";
    }
?>
</body>
</html>