<div class="container">
    <ol class="breadcrumb">
        <li><a href="#">إدارة الفواتير</a></li>
        <li><a href="#">فاتورة المشتريات</a></li>
        <li class="active">عرض الفواتير</li>
    </ol>
    <div class="row">
        <div class="panel panel-default panel-m">
            <!-- Default panel contents -->
            <div class="panel-heading" style="padding:5px 15px 15px">
                <h3 class="panel-title" style="display: inline-block;">فواتير المشتريات <br />
                    {if $message != ""}
                    {$message}
                    {/if}</h3>
                <a href="{$youtubes[22]->url}" target="_blank" class="hidden-print"><img src="../views/default/assets/img/youtube.png" class="pull-left" alt="Youtube" style="width: 2%;"></a
                <a href="buyBillController.php" class="hidden-print"><input type="button"  id="submit" style="margin-left: 12px" class="btn btn-primary pull-left " value="إضافة جديد"/></a>
            </div>

            <div class="panel-body">
                <input type="hidden" id="searchvalue" value="{$Programsettingdata->searchvalue}">
                <input type="hidden" name="withNakdi" id="withNakdi" value="{$usergroup->showClientAndSupplierOne}"/>
                <form class="form-inline hidden-print" action="buyBillController.php?do=show" method="post">
                    <div class="form-group">
                        <label class="sr-only">إسم المورد</label>
                        <!--                        <select id="supplier" name="supplier" data-placeholder="اختر المورد" class="select2 wdt100" onChange="this.form.submit();">
                                                    <option value="-1"></option>



                                                    {foreach $suppliersData as $supplier}



                                                    <option value="{$supplier->supplierid}">{$supplier->suppliername}</option>



                                                    {/foreach}



                                                </select>-->
                        <input type="hidden" name="supplier" id="supplier" class="select2Supp" onChange="this.form.submit();"/>
                        <input type="hidden" name="oldsupplierid" id="oldsupplierid" value=""/>
                        <input type="hidden" name="oldsuppliername" id="oldsuppliername" value=""/>
                    </div>
                    <div class="form-group">
                        <label class="sr-only"> المخزن</label>
                        <select id="store" name="store" class="select2 wdt100" onChange="this.form.submit();">
                            <option value="-1"></option>

                            {foreach $storesData as $store}

                            <option value="{$store->storeId}">{$store->storeName}</option>

                            {/foreach}

                        </select>
                    </div>
                    <div class="form-group">
                        <label class="sr-only">رقم تسلسل الفاتورة</label>
                        <select data-placeholder="اختر رقم التسلسل" class="select2 wdt100" name="buybillid"  onChange="this.form.submit();">
                            <option value="-1"></option>



                            {foreach $billSerials as $buybillid}



                            <option value="{$buybillid->buybillid}">{$buybillid->buybillid}</option>





                            {/foreach}



                        </select>
                    </div>
                    <div class="form-group" {if {$Programsettingdata->billid} eq 1}  style="display:none" {/if} >
                        <label class="sr-only">رقم الفاتورة</label>
                        <select data-placeholder="اختر رقم الفاتورة" class="select2 wdt100" name="serial" onChange="this.form.submit();">
                            <option value="-1"></option>




                            {foreach $billSerials as $serial}


                            <option value="{$serial->buybillSerial}">{$serial->buybillSerial}</option>




                            {/foreach}



                        </select>
                    </div>
                    <br/>
                    <div class="form-group">
                        <label class="sr-only">من التاريخ</label>
                        <input type="text" class="form-control awdatepicker" autocomplete="off"   data-date-format="yyyy-mm-dd" name="from" >
                    </div>
                    <div class="form-group">
                        <label class="sr-only">إلى التاريخ</label>
                        <input type="text" class="form-control awdatepicker" autocomplete="off"   data-date-format="yyyy-mm-dd" name="to" >
                    </div>
                    <div class="form-group">
                        <label class="sr-only">اخفاء المحذوف
                            <input type="checkbox" class="search_bill" name="hideDel" id="hideDel"  value="1"></label>
                    </div>
                    <button type="submit" class="btn btn-primary noprint">بحث</button>
                </form>
            </div>
            <hr/>
            <div class="col-lg-offset-8 col-lg-4 ">
                <div class="well well-sm">
                    <h4 class="text-center">إجمالي المشتريات:  {$totalBills}</h4>
                </div>
            </div>
            <div class="clearfix"></div>
            {literal}
            <form id="form1" name="form1" method="post" action="buyBillController.php?do=delete"onsubmit="if (Spry.Widget.Form.validate(this)) {
                        return countChoosenProducts('buyBillController');
                    } else {
                        return false;
                    }" enctype="multipart/form-data">
                {/literal}
                <!-- Table -->
                <table class="table table-black">
                    <thead>
                        <tr>
                            <th class="hidden-print">
                                <label>
                                    <input type="checkbox" name="checkAll" id="checkAll"/>
                                </label>
                            </th>
                            {if $Programsettingdata->clientserial eq 1}<th>رقم تسلسل الفاتورة</th>{/if}
                            <th>اسم المورد</th>
                            <th {if {$Programsettingdata->billid} eq 1}  style="display:none" {/if}>رقم الفاتورة</th>
                            {if $Programsettingdata->showparcode eq 1}<th>باركود  المنتجات</th>{/if}

                            {if $usergroup->showSerializeBtn eq 1}<th>إجمالي الفاتورة</th>{/if}
                            <th>التاريخ</th>
                            <th>المستخدم</th>
                            {if $usergroup->showSerializeBtn eq 1}<th class="hidden-print">تفاصيل الفاتورة</th>{/if}
                            {if $usergroup->allowEditDelInBuyBills eq 0}<th class="hidden-print">تعديل</th>{/if}
                            {if $usergroup->showSerializeBtn eq 0}<th class="hidden-print">تكويد المنتجات</th>{/if}
                            {if $Programsettingdata->importproduct eq 1}  <th class="hidden-print">pl</th>{/if}
                            {if $usergroup->showSerializeBtn eq 0}<th class="hidden-print">تكويد منتجات متعدد</th>{/if}
                            <th class="hidden-print">طباعه الباركود </th>
                            {if $usergroup->allowEditDelInBuyBills eq 0}<th class="hidden-print">حذف</th>{/if}
                        </tr>
                    </thead>
                    <tbody>

                        {foreach $buyBillData as $data}
                        {if {$data->conditions} eq 1}
                        <tr class="danger"> {elseif {$data->conditions} eq 0}
                        <tr> {/if}
                            <td class="hidden-print">
                                <label>
                                    <input type="checkbox" name="choosedItem[]" id="checked{$i}" class="choosenItem" value="{$data->buybillid}" />
                                </label>
                            </td>
                            {if $Programsettingdata->clientserial eq 1}<td> {$data->buybillid} </td>{/if}
                            <td> {$data->suppliername} </td>
                            <td {if {$Programsettingdata->billid} eq 1}  style="display:none" {/if}>{$data->buybillSerial}</td>
                            {if $Programsettingdata->showparcode eq 1}<td>{$data->parcodepro}</td>{/if}

                            {if $usergroup->showSerializeBtn eq 1}<td>{$data->buybillaftertotalbill}</td>{/if}
                            <td>{$data->buybilldate}</td>
                            <td>{$data->username} {if $data->delbyusername}<br/><span style="color: red">{$data->delbyusername}</span>{/if}</td>
                            {if $usergroup->showSerializeBtn eq 1}<td  class="hidden-print"><a href="buyBillController.php?do=details&id={$data->buybillid}" type="button" class="btn btn-default btn-lg notesicon"></a></td>{/if}
                            {if $usergroup->allowEditDelInBuyBills eq 0}<td  class="hidden-print">
                                {if {$data->conditions} eq 0}
                                <a href="buyBillController.php?do=editbill&id={$data->buybillid}" type="button" class="btn btn-default btn-lg notesicon"></a>
                                {else}
                                الفاتوره محذوفه
                                {/if}</td>{/if}
                            {if $usergroup->showSerializeBtn eq 0}<td  class="hidden-print"><a href="buyBillController.php?do=addoneserial&id={$data->buybillid}&type=2" type="button" class="btn btn-default btn-lg editicon"></a></td>{/if}

                          {if $Programsettingdata->importproduct eq 1}  <td  class="hidden-print"><a href="buyBillController.php?do=addpl&id={$data->buybillid}&type=2" type="button" class="btn btn-default btn-lg editicon"></a></td>{/if}


                            {if $usergroup->showSerializeBtn eq 0}<td  class="hidden-print"><a href="buyBillController.php?do=addserial&id={$data->buybillid}&type=2" type="button" class="btn btn-default btn-lg editicon"></a></td>{/if}
                            <td  class="hidden-print"><a href="productController.php?do=showbarcodebuybill&buybillid={$data->buybillid}&type=2" target="_new" type="button"  class="btn btn-default btn-lg editicon printparcode"> </a></td>

                            {if $usergroup->allowEditDelInBuyBills eq 0}<td  class="hidden-print"> {if {$data->conditions} eq 0} <a href="#" type="button" class="btn btn-default btn-lg deleteicon conformdel1" data-position="buyBillController.php?do=delete&id={$data->buybillid}"></a> {elseif {$data->conditions} eq 1 } تم حذف الفاتورة</td>{/if}
                            {/if} </tr>
                        {/foreach}
                    </tbody>

                </table>



                <!-- Modal -->
                <div class="hidden-print">
                    <div class="modal fade " id="myModal1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close pull-left" data-dismiss="modal" aria-hidden="true">&times;</button>
                                    <div class="modal-title">تأكيد حذف الفاتوره</div>
                                </div>
                                <div class="modal-body"> هل تريد حذف هذه الفاتوره؟ </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-default" data-dismiss="modal">لا</button>
                                    <button type="button" class="btn btn-primary savemodel1">نعم</button>
                                </div>
                            </div>
                            <!-- /.modal-content -->
                        </div>
                        <!-- /.modal-dialog -->
                    </div>
                </div>
                <!-- /.modal -->
                <div  class="hidden-print">
                    <div class="col-lg-12">
                        <div class="well well-sm">
                            <div class="form-group">
                                <div class="col-lg-4">
                                    <label for="operation"></label>
                                    <span id="spryselect2">
                                        <select name="operation" id="operation"  data-placeholder="اختر العملية" class="select2 wdt100">
                                            <option value="-1"></option>
                                            <option value="1">حذف</option>
                                        </select>
                                        <span class="selectInvalidMsg">اختر العملية</span></span></div>
                            </div>
                            <div class="form-group">
                                <div class="col-lg-4">
                                    <input name=""  class="noprint btn btn-primary" type="submit" value="تنفيذ"/>
                                </div>
                            </div>

                            <!-----------  start noElementsError alert ------------->
                            <div class="clearfix"></div>
                            <div id="noElementsError" class="alert alert-danger fade in" style="display:none;">
                                <button type="button" class="close pull-left" data-dismiss="alert" aria-hidden="true">×</button>
                                <p>يجب اختيار العناصر أولاً</p>
                                <div class="clearfix"></div>
                            </div>
                            <!----------  end noElementsError alert -------------->

                            <div class="clearfix"></div>
                        </div>
                    </div>
                    <div class="col-lg-offset-8 col-lg-4 ">
                        <div class="well well-sm">
                            <h4 class="text-center">إجمالي المشتريات:  {$totalBills}</h4>
                        </div>
                    </div>
                    <div class="clearfix"></div>
                </div>
            </form>
            <div class="clearfix"></div>
            <hr />
            <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="clearfix"></div>
        </div>
    </div>
