<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增调阅用户')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: select2-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-tempPer-add">
            <div class="form-group">
                <label class="col-sm-3 control-label">起止时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="duration" id="duration" class="form-control" placeholder="请选择起止时间" required type="text">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">选择用户：</label>
                <div class="col-sm-8">
                    <select class="form-control select2-multiple" name="users" id="users" placeholder="请选择用户" multiple>

                    </select>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: select2-js" />
    <script th:inline="javascript">
        var prefix = ctx + "system/tempPer"

        $(function() {
            /** 范围选择日期框 **/
            layui.use('laydate', function(){
                var laydate = layui.laydate;
                laydate.render({
                    elem: '#duration',
                    range: true
                });
            });
            /** 查询用户列表并构建一个分组多选下拉 **/
            queryAndMakeUserSelect();
        });

        function queryAndMakeUserSelect(){
            let url = '/system/tempPer/listUser';
            let options = '';
            $.ajax({
                url: url,
                type: "post",
                success: function(result) {
                    for(let i=0;i<result.length;i++){
                        let optgroup = '';
                        optgroup += '<optgroup label="--------'+result[i].deptName+'--------">';
                        for(let j=0;j<result[i].users.length;j++){
                            optgroup += '<option value="'+result[i].users[j].userId+'">'+result[i].users[j].userName+'</option>';
                        }
                        optgroup += '</optgroup>';
                        options += optgroup;
                    }
                    $("#users").append(options);
                }
            })
        }

        $("#form-tempPer-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-tempPer-add').serialize());
            }
        }
    </script>
</body>
</html>
