<style>
    .nav-tabs-fillup>li>a:after {
        background: none repeat scroll 0 0 #4c0870 !important;
        border: 1px solid #4c0870 !important;
    }
    .AddButtons .fa-plus,.AddButtons .fa-minus{
        cursor:pointer;
    }
    .AddButtons .fa-minus{
        color:#ed9421;
    }
    .AddButtons .fa-plus{
        color:#4c0870; 
    }
    .required-fields{
        border: red solid 1px;
    }
</style>
<div class="modal fade" id="customer_modal" data-backdrop="static" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header text-left">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="pg-close fs-20"></i>
                </button>
                <h5 class="add-label h-labels bold hidden"> <?php echo CommonLanguage::getMessage('new_customer'); ?></h5>
                <h5 class="edit-label h-labels bold hidden"> <?php echo CommonLanguage::getMessage('edit_customer'); ?></h5>
            </div>
            <div class="modal-body">
                <div class="row"><?php echo Forms::AlertDiv('customer_alert'); ?></div>
                <form method="POST" id="customer_form" onsubmit="return false">
                    <input type="hidden" name="Customer_ID" id="Customer_ID"/>
                    <div class="panel panel-transparent ">
                        <ul class="nav nav-tabs nav-tabs-fillup">
                            <li class="active">
                                <a data-toggle="tab" href="#slide1"><span><?php print CommonLanguage::getMessage('customer_info'); ?></span></a>
                            </li>
                            <li>
                                <a data-toggle="tab" href="#slide2"><span><?php print CommonLanguage::getMessage('contact_info'); ?></span></a>
                            </li> 
                            <li>
                                <a data-toggle="tab" href="#slide3"><span><?php print CommonLanguage::getMessage('shipping_address'); ?></span></a>
                            </li> 
                            <li>
                                <a data-toggle="tab" href="#slide4"><span><?php print CommonLanguage::getMessage('branches'); ?></span></a>
                            </li>
                        </ul>
                        <div class="tab-content">
                            <div class="tab-pane slide-left active" id="slide1">
                                <div class="row form-group">
                                    <div class="col-md-3">
                                        <label><?php print CommonLanguage::getMessage('Name'); ?></label><span class="red-star">*</span>
                                        <input type="text" placeholder="<?php print CommonLanguage::getMessage('Name'); ?>" id="Customer_Name" name="Customer_Name" class="form-control " required>
                                    </div>
                                    <div class="col-md-3">
                                        <label><?php print CommonLanguage::getMessage('Phone'); ?></label><span class="red-star">*</span> 
                                        <input type="text" placeholder="<?php print CommonLanguage::getMessage('Phone'); ?>" id="Customer_Mobile" name="Customer_Mobile" class="form-control " required>
                                        <span id="check_phone" style="color:red;font-size:11px;"></span>
                                    </div>
                                    <div class="col-md-3">
                                        <label><?php print CommonLanguage::getMessage('pricing_class'); ?></label><span class="red-star">*</span>
                                        <span class="fa fa-plus-circle m-l-5 openFrame" data-src="Inventory/pricing_classes?is_pop=1" style="color:#8a7dbe;cursor:pointer"></span>
                                        <input required id="Customer_Class_ID" name="Customer_Class_ID" class="full-width Customer_Class_ID" type="hidden"/>
                                    </div>
                                    <div class="col-md-3">
                                        <label><?php print CommonLanguage::getMessage('currency'); ?></label>
                                        <span class="red-star">*</span>
                                        <input type="hidden" class="full-width" value="<?php echo $default_currency['Currency_ID']; ?>" name="Currency_ID" data-select-name="<?php echo $default_currency['Currency_Name'] . ' - ' . $default_currency['Currency_Code']; ?>" id="Currency_ID" reuired/>
                                    </div>
                                </div>
                                <div class="row form-group <?php echo $permissions->hasPermission('Branches') ? '':'hidden'; ?>">
                                    <div class="col-md-3">
                                        <label><?php echo CommonLanguage::getMessage('choose_branch') ?></label>
                                        <input id="branch_id" type="hidden" class="full-width" name="branch_id"  >
                                    </div>
                                </div>

                                <hr class="b-dashed">
                                <div class="row form-group">
                                    <div class="col-md-3">
                                        <label><?php print CommonLanguage::getMessage('country'); ?></label>
                                        <input type="hidden" value="<?php echo smc_session()['default_setting']['default_country_id'] ?>" data-select-name="<?php echo smc_session()['default_country']['Country_Name'] ?>"  name="Customer_Country_ID" id="Customer_Country_ID" class="full-width"/>
                                    </div>
                                    <div class="col-md-3" >
                                        <label><?php print CommonLanguage::getMessage('city'); ?></label>
                                        <input type="hidden" value="<?php echo smc_session()['default_setting']['default_city_id'] ?>" data-select-name="<?php echo smc_session()['default_city']['Name'] ?>"  name="Customer_City_ID" id="Customer_City_ID" class="full-width"/>
                                    </div>
                                    <div class="col-md-3">
                                        <label><?php print CommonLanguage::getMessage('Email'); ?></label>
                                        <input type="email" placeholder="<?php print CommonLanguage::getMessage('Email'); ?>" id="Customer_Email" name="Customer_Email" class="form-control " >
                                    </div>
                                    <div class="col-md-3">
                                        <label><?php print CommonLanguage::getMessage('address'); ?></label>
                                        <input type="text" placeholder="<?php print CommonLanguage::getMessage('address'); ?>"  id="Customer_Address" name="Customer_Address" class="form-control" >
                                    </div>
                                </div>
                                <div class="row form-group">
                                    <div class="col-md-3">
                                        <label><?php print CommonLanguage::getMessage('second_phone'); ?></label>
                                        <input type="text" placeholder="<?php print CommonLanguage::getMessage('work_phone'); ?>" id="Customer_Work_Phone" name="Customer_Work_Phone" class="form-control " >
                                    </div>
                                    <div class="col-md-3">
                                        <label><?php print CommonLanguage::getMessage('third_phone'); ?></label>
                                        <input type="text" placeholder="<?php print CommonLanguage::getMessage('Phone'); ?>" id="Customer_Phone" name="Customer_Phone" class="form-control " >
                                    </div>
                                    <div class="col-md-3">
                                        <label><?php print CommonLanguage::getMessage('longitude'); ?></label> 
                                        <span class="fa fa-map-pin m-l-5" data-toggle="modal" data-target="#customer_map_modal" style="color:#8a7dbe;cursor:pointer"></span>
                                        <input type="text" placeholder="<?php print CommonLanguage::getMessage('longitude'); ?>" id="Customer_Lng" name="Customer_Lng" class="form-control " >
                                    </div>
                                    <div class="col-md-3">
                                        <label><?php print CommonLanguage::getMessage('latitude'); ?></label>
                                        <input type="text" placeholder="<?php print CommonLanguage::getMessage('latitude'); ?>" id="Customer_Lat" name="Customer_Lat" class="form-control " >
                                    </div>
                                </div>
                                <div class="row form-group">
                                    <div class="col-md-3">
                                        <label><?php print CommonLanguage::getMessage('company'); ?></label>
                                        <input type="text" placeholder="<?php print CommonLanguage::getMessage('company'); ?>" id="Customer_Company" name="Customer_Company" class="form-control " >
                                    </div>
                                    <div class="col-md-3">
                                        <label><?php print CommonLanguage::getMessage('commercial_registry'); ?></label>
                                        <input type="text" placeholder="<?php print CommonLanguage::getMessage('commercial_registry'); ?>" id="Customer_CR" name="Customer_CR" class="form-control " >
                                    </div>
                                    <div class="col-md-3">
                                        <label><?php print CommonLanguage::getMessage('customer_limit_balance'); ?></label>
                                        <input type="text" placeholder="<?php print CommonLanguage::getMessage('customer_limit_balance'); ?>" id="Customer_Limit_Balance" name="Customer_Limit_Balance" class="form-control " >
                                    </div>
                                    <div class="col-md-3">
                                        <label><?php print CommonLanguage::getMessage('discount'); ?></label>
                                        <input type="text" placeholder="<?php print CommonLanguage::getMessage('discount'); ?>" id="Customer_Discount" name="Customer_Discount" class="form-control " >
                                    </div>
                                </div>
                                <div class="row form-group">
                                    <div class="col-md-3">
                                        <label class="labelText"><?php print CommonLanguage::getMessage('customer_group'); ?></label>
                                        <input type="hidden"  id="customer_group_id" name="customer_group_id" class="full-width " >
                                    </div> 
                                    <div class="col-md-3">
                                        <label class="labelText"><?php print CommonLanguage::getMessage('customer_class'); ?></label>
                                        <input type="hidden"  id="cust_class_id" name="cust_class_id" class="full-width " >
                                    </div> 
                                    <div class="col-md-3">
                                        <label class="labelText"><?php print CommonLanguage::getMessage('Code'); ?></label>
                                        <input type="text" placeholder="<?php print CommonLanguage::getMessage('Name'); ?>" id="Customer_Code" name="Customer_Code" class="form-control " >
                                    </div>
                                    <div class="col-md-3">
                                        <label><?php print CommonLanguage::getMessage('parent'); ?></label>
                                        <?php if (smc_session()['default_setting']['cust_supp_required_parent'] == 1) { ?>
                                            <span class="red-star">*</span>
                                        <?php } ?>
                                        <input type="hidden"  id="account_parent_id" name="account_parent_id" class="full-width " <?php echo (smc_session()['default_setting']['cust_supp_required_parent'] == 1) ? 'required' : '' ?>>
                                    </div>

                                </div>
                                <div class="row form-group">
                                    <div class="col-md-3">
                                        <label><?php print CommonLanguage::getMessage('region'); ?></label>
                                        <input type="hidden"  id="region_id" name="region_id" class="full-width " >
                                    </div>
                                    <div class="col-md-3">
                                        <label><?php print CommonLanguage::getMessage('due_date_days'); ?></label>
                                        <input type="text" placeholder="<?php print CommonLanguage::getMessage('due_date_days'); ?>"  id="customer_due_date_period" name="customer_due_date_period" class="form-control " >
                                    </div>
                                    <div class="col-md-3">
                                        <label><?php print CommonLanguage::getMessage('user_name'); ?></label>
                                        <div><input type="text" id="customer_user_name" autocomplete="off" name="customer_user_name" class="form-control" autocomplete="off" /></div>
                                    </div>
                                    <div class="col-md-3 pasword-div">
                                        <label><?php print CommonLanguage::getMessage('password'); ?></label>
                                        <div><input type="password" id="customer_password" autocomplete="off" name="customer_password" class="form-control" autocomplete="off" /></div>
                                    </div>
                                    <div class="col-md-3 <?php echo smc_session()['default_setting']['activate_allergy_intolerance'] == 1 ? '' : 'hidden'; ?>">
                                        <label><?php print CommonLanguage::getMessage('allergy'); ?></label>
                                        <span class="fa fa-plus-circle m-l-5 openAllergyIntolerance" data-type="1" data-content="<?php print CommonLanguage::getMessage('allergy'); ?>" style="color:#8a7dbe;cursor:pointer"></span>
                                        <input type="hidden"  id="allergy_id" name="allergy_id" class="full-width">
                                    </div>
                                    <div class="col-md-3 <?php echo smc_session()['default_setting']['activate_allergy_intolerance'] == 1 ? '' : 'hidden'; ?>">
                                        <label><?php print CommonLanguage::getMessage('intolerance'); ?></label>
                                        <span class="fa fa-plus-circle m-l-5 openAllergyIntolerance" data-type="2" data-content="<?php print CommonLanguage::getMessage('intolerance'); ?>" style="color:#8a7dbe;cursor:pointer"></span>
                                        <input type="hidden"  id="intolerance_id" name="intolerance_id" class="full-width ">
                                    </div>
                                    <div class="col-md-3  <?php echo smc_session()['default_setting']['activate_autofill_other_amount']==1 ? '': 'hidden'?>">
                                        <label class="labelText"><?php print CommonLanguage::getMessage('other_acc_trans'); ?><span class="fa fa-info-circle" title="<?php echo CommonLanguage::getMessage('other_acc_trans_hint') ?>"></span></label>
                                        <input type="hidden"  id="other_pay_acc_id" name="other_pay_acc_id" class="full-width " >
                                    </div> 
                                </div>
                                <div class="row form-group">
                                    <div class="col-md-3 <?php echo smc_session()['user_details']['limit_customer_invoice'] == 1 ? 'hidden' : ''; ?>">
                                        <label><?php echo CommonLanguage::getMessage('salesman'); ?></label>
                                        <input type="hidden" name="sales_man_id" id="sales_man_id" class="full-width"/>
                                    </div>
                                    <div class="col-md-3 <?php echo smc_session()['user_details']['limit_customer_invoice'] == 1 ? 'hidden' : ''; ?>">
                                        <label><?php echo CommonLanguage::getMessage('user'); ?></label>
                                        <input type="hidden" name="user_id" id="customer_user_id" class="full-width"/>
                                    </div>
                                    <div class="col-md-3" >
                                        <label><?php echo CommonLanguage::getMessage('customer_exceed_limtit'); ?></label>
                                        <input type="checkbox" id="limit_exceed"  />
                                    </div>
                                    <div class="col-md-3 is_vendor_div" >
                                        <label><?php print CommonLanguage::getMessage('is_vendor'); ?></label>
                                        <input type="checkbox" id="Is_Vendor" />
                                    </div>
                                </div>
                                <div class="row form-group">
                                    <div class="col-md-3">
                                    <label><?php print CommonLanguage::getMessage('customer_vat'); ?></label>
                                        <input type="text" placeholder="<?php print CommonLanguage::getMessage('customer_vat'); ?>" id="customer_vat" name="customer_vat" class="form-control " >
                                    </div>
                                    <div class="col-md-3">
                                    <label><?php print CommonLanguage::getMessage('customer_postal_code'); ?></label>
                                        <input type="text" placeholder="<?php print CommonLanguage::getMessage('customer_postal_code'); ?>" id="customer_postal_code" name="customer_postal_code" class="form-control " >
                                    </div>
                                    <div class="col-md-3">
                                    <label><?php print CommonLanguage::getMessage('customer_additional_number'); ?></label>
                                        <input type="text" placeholder="<?php print CommonLanguage::getMessage('customer_additional_number'); ?>" id="customer_additional_number" name="customer_additional_number" class="form-control " >
                                    </div>
                                    <div class="col-md-3">
                                    <label><?php print CommonLanguage::getMessage('customer_other_id'); ?></label>
                                        <input type="text" placeholder="<?php print CommonLanguage::getMessage('customer_other_id'); ?>" id="customer_other_id" name="customer_other_id" class="form-control " >
                                    </div>
                                     
                                </div>
                                <div class="row form-group">
                                    <div class="col-md-6">
                                        <label><?php echo CommonLanguage::getMessage('customer_note') ?></label>
                                        <textarea id="customer_note" name="customer_note" class="form-control" style="resize: vertical" rows="4"></textarea>
                                    </div>
                                </div>
                            </div>
                            <div class="tab-pane slide-left " id="slide2">
                                <div class="row form-group">
                                    <div class="col-md-3">
                                        <label><?php echo CommonLanguage::getMessage('Name') ?></label>
                                        <span class="red-star">*</span>
                                    </div>
                                    <div class="col-md-2">
                                        <label><?php echo CommonLanguage::getMessage('position') ?></label>
                                        <span class="red-star">*</span>
                                    </div>
                                    <div class="col-md-2">
                                        <label><?php echo CommonLanguage::getMessage('Phone') ?></label>
                                    </div>
                                    <div class="col-md-3">
                                        <label><?php echo CommonLanguage::getMessage('note') ?></label>
                                    </div>
                                </div>
                                <div class="form-group row contact-row"> 
                                    <div  class=" col-md-3 "> 
                                        <input type="text" name="contact_name" autocomplete="off" class="contact_name form-control">
                                    </div> 
                                    <div  class=" col-md-2 "> 
                                        <input type="text" name="contact_position" autocomplete="off" class="contact_position form-control">
                                    </div> 
                                    <div  class=" col-md-2 "> 
                                        <input type="text" name="contact_phone_number" autocomplete="off"  placeholder="" class="contact_phone_number form-control">
                                    </div> 
                                    <div  class=" col-md-3 "> 
                                        <input type="text" name="contact_note"  autocomplete="off" class="contact_note form-control">
                                    </div> 
                                    <div class="col-md-2 AddButtons">
                                        <span class="fa fa-plus m-t-10"></span>
                                        <span class="fa fa-minus m-t-10"></span>
                                    </div>
                                </div>
                            </div>
                            <div class="tab-pane slide-left " id="slide3">
                                <div class="row form-group">
                                    <div class="col-md-2">
                                        <label><?php echo CommonLanguage::getMessage('title') ?></label>
                                        <span class="red-star">*</span>
                                    </div> 
                                    <div class="col-md-2">
                                        <label><?php echo CommonLanguage::getMessage('city') ?></label>
                                        <span class="red-star">*</span>
                                    </div>
                                    <div class="col-md-2 <?php echo count($shipping_companies) == 0 ? 'hidden' : ''; ?>">
                                        <label><?php echo CommonLanguage::getMessage('fast_pax_city'); ?></label>
                                    </div>
                                    <div class="col-md-1">
                                        <label><?php echo CommonLanguage::getMessage('street') ?></label>
                                        <span class="red-star">*</span>
                                    </div>
                                    <div class="col-md-1">
                                        <label><?php echo CommonLanguage::getMessage('building') ?></label>
                                        <span class="red-star">*</span>
                                    </div>
                                    <div class="col-md-2">
                                        <label><?php echo CommonLanguage::getMessage('shipping_address') ?></label>
                                        <span class="red-star">*</span>
                                    </div>
                                    <div class="col-md-1">
                                        <label><?php echo CommonLanguage::getMessage('is_default') ?></label>
                                    </div>
                                </div>
                                <div class="form-group row shipping-row" data-id="0">  
                                    <div  class=" col-md-2 "> 
                                        <input type="text" name="shipping_title" autocomplete="off" class="req shipping_title form-control">
                                    </div> 
                                    <div  class=" col-md-2 "> 
                                        <input type="hidden" name="shipping_city_id" autocomplete="off" class="req shipping_city_id full-width" tabindex="0">
                                    </div>  
                                    <div  class=" col-md-2 <?php echo count($shipping_companies) == 0 ? 'hidden' : ''; ?>"> 
                                        <input type="hidden" name="fastpax_city_id" autocomplete="off" class="req fastpax_city_id full-width" tabindex="0">
                                    </div> 
                                    <div  class=" col-md-1 "> 
                                        <input type="text" name="shipping_street" autocomplete="off"  placeholder="" class="req shipping_street form-control">
                                    </div>
                                    <div  class=" col-md-1 "> 
                                        <input type="text" name="shipping_building" autocomplete="off"  placeholder="" class="req shipping_building form-control">
                                    </div> 
                                    <div  class=" col-md-2 "> 
                                        <input type="text" name="shipping_address"  autocomplete="off" class="req shipping_address form-control">
                                    </div> 
                                    <div  class=" col-md-1 "> 
                                        <input type="checkbox" name="is_default_shipping"  autocomplete="off" class="is_default_shipping" checked>
                                    </div> 
                                    <div class="col-md-1 AddButtons">
                                        <span class="fa fa-plus m-t-10"></span>
                                        <span class="fa fa-minus m-t-10"></span>
                                    </div>
                                </div>
                            </div>
                            <div class="tab-pane slide-left " id="slide4">
                                <div class="row form-group">
                                    <div class="col-md-4">
                                        <label><?php echo CommonLanguage::getMessage('Name') ?></label>
                                        <span class="red-star">*</span>
                                    </div>
                                </div>
                                <div class="form-group row branch-row"> 
                                    <div  class=" col-md-3 "> 
                                        <input type="text" name="customer_branch_name" autocomplete="off" class="customer_branch_name form-control">
                                    </div> 
                                    <div class="col-md-2 AddButtons">
                                        <span class="fa fa-plus m-t-10"></span>
                                        <span class="fa fa-minus m-t-10"></span>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button data-dismiss="modal" class="btn btn-default btn-cons">
                    <?php print CommonLanguage::getMessage('Close'); ?>
                </button>
                <button class="btn btn-primary btn-cons"  id="submit_customer" type="button">
                    <?php print CommonLanguage::getMessage('Save'); ?>
                </button>
            </div>
            <?php echo Forms::ModalLoader('customer_loader'); ?>
        </div>
    </div>
