<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <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" href="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/style/admin.css" media="all">
    <link rel="stylesheet" href="${ctxPlugin}/static/css/reset.css" media="all">
    <link rel="stylesheet" href="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/dist/viewer.css" media="all">
    <style>
        .layui-form-select dl{
            max-height:200px;
            width: 80%;
        }
        html{
            background-color: #fff;
        }
    </style>
</head>
<body>
<div class="layui-form" id="LAY_app">
    <div class="layui-card">
        <div class="layui-card-body over pt-20 pb-40 h-250" id="detailSet">
            <div class="layui-col-xs12 mb-15 pr-30">
                <div class="layui-inline layui-col-xs6">
                    <label class="layui-form-label w-130"><i class="red">*</i>所属服务商：</label>
                    <div class="layui-input-block ">
                        <select class="" id="site" name="site" lay-verify="site" lay-filter="site" lay-search  <c:if test="${!empty emp}">disabled="disabled" </c:if>>
                            <option value="">请选择服务商</option>
                            <c:forEach items="${siteList}" var="site">
                                <option value="${site.columns.id}" title="${site.columns.name}" <c:if test="${emp.siteId == site.columns.id}">selected</c:if>>${site.columns.name}</option>
                            </c:forEach>
                        </select>

                        <%--<c:if test="${empty emp}">
                            <div class="layui-input-block ">
                                <input type="text" name="site" lay-verify="site" autocomplete="off"  class="layui-input" value="${emp.mobile}">
                            </div>
                        </c:if>--%>
                    </div>
                </div>
                <div class="layui-inline layui-col-xs6">
                    <label class="layui-form-label w-130"><i class="red">*</i>姓名：</label>
                    <div class="layui-input-block w-200" >
                        <input type="text" name="name" lay-verify="name" autocomplete="off" placeholder="请输入姓名" class="layui-input" value="${emp.name}">
                    </div>
                </div>
            </div>
            <div class="layui-col-xs12 mb-10 pr-30">
                <div class="layui-inline layui-col-xs6">
                    <label class="layui-form-label w-130"><i class="red">*</i>手机号：</label>
                    <div class="layui-input-block ">
                        <input type="text" name="mobile" lay-verify="mobile" autocomplete="off" placeholder="请输入手机号" class="layui-input" value="${emp.mobile}">
                    </div>
                </div>
                <div class="layui-inline layui-col-xs6">
                    <label class="layui-form-label w-130">工作年限：</label>
                    <div class="layui-input-block w-200" >
                        <input type="text" name="years" lay-verify="years"  autocomplete="off" class="layui-input" value="${emp.years}">
                    </div>
                </div>
            </div>
            <div class="layui-col-xs12 mb-10 pr-30">
                <div class="layui-inline layui-col-xs6">
                    <label class="layui-form-label w-130"><i class="red">*</i>登录账号：</label>
                    <div class="layui-input-block ">
                        <input type="text"  name="loginName" lay-verify="loginName" autocomplete="off" placeholder="请输入登录账号" class="layui-input" value="${user.loginName}">
                    </div>
                </div>
                <div class="layui-inline layui-col-xs6">
                    <label class="layui-form-label w-130"><i class="red">*</i>登录密码：</label>
                    <div class="layui-input-block w-200" >
                        <input type="password" name="password" lay-verify="password" autocomplete="new-password" placeholder="请输入登录密码" class="layui-input" <c:if test="${user.password eq null}">value=""</c:if> value="default">
                    </div>
                </div>
            </div>
            <div class="layui-col-xs12 layui-form-item">
                <label class="layui-form-label">相关资质：</label>
                <div class="layui-input-block">
                    <div id="imgsDiv">
                        <c:forEach items="${imglist}" var="img">
                            <div class="bj-img imgSpan"><span class="layui-icon-close layui-icon" onclick="delImg(this)"></span>
                                <img src="${commonStaticImgPath.concat(img)}" data-img="${commonStaticImgPath.concat(img)}" height="700" width="1200"/>
                                <input type="hidden" class="imgPicker" value="${img}"/>
                            </div>
                        </c:forEach>
                    </div>
                    <div class="upload-container" id="uploader">
                        <img src="" alt="img placeholder" id="imgsAdd">
                    </div>
                </div>
            </div>
            &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;(最多上传3张图片)
            <input type="hidden" name="id" id="id" value="${emp.id}">
            <input type="hidden" name="imgs" id="imgs" value="${emp.licenseImg}">
        </div>
    </div>
    <div class="operate-footer">
        <input type="button" lay-submit lay-filter="confirm" id="confirm" class="layui-btn " value="保存">
        <input type="button"   value="取消" class="layui-btn layui-btn-primary " onclick="closeDiv();" >
    </div>
