<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>新建</title>
    <link rel="stylesheet" th:href="@{/lib/layui/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/css/public.css}" media="all">
    <link rel="stylesheet" th:href="@{/lib/font-awesome-4.7.0/css/font-awesome.min.css}" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<form class="layui-form" action="" lay-filter="formDemo">
    <div class="layui-form layuimini-form">
        <input type="hidden" id="xm" name="xm" th:value="${xm}">
        <div class="layui-form-item layui-inline">
            <label class="layui-form-label required">类型</label>
            <div class="layui-input-inline" style="width: 300px">
                <select name="lx" id="lx" lay-filter="lx" lay-verify="required" lay-reqtext="类型不能为空">
                    <option value="">请选择</option>
                    <option value="t1">管理单位</option>
                    <option value="t2">出资单位</option>
                    <option value="t3">承担单位</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item layui-inline">
            <label class="layui-form-label required">单位</label>
            <div class="layui-input-inline" style="width: 720px">
                <input type="hidden" id="dw" name="dw">
                <input type="text" id="dwmc" name="dwmc" class="layui-input" lay-verify="required" lay-reqtext="单位不能为空"
                       readonly>
            </div>
        </div>

        <div class="layui-form-item layui-inline">
            <label class="layui-form-label required">联系人</label>
            <div class="layui-input-inline" style="width: 300px">
                <input type="hidden" id="lxr" name="lxr" class="layui-input">
                <input type="text" id="lxrxm" name="lxrxm" class="layui-input" lay-verify="required"
                       lay-reqtext="联系人不能为空" readonly>
            </div>
            <label class="layui-form-label">固定电话</label>
            <div class="layui-input-inline" style="width: 300px">
                <input type="text" id="gddh" name="gddh" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-inline">
            <label class="layui-form-label">移动电话</label>
            <div class="layui-input-inline" style="width: 300px">
                <input type="text" id="yddh" name="yddh" autocomplete="off" class="layui-input">
            </div>
            <label class="layui-form-label">电子邮件</label>
            <div class="layui-input-inline" style="width: 300px">
                <input type="text" id="dzyj" name="dzyj" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item layui-inline">
            <div class="layui-form-label">邮政编码</div>
            <div class="layui-input-inline" style="width: 300px">
                <input type="text" id="yzbm" name="yzbm" autocomplete="off" class="layui-input">
            </div>
            <label class="layui-form-label">传真</label>
            <div class="layui-input-inline" style="width: 300px">
                <input type="text" id="cz" name="cz" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-inline">
            <label class="layui-form-label">邮寄地址</label>
            <div class="layui-input-inline" style="width: 720px">
                <input type="text" id="yjdz" name="yjdz" autocomplete="off" class="layui-input">
            </div>
        </div>


        <!-- 右侧悬浮按钮 -->
        <div class="right-bottom-btn">
            <button class="layui-btn" lay-submit lay-filter="saveBtn">
                <i class="layui-icon layui-icon-ok"></i>确定
            </button>
            <button class="layui-btn" lay-filter="cancleBtn">
                <i class="layui-icon layui-icon-close"></i>取消
            </button>
        </div>
    </div>
</form>