</div>
{literal}
<script type="text/javascript">
    var spryselect2 = new Spry.Widget.ValidationSelect("spryselect2", {invalidValue: "-1", isRequired: false});
    $('.printparcode').mousedown(function (e) {
        e.preventDefault();
        var href = $(this).attr('href') + '&parcodeTypeToPrint=' + Number(localStorage.getItem("parcodeTypeToPrint"));
        window.open(href)
    });


    $('.conformdel1').click(function (e) {

        e.preventDefault();

        var elemposition = $(this).data('position');

        $("#myModal1").find(".savemodel1").attr('data-position', elemposition);
        $('#myModal1').modal('show');
    });
    $('.savemodel1').on('click', function () {

        $('#myModal1').modal('hide');
        var pos = $(this).attr('data-position');

        window.location.href = pos;
    });


    $(function () {
        $(".select2Supp").select2({
            placeholder: "المورد",
            minimumInputLength: document.getElementById("searchvalue").value,
            //inimumInputLength: 0,
            multiple: false,
            quietMillis: 100,
            ajax: {
                url: "supplierAjaxController.php?do=select2supplier",
                dataType: 'json',
                data: function (term, page) {
                    return {
                        term: term, //search term
                        page_limit: 7, // page size
                        withAddOption: 0,
                        withNakdi: function () {
                            return (Number($('#withNakdi').val()) == 1) ? 0 : 1;
                        },
                        //jsonRowRet: 1,
                    };
                },
                results: function (data) {
                    return {results: data};
                }
            },
            formatResult: function (data) {
                if (Number(data.id) == -2) {
                    return  '<span class="newoption">' + data.text + '</span >';
                } else {
                    return  '<span>' + data.text + '</span >';
                }
            },
            initSelection: function (element, callback) {
                var item = element.val().split(':');
                //  var data = {id: item[0], text: item[1]};
                var data = {id: item[0], text: item[1], productquantity: item[2], price: item[3]};
                callback(data);
            },
            //dropdownCssClass: "bigdrop", // apply css that makes the dropdown taller
        });
        //this is needed for ajax =>to obtain id from it  and use in ajax
        $('#supplier').select2('data', {id: $('#oldsupplierid').val(), text: $('#oldsuppliername').val()});
    });
</script>
{/literal}