<!DOCTYPE html>
<html>
<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">
    <link rel="stylesheet" href="../../layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../layuiadmin/style/admin.css" media="all">
    <link rel="stylesheet" type="text/css" href="../../layuiadmin/style/formSelects-v4.css"/>
    <style>
        #group_selecter dl {
            max-height: 200px;
        }
    </style>
</head>
<body>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md6">
            <div class="layui-card">

                <div class="layui-card-body">
                    <form class="layui-form" action="" lay-filter="component-form-element">
                        <div class="layui-row layui-col-space10 layui-form-item">
                            <div class="layui-col-lg6">
                                <label class="layui-form-label"><font color='red' size="4">*</font>姓名：</label>
                                <div class="layui-input-block">
                                    <input type="text" name="name" lay-verify="required" placeholder="请输入姓名"
                                           autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-col-lg6">
                                <label class="layui-form-label"><font color='red' size="4">*</font>电话：</label>
                                <div class="layui-input-block">
                                    <input type="text" name="tel" placeholder="请输入电话" lay-verify="required"
                                           autocomplete="off" class="layui-input">
                                </div>
                            </div>

                            <div class="layui-col-lg6">
                                <label class="layui-form-label"><font color='red' size="4">*</font>账号：</label>
                                <div class="layui-input-block">
                                    <input type="text" name="username" lay-verify="required" placeholder="请输入账号"
                                           autocomplete="off" class="layui-input">
                                </div>
                            </div>

                            <div class="layui-col-lg6">
                                <label class="layui-form-label">密码：</label>
                                <div class="layui-input-block">
                                    <input type="password" name="password" placeholder="请输入密码" autocomplete="off"
                                           class="layui-input">
                                    <div class="layui-word-aux">默认密码：1111</div>
                                </div>
                            </div>

                            <div class="layui-col-lg6">
                                <label class="layui-form-label"><font color='red' size="4">*</font>性别：</label>
                                <div class="layui-input-block">
                                    <input type="radio" name="gender" value="2" title="男" checked>
                                    <input type="radio" name="gender" value="1" title="女">
                                </div>
                            </div>

                            <div class="layui-col-lg6">
                                <label class="layui-form-label">在职状态：</label>
                                <div class="layui-input-block">
                                    <select name="status" style="width: 40px;">
                                        <option value="2" selected>在职</option>
                                        <option value="3">离职</option>
                                    </select>
                                </div>
                            </div>
                            <div class="layui-col-lg6">
                                <label class="layui-form-label"><font color='red' size="4">*</font>总代理：</label>
                                <div class="layui-input-block">
                                    <select id="id_general" name="general_agent" lay-verify="required" lay-filter="onGeneralAgentChange">
                                        <option value="">请选择总代理</option>
                                    </select>
                                </div>
                            </div>
                            <div class="layui-col-lg6">
                                <label class="layui-form-label">代理商：</label>
                                <div class="layui-input-block">
                                    <select id="id_agents" name="agent"
                                            lay-filter="onAgentChange">
                                           <option value="">请选择代理商</option>
                                    </select>
                                </div>
                            </div>
                            <div class="layui-col-lg6">
                                <label class="layui-form-label">门店：</label>
                                <div class="layui-input-block">
                                    <select id="id_stores" name="store" lay-search="">
                                        <option value="">请选择门店</option>
                                    </select>
                                </div>
                            </div>

                            <div class="layui-col-lg6">
                                <label class="layui-form-label">是否在用：</label>
                                <div class="layui-input-block">
                                    <input type="checkbox" name="is_active" lay-skin="switch" lay-text="是|否" checked=""
                                           value="1">
                                </div>
                            </div>

                            <div class="layui-col-lg6">
                                <label class="layui-form-label">权限组：</label>
                                <div class="layui-input-block">
                                    <select name="groups" xm-select="selectGroup"></select>
                                </div>
                            </div>
                            <button class="layui-btn" id="id_user_save" lay-submit lay-filter="component-form-element"
                                    style="display: none">保存
                            </button>
                        </div>

                    </form>
                </div>
            </div>
        </div>
    </div>
</div>


<script src="../../layuiadmin/layui/layui.js"></script>

