{literal}

<script>
jQuery(document).ready(function () {

    $(".autosave").live("change", function (e) {
        var name = $(this).attr('data-name');
        var detailid = $(this).attr('data-detailid');// e.val;
        var values = $(this).val();
        var id = $(this).attr('data-id');// e.val;
        $.post('buyBillControllerAjax.php?do=autosave', {id: id, detailid: detailid, values: values, name: name});
    });

    $(".texts2").live("change", function (e) {
        var detailid = $(this).attr('data-detailid');// e.val;
        var values = $(this).val();
        var id = $(this).attr('data-id');
        var name = $(this).attr('data-name');
        var quantity = $("#detailquantity"+detailid ).val();
        var allquantity = 0;

        $('.texts2'+detailid).each(function (e) {
            allquantity += Number($(this).val());
            
        });
        if (quantity < allquantity) {
           $(this).val(0);
        }else{
            $.post('buyBillControllerAjax.php?do=autosave', {id: id, detailid: detailid, values: values, name: name});
          $("#stayquantity"+detailid).text(quantity - allquantity);  
        }
       
    });

    $(".addpl").live("click", function (e) {
        var detailid = $(this).attr('data-detailid');// e.val;
        $.post('buyBillControllerAjax.php?do=addplnew', {detailid: detailid}, function (data) {
           $('#buybilldetailid'+detailid).after(data);            
         });

    });

    $(".removepl").live("click", function (e) {
        var detailid = $(this).attr('data-detailid');
        var id = $(this).attr('data-id');// e.val;
        $.post('buyBillControllerAjax.php?do=removepl', {id: id, detailid: detailid}, function (data) {
            $('#rpl'+id).remove();  
            var quantity = $("#detailquantity"+detailid).val();
            var allquantity = 0;
            $('.texts2'+detailid).each(function (e) {
                allquantity += Number($(this).val());
            });
            $("#stayquantity"+detailid).text(quantity - allquantity);  

            

        });

    });
});
</script>
{/literal}

