<style>
    .wdthpre13{
        width: 13%;
    }
    .deserialize{
        display:none;
        opacity: unset;
        color:white!important;
    }
    .repeated{
        background-color: yellow;
    }
    .repeated2{
        background-color: rgb(204, 58, 58);
    }
</style>
<script>
    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);
    var quantitySold = 0;
    if ($('#quantitySold' + x + i).length > 0){
    quantitySold = Number($('#quantitySold' + x + i).val());
    }
    // alert('tt'+tt);
    mytotal = mytotal + quantity + quantitySold;
    // 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)
    {
    var rowid = $('#productserailid' + id).val();
    var delIds = $('#delIds').val();
    delIds += rowid + ',';
    $('#delIds').val(delIds);
    // 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="buyBillController.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}"/>
            <input type="hidden" name="searchvalue" id="searchvalue" value="0"/>

            <!-- ==========================  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">
                        <input type="button" id="deserializeBtn" class="btn btn-primary pull-left" value="عدم سريلة" style="display: none" onclick="$('.deserialize').show()">
                    </div>
                </div>
                <!-- end of title row-->
                <div class="table-responsive">
                    <table class="table billtable table-bordered table-black" id="containerpros">
                        <thead>


                            <!--tr class="aw_td" id="newProduct1" style="background:#A3B0B1" >

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



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



                                 </div>
                               </td>


                             </tr>
                             <tr>
                                <th>السيريال</th>
                                <th>التعليق</th>
                                <th>تاريخ البدايه</th>
                                <th>تاريخ النهايه</th>
                             </tr-->


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

                                <td colspan="{if $Programsettingdata->isCarBill == 1}8{else}5{/if}">
                                    <div id="productdiv1" class="aw_width aw_mt5">



                                        اسم المنتج: {$detail->productName} / {$detail->productCatName}
                                        {if $detail->sizeName != ''} / {$detail->sizeName}{/if}
                                        {if $detail->colorName != ''} / {$detail->colorName}{/if}
                                        ------------العدد:{$totalproduct{$x}}


                                        <button type="button" class="close pull-left deserialize removethis" data-proitr="{$x}">×</button>
                                    </div>

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


                            </tr>
                            <tr data-proitr="{$x}">
                                <th class="wdthpre13">السيريال</th>
                                <th class="wdthpre13">الكميه</th>
                                <th class="wdthpre13">التعليق</th>
                                <th class="wdthpre13">تاريخ البدايه</th>
                                <th class="wdthpre13">تاريخ النهايه</th>
                                {if $Programsettingdata->isCarBill == 1}
                                <th class="wdthpre13">رقم الشاسيه</th>
                                <th class="wdthpre13">رقم الموتور</th>
                                <th class="wdthpre13">اللون</th>
                                {/if}
                            </tr>



                            <tr data-proitr="{$x}">
                                <td  colspan="{if $Programsettingdata->isCarBill == 1}8{else}5{/if}">
                                    <table width="100%" class="table-black">
                                        {if  empty($allseraildata{$x})}
                                        {assign var=m value=1}
                                        {for $foo=1 to $totalproduct{$x}}<!--not serialized product-->

                                        <tr>
                                            <td><input name="serialnumber{$foo}{$x}" id="serialnumber{$foo}{$x}"  value="" class="myserial" data-allitr="{$allitr}" type="text"></td><!-- it is suggested serial for unserialized product  value="{$returnedSerial{$x}{$m}}"-->
                                            <td><input name="quantity{$foo}{$x}" id="quantity{$foo}{$x}"  type="text" onkeyup="gettotal({$x});" value="1"></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>
                                            {if $Programsettingdata->isCarBill == 1}
                                            <td><input name="chassisNo{$foo}{$x}"  type="text" class="chassisNo"></td>
                                            <td><input name="motorNo{$foo}{$x}"  type="text" class="motorNo"></td>
                                            <td><input name="theColor{$foo}{$x}"  type="text" class="theColor"></td>
                                            {/if}
                                        </tr>
                                        {capture assign=m}{$m+1}{/capture}
                                        {assign var=allitr value=$allitr+1}
                                        {/for}
                                        {else}
                                        {assign var=z value=1}
                                        {assign var=totaldonAndSold value=0}
                                        {foreach $allseraildata{$x} as $myallseraildata{$x}}<!--serialized product-->

                                        <tr>
                                            <td><input type="hidden" name="productserailid{$z}{$x}" id="productserailid{$z}{$x}" value="{$myallseraildata{$x}->productserailid}"/>
                                                <input type="hidden" name="quantitySold{$z}{$x}" id="quantitySold{$z}{$x}" value="{$myallseraildata{$x}->quantity}"/>
                                                <input name="serialnumber{$z}{$x}" id="serialnumber{$z}{$x}" {if $myallseraildata{$x}->quantity > 0}disabled{/if} value="{$myallseraildata{$x}->serialnumber}" class="myserial" data-allitr="{$allitr}" type="text"></td>
                                            <td><input name="quantity{$z}{$x}" id="quantity{$z}{$x}" {if $myallseraildata{$x}->quantity > 0}disabled{/if} value="{$myallseraildata{$x}->don}" type="text" onkeyup="gettotal({$x});"></td>
                                            <td><textarea name="comment{$z}{$x}"  {if $myallseraildata{$x}->quantity > 0}disabled{/if}>{$myallseraildata{$x}->comment}</textarea></td>
                                            <td><input name="stratdate{$z}{$x}" {if $myallseraildata{$x}->quantity > 0}disabled{/if} value="{$myallseraildata{$x}->startdate}"  type="text" class="stratdate"></td>
                                            <td><input name="enddate{$z}{$x}"  {if $myallseraildata{$x}->quantity > 0}disabled{/if} value="{$myallseraildata{$x}->enddate}"  type="text" class="enddate"></td>
                                            {if $Programsettingdata->isCarBill == 1}
                                            <td><input name="chassisNo{$z}{$x}" {if $myallseraildata{$x}->quantity > 0}disabled{/if}  value="{$myallseraildata{$x}->chassisNo}"  type="text" class="chassisNo"></td>
                                            <td><input name="motorNo{$z}{$x}" {if $myallseraildata{$x}->quantity > 0}disabled{/if} value="{$myallseraildata{$x}->motorNo}"  type="text" class="motorNo"></td>
                                            <td><input name="theColor{$z}{$x}"  {if $myallseraildata{$x}->quantity > 0}disabled{/if} value="{$myallseraildata{$x}->theColor}"  type="text" class="theColor"></td>
                                            {/if}
                                            <td>
                                                <!--{*if {$countmyusedserial} > 0*}-->
                                                {if $myallseraildata{$x}->quantity > 0}
                                                لا يمكن حذفه
                                                {else}
                                                <input type="button" onclick="deleteitr({$z}{$x})" value="حذف" />
                                                {/if}
                                            </td>
                                        </tr>
                                        {capture assign=z}{$z+1}{/capture}
                                        {assign var=totaldonAndSold value=$totaldonAndSold + $myallseraildata{$x}->don + $myallseraildata{$x}->quantity}
                                        {assign var=allitr value=$allitr+1}
                                        {/foreach}


                                        {assign var=totaldonAndSold value=$totaldonAndSold+1}<!--to correct for itration-->
                                        {for $foo=$totaldonAndSold to $totalproduct{$x}}<!--not serialized in serialized product "their serial was deleted"-->

                                        <tr>
                                            <td><input name="serialnumber{$foo}{$x}" id="serialnumber{$foo}{$x}"  value="" class="myserial" data-allitr="{$allitr}" type="text"></td><!-- it is suggested serial for unserialized product  value="{$returnedSerial{$x}{$m}}"-->
                                            <td><input name="quantity{$foo}{$x}" id="quantity{$foo}{$x}"  type="text" onkeyup="gettotal({$x});" value="1"></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>
                                            {if $Programsettingdata->isCarBill == 1}
                                            <td><input name="chassisNo{$foo}{$x}"  type="text" class="chassisNo"></td>
                                            <td><input name="motorNo{$foo}{$x}"  type="text" class="motorNo"></td>
                                            <td><input name="theColor{$foo}{$x}"  type="text" class="theColor"></td>
                                            {/if}
                                            <td></td>
                                        </tr>
                                        {capture assign=m}{$m+1}{/capture}
                                        {assign var=allitr value=$allitr+1}
                                        {/for}


                                        {/if}
                                        <input name="totalproduct{$x}" value="{$totalproduct{$x}}" type="hidden">

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

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




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

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

        <input type="hidden" id="addserialPage" value="1"/>
    </div>
</div>