<script>
    layui.config({
        base: '../../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index',
        formSelects: 'formSelects-v4'
    }).use(['index', 'form', 'utils', 'formSelects',], function () {
        var $ = layui.$
            , admin = layui.admin
            , form = layui.form
            , formSelects = layui.formSelects

        var id = layui.view.getParameterByName('id');

        form.val("component-form-element", {
            'password': '1111'
        });
        var loadData = function (agents, stores) {
            if (id) {
                var editdata = JSON.parse(JSON.stringify(parent.layui.table.editdata)); // 框架有Bug所以这么转换
                //渲染select
                renderSelect(agents, stores, editdata.general_agent, editdata.agent);
                form.val("component-form-element", {
                    'password': ''
                });
                form.val("component-form-element", editdata);
            }
        }
        form.render(null, 'component-form-element');

        //获取字典
        function getDict(val){
            return new Promise((resolve, reject)=>{
                admin.req({
                    url: '/agent/dict/',
                    done: function (res) {
                        var general_agents = res.data.general_agents;
                        var agents = res.data.agents;
                        var stores = res.data.stores;
                        if(val === 'firstRender'){
                             //总代理下拉框更新
                            var general_node = $('#id_general');
                            for (var i in general_agents) {
                                var pid = general_agents[i].id;
                                var general_agents_value = general_agents[i].name;
                                general_node.append("<option value='" + pid + "'>" + general_agents_value + "</option>");
                            }
                            form.render();
                            loadData(agents, stores);
                        }
                        var tempObj = {};
                        tempObj['agents'] = agents;
                        tempObj['stores'] = stores;
                        //async函数调用成功的回调
                        resolve(tempObj);
                    }
                });
            })
        }
        //渲染选择框（修改的时候执行）
        function renderSelect(agents, stores, general_id, agent_id){
            var agents_node = $('#id_agents');
            var optionsHtml = "<option value=\"\">请选择代理商</option>";
            if(agents && agents.length > 0){
                agents.forEach(item=>{
                    if(parseInt(general_id) === item['general_agent_id']){
                        var pid = item.id;
                        var agents_value = item.name;
                        optionsHtml += "<option value='" + pid + "'>" + agents_value + "</option>\n";
                    }
                });
                agents_node.html(optionsHtml);
            }
            var stores_node = $('#id_stores');
            var optionsStoresHtml = "<option value=\"\">请选择门店</option>";
            if(stores && stores.length > 0){
                stores.forEach(item=>{
                    if(parseInt(agent_id) === item['agent_id']){
                        var pid = item.id;
                        var agents_value = item.name;
                        optionsStoresHtml += "<option value='" + pid + "'>" + agents_value + "</option>\n";
                    }
                });
                stores_node.html(optionsStoresHtml);
            }
            form.render();
        };
        //页面初次加载渲染页面
        getDict('firstRender');

        // 选择总代理
        form.on('select(onGeneralAgentChange)', async function (data) {
            //清空门店选择
            var stores_node = $('#id_stores');
            var optionsStoreHtml = "<option value=\"\">请选择门店</option>";
            stores_node.html(optionsStoreHtml);
            let general_id = data.value;
            let obj = await getDict();
            let agentList = obj['agents'];
            var agents_node = $('#id_agents');
            var optionsHtml = "<option value=\"\">请选择代理商</option>";
            if(agentList && agentList.length > 0){
                agentList.forEach(item=>{
                    if(parseInt(general_id) === item['general_agent_id']){
                        var pid = item.id;
                        var agents_value = item.name;
                        optionsHtml += "<option value='" + pid + "'>" + agents_value + "</option>\n";
                    }
                });
                agents_node.html(optionsHtml);
                form.render();
            };
        });
        //选择代理商
        form.on('select(onAgentChange)', async function (data) {
            let agent_id = data.value;
            let obj = await getDict();
            let storesList = obj['stores'];
            var stores_node = $('#id_stores');
            if(storesList && storesList.length > 0){
                var optionsHtml = "<option value=\"\">请选择门店</option>";
                storesList.forEach(item=>{
                    if(parseInt(agent_id) === item['agent_id']){
                        var pid = item.id;
                        var agents_value = item.name;
                        optionsHtml += "<option value='" + pid + "'>" + agents_value + "</option>\n";
                    }
                });
                stores_node.html(optionsHtml);
                form.render();
            }
        });
        formSelects.config('selectGroup', {
            beforeSuccess: function (eid, url, searchVal, result) {
                if (id) {
                    var groupIds = parent.layui.table.editdata.group_ids;
                    for (var n in result.data) {
                        var item = result.data[n];
                        if (groupIds.indexOf(parseInt(item.value)) > -1)
                            item.selected = 'selected';
                    }
                }
                return result;
            }
        });
        formSelects.data('selectGroup', 'server', {
            url: '/account/permission/dict/'
        });
        form.on('submit(component-form-element)', function (data) {
            //layer.msg(JSON.stringify(data.field));
            var submitData = data.field;
            var groups = formSelects.value('selectGroup', 'val');
            submitData.groups = JSON.stringify(groups);

            if (id) {
                var url = '/account/employee/' + id + '/';
                var type = 'put';
            } else {
                url = '/account/employee/';
                type = 'post';
            }
            admin.req({
                url: url
                , data: data.field
                , type: type
                , done: function (res) {
                    parent.layui.onSubmitChild(res);
                }
            });
            return false;
        });

        parent.layui.submitChild = function () {
            $("#id_user_save").click();
        };
    });
</script>
</body>
</html>
