<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<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, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/lib/layui/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/css/public.css}" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form" style="margin-left: -20px; margin-right: 20px">
    <div class="layui-form-item" style="width: 100%">
        <label class="layui-form-label required" style="width: 10%;">账号</label>
        <div class="layui-input-block" style="width: 80%">
            <input type="hidden" name="userId" th:value="${userInfo.userId}">
            <input type="text" name="account" th:value="${userInfo.account}" class="layui-input" readonly>
        </div>
    </div>

    <div class="layui-form-item" style="width: 100%">
        <label class="layui-form-label required" style="width: 10%">姓名</label>
        <div class="layui-input-block" style="width: 80%">
            <input type="text" name="name" lay-verify="required" lay-reqtext="姓名不能为空" autocomplete="off"
                   th:value="${userInfo.name}" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item" style="width: 100%">
        <label class="layui-form-label required" style="width: 10%">所属部门</label>
        <div class="layui-input-block" style="width: 80%">
            <input type="text" id="deptId" name="deptId" lay-verify="required" lay-reqtext="所属部门不能为空">
            <option th:each="item:${userInfo}" th:text="${item.deptName}"
                    th:value="${item.deptId}"></option>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">性别</label>
        <div class="layui-input-block" th:if="${userInfo.sex eq '0'}">
            <input type="radio" name="sex" value="1" title="男">
            <input type="radio" name="sex" value="2" title="女">
        </div>
        <div class="layui-input-block" th:if="${userInfo.sex eq '1'}">
            <input type="radio" name="sex" value="1" title="男" checked>
            <input type="radio" name="sex" value="2" title="女">
        </div>
        <div class="layui-input-block" th:if="${userInfo.sex eq '2'}">
            <input type="radio" name="sex" value="1" title="男">
            <input type="radio" name="sex" value="2" title="女" checked>
        </div>
    </div>

    <div class="layui-form-item" style="width: 100%">
        <label class="layui-form-label" style="width: 10%">手机</label>
        <div class="layui-input-block" style="width: 80%">
            <input type="phone" name="phone" autocomplete="off" th:value="${userInfo.phone}" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item" style="width: 100%">
        <label class="layui-form-label" style="width: 10%">状态</label>
        <div class="layui-input-block" style="width: 80%">
            <select name="status" th:value="${userInfo.status}">
                <option th:each="item:${baseDictList}" th:text="${item.name}" th:value="${item.code}"
                        th:selected="(${userInfo.status} eq ${item.code})"></option>
            </select>
        </div>
    </div>
    <div class="layui-form-item" style="width: 100%">
        <label class="layui-form-label" style="width: 10%">邮箱</label>
        <div class="layui-input-block" style="width: 80%">
            <input type="text" name="email" autocomplete="off" th:value="${userInfo.email}" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item" style="width: 100%">
        <label class="layui-form-label" style="width: 10%">岗位</label>
        <div class="layui-input-block" style="width: 80%">
            <div id="demo1" name="postSelect" class="xm-select-demo"></div>
        </div>
    </div>
    <div class="layui-form-item" style="width: 100%;display: none" id="bzs">
        <label class="layui-form-label" style="width: 10%">备注</label>
        <div class="layui-input-block" style="width: 80%">
            <textarea name="bz" id="bz" class="layui-textarea"></textarea>
        </div>
    </div>
    <!--    <div class="layui-form-item">-->
    <!--        <label class="layui-form-label">用户头像</label>-->
    <!--        <div class="layui-input-block">-->
    <!--            <input type="hidden" name="avatar" th:value="${userInfo.avatar}">-->
    <!--            <div style="height: 100px;width: 100px;position: relative;">-->
    <!--                <input type="file" accept="image/*" id="pic-input"-->
    <!--                       style="position: absolute;left: 0;top: 0;opacity: 0;filter: alpha(opacity=0);height: 100%;width: 100%;z-index: 99">-->
    <!--                <img th:src="${userInfo.avatarPreUrl}" alt="" id="pic-img"-->
    <!--                     style="height: 100%;width: 100%;position: absolute;left: 0;top: 0;border: 1px solid #1E9FFF;">-->
    <!--            </div>-->
    <!--        </div>-->
    <!--    </div>-->
    <!--    <div class="layui-form-item layui-form-text">-->
    <!--        <label class="layui-form-label">备注信息</label>-->
    <!--        <div class="layui-input-block">-->
    <!--            <textarea name="remark" class="layui-textarea" autocomplete="off" th:text="${userInfo.account}"></textarea>-->
    <!--        </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>
