<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/webpage/include/taglib.jsp"%>
<html>
<head>
    <title>导入Excel</title>
    <meta name="decorator" content="ani"/>
    <script>
        function importExcel(url, fn) {
            var importForm =$("#importForm")[0];
            var file = $("#uploadFile").val();
            var itemBatch = $("#itemBatch").val();
            var suppliesBatch = $("#suppliesBatch").val();
            if(file.length === 0){
                layer.msg("请选择文件",{icon: 3,offset:['40%','40%']});
                return false;
            }
            if(itemBatch == null || itemBatch === ''){
                layer.msg("请输入项目批次",{icon: 3,offset:['40%','40%']});
                return false;
            }
            if(suppliesBatch == null || suppliesBatch === ''){
                layer.msg("请选择物料批次",{icon: 3,offset:['40%','40%']});
                return false;
            }
            jp.loading('  正在导入，请稍等...');
            // jp.uploadFile(importForm, url,function (data) {
            //    fn(data);
            // })
                var data = new FormData(importForm);
                // data.append("CustomField", "This is some extra data, testing");//如果要添加参数
                $.ajax({
                    type: "POST",
                    enctype: 'multipart/form-data',
                    url: url,
                    data: data,
                    processData: false, //prevent jQuery from automatically transforming the data into a query string
                    contentType: false,
                    cache: false,
                    timeout: 600000,
                    success: function (result) {
                        if(result.success && result.errorCode === '-1'){
                            jp.confirm('该项目已存在，确定覆盖嘛？', function(){
                                jp.loading('  正在导入，请稍等...');
                                $.ajax({
                                    type: "POST",
                                    enctype: 'multipart/form-data',
                                    url: '${ctx}/managementcenter/materialinfo/import',
                                    data: data,
                                    processData: false, //prevent jQuery from automatically transforming the data into a query string
                                    contentType: false,
                                    cache: false,
                                    timeout: 600000,
                                    success: function (result) {
                                        fn(result);
                                    }
                                })
                            });
                        }else if((result.success && result.errorCode === '0')){
                                   fn(result)
                        }
                        // fn(result);
                    },
                    error:function(xhr, textStatus){
                        if(xhr.status == 0){
                            jp.info("连接失败，请检查网络!")
                        }else if(xhr.status == 404){
                            var errDetail ="<font color='red'>404,请求地址不存在！</font>";
                            top.layer.alert(errDetail , {
                                icon: 2,
                                area:['auto','auto'],
                                title:"请求出错"
                            })
                        }else if(xhr.status && xhr.responseText){
                            var errDetail ="<font color='red'>"+ xhr.responseText.replace(/[\r\n]/g,"<br>").replace(/[\r]/g,"<br>").replace(/[\n]/g,"<br>")+"</font>";
                            top.layer.alert(errDetail , {
                                icon: 2,
                                area:['80%','70%'],
                                title:xhr.status+"错误"
                            })
                        }else{
                            var errDetail =xhr.responseText=="<font color='red'>未知错误!</font>";
                            top.layer.alert(errDetail , {
                                icon: 2,
                                area:['auto','auto'],
                                title:"真悲剧，后台抛出异常了"
                            })
                        }

                    }
                })
        }
    </script>
</head>
<body class="bg-white">
    <div class="wrapper wrapper-content">
        <div id="importBox" >
            <form id="importForm" action="${url}" method="post" enctype="multipart/form-data"
                  style="padding-left:20px;text-align:center;" onsubmit="loading('正在导入，请稍等...');">
                <div>
                    <label class="label-item single-overflow pull-left" title="选择文件：">选择文件：</label>
                <input id="uploadFile" name="file" type="file" class="form-control" style="width:300px"/>
                </div>
                <div style="margin-top: 4px;">
                    <label class="label-item single-overflow pull-left" title="项目批次：">项目批次：</label>
                    <input id="itemBatch" name="itemBatch" type="text" class="form-control" style="width:300px"/>
                </div>
                <div style="margin-top: 4px;">
                    <label class="label-item single-overflow pull-left" title="物料批次：">物料批次：</label>
                <select name="batch"  class="form-control   simple-select" id="suppliesBatch" style="width:300px"/>
                    <c:forEach var="item" items="${batch}">
                        <option value="${item.suppliesBatch}" label="${item.suppliesBatch}"></option>
                    </c:forEach>
                </select>
                </div>　

            </form>
        </div>
    </div>
</body>
</html>