</div>
<div id="lookImg" class="hide pos-r">
    <div style="position:relative;width:100%;height:500px;">
        <div class="w-500 h-500 lookImg">
            <img src="" width="80%" id="image">
        </div>
    </div>
</div>
<script src="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/layui/layui.js"></script>
<script src="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/layui/data.js"></script>
<script src="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/dist/viewer.js"></script>
<script type="text/javascript">
    var viewer = new Viewer(document.getElementById('LAY_app'));
    //初始数据
    var $form;
    var form;
    var upload;
    var $;
    var imgsCount=${imgsCount};
    var imgs='';
    var myreg=/^[1][3,4,5,6,7,8,9][0-9]{9}$/;
    var reg =/^(0|[1-9][0-9]*)(\.\d+)?$/;
    layui.use(['form', 'table', 'upload', 'jquery'], function () {
        $ = layui.jquery;
        form = layui.form;
        upload = layui.upload;
        $form = $('form');

        if (imgsCount>=3) {
            $("#uploader").hide();
        }


        form.verify({
            site:function(value,item){
                if(!$.trim(value)){
                    return "所属服务商不能为空,请选择服务商后保存！";
                }
            },
            loginName:function(value,item){
                if(!$.trim(value)){
                    return "登录账号不能为空,请输入账号后保存！";
                }
                if($.trim(value).length<6){
                    return "登录账号不得小于6位,请重新输入！";
                }
            },
            password:function(value,item){
                if(!$.trim(value)){
                    return "登录密码不能为空,请输入登录密码后保存！";
                }
            },
            name:function(value,item){
                if(!$.trim(value)){
                    return "姓名不能为空,请输入姓名后保存！";
                }
            },
            years:function(value,item){
                if($.trim(value)){
                    if(!reg.test($.trim(value))){
                        return "工作年限不能为负数！";
                    }
                }
            },
            mobile:function(value,item){
                if(!$.trim(value)){
                    return "手机号码不能为空,请输入手机号码后保存！";
                }
                if(!myreg.test($.trim(value))){
                    return "请输入正确的手机号码后保存！";
                }
            }
        });

        form.on('submit(confirm)', function(data){
            $.ajax({
                url:'${ctx}/employeInformation/employeSave',
                method:'post',
                data:data.field,
                dataType:'JSON',
                success:function(res){
                    console.log(res);
                    if(res.code=="200"){
                        layer.msg("保存成功",{time:1000});
                        closeDiv();
                        parent.table.reload('test');
                    }else if(res.code=="201") {
                        layer.msg("该登录账号已存在，请重新输入",{time:1500});
                    }else if(res.code=="203") {
                        layer.msg("该工程师姓名已存在，请重新输入",{time:1500});
                    }else{
                        layer.msg("保存失败",{time:1000});
                    }
                },
                error:function (data) {
                }
            }) ;
        });

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

                });
            }
            , done: function (res) {
                /* imgs+=res.path+',';
                 $("#imgs").val(imgs);*/
                //上传完毕回调
                addImg(res.path);
            }
            , error: function () {
                //请求异常回调
            }
        });
    })

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

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

    function addImg(data){
        imgs = '';
        imgsCount++;
        if (imgsCount >= 3) {
            $("#uploader").hide();
        }else{
            $("#uploader").show();
        }
        var html = '<div class="bj-img imgSpan"><span class="layui-icon-close layui-icon" onclick="delImg(this)"></span>';
        html += '<img src="${commonStaticImgPath}'+data+'" height="700" width="1200"/>';
        html += '<input type="hidden" class="imgPicker" value="'+data+'"/> </div>';
        $("#imgsDiv").append(html);
        var imgSelector = $(".imgPicker");
        imgSelector.each(function (index) {
            var img = $(this).val();
            if (!isBlank(img)) {
                imgs += img + ",";
            }
        });
        $("#imgs").val(imgs);
    }

    function delImg(that){
        imgsCount--;
        if (imgsCount < 3) {
            $("#uploader").show();
        }
        $(that).parent().remove();
        var imgSelector = $(".imgPicker");
        imgSelector.each(function (index) {
            var img = $(this).val();
            if (!isBlank(img)) {
                imgs += img + ",";
            }
        });
        $("#imgs").val(imgs);
    }

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