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

</style>
<div class="modal fade" role="dialog" id="UploadExcelFile" 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("UploadProductExcel"); ?>
            <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="Upload_Excel" onsubmit="return false">
                    <div class="row">
                        <div class="  m-t-10"  id="alert_xls" ></div>
                    </div>
                    <div class="row form-group" >
                        <div class="col-md-12">
                            <a style="color:#652d90" href="<?php echo SITE_LINK; ?>uploads/Templatexlsx/Import-Products.xlsx" id="TemplateReference"><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-complete-darker" id="InputFile" 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, '#InputFile')" name="Document_File_Name" value="Upload"></div>
                        </div>
                    </div>
                    <div class="row form-group" >   
                        <div class="col-md-12">
                            <label><?php echo CommonLanguage::getMessage('max_discount') ?> %</label>
                            <input type="text" class="form-control" id="max_discount_per" name='max_discount_per'/>
                        </div>
                    </div>
                    <div class="row form-group" >   
                        <div class="col-md-6">
                            <label><?php echo CommonLanguage::getMessage('upload_qty_cost_only') ?> </label>
                            <input type="checkbox" id="upload_qty_cost_only" value="1" name="upload_qty_cost_only"/>
                        </div> 
                        <div class="col-md-6">
                            <label><?php echo CommonLanguage::getMessage('auto_generate_code') ?> </label>
                            <input type="checkbox" id="auto_generate_code" value="1" name="auto_generate_code"/>
                        </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-complete-darker text-white"  id="SaveExcelSheet"><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')); ?>,
        alert_excel = <?php echo json_encode(CommonLanguage::getMessage('excel_type_err')); ?>;
        $(function () {
            new Switchery($("#upload_qty_cost_only").get(0), {color: $.Pages.getColor("complete-darker")});
            new Switchery($("#auto_generate_code").get(0), {color: $.Pages.getColor("complete-darker")});
            $("#CancelUpload").on("click", function () {
                $("#InputFile >.fa-check,#NewFilewarning2").hide()
                $("#InputFile >input").val('')
            })
        })
 
    $("#SaveExcelSheet").on("click", function () {
        $("#ValidationAlert").remove();
        $('#max_discount_per').parent().removeClass('has-error');
        //
        if ($('#max_discount_per').val() != '' && (isNaN($('#max_discount_per').val()) || $('#max_discount_per').val() < 0 || $('#max_discount_per').val() > 100)) {
            $('#max_discount_per').parent().addClass('has-error').end().focus();
            return;
        }
        //
        var FileName = $.trim($("#InputFile > input").val());
        var FileType = FileName.substring(FileName.lastIndexOf(".")).toLowerCase();
        if (FileType == ".xlsx" || FileType == ".xls") {
            $("#alert_xls").addClass("hidden");
            var Data = new FormData(document.getElementById("Upload_Excel"));
            $("#SaveExcelSheet").prop('disabled', true);
            $("#UploadProductExcel").removeClass('hidden');
            $.ajax({
                url: 'StockAjax/importProducts',
                data: Data,
                method: "POST",
                processData: false,
                contentType: false,
                dataType: "json",
                complete: function (v1, v2) {
                    if (v2 == 'success') {
                        console.log(v1.responseJSON);
                        if (v1.responseJSON.is_error == '0') {  //if ok no error
                            $('body').append(v1.responseJSON.html);
                            $("#alert_xls").attr('class', 'alert alert-success').html(Submitted_Successfully);
                        } else
                            $("#alert_xls").attr('class', 'alert alert-danger').html(v1.responseJSON.html);
                    } else
                        $("#alert_xls").attr('class', 'alert alert-danger').html(v2 + ': ' + v1.responseText);
                    //
                    setTimeout(function () {
                        $("#alert_xls").addClass("hidden");
                    }, 6000);
                    $("#InputFile >input").val('');
                    $("#InputFile >.fa-check").hide();
                    $("#SaveExcelSheet").prop('disabled', false);
                    $("#UploadProductExcel").addClass('hidden');
                }
            });
        } else {
            $("#alert_xls").attr('class', 'alert alert-danger').html(alert_excel)
            setTimeout(function () {
                $("#alert_xls").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>