<style>
    #supp_doc_div{
        width:227px;
        color:#fff;
    }
    #supp_doc_div > input{
        position:absolute;
        cursor:pointer;
        left:0px;
        top:0px;
        width:100%;
        height:100%;
        opacity:0.01;
    }

</style>
<div class="modal fade" role="dialog" id="import_supp_modal" aria-hidden="true" style="display: none;" data-backdrop="static">
    <div class="modal-dialog" style="max-width:500px;">
        <div class="modal-content">
            <?php echo Forms::ModalLoader("import_customers_loader"); ?>
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="pg-close fs-20"></i>
                </button>
                <h5 class="bold text-left" id="myModalLabel"><?php print CommonLanguage::getMessage('Import_Data'); ?></h5>
            </div><!-- end of modal-header -->
            <div class="modal-body">
                <form method="post" enctype="multipart/form-data" id="import_supp_form" onsubmit="return false">
                    <div class="row">
                        <div  id="import_supplier_alert" class=" m-t-10" ></div>
                    </div>
                    <div class="row form-group" >
                        <div  class="col-md-12">
                            <a style="color: #652d90;" href="<?php echo SITE_LINK; ?>uploads/Templatexlsx/import_supplier.xlsx" id="import_supplier_template"><i class="fa fa-download"></i> <?php print CommonLanguage::getMessage('Download_Template'); ?></a>
                        </div>
                    </div>
                    <div class="row form-group" >   
                        <div class="col-md-12" >    
                            <label><?php print CommonLanguage::getMessage('Upload_Downloaded_File'); ?> </label>
                            <div style="color:white !important;" class="btn bg-primary" id="supp_doc_div" style="cursor:pointer;"><i class="fa fa-upload"></i> <?php print CommonLanguage::getMessage('Upload'); ?> <input type="file" onchange="window.NewCurrentFileUploadadd = event.target.files;ValidateForNewFile(this, '#supp_doc_div')" name="import_supplier_document" value="Upload"></div>
                        </div>
                    </div>
                    <div class="row form-group" >   
                        <div class="col-md-6">
                            <label><?php echo CommonLanguage::getMessage('opening_balance_date_supp') ?><span class="red-star">*</span></label>
                            <input type="text" class="form-control" id="opening_balance_date_supp" name='opening_balance_date_supp'/>
                        </div>
                        <div class="col-md-6">
                            <label><?php echo CommonLanguage::getMessage('closing_account') ?> <span class="red-star">*</span></label>
                            <input type="hidden" id="close_opening_jv_acc_supp" class="full-width"  name="close_opening_jv_acc_supp"/>
                        </div> 
                    </div>
                    <div class="row form-group" >   
                        <div hidden class="col-md-6">
                            <label><?php echo CommonLanguage::getMessage('merge_to_jv_supp') ?><i class="fa fa-info-circle" title="<?php echo CommonLanguage::getMessage('merge_to_jv_supp_lang')?>"></i></label>
                            <input type="number" class="form-control" id="merge_to_jv_supp" name='merge_to_jv_supp'/>
                        </div>
                        <div class="col-md-6" style="display: grid;">
                            <label><?php echo CommonLanguage::getMessage('only_opening_balance_supp') ?> <i class="fa fa-info-circle" title="<?php echo CommonLanguage::getMessage('only_opening_lang')?>"></i></label>
                            <input type="checkbox" id="only_opening_balance_supp"  name="only_opening_balance_supp"/>
                        </div>

                    </div>

                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal" id="CancelUpload"><?php print CommonLanguage::getMessage('Close'); ?></button>
                <button style="color:white !important;" class="btn bg-primary text-white"  id="save_supp_import"><i class="fa fa-check"></i> <?php print CommonLanguage::getMessage('Save'); ?></button>
            </div>
        </div>
    </div>
