<script type="text/javascript">
    <?= 'var cID='.$cID.';'?>
    <?= 'var base_customers='.json_encode($base_customers).';' ?>
    <?= 'var USE_PRODUCT_CATEGORY='.$use_product_category.';'?>
    <?= 'var productData='.json_encode($productData).';'?>
    <?= 'var categoryData='.json_encode($categoryData).';'?>
    <?= 'var customers='.json_encode($customers).';'?>
    const all_truk = <?= json_encode($all_truk) ?>;
    const all_additional_expense = <?= json_encode($all_additional_expense) ?>;
    const all_pay_by = <?= json_encode($pay_by) ?>;
    function get_customer_due_day(customer_id){  
        $('#due_day').val(parse_int(customers[customer_id].due_day))
    }
    $(document).on('change','#cID',function(){get_customer_due_day(this.value)});

    $(document).on('change','#base_id',function(){base_wise_customer(this.value,'cID')});
    $(document).ready(function(){
        base_wise_customer(<?=$base_id?>,'cID',<?=$cID?>);
    <?php 
        if($cID>0){
        ?>
            sale_customer_change(<?=$cID?>);
        <?php 
        }
    ?>
});
</script>
<div class="col-sm-12">
    <div class="white-box border-box">
        <div><?php show_msg();?></div>
        <div class="row">
            <div class="col-md-12">

                <input type="hidden" id="draftID" value="<?php echo $draftID;?>">

            </div>
        </div>
        <hr>
        <div class="row">
            <div class="col-xs-4 col-md-4 col-sm-4">
                <?php
                $general->inputBoxSelect($base,'Base','base_id','id','title',$base_id);
                $general->inputBoxSelect($pay_by, 'Truck rent pay by', 'truk_rent_pay_by', 'id', 'name');
                ?>
            </div>
            <div class="col-xs-4 col-md-4 col-sm-4">
                <div id="customer_input_area"><?php $general->inputBoxSelect([],'Customer','cID','id','name',$cID);?></div>
                <?php $general->inputBoxText('due_day','Due day',$due_day);?>
            </div>

            <div class="col-xs-4 col-md-4 col-sm-4">
                <?php $general->inputBoxText('saleDate','Date',$purDate,'','daterangepicker');?>
                <?php $general->inputBoxSelect([['id'=>PAY_TYPE_CREDIT,'title'=>'Credit'],['id'=>PAY_TYPE_CASH,'title'=>'Cash']],'Pay type','pay-type','id','title',$pay_type);?>
            </div>
            <div class="col-xs-4 col-md-4 col-sm-4">

            </div>
        </div>
        <hr>
        <div class="row">
            <div class="col-md-6">
                <?php
                    $general->inputBoxSelect($all_truk, 'Truk', 'truk_id', 'id', 'title');
                ?>
                <?php
                    $general->inputBoxText('truk_rent', 'Rent');
                ?>
                <button onclick="operation_truk_add_to_cart()" class="btn btn-info">Add Truk</button>
            </div>
            <div class="col-md-6">
                <table class="table table-border">
                    <thead>
                        <tr>
                            <th>#</th>
                            <th>Truk</th>
                            <th class="amount_td">Rent</th>
                            <th class="amount_td">Remove</th>
                        </tr>
                    </thead>
                    <tbody id="truk_list"></tbody>
                    <tfoot>
                        <tr>
                            <td colspan="2" class="amount_td"><b>Total</b></td>
                            <td class="amount_td"><b id="truk_total">0.00</b></td>
                            <td>&nbsp;</td>
                        </tr>
                    </tfoot>
                </table>
            </div>
        </div>
        <hr>
        <div class="row">
            <div class="col-md-3">
                <?php
                $general->inputBoxSelect($all_truk, 'Truk', 'ade_truk_id', 'id', 'title');
                ?>
            </div>
            <div class="col-md-2">
                <?php
                $general->inputBoxSelect($all_additional_expense, 'Expense', 'ade_id', 'id', 'title');
                ?>
            </div>
            <div class="col-md-2">
                <?php
                $general->inputBoxSelect($pay_by, 'Pay by', 'ade_pay_by', 'id', 'name');
                ?>
            </div>
            <div class="col-md-2">
                <?php
                $general->inputBoxText('ade_amount', 'Amount');
                ?>
            </div>
            <div class="col-md-3">
                <button onclick="operation_adc_add_to_cart()" class="btn btn-info">Add Additional cost</button>
            </div>
        </div>
        <div class="row">
            <div class="col-md-12">
                <table class="table table-border">
                    <thead>
                        <tr>
                            <th>#</th>
                            <th>Truk</th>
                            <th>Expense</th>
                            <th>Pay by</th>
                            <th class="amount_td">Amount</th>
                            <th class="amount_td">Remove</th>
                        </tr>
                    </thead>
                    <tbody id="ade_list">

                    </tbody>
                    <tfoot></tfoot>
                    <tr>
                        <td colspan="4" class="amount_td"><b>Total</b></td>
                        <td class="amount_td"><b id="ade_total">0.00</b></td>
                        <td>&nbsp;</td>
                    </tr>
                </table>
            </div>
        </div>

        <hr>
        <div class="row">
            <div class="col-xs-6 col-md-3 col-sm-6">
                
                <?php $general->inputBoxSelect($productData,'Product','pID','id','t',script:'onchange="saleProductChange(this.value)"');?>
            </div> 
            <div class="col-xs-6 col-md-3 col-sm-6">
                <div class="form-group row">
                    <label for="qty" class="col-md-4 col-form-label">Quantity</label>
                    <div class="col-md-8">
                        <div class="input-group qty-input-group">
                            <input class="form-control amount_td" value="" placeholder="Quantity" id="product_quantity" type="text">
                            <div class="input-group-append">
                                <span class="input-group-text" id="quantity_label">--</span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="col-xs-6 col-md-3 col-sm-6">
                <?php $general->inputBoxText('product_sale_price','TP','','','amount_td');?>
            </div>
            <div class="col-xs-6 col-md-3 col-sm-6">
            <?php $general->inputBoxText('total','Total','','','amount_td','readonly');?>
                <div class="col-sm-12">
                    <button onclick="productSaleAddToCart()" class="p-2 m-2 btn btn-info waves-effect waves-light pull-right m-t-10">Add to cart</button>
                </div>
            </div>
        </div>
        <div class="row">
            <div style="display: none;">
                <table>
                    <tbody>
                        <tr id="saleProductsTr">
                            <td class="autoSerial"></td>
                            <td>
                                <input type="hidden" class="pID" value="">
                                <span class="pTitle"></span>
                            </td>
                            <td class="unTitle"></td>
                            <td class="salePrice amount_td"></td>
                            <td class="qty amount_td"></td>
                            <td class="total amount_td"></td>
                            <td class="amount_td"><button class="btn btn-danger remove">X</button></td>
                        </tr>
                        <tr id="truk_tr">
                            <td class="autoSerial"></td>
                            <td><input type="hidden" class="truk_id" value=""><span class="truk_title"></span></td>
                            <td class="truk_rent amount_td"></td>
                            <td class="amount_td"><button class="btn btn-danger remove">X</button></td>
                        </tr>
                        <tr id="adc_tr">
                            <td class="autoSerial"></td>
                            <td><input type="hidden" class="truk_id" value=""><span class="truk_title"></span></td>
                            <td><input type="hidden" class="ade_id" value=""><span class="ade_title"></span></td>
                            <td><input type="hidden" class="pay_by" value=""><span class="pay_title"></span></td>
                            <td class="amount amount_td"></td>
                            <td class="amount_td"><button class="btn btn-danger remove">X</button></td>
                        </tr>
                    </tbody>
                </table>
            </div>
            <div class="col-md-12" style="overflow:auto">
                <table class="table table-border">
                    <thead>
                    <tr>
                        <th  style="width: 2%;">#</th>
                        <th>Product</th>
                        <th >Unit</th>
                        <th  class="amount_td">Sale Price</th>
                        <th class="amount_td">Qty</th>
                        <th style="width: 10%;" class="amount_td">Total</th>
                        <th style="width: 3%;" class="amount_td">X</th>
                    </tr>
                    <thead>
                    <tbody id="saleProducts">
                        <?php
                            $serial=1;
                            $subTotal = 0;
                            $total_product_discount = 0;
                            if(!empty($product)){
                                foreach($product as $pID=>$pr){
                                    $p=$productData[$pID];
                                    $id='pd_'.$pID.'_'.$serial;
                                    if($use_product_category==1){
                                        $c = $categories[$p['pc']];
                                    }
                                    $subTotal+= (float)($p['s']*$pr['qty']);
                                ?>
                                <tr id="<?php echo $id?>">
                                    <td style="width: 5%;"><?php echo $serial++?></td>
                                    <td>
                                        <input type="hidden" class="pID" value="<?php echo $pID?>">
                                        <span class="pTitle"><?php echo $p['t']?></span>
                                    </td>
                                    <td class="unTitle"><?php echo $p['u']?></td>
                                    <td class="salePrice-tp amount_td"><?=floatval($p['s'])?></td>
                                    <td class="salePrice amount_td"><?=floatval(@$pr['salePrice'])?></td>
                                    <td class="qty amount_td"><?=intval(@$pr['qty'])?></td>
                                    <td class="free_qty amount_td"><?=intval(@ $pr['free_qty'])?></td>
                                    <td class="total_qty amount_td"><?=intval(@$pr['total_qty'])?></td>
                                    <td class="sub_total amount_td"><?=(floatval(@$p['s'])*intval(@$pr['qty']))?></td>
                                    <td class="discount amount_td"><?=floatval(@$pr['discount'])?></td>
                                    <td class="VAT amount_td"><?=floatval(@$pr['VAT'])?></td>
                                    <td class="total amount_td"><?=(floatval(@$p['s'])*intval(@$pr['qty']))-floatval(@$pr['discount'])?></td>
                                    <td class="amount_td"><button class="btn btn-danger remove" onclick="remove_row_by_id('<?php echo $id?>');productSaleSubTotal()">X</button></td>
                                </tr>
                                <?php
                                }
                            }
                            $netPayble = $subTotal;
                            if(!empty($discount)){
                                $netPayble-=$discount;
                            }
                        ?>
                    </tbody>
                    <tfoot>
                        <tr>
                            <td colspan="5" class="amount_td"><b>Total</b></td>    
                            <td class="amount_td"><b id="subTotal"><?=$subTotal?></b></td>
                            <td>&nbsp;</td>
                        </tr>
                        <tr>
                            <td colspan="5" class="amount_td"><b>Discount</b></td>
                            <td class="amount_td"><input type="text" class="form-control amount_td" value="<?=$discount?>" id="saleDiscount"></td>
                            <td>&nbsp;</td>
                        </tr>
                        <tr>
                            <td colspan="5" class="amount_td"><b>Net Payable</b></td>
                            <td class="amount_td"><b id="netPayable"><?=$netPayble?></b></td>
                            <td>&nbsp;</td>
                        </tr>
                        <tr>
                            <td colspan="5" class="amount_td"><b>Note</b></td>
                            <td class="amount_td"><textarea type="text" class="form-control" value="" id="note"><?=$note?></textarea></td>
                            <td>&nbsp;</td>
                        </tr>


                        <tr>
                            <td colspan="6" class="amount_td">

                                <button  onclick="productsSale()" class="mt-1 btn-lg btn-success pull-right productsSale">Sale</button>

                            </td>

                        </tr>
                        <tr>
                            <td colspan="6" id="sale-print-btn" class="amount_td">

                            </td>

                        </tr>
                    </tfoot>
                </table>
            </div>
        </div>
    </div>
</div>
<script type="">
    $(document).on('keyup','#saleDiscount',function(){
        productSaleSubTotal();
    });
</script>