<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>备件信息</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <meta name="decorator" content="base"/>
    <link rel="stylesheet" type="text/css" href="${ctxPlugin}/static/css/reset.css"/>
    <link rel="stylesheet" href="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/dist/viewer.css" media="all">
    <style>
        .layui-form-selected dl {
            height: 200px;
        }
        html {
            background-color: #fff;
        }
        .load-icon{background: url('${ctxPlugin}/static/img/shili_img.png') center center no-repeat; width: 12px; height: 12px; display: inline-block; border-radius: 50%;
            vertical-align: text-bottom;transform: translate(0px, -1px);margin-right: 2px;}
    </style>
</head>
<body>
<div class="layui-card md-0 layui-form" lay-filter="layuiadmin-form-admin" id="LAY_app">
    <div class="layui-card-body">
            <div class="layui-form-item layui-col-xs12 ">
                <input hidden name="id" value="${warehouse.id}">

                <div class="layui-col-xs6 mb-5">
                    <label class="layui-form-label"><i class="mark ">*</i>仓库名称：</label>
                    <div class="layui-input-block">
                        <input type="text" id="name" name="name" class="layui-input " lay-verify="required"
                               value="${warehouse.name}">
                    </div>
                </div>

                <div class="layui-col-xs6 mb-5">
                    <label class="layui-form-label"><i class="mark ">*</i>外部编码：</label>
                    <div class="layui-input-block">
                        <input type="text" id="qqCode" name="qqCode" class="layui-input " lay-verify="required"
                               value="${warehouse.qqCode}">
                    </div>
                </div>

                <input hidden name="ascriptionName" value="${warehouse.ascriptionName}">


                <div class="layui-col-xs6 mb-5">
                    <label class="layui-form-label"><i class="mark ">*</i>接货人：</label>
                    <div class="layui-input-block">
                        <input type="text" name="personInCharge" id="personInCharge" class="layui-input "
                               lay-verify="required" value="${warehouse.personInCharge}">
                    </div>
                </div>
                <div class="layui-col-xs6 mb-5">
                    <label class="layui-form-label"><i class="mark ">*</i>联系方式：</label>
                    <div class="layui-input-block">
                        <input type="text" id="mobile" name="mobile" class="layui-input " lay-verify="telephone"
                               value="${warehouse.mobile}">
                    </div>
                </div>

                <div class="layui-col-xs12 mb-10">
                    <label class="layui-form-label"><i class="red">*</i>仓库地址：</label>
                    <div class="layui-input-block ml-130">
                        <div class="layui-col-xs4 pr-20">
                            <select class="province" id="province" name="province"
                                    lay-filter="province" lay-search lay-verify="required">
                                <option value="">请选择省</option>
                                <c:forEach items="${province}" var="pro">
                                    <option value="${pro.columns.ProvinceName}" title="${pro.columns.ProvinceID}"
                                            <c:if test="${warehouse.province == pro.columns.ProvinceName}">selected</c:if>>${pro.columns.ProvinceName}</option>
                                </c:forEach>
                            </select>
                        </div>
                        <div class="layui-col-xs4 pr-20">
                            <select class="city" name="city" id="city" lay-filter="city" lay-search
                                    lay-verify="required">
                                <option value="">请选择市</option>
                                <c:forEach items="${city}" var="cityList">
                                    <option value="${cityList.columns.CityName}" title="${cityList.columns.CityID}"
                                            <c:if test="${warehouse.city==cityList.columns.CityName}">selected</c:if>>${cityList.columns.CityName}</option>
                                </c:forEach>
                            </select>
                        </div>
                        <div class="layui-col-xs4">
                            <select class="area" name="area" id="area" lay-filter="area" lay-search
                                    lay-verify="required">
                                <option value="">请选择县/区</option>
                                <c:forEach items="${area}" var="areaList">
                                    <option value="${areaList.columns.DistrictName}"
                                            <c:if test="${warehouse.area==areaList.columns.DistrictName}">selected</c:if>>${areaList.columns.DistrictName}</option>
                                </c:forEach>
                            </select>
                        </div>
                        <div class="layui-col-xs12 mt-10">
                            <input type="text" id="detailAddress" name="address"
                                   class="layui-input layui-col-lg2"
                                   value="${warehouse.address}" lay-verify="required">
                        </div>
                    </div>
                </div>
                <c:if test="${userType eq '6' || userType eq '7'}">
                <div class="layui-col-xs12 layui-form-item  mb-10">
                    <label class="layui-form-label"><i class="mark ">*</i>
                        授权委托书：<br>
                        <%--<p style="margin-right: 25px"><a style="color:#D9B589;"  href="https://sky-energy.oss-cn-beijing.aliyuncs.com/energy_img/prohibitDeletion/%E4%BB%A3%E7%90%86%E5%95%86%E6%94%B6%E8%B4%A7%E4%BA%BA%E6%8E%88%E6%9D%83%E5%A7%94%E6%89%98%E4%B9%A6.docx" style="line-height: 28px; font-size: 12px; margin-left: 10px; color: #0099ff">模板下载</a></p>--%>
                    </label>
                    <div class="layui-input-block">
                        <div id="imgsDiv">
                            <c:if test="${not empty warehouse.imgs}">
                                <div class="bj-img imgSpan">
                                    <span class="layui-icon-close layui-icon" onclick="delImg(this,'imgs')"></span>
                                    <img src="${commonStaticImgPath}${warehouse.imgs}" height="700" width="1200">
                                    <input type="hidden" class="imgPicker" value="${warehouse.imgs}">
                                </div>
                            </c:if>
                        </div>

                            <div class="upload-container" id="uploader" <c:if test="${not empty warehouse.imgs}">hidden</c:if>>
                                <img src="" alt="img placeholder" id="imgsAdd">
                            </div>
                    </div>
                </div>
                </c:if>
                <div class="layui-col-xs12 layui-form-item  mb-10">
                    <label class="layui-form-label">资质材料：
                        <%--<br/><a href="${commonStaticImgPath}/prohibit_delete/示例图/不动产登记证明示例.png" style="font-size: 12px;color: red;margin-right: 5px;"><i class="load-icon"></i>下载示例图</a>--%>
                    </label>
                    <div class="layui-input-block">
                        <div id="qualificationsImgsDev">
                            <c:if test="${not empty warehouse.qualificationsImgs}">
                                <c:forEach var="quaim" items="${quaimgs}">
                                <div class="bj-img imgSpan">
                                    <span class="layui-icon-close layui-icon" onclick="delImg(this,'uploaderQuali')"></span>
                                    <img src="${commonStaticImgPath}${quaim}" height="700" width="1200">
                                    <input type="hidden" class="quaImgPicker" value="${quaim}">
                                </div>
                                </c:forEach>
                            </c:if>
                        </div>

                        <div class="upload-container" id="uploaderQuali" <c:if test="${quaimgsCount*1 >=5}">hidden</c:if>>
                            <img src="" alt="img placeholder" >
                        </div>
                    </div>
                </div>

                <div class="layui-col-xs12 layui-form-item  mb-5">
                    <label class="layui-form-label">备注：</label>
                    <div class="layui-input-block">
                        <textarea name="remarks" id="remarks" placeholder="" class="layui-textarea" >${warehouse.remarks}</textarea>
                    </div>
                </div>

            </div>
    </div>
    <div class="operate-footer">
        <button class="layui-btn" type="button" lay-submit="" lay-filter="component-form-demo1" id="tijiaoBtn">保存</button>
        <button class="layui-btn layui-btn-primary" type="button" onclick="cancels()">取消</button>
    </div>