</div>
<script>
    var phone_uset_by_lang ='<?php echo CommonLanguage::getMessage('phone_uset_by') ?>';
    var country_id = <?php echo json_encode(smc_session()['default_setting']['default_country_id']) ?>,
            city_id = <?php echo json_encode(smc_session()['default_setting']['default_city_id']) ?>,
            city_name = <?php echo json_encode(smc_session()['default_city']['Name']) ?>,
            country_name = <?php echo json_encode(smc_session()['default_country']['Country_Name']) ?>;
    $(function () {
        build_select2($('#customer_user_id'), 'xhr/searchCustomerUsers?is_app=1', 1);
        build_select2($('#sales_man_id'), 'xhr/employeeSearch', 1);
        build_select2($("#customer_group_id"), "xhr/general_group_Search?group_type=cust");
        build_select2($("#cust_class_id"), "xhr/general_class_Search?class_type=cust");
        build_select2($("#Currency_ID"), "xhr/CurrencySearch");
        build_select2($(".Customer_Class_ID"), "xhr/PricingClassSearch");
        $('#Customer_Class_ID').val(1).attr('data-select-name', 'Retail').trigger('change');
        build_select2($("#branch_id"), "xhr/CommonSearch?col_name=branch_name&col_id=branch_id&table=tbl_organization_branches");

 
 

        build_select2($("#account_parent_id"), "xhr/acc_subtype_search?type=cust");
        build_select2($("#Customer_Country_ID"), "xhr/CommonSearch?col_id=Country_ID&col_name=Country_Name&table=tbl_all_countries");
        build_select2($("#Customer_City_ID"), "xhr/searchCity?Country_ID=" + $("#Customer_Country_ID").val());
        build_select2($("#allergy_id"), "xhr/CommonSearch?col_id=allergy_id&col_name=allergy_name&table=tbl_allergy", 1);
        build_select2($("#intolerance_id"), "xhr/CommonSearch?col_id=intolerance_id&col_name=intolerance_name&table=tbl_intolerance", 1);
        build_select2($("#region_id"), "xhr/CommonSearch?col_id=region_id&col_name=region_name&table=tbl_regions");
        new Switchery($("#Is_Vendor").get(0), {color: $.Pages.getColor("primary")});
        new Switchery($("#limit_exceed").get(0), {color: $.Pages.getColor("primary")});
        build_select2($("input[name=shipping_city_id]"), "xhr/searchCity?show_country_name=1");
        build_select2($("input[name=fastpax_city_id]"), "xhr/searchFastpaxCities");
        build_select2($("#other_pay_acc_id"), ("xhr/accIDSearch?type=Payment"))
        $("#Customer_Country_ID").on('change', function () {
            $("#Customer_City_ID").val('').trigger('change');
            build_select2($("#Customer_City_ID"), "xhr/searchCity?Country_ID=" + $("#Customer_Country_ID").val());
        });
        $("#slide2").on("click", ".AddButtons .fa-plus", function (e) {
            var Parent = $(this).parents(".contact-row"), NewItem = Parent.clone().insertAfter(Parent);
            NewItem.find("input").val('');
        });
        $("#slide2").on("click", ".AddButtons .fa-minus", function (e) {
            if ($(".contact-row").length > 1)
                $(this).closest(".contact-row").empty().remove();
        });
        $("#slide3").on("click", ".AddButtons .fa-plus", function (e) {
            var Parent = $(this).parents(".shipping-row"), NewItem = Parent.clone().insertAfter(Parent);
            NewItem.find("input").val('');
            NewItem.attr('data-id', 0)
            NewItem.find(".select2-container").remove();
            build_select2($("input[name=shipping_city_id]", NewItem), "xhr/searchCity?show_country_name=1");
            build_select2($("input[name=fastpax_city_id]", NewItem), "xhr/searchFastpaxCities");
            $('input[name=is_default_shipping]', NewItem).attr('checked', false);
            $('select.shipping_type_id', NewItem).select2();
            $(".select2-container", NewItem).show();
        });
        $("#slide3").on("click", ".AddButtons .fa-minus", function (e) {
            if ($(".shipping-row").length > 1)
                $(this).closest(".shipping-row").empty().remove();
        });
        $('#slide3').on('click', 'input.is_default_shipping', function () {
            var index = $('#slide3 input.is_default_shipping').index(this);
            $('input.is_default_shipping').not(":eq(" + index + ")").prop('checked', false);
            var parent = $(this).closest('.AddRow');
            $('input.is_default_shipping', parent).prop('checked', $('input.is_default_shipping', parent).is(":checked") ? true : false);
        });
        $("#slide4").on("click", ".AddButtons .fa-plus", function (e) {
            var Parent = $(this).parents(".branch-row"), NewItem = Parent.clone().insertAfter(Parent);
            NewItem.find("input").val('');
        });
        $("#slide4").on("click", ".AddButtons .fa-minus", function (e) {
            if ($(".branch-row").length > 1)
                $(this).closest(".branch-row").empty().remove();
        });
        $('.new_customer').on('click', function () {
            $("#customer_modal .add-label,.pasword-div,.is_vendor_div").removeClass('hidden');
            $('#Customer_ID').val('');
            $('#customer_password,#Customer_Email').val('');
            $('#Customer_Country_ID').val(country_id).attr('data-select-name', country_name).trigger('change');
            $('#Customer_City_ID').val(city_id).attr('data-select-name', city_name).trigger('change');
            $('.shipping-row:gt(0),.contact-row:gt(0),.branch-row:gt(0)').remove();
            $('#Currency_ID').val('<?php echo $default_currency['Currency_ID']; ?>').attr('data-select-name', '<?php echo $default_currency['Currency_Name'] . ' - ' . $default_currency['Currency_Code']; ?>').trigger('change');
            $('input[name=shipping_city_id],input[name=fastpax_city_id]').val('').trigger('change');
            build_select2($("input[name=shipping_city_id]"), "xhr/searchCity?show_country_name=1");
            build_select2($("input[name=fastpax_city_id]"), "xhr/searchFastpaxCities");
            $('select[name=shipping_type_id]').val('1').select2();
        });

        $("#Customer_Mobile").on("change", function () {
            $.ajax({
            url: "InventoryAjax/check_customer_phone",
            method: "GET",
            data: {Customer_Mobile: $("#Customer_Mobile").val()},
            dataType: 'json',
            complete: function (v1, v2) {
                    if (v2 == 'success') {
                        console.log('v1')
                        console.log(v1)
                        if(v1.responseJSON.error == 'error'){
                            $("#submit_customer").prop('disabled',false)
                            $("#check_phone").text('');

                        }
                        else{
                            $("#submit_customer").prop('disabled',true)
                            $("#check_phone").text(phone_uset_by_lang+v1.responseJSON.Customer_Name);

                        }
                    }
                }
            })
        })
        $("#submit_customer").on("click", function () {
            if (!$("#customer_form").valid())
                return;
            var contact_data = [], shipping_address = [],
                    shipping_address_ids = [], branches = [],
                    is_contact_empty = false, is_shipping_address_empty = false;
            $('.contact-row').each(function () {
                if (($(this).find("input[name=contact_name]").val() && !$(this).find("input[name=contact_position]").val()) || (!$(this).find("input[name=contact_name]").val() && $(this).find("input[name=contact_position]").val()))
                    is_contact_empty = true;
                if ($(this).find("input[name=contact_name]").val() && $(this).find("input[name=contact_position]").val()) {
                    contact_data.push({
                        contact_name: $(this).find("input[name=contact_name]").val(),
                        contact_position: $(this).find("input[name=contact_position]").val(),
                        contact_phone_number: $(this).find("input[name=contact_phone_number]").val(),
                        contact_note: $(this).find("input[name=contact_note]").val(),
                    });
                }
            });
            $('.branch-row').each(function () {
                if ($(this).find("input[name=customer_branch_name]").val()) {
                    branches.push({
                        customer_branch_name: $(this).find("input[name=customer_branch_name]").val(),
                    });
                }
            });
            $('.shipping-row').each(function () {
                var parent = $(this), not_empty_count = 0;//required-fields
                $('input.req', parent).each(function () {
                    if ($(this).val())
                        not_empty_count++;
                });
                if (not_empty_count != 0 && not_empty_count < 5)
                    is_shipping_address_empty = true;
                if ($("input[name=shipping_title]", parent).val() &&
                        $("input[name=shipping_city_id]", parent).val() &&
                        $("input[name=shipping_street]", parent).val() &&
                        $("input[name=shipping_building]", parent).val() &&
                        $("input[name=shipping_address]", parent).val()) {
                    shipping_address.push({
                        shipping_address_id: parent.attr('data-id'),
                        shipping_title: $("input[name=shipping_title]", parent).val(),
                        fastpax_city_id: $("input[name=fastpax_city_id]", parent).val(),
                        city_id: $("input[name=shipping_city_id]", parent).val(),
                        shipping_street: $("input[name=shipping_street]", parent).val(),
                        shipping_building: $("input[name=shipping_building]", parent).val(),
                        shipping_address: $("input[name=shipping_address]", parent).val(),
                        is_default: $('input[name=is_default_shipping]', parent).is(":checked") ? 1 : 0,
                    });
                    shipping_address_ids.push(parent.attr('data-id'));
                }
            });
            if (shipping_address.length > 0 && $('input[name=is_default_shipping]:checked').length == 0) {
                AddAlert("#customer_alert", "<?php echo CommonLanguage::getMessage('fill_def_ship_add'); ?>", "danger");
                return;
            }
            if (is_contact_empty) {
                AddAlert("#customer_alert", "<?php echo CommonLanguage::getMessage('required_fields_contact_info'); ?>", "danger");
                return;
            }
            if (is_shipping_address_empty) {
                AddAlert("#customer_alert", "<?php echo CommonLanguage::getMessage('required_fields_shipp_address_info'); ?>", "danger");
                return;
            }
            <?php echo smc_session()['user_details']['limit_customer_invoice'] == 1  ? "$('#sales_man_id').select2('val', [".smc_session()['user_details']['Employee_ID']."])" :''; ?>


            var Data = new FormData(document.getElementById("customer_form"));
            Data.append('shipping_address_ids', JSON.stringify(shipping_address_ids));
            Data.append('shipping_address', JSON.stringify(shipping_address));
            Data.append('contacts', JSON.stringify(contact_data));
            Data.append('branches', JSON.stringify(branches));
            Data.append('limit_exceed', $('#limit_exceed').is(":checked") ? 1 : 0);
            Data.append('Is_Vendor', $('#Is_Vendor').is(":checked") ? 1 : 0);
            $("#customer_loader").removeClass("hidden");
            $.ajax({
                url: "InventoryAjax/submitCustomer",
                method: "POST",
                data: Data,
                dataType: 'json',
                processData: false,
                contentType: false,
                complete: function (jqXHR, status) {
                    if (status == 'success') {
                        if (jqXHR.responseJSON[1] == "success") {
                            if ($('#Customer_ID').val() == 0)
                                RebuildDataTable($('.panel-heading .panel-title').html(), [0, 1, 2, 3, 4, 5, 6], 'InventoryAjax/CustomerListRequest', {salesman_id: $('#salesman').val(), status_id: $('#customer_status').val()});
                            hideModalAlert('customer_modal', 'customer_alert');
                        }
                        AddAlert("#customer_alert", jqXHR.responseJSON[0], jqXHR.responseJSON[1]);
                    }
                    $("#customer_loader").addClass('hidden')
                }
            });
        });
        $('#edit_customer').on('click', function () {
            $("#customer_modal .edit-label").removeClass('hidden');
            $(".pasword-div,.is_vendor_div").addClass('hidden');
            $('#Customer_ID').val($(this).attr('data-id'));
            CommonViewById('InventoryAjax/getCustomerDetails', {Customer_ID: $(this).attr('data-id')},
                    '#customer_loader', function (response) {
                        var customer_main = response['main'],
                                shipp_address = response['shipping_address'],
                                contacts = response['contacts'],
                                branches = response['branches'],
                                intolerances = response['intolerances'],
                                allergies = response['allergies'],
                                customer_user_data = response['customer_user_data'],
                                customer_sales_man = response['customer_sales_man'],
                                current_contact_row = $('#slide2 .contact-row:eq(0)').clone(),
                                current_shipping_row = $('#slide3 .shipping-row:eq(0)').clone(),
                                current_branch_row = $('#slide4 .branch-row:eq(0)').clone();
                        $('#Customer_Name').val(customer_main.Customer_Name);
                        $('#Customer_Mobile').val(customer_main.Customer_Mobile);
                        $('#Customer_Email').val(customer_main.Customer_Email);
                        $('#Customer_Address').val(customer_main.Customer_Address);
                        $('#Customer_Work_Phone').val(customer_main.Customer_Work_Phone);
                        $('#Customer_Phone').val(customer_main.Customer_Phone);
                        $('#customer_vat').val(customer_main.customer_vat);

                        $('#customer_additional_number').val(customer_main.customer_additional_number);
                        $('#customer_postal_code ').val(customer_main.customer_postal_code );
                        $('#customer_other_id').val(customer_main.customer_other_id);

                        $('#Customer_Lng').val(customer_main.Customer_Lng);
                        $('#Customer_Lat').val(customer_main.Customer_Lat);
                        $('#Customer_Company').val(customer_main.Customer_Company);
                        $('#Customer_CR').val(customer_main.Customer_CR);
                        $('#Customer_Limit_Balance').val(customer_main.Customer_Limit_Balance);
                        $('#Customer_Discount').val(customer_main.Customer_Discount);
                        $('#Customer_Code').val(customer_main.Customer_Code);
                        $('#customer_due_date_period').val(customer_main.customer_due_date_period);
                        $('#customer_user_name').val(customer_main.user_name);
                        $('#customer_note').val(customer_main.customer_note);

                        $('#Customer_Class_ID').val(customer_main.Class_ID).attr('data-select-name', customer_main.Class_Name).trigger('change');
                        $('#Currency_ID').val(customer_main.Customer_Currency).attr('data-select-name', customer_main.Currency_Name + ' - ' + customer_main.Currency_Symbol).trigger('change')
                        $('#Customer_Country_ID').val(customer_main.Customer_Country_ID).attr('data-select-name', customer_main.Country_Name).trigger('change')
                        $('#Customer_City_ID').val(customer_main.Customer_City_ID).attr('data-select-name', customer_main.Name).trigger('change')
                        $('#account_parent_id').val(customer_main.account_parent_id).attr('data-select-name', customer_main.account_parent_name).trigger('change');
                        $('#customer_group_id').val(customer_main.customer_group_id).attr('data-select-name', customer_main.group_name).trigger('change');
                        $('#other_pay_acc_id').val(customer_main.other_pay_acc_id).attr('data-select-name', customer_main.other_pay_acc_name).trigger('change');
                        $('#cust_class_id').val(customer_main.customer_class_id).attr('data-select-name', customer_main.cust_class_name).trigger('change');
                        $('#region_id').val(customer_main.region_id).attr('data-select-name', customer_main.region_name).trigger('change');
                        $('#branch_id').val(customer_main.branch_id).attr('data-select-name', customer_main.branch_name).trigger('change');

                        if (customer_main.stop_invoice == 1 && !$('#limit_exceed').is(":checked"))
                            $('#limit_exceed').trigger('click');

                        if (allergies.length > 0) {
                            $('#allergy_id').select2('val', [])
                            $('#allergy_id').data().select2.updateSelection(allergies);
                        }
                        if (intolerances.length > 0) {
                            $('#intolerance_id').select2('val', [])
                            $('#intolerance_id').data().select2.updateSelection(intolerances);
                        }
                        if (customer_sales_man.length > 0) {
                            $('#sales_man_id').select2('val', [])
                            $('#sales_man_id').data().select2.updateSelection(customer_sales_man);
                        }
                        if (customer_user_data.length > 0) {
                            $('#customer_user_id').select2('val', [])
                            $('#customer_user_id').data().select2.updateSelection(customer_user_data);
//                            build_select2($('#customer_user_id'), 'xhr/searchCustomerUsers?is_app=1', 1);

                        }
//                            if(customer_user_data != null){
//                                $('#customer_user_id').val(customer_user_data.User_ID).attr('data-select-name',customer_user_data.User_Full_Name).trigger('change');
//                            }

                        if (contacts.length > 0) {
                            $("#slide2 .contact-row").remove()
                            $.each(contacts, function (index, value) {
                                current_contact_row = current_contact_row == null ? $("#slide2 .contact-row:eq(0)").clone() : current_contact_row;
                                $('input[name=contact_name]', current_contact_row).val(contacts[index].contact_name);
                                $('input[name=contact_position]', current_contact_row).val(contacts[index].contact_position);
                                $('input[name=contact_phone_number]', current_contact_row).val(contacts[index].contact_phone_number);
                                $('input[name=contact_note]', current_contact_row).val(contacts[index].contact_note);
                                $("#slide2").append(current_contact_row);
                                current_contact_row = null
                            });
                        }
                        if (branches.length > 0) {
                            $("#slide4 .branch-row").remove()
                            $.each(branches, function (index, value) {
                                current_branch_row = current_branch_row == null ? $("#slide4 .branch-row:eq(0)").clone() : current_branch_row;
                                $('input[name=customer_branch_name]', current_branch_row).val(branches[index].customer_branch_name);
                                $("#slide4").append(current_branch_row);
                                current_branch_row = null
                            });
                        }
                        if (shipp_address.length > 0) {
                            $("#slide3 .shipping-row").remove();
                            $.each(shipp_address, function (index, value) {
                                current_shipping_row = current_shipping_row == null ? $("#slide3 .shipping-row:eq(0)").clone() : current_shipping_row;
                                current_shipping_row.attr('data-id', shipp_address[index].shipping_address_id);
                                $('input[name=shipping_title]', current_shipping_row).val(shipp_address[index].shipping_title);
                                $('input[name=shipping_building]', current_shipping_row).val(shipp_address[index].shipping_building);
                                $('input[name=shipping_street]', current_shipping_row).val(shipp_address[index].shipping_street);
                                $('input[name=shipping_address]', current_shipping_row).val(shipp_address[index].shipping_address);
                                $("#slide3").append(current_shipping_row);
                                $(".select2-container", current_shipping_row).remove();

                                $("input.shipping_city_id", current_shipping_row).val(shipp_address[index].city_id).attr("data-select-name", shipp_address[index].city_name + ' - ' + shipp_address[index].Country_Name);
                                build_select2($("input.shipping_city_id", current_shipping_row), "xhr/searchCity?show_country_name=1");

                                $("input.fastpax_city_id", current_shipping_row).val(shipp_address[index].fastpax_city_id).attr("data-select-name", shipp_address[index].fastpax_city_name + ' - ' + shipp_address[index].district_name);
                                build_select2($("input.fastpax_city_id", current_shipping_row), "xhr/searchFastpaxCities");

                                $('input[name=is_default_shipping]', current_shipping_row).prop('checked', shipp_address[index].is_default == 1 ? true : false);
                                $('select.shipping_type_id', current_shipping_row).val(shipp_address[index].shipping_type_id).select2();
                                $(".select2-container", current_shipping_row).show();

                                current_shipping_row = null
                            });
                        }
                    });
        });
    });
</script>