layui.extend({
    common: 'js/common'
}).use(['jquery', 'layer', 'common', 'upload', 'element', 'form'], function () {
    //  弹窗插件
    var layer = layui.layer;
    //  Jquery插件
    var $ = layui.jquery;

    //  引入上传
    var upload = layui.upload;

    var element = layui.element;

    var form = layui.form;

    // 使用ajax的xhr属性
    var xhrOnProgress = function (fun) {
        xhrOnProgress.onprogress = fun; //绑定监听
        //使用闭包实现监听绑
        return function () {
            //通过$.ajaxSettings.xhr();获得XMLHttpRequest对象
            var xhr = $.ajaxSettings.xhr();
            //判断监听函数是否为函数
            if (typeof xhrOnProgress.onprogress !== 'function')
                return xhr;
            //如果有监听函数并且xhr对象支持绑定时就把监听函数绑定上去
            if (xhrOnProgress.onprogress && xhr.upload) {
                xhr.upload.onprogress = xhrOnProgress.onprogress;
            }
            return xhr;
        }
    };

    //普通图片上传
    var uploadIns = upload.render({
        elem: '#import'
        , multiple: false
        , accept: 'file'
        ,exts: 'jpg|png|jpeg'
        , auto: false
        , bindAction: '#start_upload'
        , drag: true
        // , acceptMime: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel'
        , url: '/common/file/uploadImageZip'
        , xhr: xhrOnProgress
        , progress: function (percent) {//上传进度回调
            element.progress('upload_progress', percent + '%')//设置页面进度条
        }
        , data: {
            flag: function () {
                return $('#flag').val();
            },
            isUpdate: function () {
                return $('#isUpdate').val();
            }
        }
        , choose: function (obj) {
            obj.preview(function (index, file, result) {
                $('#import').hide();
                $('#file_name').html(file.name).show();
            });
        }
        , before: function (obj) {
            $('#delete_file').remove();
        }
        , done: function (res) {
            if (res != null && res.code == '200' && res.list == null) {
                layer.msg("导入成功", {
                    time: 500
                }, function () {
                    //获取当前弹窗出层的名字
                    var index = parent.layer.getFrameIndex(window.name);
                    //关闭弹窗层
                    parent.layer.close(index);
                    // 父页面刷新
                    parent.location.reload();
                });
            } else if (res.list != null) {
                layer.msg("部分数据导入失败，请查看生成的新的错误报告表格", {
                    time: 2000
                }, function () {
                    var array = res.list;
                    var flag = $('#flag').val();
                    var data = JSON.stringify(array);
                    var isUpdate = $('#isUpdate').val();

                    // window.location.href = "/common/excel/exportErrorImportData?data="+encodeURIComponent(data)+"&flag="+flag;
                    document.write("<form action='/common/excel/exportErrorImportData' method='post' target='_blank' name='form' style='display:none'>");
                    document.write("<input type='hidden' name='flag' value='" + flag + "'>");
                    document.write("<input type='hidden' name='data' value='" + data + "'>");
                    document.write("<input type='hidden' name='isUpdate' value='" + isUpdate + "'>");
                    document.write("</form>");
                    document.form.submit();

                    //获取当前弹窗出层的名字
                    var index = parent.layer.getFrameIndex(window.name);
                    //关闭弹窗层
                    parent.layer.close(index);
                    // 父页面刷新
                    parent.location.reload();


                });
            }
        }
        , error: function (res) {
            //关闭加载动画
            $('#delete_file').show();
            layer.msg("导入失败");
        }
    });

    //取消按钮
    $('#cancel_upload').on("click", function () {
        //获取当前弹窗出层的名字
        var index = parent.layer.getFrameIndex(window.name);
        //关闭弹窗层
        parent.layer.close(index);
        parent.location.reload();
    });

    //取消按钮
    $('#delete_file').on("click", function () {
        $('#import').show();
        $('#file_name').html('').show();
        uploadIns.config.elem.next()[0].value = '';
    });

    $('#excel_download').on("click", function () {
        var flag = $('#flag').val();
        window.location.href = "/common/excel/exportExcelTemplet/" + flag;
    });

});