</div>


<script src="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/layui/layui.js"></script>
<script src="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/dist/viewer.js"></script>
<script>
    var viewer = new Viewer(document.getElementById('LAY_app'));
    var imgsCount;
    var quaimgsCount;
    var traderHtml = "";
    var supperHtml = "";
    var myreg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
    layui.use(['form', 'jquery', 'laydate', 'upload'], function () {
        $ = layui.$
            , element = layui.element
            , form = layui.form
            , laydate = layui.laydate;
        var upload = layui.upload;
        imgsCount = '${imgsCount}';
        quaimgsCount = '${quaimgsCount}'


        form.on('select(type)', function (data) {
            var type = data.value
            if ('2' == type) {
                $("#ascriptionId").removeAttr("disabled");
                $("#ascriptionId").empty();
                $("#ascriptionId").append(traderHtml);
                form.render('select');
            } else if ('3' == type) {
                $("#ascriptionId").removeAttr("disabled");
                $("#ascriptionId").empty();
                $("#ascriptionId").append(supperHtml);
                form.render('select');
            } else {
                $("#ascriptionId").empty();
                $("#ascriptionId").attr("disabled", "disabled");
                form.render('select');
            }
        });

        form.verify({
            ascriptionId: function (value, item) {
                var type = $("#type").val();
                if (type == '2' || type == '3') {
                    if (!$.trim(value)) {
                        return "归属名称不能为空！";
                    }
                    $("input[name='ascriptionName']").val($("#ascriptionId").find("option:selected").text());
                }

            },
            telephone: function (value, item) {
                if (!$.trim(value)) {
                    return "手机号码不能为空,请输入手机号码后保存！";
                }
                if (!myreg.test($.trim(value))) {
                    return "请输入正确的手机号码后保存！";
                }
            },
        })

        form.on('select(province)', function (data) {
            var province = $(data.elem).find("option:selected").attr("title");
            $("#city").empty();
            $("#detailAddress").val("");
            if (isBlank(province)) {
                $("#city").append("<option value=''>请选择市</option>");
                return "";
            }
            $.ajax({
                type: "POST",
                url: "${ctx}/main/redirect/getCitysByProId",
                data: {
                    province: province
                },
                success: function (result) {
                    if (result == "205") {
                        layer.msg("暂无数据!")
                        return
                    }
                    var html = "";
                    for (var i = 0; i < result.length; i++) {
                        var city = result[i];
                        html = html + '<option value="' + city.columns.CityName + '" title="' + city.columns.CityID + '">' + city.columns.CityName + '</option>';
                    }
                    $("#city").append(html);
                    form.render('select');
                },
                complete: function () {
                    var city = $("#city").find("option:selected").attr("title");
                    $("#area").empty()
                    if (isBlank(city)) {
                        $("#area").append("<option value=''>请选择县/区</option>");
                        return "";
                    }
                    $.ajax({
                        type: "POST",
                        url: "${ctx}/main/redirect/getAreaByCityId",
                        data: {
                            city: city
                        },
                        success: function (result) {
                            if (result == "205") {
                                layer.msg("暂无数据!")
                                return
                            }
                            var html = "";
                            for (var i = 0; i < result.length; i++) {
                                var area = result[i];
                                html = html + '<option value="' + area.columns.DistrictName + '" >' + area.columns.DistrictName + '</option>';
                            }
                            $("#area").append(html);
                            form.render('select');
                        }
                    });
                }

            });

        });

        form.on('select(city)', function (data) {
            var city = $(data.elem).find("option:selected").attr("title");
            $("#area").empty();
            $("#detailAddress").val("");
            if (isBlank(city)) {
                $("#area").append("<option value=''>请选择县/区</option>");
                return "";
            }
            $.ajax({
                type: "POST",
                url: "${ctx}/main/redirect/getAreaByCityId",
                data: {
                    city: city
                },
                success: function (result) {
                    if (result == "205") {
                        layer.msg("暂无数据!")
                        return
                    }
                    var html = "";
                    for (var i = 0; i < result.length; i++) {
                        var area = result[i];
                        html = html + '<option value="' + area.columns.DistrictName + '" >' + area.columns.DistrictName + '</option>';
                    }
                    $("#area").append(html);
                    form.render('select');
                }
            });
        });
        //执行实例
        var uploadInst = upload.render({
            elem: '#uploader' //绑定元素
            , url: '${ctx}/common/uploadFile' //上传接口
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {

                });
            }
            , done: function (res) {
                console.log(res.path);
                //上传完毕回调
                imgsCount++;
                if (imgsCount >= 1) {
                    $("#uploader").hide();
                } else {
                    $("#uploader").show();
                }
                addImg(res.path,"imgs","imgsDiv","imgPicker");
            }
            , error: function () {
                //请求异常回调
            }
        });

        upload.render({
            elem: '#uploaderQuali' //绑定元素
            , url: '${ctx}/common/uploadFile' //上传接口
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {

                });
            }
            , done: function (res) {
                console.log(res.path);
                //上传完毕回调
                quaimgsCount++;
                if (quaimgsCount >= 5) {
                    $("#uploaderQuali").hide();
                } else {
                    $("#uploaderQuali").show();
                }
                addImg(res.path,"quaimgs","qualificationsImgsDev","quaImgPicker");
            }
            , error: function () {
                //请求异常回调
            }
        });



        var ckeckFi = false;
        form.on('submit(component-form-demo1)', function (data) {
            $("#tijiaoBtn").attr("disabled", "true"); //改变提交按钮上的文字并将按钮设置为不可点击
            if (ckeckFi) {
                return
            }
            var url = "${ctx}/warehouseSetting/addWarehouse";
            var imgs = "";
<c:if test="${userType eq '6' || userType eq '7'}">
            url = "${ctx}/apply/warehouseApply/saveWarehouseApplyFromWareEdit";
            var imgs = "";
            $(".imgPicker").each(function () {
                if (isBlank(imgs)) {
                    imgs = $(this).val();
                } else {
                    imgs = imgs + "," + $(this).val();
                }
            });
            if(isBlank(imgs)){
                layer.msg('授权委托书资质材料不能为空！');
                $("#tijiaoBtn").removeAttr("disabled");
                return false;
            }
            </c:if>
            var quaimgs = "";
            $(".quaImgPicker").each(function () {
                if (isBlank(quaimgs)) {
                    quaimgs = $(this).val();
                } else {
                    quaimgs = quaimgs + "," + $(this).val();
                }
            });
            /**
             * 2024-03-15 资质材料可以为空
             * @type {string}
             */
            // if(isBlank(quaimgs)){
            //     layer.msg('资质材料不能为空！');
            //     $("#tijiaoBtn").removeAttr("disabled");
            //     return false;
            // }


            var articleFrom = data.field;
            articleFrom.imgs = imgs;
            articleFrom.qualificationsImgs = quaimgs;
            ckeckFi = true;

            $.ajax({
                type: "POST",
                url: url,
                data: articleFrom,
                success: function (data) {
                    console.log(data)
                    if (data.code == '200') {
                        layer.msg('提交成功！', {icon: 1, time: 1000}, function () {
                            parent.layer.closeAll();
                            parent.window.reload();
                        });
                    } else if (data.code == '501') {
                        layer.msg(data.errMsg, {icon: 2}, function () {
                            ckeckFi = false;
                            $("#tijiaoBtn").removeAttr("disabled");
                        });
                    } else {
                        layer.msg("保存失败", {icon: 2}, function () {
                            ckeckFi = false;
                            $("#tijiaoBtn").removeAttr("disabled");
                        });
                    }
                },  error: function (e) {
                    layer.msg("请刷新！", {icon: 2});
                    return;
                }
            });

        })
    });

    function addImg(data,imgType,div,imgPicker) {

        var html = '<div class="bj-img imgSpan"><span class="layui-icon-close layui-icon" onclick="delImg(this, \''+imgType+'\')"></span>';
        html += '<img src="${commonStaticImgPath}' + data + '" height="700" width="1200"/>';
        html += '<input type="hidden" class="'+imgPicker+'" value="' + data + '" /> </div>';
        $("#"+div).append(html);
    }

    function delImg(that,imgType) {
        if(imgType=='imgs'){
            imgsCount--;
            if (imgsCount < 1) {
                $("#uploader").show();
            }
        }else{
            quaimgsCount--;
            if (quaimgsCount < 5) {
                $("#uploaderQuali").show();
            }
        }
        $(that).parent().remove();
    }


    function isBlank(val) {
        if (val == null || val == '' || val == undefined) {
            return true;
        }
        return false;
    }

    function cancels() {
        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index);
    }




</script>
</body>
</html>
