<script>

    function checkserail() {
        var serail = document.getElementById("voucher").value;
        //alert(serail);
        //alert('clientPayedDeptControllerAjax.php.php?do=checkserail&serail='+serail);

        $.ajax({
            url: 'supplierPayedDeptControllerAjax.php?do=checkserail&serail=' + serail,
            success: function (x) {
                //alert(x);

                if (x == '1') {

                    alert('عفوا هذا الرقم موجود من قبل');
                    document.getElementById("voucher").value = document.getElementById("oldvoucher").value;
                    //document.getElementById("subbtn").style.display='none';

                } else {
                    //document.getElementById("subbtn").style.display='block'
                }

            }
        });

    }

    function checkPaySerialNo() {
        var serail = document.getElementById("paySerialNo").value;
        //alert(serail);
        //alert('clientPayedDeptControllerAjax.php.php?do=checkserail&serail='+serail);

        $.ajax({
            url: 'supplierPayedDeptControllerAjax.php?do=checkpayserialno&serail=' + serail,
            success: function (x) {
                //alert(x);

                if (x == '1') {

                    alert('عفوا هذا الرقم موجود من قبل');
                    document.getElementById("paySerialNo").value = document.getElementById("oldpaySerialNo").value;
                    //document.getElementById("subbtn").style.display='none';

                } else {
                    //document.getElementById("subbtn").style.display='block'
                }

            }
        });

    }

