<script>

    $(document).ready(function(e) {

    var itr = document.getElementById("itr").value;
            for (var p = 1; p <= itr; p++)
    {
    var productid = document.getElementById("productid" + p).value;
            var totalproduct = document.getElementById("totalproduct" + p).value;
            for (var d = 1; d <= totalproduct; d++)
    {


    // appendserial(productid,d,p);

    }
    }
    });
            function appendserial(productid, d, p)
            {

            // alert('buyBillControllerAjax.php?do=getserail&productid='+productid);
            $.ajax({
            url: 'buyBillControllerAjax.php?do=getserail&productid=' + productid,
                    type: "get",
                    success: function(data) {
                    //alert('p'+p);
                    alert("data" + data);
                            alert("serialnumber" + d + p);
                            document.getElementById("serialnumber" + d + p).value = data;
                    }


            });
            }
    function gettotal(i)
    {

    var totalproduct = Number(document.getElementById('totalproduct' + i + '').value);
            // alert(totalproduct);
            var mytotal = 0;
            for (var x = 1; x <= totalproduct; x++)
    {
    // alert('quantity'+x+i+'');
    //alert (document.getElementById('quantity'+x+i+'').value);
    var quantity = Number(document.getElementById('quantity' + x + i + '').value);
            // alert('tt'+tt);
            mytotal = mytotal + quantity;
            // alert('mytotal'+mytotal);
            if (mytotal > totalproduct)
    {
    //alert('in if')
    alert('من فضلك  عدل اعددا المنتجات لتصل الى ' + totalproduct);
            document.getElementById("subbtn").style.display = "none";
            break;
    }
    else
    {
    //alert('in else')
    document.getElementById("subbtn").style.display = "block";
    }
    }

    }

    function deleteitr(id)
    {

    // delete by parents
    $("#serialnumber" + id).parent().parent().remove();
            return false;
    }
</script>
<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="buyAndReturnBillController.php?do=saveserail" method="post" id="billform">

            <!-- =========================== START BUY BILL SETTINGS ================================== -->
            <input type="hidden" name="billnameid" id="billnameid" value="{$billnameid}"/>
            <input type="hidden" name="returnBillSetting" id="returnBillSetting" value="{$billsettingsData[0]->billsettingsvalue}"/>
            <input type="hidden" name="storeSetting" id="storeSetting" value="{$billsettingsData[2]->billsettingsvalue}"/>
            <input type="hidden" name="parcodeSetting" id="parcodeSetting" value="{$billsettingsData[4]->billsettingsvalue}"/>
            <input type="hidden" name="dateSetting" id="dateSetting" value="{$billsettingsData[5]->billsettingsvalue}"/>
            <input type="hidden" name="newProductSetting" id="newProductSetting" value="{$billsettingsData[6]->billsettingsvalue}"/>
            <input type="hidden" name="noteSetting" id="noteSetting" value="{$billsettingsData[7]->billsettingsvalue}"/>

            <!-- ==========================  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} style="display:block" {else} style="display:none" {/if}>
                        <label class="col-lg-4 control-label">رقم  الفاتورة</label>
                        <div class="col-lg-4">
                            {$buyBillData->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">
                            {$buyBillData->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">   {$buyBillData->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>
                            {$buyBillData->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">
                            {$buyBillData->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>


                        </thead>

                        </thead>
                        <tbody>
                            {assign var=x value=1}
                            {foreach $buyBillDetails as $detail}
                            <tr class="aw_td" id="newProduct1" style="background:#A3B0B1" >

                                <td colspan="5">
                                    <div id="productdiv1" class="aw_width aw_mt5">



                                        اسم المنتج: {$detail->productName} / {$detail->productCatName} ------------العدد:{$totalproduct{$x}}



                                    </div>
                                </td>


                            </tr>
                            <tr>
                                <th class="color-white">السيريال</th>
                                <th class="color-white">الكميه</th>
                                <th class="color-white">التعليق</th>
                                <th class="color-white">تاريخ البدايه</th>
                                <th class="color-white">تاريخ النهايه</th>
                            </tr>

                        <input name="productid{$x}" id="productid{$x}" type="hidden" value="{$detail->buybilldetailproductid}">
                        <input name="buybilldetailid{$x}" type="hidden" value="{$detail->buybilldetailid}">
                        <input name="totalproduct{$x}"  id="totalproduct{$x}" type="hidden" value="{$totalproduct{$x}}">

                        <tr>
                            <td  colspan="5">
                                <table width="100%" class='table-black'>
                                    {if  empty($allseraildata{$x})}
                                    {assign var=m value=1}
                                    {for $foo=1 to $totalproduct{$x}}

                                    <tr>
                                        <td><input name="serialnumber{$foo}{$x}" id="serialnumber{$foo}{$x}" value="{$returnedSerial{$x}{$m}}"  type="text"></td>
                                        <td><input name="quantity{$foo}{$x}" id="quantity{$foo}{$x}"  type="text" onkeyup="gettotal({$x});"></td>
                                        <td><textarea name="comment{$foo}{$x}"></textarea></td>
                                        <td><input name="stratdate{$foo}{$x}" type="text" class="stratdate"></td>
                                        <td><input name="enddate{$foo}{$x}"  type="text" class="enddate"></td>
                                    </tr>
                                    {capture assign=m}{$m+1}{/capture}
                                    {/for}
                                    {else}
                                    {assign var=z value=1}
                                    {foreach $allseraildata{$x} as $myallseraildata{$x}}

                                    <tr>
                                        <td><input name="serialnumber{$z}{$x}" id="serialnumber{$z}{$x}"  value="{$myallseraildata{$x}->serialnumber}"  type="text"  {if {$countmyusedserial} > 0}
                                            readonly="readonly" {/if}></td>
                                        <td><input name="quantity{$z}{$x}" id="quantity{$z}{$x}"
                                                   {if {$countmyusedserial} > 0}
                                            readonly="readonly" {/if} value="{$myallseraildata{$x}->don}" type="text" onkeyup="gettotal({$x});"></td>
                                        <td><textarea name="comment{$z}{$x}">{$myallseraildata{$x}->comment}</textarea></td>
                                        <td><input name="stratdate{$z}{$x}" value="{$myallseraildata{$x}->startdate}"  type="text" class="stratdate"></td>
                                        <td><input name="enddate{$z}{$x}"  value="{$myallseraildata{$x}->enddate}"  type="text" class="enddate"></td>
                                        <td> {if {$countmyusedserial} > 0}
                                            لا يمكن حذفه
                                            {else}
                                            <input type="button" onclick="deleteitr({$z}{$x})" value="حذف" />
                                            {/if}
                                        </td>
                                    </tr>
                                    {capture assign=z}{$z+1}{/capture}
                                    {/foreach}
                                    {/if}
                                    <input name="totalproduct{$x}" value="{$totalproduct{$x}}" type="hidden">

                                </table>
                            </td>
                        </tr>
                        {capture assign=x}{$x+1}{/capture}
                        {/foreach}



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





            <div class="row">
                <button type="submit"  id="subbtn" class="btn btn-primary">حفظ</button>
            </div><!-- end of fifth row-->

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


    </div>
</div>