<script th:src="@{/lib/jquery-3.4.1/jquery-3.4.1.min.js}" charset="utf-8"></script>
<script th:src="@{/lib/layui/layui.js}" charset="utf-8"></script>
<script th:src="@{/lib/coco-message/coco-message.js}" charset="utf-8"></script>
<script th:src="@{/js/lay-config.js}" charset="utf-8"></script>
<script type="text/javascript" th:inline="javascript">
    AjaxUtil.ctx = /*[[@{/}]]*/'';
    layui.use(['form', 'layer', 'tableSelect'], function () {
        var form = layui.form,
            tableSelect = layui.tableSelect;
        var xmId = $("#xm").val();
        var url = AjaxUtil.ctx + 'baseCompany/dwlist';

        function clearValue() {
            $("#lxr").val("");
            $("#lxrxm").val("");
            $("#gddh").val("");
            $("#yddh").val("");
            $("#dzyj").val("");
            $("#yzbm").val("");
            $("#cz").val("");
            $("#yjdz").val("");
        }

        form.on('select(lx)', function () {
            $("#dwmc").val("");
            $("#dw").val("");
            clearValue();

            var searchKey = "";
            var dwlx = $("#lx").val();
            if (dwlx == "t1") {
                searchKey = "mc";
                // url = AjaxUtil.ctx + 'prjCompletecompany/dwlist/' + xmId;
                url = AjaxUtil.ctx + 'baseCompany/dwlist';
            } else if (dwlx == "t2") {
                searchKey = "dwmc";
                url = AjaxUtil.ctx + 'prjFeescheme/dwlist/' + xmId;
            } else if (dwlx == "t3") {
                searchKey = "mc";
                // url = AjaxUtil.ctx + 'prjUndertakecompany/dwlist/' + xmId;
                url = AjaxUtil.ctx + 'baseCompany/dwlist';
            }

            //单位
            tableSelect.render({
                elem: '#dwmc',
                checkedKey: 'id',
                searchKey: searchKey,
                searchPlaceholder: '搜索单位',
                table: {
                    url: url,
                    cols: [
                        [
                            {type: 'radio',title: '😊'},
                            {type: 'numbers', align: 'center',title: '序号'},
                            {field: searchKey, title: '单位',width:'60%'}
                        ]
                    ]
                },
                done: function (elem, data) {
                    var NEWJSON = [];
                    var NEWJSON1 = [];
                    layui.each(data.data, function (index, item) {
                        if (dwlx == "t1") {
                            NEWJSON.push(item.mc);
                            NEWJSON1.push(item.id);
                        }
                        if (dwlx == "t2") {
                            NEWJSON.push(item.dwmc);
                            NEWJSON1.push(item.czdw);
                        }
                        if (dwlx == "t3") {
                            NEWJSON.push(item.mc);
                            NEWJSON1.push(item.id);
                        }
                    })
                    elem.val(NEWJSON.join(","));
                    $("#dw").val(NEWJSON1.join(","));
                    clearValue();

                    setPersonList();
                }
            });

            //联系人
            function setPersonList() {
                var dwId = $("#dw").val();

                if (dwId == "")
                    dwId = -1;
                console.log(dwId);
                tableSelect.render({
                    elem: '#lxrxm',
                    checkedKey: 'id',
                    searchKey: 'xm',
                    searchPlaceholder: '搜索姓名',
                    table: {
                        url: AjaxUtil.ctx + 'basePerson/dwrylist/' + dwId,
                        // url: AjaxUtil.ctx + 'basePerson/rylist',
                        cols: [
                            [
                                {type: "radio", fixed: 'left',title: '😊'},
                                {type: 'numbers', align: 'center',title: '序号'},
                                {field: 'xm', width: '15%', title: '姓名'},
                                {field: 'dwmc', title: '公司/单位',width: '60%'}
                            ]
                        ],
                        done: function (res, curr, count) {

                        },
                        page: true
                    },
                    done: function (elem, data) {
                        var NEWJSON = [];
                        var NEWJSON1 = [];
                        var NEWJSON2 = [];
                        var NEWJSON3 = [];
                        var NEWJSON4 = [];
                        var NEWJSON5 = [];
                        var NEWJSON6 = [];
                        var NEWJSON7 = [];
                        layui.each(data.data, function (index, item) {
                            NEWJSON.push(item.xm);
                            NEWJSON1.push(item.id);
                            NEWJSON2.push(item.gddh);
                            NEWJSON3.push(item.yddh);
                            NEWJSON4.push(item.dzyj);
                            NEWJSON5.push(item.yzbm);
                            NEWJSON6.push(item.cz);
                            NEWJSON7.push(item.txdz);
                        })
                        elem.val(NEWJSON.join(","));
                        $("#lxr").val(NEWJSON1.join(","));
                        $("#gddh").val(NEWJSON2.join(","));
                        $("#yddh").val(NEWJSON3.join(","));
                        $("#dzyj").val(NEWJSON4.join(","));
                        $("#yzbm").val(NEWJSON5.join(","));
                        $("#cz").val(NEWJSON6.join(","));
                        $("#yjdz").val(NEWJSON7.join(","));
                    }
                });
            }
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            console.log(data.field);
            AjaxUtil.post({
                url: AjaxUtil.ctx + "prjContacts/doAdd",
                data: data.field,
                success: function (res) {
                    if (res.code === 0) {
                        parent.setUpdateFlag();
                        Message.success(1500, res.message, function () {

                        });
                    } else {
                        Message.error(res.message, 1000);
                    }
                },
                error: function (error) {
                }
            });

            parent.layer.close(parent.layer.getFrameIndex(window.name));// 关闭子窗口
            return false;
        });

        // 监听取消按钮
        $("button[lay-filter='cancleBtn']").click(function () {
            var iframeIndex = parent.layer.getFrameIndex(window.name);
            parent.layer.close(iframeIndex);
        });
    });
</script>
</body>
</html>