<div class="row">
    <div class="container">
        <ol class="breadcrumb">
            <li><a href="#">إدارة الفواتير</a></li>
            <li><a href="#">فاتورة الشراء</a></li>
            <li class="active">عرض فاتورة</li>
        </ol>
        <h3>فاتورة المشتريات</h3>
        <hr/>
        <form class="form-horizontal" role="form" action="buyBillController.php?do=saveserail" method="post" id="billform">

            <!-- =========================== START BUY BILL SETTINGS ================================== -->
       

            <!-- ==========================  END BUY BILL SETTINGS ============================ -->

            <!-- =========================== START GENERAL INFO PANEL =====================-->
            <div class="row">
                <div class="col-lg-8">
                    <div class="form-group" {if {$billsettingsData[61]->billsettingsvalue} eq 0}  {else} style="display:none !important" {/if}>
                        <label class="col-lg-4 control-label">رقم  الفاتورة</label>
                        <div class="col-lg-4">
                            {$buybill.buybillSerial}
                        </div>
                    </div>
                    <!-- end of form-group-->

                    <div class="form-group"  {if $billsettingsData[5]->billsettingsvalue eq 0} style="display:block !important;" {else} style="display:none !important;" {/if}>
                        <label class="col-lg-4 control-label">تاريخ الفاتورة</label>
                        <div class="col-lg-4">
                            {$buybill.buybilldate}
                        </div>
                    </div>
                    <!-- end of form-group-->
                </div>
                <!-- end of bill no-->
                <div class="col-lg-4">
                    <div class="form-group mg-bt5">
                        <label class="col-lg-4 control-label">اسم المورد</label>
                        <div class="col-lg-8">
                            <div id="supplierIdDiv">   {$supplier.suppliername}
                                <input type="hidden" name="buybillid" id="buybillid" value="{$buyBillData->buybillid}"/> </div>
                        </div>
                    </div>
                    <!-- end of form-group-->
                    <div class="form-group mg-bt5" >
                        <label class="col-lg-4 control-label">رصيد الدين</label>
                        <div class="col-lg-8">
                            <div id="debt" class="form-control-static"> </div>
                            {$buybill.buybilltotaldeptbefor}</div>
                    </div>
                    <!-- end of form-group-->
                    <hr class="mg-bt5 mg-tp5"/>
                    <div class="form-group mg-bt5"   {if $billsettingsData[2]->billsettingsvalue eq 0} style="display:block !important;" {else} style="display:none !important;" {/if}>
                        <label class="col-lg-4 control-label">المخزن</label>
                        <div class="col-lg-8">
                            {$store.storeName}
                        </div>
                    </div>
                    <!-- end of form-group-->

                    <!-- end of form-group-->
                </div>
                <!-- end of client info-->

            </div>
            <!-- =========================== END GENERAL INFO PANEL ====================== -->
            <input name="countmyusedserial"  value="{$countmyusedserial}" type="hidden" />
            <!-- =========================== START BUY BILL PANEL ======================= -->
            <div class="row">
                <div class="row">
                    <hr/>
                    <div class="col-lg-6">
                        <h4>المشتريات</h4>
                    </div>
                    <div class="col-lg-6">
                    </div>
                </div>
                <!-- end of title row-->
                <div class="table-responsive">
                    <table class="table billtable table-bordered table-black" id="containerpros">
                        <thead>


                          
                             <tr>
                                <th>  اسم المنتج: </th>
                                <th>الوحده</th>
                                <th>الكميه</th>
                                <th>الوزن</th>
                                <th>المنشأ</th>
                                <th>رمز النطام</th>
                                <th>المتبفى </th>
                                <th class="hidden-print">اضافه </th>
                             </tr>


                        </thead>
                        <tbody>
                            {assign var=x value=1}
                            {foreach $buybilldetails as $detail}
                        
                            <tr>
                                <td>{$detail.productName}</td>
                                <td>{$detail.unitName}</td>
                                <td>{$detail.buybilldetailquantity}
                                    <input id="detailquantity{$detail.buybilldetailid}" type="hidden" value="{$detail.buybilldetailquantity}"></td>
                                <td>{$detail.weight}</td>
                                <td>{$detail.origin}</td>
                                <td>{$detail.hscode}</td>
                                <td id="stayquantity{$detail.buybilldetailid}">{$detail.buybilldetailquantity - $detail.stayquantity}</td>
                                <td class="hidden-print"><a  type="button"  class="btn btn-default btn-lg addpl " data-detailid="{$detail.buybilldetailid}"> + </a></td>



                            </tr>
                            </tbody>
                    </table>
                    <br><br>
                    <table class="table billtable table-bordered table-black">
                        <thead>
                            <tr>
                                <th colspan="2">العدد</th>
                                <th colspan="2">الصندوق</th>
                                <th colspan="2">ملاحظة</th>
                                <th colspan="2" class="hidden-print">حذف</th>
                            </tr>
                        </thead>
                        <tbody>
                            
                            
                             {assign var=x2 value=1}
                             {foreach $detail.buybilldetailpls as $pl}
                            <tr  style="background: #ccc" id="rpl{$pl.id}">
                                
                                <td colspan="2"><input data-name="text2" data-detailid="{$detail.buybilldetailid}" name="text2" class=" pl{$detail.buybilldetailid}  texts2 texts2{$detail.buybilldetailid}" data-id="{$pl.id}" type="text" value="{$pl.text2}"></td>
                                <td colspan="2"><input data-name="text1" data-detailid="{$detail.buybilldetailid}" name="text1" class="autosave pl{$detail.buybilldetailid}" data-id="{$pl.id}" type="text" value="{$pl.text1}"></td>
                                <td colspan="2"><input data-name="text3" data-detailid="{$detail.buybilldetailid}" name="text3" class="autosave pl{$detail.buybilldetailid}" data-id="{$pl.id}" type="text" value="{$pl.text3}"></td>
                                <td colspan="2" class="hidden-print"><a  type="button"  class="btn btn-default btn-lg removepl hidden-print" data-detailid="{$detail.buybilldetailid}" data-id="{$pl.id}"> x </a></td>
                            </tr>
                            
                            {capture assign=x2}{$x2+1}{/capture}
                            {/foreach}
                            <tr id="buybilldetailid{$detail.buybilldetailid}"></tr>
                            <tr class="hidden-print">
                                <td colspan="7" style="background: #000">.</td>
                                <td class="hidden-print" style="background: #000"></td>
                            </tr>    

                            


                        {capture assign=x}{$x+1}{/capture}
                        {/foreach}



                        <input name="itr" type="hidden" value="{$countBillDetails}">
                        </tbody>
                    </table>
                </div>
            </div>
            <!-- end of second row-->
            <!-- =========================== END BUY BILL PANEL ==================== -->

            <div  class="hidden-print">
                <div class="col-lg-12">
                    <div class="form-group">
                        <div class="col-lg-4">
                            <button type="button" onclick="print_Custom('mycustomheader');" class="btn btn-success pull-right">طباعة</button>
                        </div>
                    </div>
                </div>
                <div class="clearfix"></div>
            </div>



            <!-- <div class="row">
                <a href="buyBillControllerAjax.php?do=printaddpl&id={$buybill.buybillid}&print=1" type="button" class="btn btn-primary  " target="_blank">طباعه</a>
            </div> -->
            <!-- end of fifth row-->

            <!-- =========================== END TOTAL VALUESPANEL ====================== -->
        </form>


    </div>
</div>