</script>
<div class="row">
    <div class="container">
        <ol class="breadcrumb">
            <li><a href="supplierPayedDeptController.php">خصائص عامة</a></li>
            <li><a href="supplierPayedDeptController.php?do=show">الموردين</a> </li>
            <!--<li><a href="clientPayedDeptController.php?do=show">العملاء</a></li>-->
            <li class="active">{if $do == "getmoney"} تحصيل من مورد{else}ايصال دفع لمورد {/if}</li>
        </ol>
        <div class="panel panel-default panel-m">
            <!-- Default panel contents -->
            <div class="panel-heading">
                <h3 class="panel-title">{if $do == "getmoney"} تحصيل من مورد{else}ايصال دفع لمورد {/if}</h3>
            </div>
            <div class="panel-body"> {literal}
                <form id="mySupPayForm" class="form-horizontal" action="supplierPayedDeptController.php?do=add"
                      method="post">
                    <!--validate the form before checking name-->
                    {/literal}
                    <input type="hidden" name="do" id="do" value="{$do}"/>
                    <div class="form-group" style="display:none">
                        <label class="col-lg-2 control-label">اختر عميل اومورد </label>
                        <div class="col-lg-4">
                            <input type="radio" class="clientorsupplier" id="supplier" name="clientorsupplier" value="1"
                                   {if $do != "getmoney"} checked {/if}>
                            <label for="supplier">مورد</label>
                            <input type="radio" class="clientorsupplier" id="client" name="clientorsupplier" value="2" {if $do == "getmoney"} checked {/if}>
                            <label for="client">عميل </label>

                        </div>
                    </div>
                    <div class="form-group supplierform">
                        <label class="col-lg-2 control-label">اسم المورد</label>
                        <div class="col-lg-4">
                            <span id="spryselect2">
                                <input type="hidden" id="suppliersearchvalue" value="2" />
                                <div id="supplierDiv">
                                    <input type="hidden" name="supplierid" id="clientid"
                                           class="select2Supplier wdt100" />
                                    <input type="hidden" id="oldsupplierid" />
                                    <input type="hidden" id="oldsuppliername" />
                                </div>
                                <span class="selectInvalidMsg">*</span>
                            </span>
                        </div>
                    </div>
                    <div class="form-group clientform" style="display: none;">
                        <label for="partnerid" class="col-lg-2 control-label">العميل</label>
                        <div class="col-lg-4">
                            <select class="select2Supp wdt100" name="clientid" id="clientid">
                                <option value="-1">اختر عميل</option>
                                {foreach $clientsData as $data }
                                <option value="{$data->clientid}" data-mobile="{$data->clientmobile}">
                                    {$data->clientname}</option>
                                {/foreach}
                            </select>
                            <div id="clientError" style="display:none;"></div>

                        </div>

                    </div>
                    <div class="form-group" {if $Programsettingdata->costcenter neq 0 &&
                        $Programsettingdata->realestatedev neq 0 } style="display:none" {/if}>
                        <label class="col-lg-2 control-label">مركز التكلفه</label>
                        <div class="col-lg-4">
                            <select class="select2 wdt100" name="Costcenterid" id="Costcenterid">
                                <option value="-1">اختر مركز التكلفه</option>
                                {foreach $allCostcenter as $myallCostcenter}
                                <option value="{$myallCostcenter->id}" {if $myallCostcenter->id eq
                                    $projectid}selected=""{/if}>{$myallCostcenter->name}</option>
                                {/foreach}
                            </select>
                        </div>
                    </div>

                    <div class="form-group saveorbankform">
                        <label class="col-lg-2 control-label">اختر خزنه او بنك </label>
                        <div class="col-lg-4">
                            <input type="radio" class="saveorbank" name="saveorbank" value="1" checked>
                            <label for="client">خزنه</label>
                            <input type="radio" class="saveorbank" name="saveorbank" value="2">
                            <label for="supplier">بنك</label>
                        </div>
                    </div>

                    <div class="saveshow">
                        {if $userData->searchinonesave eq 0}
                        <div class="form-group">
                            <label class="col-lg-2 control-label">الخزنة</label>
                            <div class="col-lg-4">
                                <select class="select2 wdt100" name="saveid" id="saveid">
                                    {foreach $allSave as $data}
                                    <option value="{$data->saveid}" data-curr="{$data->currencyId}" {if $userData->
                                        saveid == $data->saveid}selected{/if}>{$data->savename} {if
                                        $Programsettingdata->showCurrency == 1}- {$data->currencyName}{/if}</option>
                                    {/foreach}
                                </select>
                            </div>
                        </div>
                        {else}
                        <div class="form-group">
                            <label for="inputEmail1" class="col-lg-2 control-label">الخزنة</label>
                            <div class="col-lg-4">
                                {foreach $allSave as $data}
                                {if $userData->saveid == $data->saveid}{$data->savename} {if
                                $Programsettingdata->showCurrency == 1}- {$data->currencyName}{/if}{/if}
                                <input type="hidden" name="saveid" id="saveid" value="{$userData->saveid}" />
                                {/foreach}

                            </div>
                        </div>
                        {/if}
                        <div class="form-group">
                            <label for="lastDate" class="col-lg-2 control-label">رقم الايصال </label>
                            <div class="col-lg-4">
                                <input type="text" name="voucher" id="voucher" readonly="" onkeyup="checkserail();"
                                       value="{$todayx}" class="form-control" />
                                <div id="voucherError" style="display:none;"></div>
                            </div>
                        </div>
                        <!-- =================================================   -->
                        <div class="form-group">
                            <label for="lastDate" class="col-lg-2 control-label"> الرقم التسلسلي </label>
                            <div class="col-lg-4">
                                <input type="text" name="paySerialNo" id="paySerialNo" readonly=""
                                       onkeyup="checkPaySerialNo();" value="{$paySerialNo}" class="form-control" />
                                <div id="paySerialNoError" style="display:none;"></div>
                            </div>
                        </div>
                        <input name="oldpaySerialNo" id="oldpaySerialNo" type="hidden" value="{$paySerialNo}" />
                        <!--==============================-->

                        <div class="form-group">
                            <label class="col-lg-2 control-label">تاريخ سداد اخر عمليه</label>
                            <div class="col-lg-4">
                                <input name="lastDate" type="text" id="lastDate" readonly="readonly"
                                       class="form-control" />
                            </div>
                        </div>

                        <!--==============================-->

                        <div class="form-group">
                            <label class="col-lg-2 control-label">تاريخ السداد الحالى</label>
                            <div class="col-lg-4">
                                <!--<input name="currentDate" id="currentDate" type="text" value="{$today}"  class="form-control {if $Programsettingdata->allowdateedit != 1}awdatepicker{/if}" autocomplete="off"  {if $Programsettingdata->allowdateedit == 1}readonly="readonly"{/if} data-date-format="yyyy-mm-dd"  //>-->
                                {if $Programsettingdata->allowdateedit != 1}
                                <div class="input-group date form_datetime col-md-5" data-date=""
                                     data-date-format="yyyy-mm-dd  h:i:s" data-link-field="dtp_input1">
                                    <input class="form-control" size="16" type="text" value="{$today}"
                                           name="currentDate" id="currentDate" readonly="">
                                    <span class="input-group-addon "><span
                                            class=" glyphicon glyphicon-remove col-lg-4"></span></span>
                                    <span class="input-group-addon"><span
                                            class="glyphicon glyphicon-th col-lg-4"></span></span>
                                </div>
                                {else}
                                <input name="currentDate" id="currentDate" type="text" value="{$today}"
                                       class="form-control" autocomplete="off" readonly="readonly" />
                                {/if}
                            </div>
                        </div>

                        <!--==============================-->
                        <input name="suplliernegative" type="hidden" value="{$suplliernegative}"
                               id="suplliernegative" />
                        <div class="form-group">
                            <label class="col-lg-2 control-label">الدين السابق</label>
                            <div class="col-lg-4">
                                <div class="input-group">
                                    <input name="debtBefore" id="debtBefore" type="text" readonly="readonly"
                                           class="form-control" />
                                    <span class="input-group-addon">{$currency}</span>
                                </div>
                            </div>
                        </div>

                        <!--==============================-->
                        <div class="form-group" {if $Programsettingdata->showCurrency != 1}style="display:none;"{/if}>
                            <label class="col-lg-2 control-label">العملة</label>
                            <div class="col-lg-4">
                                <span id="spryselect1">
                                    <select class="select2" name="currencyId" id="currencyId">
                                        {foreach $allCurrency as $data}
                                        <option value="{$data->id}" data-cfactor="{$data->conversionFactor}"
                                                data-othercfactor="{$data->otherconversionFactor}">{$data->name}</option>
                                        {/foreach}
                                    </select>
                                    <span class="selectRequiredMsg">*</span></span>
                            </div>
                        </div>
                        <div class="form-group" {if $Programsettingdata->showCurrency != 1}style="display:none;"{/if}>
                            <label class="col-lg-2 control-label">معامل التحويل</label>
                            <div class="col-lg-4">
                                <input type="text" name="cFactor" id="cFactor"
                                       class="form-control positivefloat calcOther" onkeyup="populateDebtAfter();"
                                       style="display: inline-block;width: 35%" />
                                =
                                <div class="input-group" style="display: inline;">
                                    <input type="text" name="otherCFactor" id="otherCFactor"
                                           class="form-control positivefloat calcOther"
                                           style="display: inline-block;width: 35%" />
                                    <span class="input-group-addon"
                                          style="display: inline-block;width: 24%">{$currency}</span>
                                </div>
                            </div>
                        </div>
                        <!-- =================================================   -->
                        <div class="form-group">
                            <label class="col-lg-2 control-label">المدفوع</label>
                            <div class="col-lg-4">
                                <div class="input-group">
                                    <input type="text" name="payedDeptCurr" id="payedDeptCurr"
                                           onkeyup="populateDebtAfter('payedDeptCurr');"
                                           class="{if $suplliernegative==1}numberinput{else}positivefloat{/if} form-control" />
                                    <span class="input-group-addon" id="currencyText">{$currency}</span>
                                </div>
                                <div id="valueError" style="display:none;"></div>
                            </div>
                        </div>
                        <div class="form-group" {if $Programsettingdata->showCurrency != 1}style="display:none;"{/if}>
                            <label class="col-lg-2 control-label">المدفوع بالعملة الرئيسية</label>
                            <div class="col-lg-4">
                                <div class="input-group">
                                    <input type="text" name="payedDept" id="payedDept"
                                           onkeyup="populateDebtAfter('payedDept');"
                                           class="{if $suplliernegative==1}numberinput{else}positivefloat{/if} form-control" />
                                    <span class="input-group-addon">{$currency}</span>
                                </div>
                            </div>
                        </div>

                        <!--==============================-->

                        <div class="form-group">
                            <label class="col-lg-2 control-label">الدين الحالى</label>
                            <div class="col-lg-4">
                                <div class="input-group">
                                    <input type="text" name="debtAfter" disabled id="debtAfter" class="form-control" />
                                    <span class="input-group-addon">{$currency}</span>
                                </div>
                            </div>
                        </div>

                        <!--==============================-->


                        <div class="form-group clientform" style="display:none;">
                            <label class="col-lg-2 control-label">المندوب</label>
                            <div class="col-lg-4">
                                <select class="select2 wdt100" name="seller" id="seller">
                                    <option value=""></option>
                                    {foreach $sellers as $data}
                                    <option value="{$data->userid}">{$data->employeename}</option>
                                    {/foreach}
                                </select>
                            </div>
                        </div>

                        <!-- =================================================   -->


                        <div class="form-group">
                            <label for="comment" class="col-lg-2 control-label">التعليق</label>
                            <div class="col-lg-4">
                                <div class="input-group">
                                    <textarea style="width: 358px;" name="comment" cols="50" rows="5"
                                              class="form-control"></textarea>
                                </div>
                            </div>
                        </div>
                    </div>
                    <!--end Save-->

                    <!--bank-->
                    <div class="bankshow" style="display: none;">
                        <div class="form-group">
                            <label for="ddlBank" class="col-lg-2 control-label">البنك</label>

                            <div class="col-lg-4">

                                <select class="select2 wdt100" name="ddlBank" id="ddlBank"
                                        onchange="loadAccount(this.value);">
                                    <option value="-1">اختر بنك</option>

                                    {foreach $bank as $b}
                                    <option value="{$b->bankid}">{$b->bankname}</option>
                                    {/foreach}

                                </select>
                                <div id="bankerror">

                                </div>
                            </div>
                            <div class="col-lg-1">
                                <span class="requiredField">*</span>
                            </div>
                        </div>

                        <!-- =================================================   -->


                        <div class="form-group">
                            <label for="accountbeginingbalanceDis" class="col-lg-2 control-label">دين المورد</label>
                            <div class="col-lg-4">
                                <div class="input-group">
                                    <input type="text" name="debtBeforebank" id="debtBeforebank"
                                           class="form-control numberinput" readonly="readonly" value="0"
                                           placeholder="دين المورد" />

                                    <span class="input-group-addon">{$currency}</span>
                                </div>
                            </div>
                        </div>
                        <!-- =================================================   -->


                        <div class="form-group">
                            <label for="accountid" class="col-lg-2 control-label">اسم الحساب</label>

                            <div class="col-lg-4">
                                <div id="accountName">

                                    <select class="select2 wdt100" name="accountid" id="accountid">
                                        <option value="-1">اختر حساب</option>

                                    </select>

                                </div>

                                <div id="counterror">

                                </div>
                            </div>
                            <div class="col-lg-1">
                                <span class="requiredField">*</span>
                            </div>
                        </div>


                        <div class="form-group">
                            <label class="col-lg-2 control-label">التاريخ </label>
                            <div class="col-lg-4"> <span id="sprydate">
                                    <input type="text" class="form-control awdatepicker" autocomplete="off"
                                           data-date-format="yyyy-mm-dd" placeholder="التاريخ" value="{$today}"
                                           name="checkdate" id="bn">
                                    <span class="textfieldRequiredMsg">*</span></span>
                            </div>
                            <div class="col-lg-1">
                                <span class="requiredField">*</span>
                            </div>
                        </div>

                        <div id="accountnumber">
                            <!-- =================================================   -->


                            <div class="form-group">
                                <label for="accountnumberDis" class="col-lg-2 control-label">رقم الحساب</label>
                                <div class="col-lg-4">
                                    <input name="txtPhone" type="text" id="accountnumberDis" name="accountnumberDis"
                                           class="numberinput form-control" placeholder="رقم الحساب" readonly="readonly" />

                                    <input type="hidden" name="accountnumber" id="accountnumber" />

                                </div>
                            </div>

                            <!--==============================-->

                            <div class="form-group">
                                <label for="accountbeginingbalanceDis" class="col-lg-2 control-label">رصيد
                                    سابق</label>
                                <div class="col-lg-4">
                                    <div class="input-group">
                                        <input type="text" name="accountbeginingbalanceDis"
                                               id="accountbeginingbalanceDis" class="form-control numberinput"
                                               readonly="readonly" value="0" placeholder="رصيد سابق" />

                                        <span class="input-group-addon">{$currency}</span>
                                    </div>

                                    <input type="hidden" name="accountbeginingbalance" id="accountbeginingbalance" />
                                </div>
                            </div>

                            <!--==============================-->

                        </div>
                        <!--==============================-->


                        <div class="form-group">
                            <label for="txtAmount" class="col-lg-2 control-label">قيمة السحب</label>
                            <div class="col-lg-4">
                                <div class="input-group">
                                    <input type="text" name="txtAmount" id="txtAmountxx"
                                           class="form-control {if $suplliernegative==1}numberinput{else}positivefloat{/if}"
                                           onkeyup="populateDebtAfterBank('txtAmount')" placeholder="قيمة السحب" />
                                    <span class="input-group-addon">{$currency}</span>
                                    <div id="chekamount">

                                    </div>
                                </div>
                                <div id="chekamountError">

                                </div>

                            </div>
                            <div class="col-lg-1">
                                <span class="requiredField">*</span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="debtAfterbank" class="col-lg-2 control-label"> دين المورد الحالي</label>
                            <div class="col-lg-4">
                                <div class="input-group">
                                    <input type="text" name="debtAfterbank" id="debtAfterbank" class="form-control"
                                           disabled />
                                    <span class="input-group-addon">{$currency}</span>
                                </div>
                            </div>
                        </div>
                        <!--==============================-->
                        <div class="form-group">
                            <label for="txtcheckNum" class="col-lg-2 control-label">رقم عملية السحب</label>
                            <div class="col-lg-4">

                                <input type="text" name="txtcheckNum" id="txtcheckNum"
                                       class="form-control numberinput positivefloat" placeholder="رقم عملية السحب" />
                                <div id="chekerror">

                                </div>
                                <div id="chekerror2">

                                </div>
                            </div>
                        </div>

                        <!--==============================-->


                        <div class="form-group">
                            <label for="txtUser" class="col-lg-2 control-label">اسم المستفيد</label>
                            <div class="col-lg-4">

                                <input type="text" name="txtUser" id="txtUser" class="form-control "
                                       placeholder="اسم المستفيد" />

                                <div id="userdiv">

                                </div>

                            </div>
                        </div>


                        <!--==============================-->


                        <div class="form-group">
                            <label for="textNote" class="col-lg-2 control-label">تفاصيل اخرى</label>
                            <div class="col-lg-4">

                                <textarea class="form-control" rows="3" id="textNote" name="textNote"></textarea>

                            </div>
                        </div>

                    </div>
                    <!--end Bank-->



                    <!--==============================-->
                    <input type="hidden" name="add_print" id="add_print" value="0" />

                    <div class="form-group">
                        <div class="col-lg-offset-2 col-lg-4">
                            <button name="saveSupPayBtn" type="button" id="saveSupPayBtn"
                                    class="btn btn-primary ">حفظ</button>
                            <button type="button" id="add_print_btn" class="btn btn-success">حفظ وطباعة</button>

                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
{literal}
<script type="text/javascript">
    var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1");
    var sprytextfield2 = new Spry.Widget.ValidationTextField("sprytextfield2", "real");
    var spryselect1 = new Spry.Widget.ValidationSelect("spryselect1", {invalidValue: "-1"});
    var sprytextfield3 = new Spry.Widget.ValidationTextField("sprytextfield3", "real");
    var spryselect2 = new Spry.Widget.ValidationSelect("spryselect2", {invalidValue: "-1"});