<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="@{/lib/xm-select-v1.2.2/xm-select.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', 'layCascader'], function () {
        var form = layui.form,
            layCascader = layui.layCascader,
            layer = layui.layer;

        redenXmSelect();

        var demo1;

        function redenXmSelect() {
            var index = layer.load(0, {shade: 0.1}); //0代表加载的风格，支持0-2

            // 隐藏代码
            setTimeout(function () {
                var bzs = document.getElementById('bzs');
                var userid = [[${userInfo.account}]]

                console.log('userid'+userid);
                if (userid === 'admin'){
                    bzs.style.display = 'block';
                 document.getElementById('bz').value = [[${userInfo.bz}]]
                }

            },1000);// 2 秒之后 开始执行


            AjaxUtil.get({
                url: AjaxUtil.ctx + 'userinfo/listPostByUserId?userId=' + $('input[name="userId"]').val(),
                success: function (res) {
                    layer.close(index);
                    demo1 = xmSelect.render({
                        el: '#demo1',
                        data: res.data,
                        theme: {
                            color: '#1E9FFF'
                        }
                    })
                },
                error: function (error) {
                    layer.close(index);
                    Message.error('获取岗位信息失败！', 1000);
                }
            });
        }

        // 初始化cascader所属部门选择
        var index2 = layer.load(0, {shade: 0.1});
        AjaxUtil.get({
            url: AjaxUtil.ctx + 'userinfo/listDeptForSelect',
            success: function (res) {
                layer.close(index2);

                var datalist = [];
                for (let item of res.data) {
                    item.value = item.deptId;
                    item.label = item.deptName;
                    datalist.push(item);
                }
                // 扁平数据转树
                var options = arrayToTree(datalist, "0");

                // 获取父级Id
                var deptId = $("input[name='deptId']").val();

                // 初始化cascader实例
                var cascader = layCascader({
                    elem: '#deptId',
                    value: deptId, // 回显
                    clearable: true,
                    showAllLevels: false, // 配置仅显示最后一级
                    props: {
                        checkStrictly: true, // 配置选择任意一级选项
                    },
                    options: options
                });

                // 监听cascader变化
                cascader.changeEvent(function (value, node) {
                    // 主动关闭面板
                    cascader.close();
                    $("input[name='deptId']").val(value)
                });
            },
            error: function (error) {
                layer.close(index2);
                Message.error('获取部门信息失败！', 1000);
            }
        });


        form.on('submit(saveBtn)', function (data) {
            var selectArr = demo1.getValue('valueStr');
            data.field.postStr = selectArr;
            var index = layer.alert('确认提交吗？', function () {
                var loading = layer.load(0, {shade: 0.1}); //0代表加载的风格，支持0-2
                AjaxUtil.post({
                    url: AjaxUtil.ctx + 'userinfo/doUpdate',
                    data: data.field,
                    success: function (res) {
                        if (res.code === 0) {
                            layer.close(index);
                            layer.close(loading);
                            Message.success(1500, res.message, function () {
                                parent.setUpdateFlag();
                                // 重载表格
                                var iframeIndex = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(iframeIndex);
                            });
                        } else {
                            layer.close(loading);
                            Message.error(res.message, 1000);
                        }
                    },
                    error: function (error) {
                    }
                });
            });

            return false;
        });


        $('#pic-input').change(function () {
            var formData = new FormData();
            formData.append("files", $("#pic-input")[0].files[0]);
            formData.append("fileName", $("#pic-input")[0].files[0].name); // 文件名字
            var index = layer.load(0, {shade: 0.1}); //0代表加载的风格，支持0-2

            AjaxUtil.upload({
                url: AjaxUtil.ctx + 'fileupload/put',
                data: formData,
                success: function (res) {
                    layer.close(index);
                    if (0 === res.code) {
                        $("input[name='avatar']").val(res.data.fileId);
                        $('#pic-img').attr('src', res.data.previewUrl);
                        Message.success(res.message, 1000);
                    } else {
                        Message.error(res.message, 1000);
                    }
                },
                error: function (error) {
                    layer.close(index);
                    Message.error("服务器异常！", 1000);
                }
            });

        });


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

    /**
     *  扁平数据转树结构
     * @returns {[]}
     * @param data
     * @param firstPid
     */
    function arrayToTree(data, firstPid) {
        const result = [];
        getChildren(data, result, firstPid)
        return result;
    }

    function getChildren(data, result, parentId) {
        for (const item of data) {
            if (item.parentId === parentId) {
                const newItem = {...item, children: []};
                result.push(newItem);
                getChildren(data, newItem.children, item.deptId);
            }
        }
    }
</script>
</body>
</html>