</div>
<script>
    var Submitted_Successfully = <?php echo json_encode(CommonLanguage::getMessage('Submitted_Successfully')); ?>,
     Required_Fields = <?php echo json_encode(CommonLanguage::getMessage('Required_Fields')); ?>,
            alert_excel = <?php echo json_encode(CommonLanguage::getMessage('excel_type_err')); ?>;
    $(function () {
        new Switchery($("#only_opening_balance_supp").get(0), {color: $.Pages.getColor("primary")});

        build_select2($("#close_opening_jv_acc_supp"), "xhr/accIDSearch?type=Payment")
        $("#opening_balance_date_supp").datepicker({autoclose: true});
        $("#CancelUpload").on("click", function () {
            $("#supp_doc_div >.fa-check,#NewFilewarning2").hide()
            $("#supp_doc_div >input").val('')
        })
        $("#import_supp_modal").on("shown.bs.modal", function () {
            if ($("#only_opening_balance_supp").is(":checked"))
                $("#only_opening_balance_supp").trigger("click")
        })
        $("#merge_to_jv_supp").on("input", function () {
            if ($(this).val() != '') {
                $("#opening_balance_date_supp").val("").prop("disabled", true)
            } else {
                $("#opening_balance_date_supp").prop("disabled", true)

            }
        })

    })

    $("#save_supp_import").on("click", function () {
        $("#import_supplier_alert").addClass("hidden");
        var number_pattern = /^\d+(\.\d+)?$/;
        if ($("#close_opening_jv_acc_supp").val() == "" || ($("#opening_balance_date_supp").val() == "" && $("#merge_to_jv_supp").val() == "") || ($("#merge_to_jv_supp").val() != "" && number_pattern.test($("#merge_to_jv_supp").val()))) {
            $("#import_supplier_alert").attr("class", "alert alert-danger").html(Required_Fields)
            return;
        }
        //
        var FileName = $.trim($("#supp_doc_div > input").val());
        var FileType = FileName.substring(FileName.lastIndexOf(".")).toLowerCase();
        if (FileType == ".xlsx" || FileType == ".xls") {
            $("#import_supplier_alert").addClass("hidden");
            var Data = new FormData(document.getElementById("import_supp_form"));
            $("#save_supp_import").prop('disabled', true);
            $("#import_customers_loader").removeClass('hidden');
            $.ajax({
                url: 'InventoryAjax/import_supplier_excel',
                data: Data,
                method: "POST",
                processData: false,
                contentType: false,
                dataType: "json",
                complete: function (v1, v2) {
                    if (v2 == 'success') {
                        if (v1.responseJSON.is_error == '0') {  //if ok no error
                            $('body').append(v1.responseJSON.html);
                            $("#import_supplier_alert").attr('class', 'alert alert-success').html(Submitted_Successfully);
                        } else
                            $("#import_supplier_alert").attr('class', 'alert alert-danger').html(v1.responseJSON.html);
                    } else
                        $("#import_supplier_alert").attr('class', 'alert alert-danger').html(v2 + ': ' + v1.responseText);
                    //
                    setTimeout(function () {
                        $("#import_supplier_alert").addClass("hidden");
                    }, 6000);
                    $("#supp_doc_div >input").val('');
                    $("#supp_doc_div >.fa-check").hide();
                    $("#save_supp_import").prop('disabled', false);
                    $("#import_customers_loader").addClass('hidden');
                }
            });
        } else {
            $("#import_supplier_alert").attr('class', 'alert alert-danger').html(alert_excel)
            setTimeout(function () {
                $("#import_supplier_alert").addClass("hidden");
            }, 2000);
        }
    });
    // ValidateFunction for file
    function ValidateForNewFile(input, inputID) {
        $(inputID + " .fa-check").remove();
        if (input.files && input.files[0]) {
            //            var reader = new FileReader();
            //            reader.onload = function () {
            $(inputID + " > .fa-times").hide();
            $(inputID).append(" <span class='fa fa-check'></span>");
            //            };
            //            reader.readAsDataURL(input.files[0]);
            return true;
        }
        return false;
    }

</script>