</script>
{/literal}

{literal}
<script type="text/javascript">
    jQuery(function () {
        $("form").submit(function () {
            // submit more than once return false
            $(this).submit(function () {
                return false;
            });
            // submit once return true
            return true;
        });
    });
</script>
{/literal}

{literal}
<script type="text/javascript">
    jQuery(document).ready(function () {

        $(".select2Supplier").select2({
            placeholder: "المورد",
            minimumInputLength: document.getElementById("suppliersearchvalue").value,
            inimumInputLength: 0,
            quietMillis: 100,
            formatInputTooShort: function (input, min) {
                var n = min - input.length;
                $lenMsg = '<ul class="select2-results">';
                // if (supplierWithAddOption == 1)
                // $lenMsg += '<li class="select2-results-dept-0 select2-result select2-result-selectable" onclick="newOptionAtInputTooShort();"><div class="select2-result-label newoption"><span class="newoption">اضافة عميل جديد</span></div></li>';
                $lenMsg += '</ul><div class="select2-result-label"><span>من فضلك أدخل ' + n + ' حروف أكثر</span></div>';
                // Append it to the select
                return $lenMsg;
            },
            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: 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], mobile: item[6]};
                callback(data);
            },
            //dropdownCssClass: "bigdrop", // apply css that makes the dropdown taller
        }).on('select2-loaded', function (e) {// not "select2-open" nor "select2-opening" //http://mkurz.github.io/select2/index.html
            $('.newoption').parent().addClass('newoption');
        }).on("select2-selecting", function (e) {
            // the class to make diffrence of change from select2 itself "choose another option" ,it wont exist for triggering change
            $('#supplierid').addClass('selectingClass');
        })
        //this is needed for ajax =>to obtain id from it  and use in ajax
        $('#supplierid').select2('data', {id: $('#oldsupplierid').val(), text: $('#oldsuppliername').val()});
        setTimeout(function () {
            //this is needed as we need html to end drawing then put value => without it text will be undefined
            $('#supplierid').select2('data', {id: $('#oldsupplierid').val(), text: $('#oldsuppliername').val()});
        }, 0);
    });
</script>
{/literal}