<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
	<th:block th:include="include :: header('修改值班配置')" />
	<th:block th:include="include :: select2-css" />
	<th:block th:include="include :: datetimepicker-css" />
</head>
<body>
    <div class="main-content">
        <form id="form-dutyConfig-edit" class="form-horizontal">
            <input name="id" th:value="${dutyConfig.id}" type="hidden">
            <h4 class="form-header h4">基本信息</h4>
            <div class="row">
            	<div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label is-required">配置名称：</label>
                        <div class="col-sm-8">
                            <input name="configName" th:value="${dutyConfig.configName}" placeholder="请输入配置名称" class="form-control" type="text" maxlength="100" required>
                        </div>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">状态：</label>
                        <div class="col-sm-8">
                            <label class="toggle-switch switch-solid">
	                            <input type="checkbox" id="status" th:checked="${dutyConfig.status == '1'}">
	                            <span></span>
	                        </label>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label is-required">开始时间：</label>
                        <div class="col-sm-8">
                            <div class="input-group date">
                                <input name="startTime" th:value="${#dates.format(dutyConfig.startTime, 'yyyy-MM-dd HH:mm:ss')}" class="form-control" placeholder="yyyy-MM-dd HH:mm:ss" type="text" required>
                                <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label is-required">结束时间：</label>
                        <div class="col-sm-8">
                            <div class="input-group date">
                                <input name="endTime" th:value="${#dates.format(dutyConfig.endTime, 'yyyy-MM-dd HH:mm:ss')}" class="form-control" placeholder="yyyy-MM-dd HH:mm:ss" type="text" required>
                                <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
            	<div class="col-sm-12">
                    <div class="form-group">
                        <label class="col-xs-2 control-label is-required">值班人员：</label>
                        <div class="col-xs-10">
                            <select id="dutyUserIds" name="dutyUserIds" class="form-control select2-multiple" multiple required>
								<option th:each="user:${users}" th:value="${user.userId}" th:text="${user.userName}" 
								        th:disabled="${user.status == '1'}"
								        th:selected="${dutyConfig.dutyUsers != null and #lists.contains(dutyConfig.dutyUsers.![userId], user.userId)}"></option>
							</select>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-12">
                    <div class="form-group">
                        <label class="col-xs-2 control-label">备注：</label>
                        <div class="col-xs-10">
                            <textarea name="remark" th:text="${dutyConfig.remark}" maxlength="500" class="form-control" rows="3" placeholder="请输入备注信息"></textarea>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>
      
    <div class="row">
        <div class="col-sm-offset-5 col-sm-10">
            <button type="button" class="btn btn-sm btn-primary" onclick="submitHandler()"><i class="fa fa-check"></i>保 存</button>&nbsp;
            <button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i class="fa fa-reply-all"></i>关 闭 </button>
        </div>
    </div>
	<th:block th:include="include :: footer" />
	<th:block th:include="include :: select2-js" />
	<th:block th:include="include :: datetimepicker-js" />
	<script>
	    var prefix = ctx + "system/dutyConfig";
	
        $("#form-dutyConfig-edit").validate({
        	onkeyup: false,
        	rules:{
        		configName:{
        			required: true,
        			minlength: 2,
        			maxlength: 100
        		},
        		startTime:{
        			required: true
        		},
        		endTime:{
        			required: true
        		},
        		dutyUserIds:{
        			required: true
        		}
        	},
        	messages: {
                "configName": {
                    required: "请输入配置名称",
                    minlength: "配置名称不能小于2个字符",
                    maxlength: "配置名称不能超过100个字符"
                },
                "startTime": {
                    required: "请选择开始时间"
                },
                "endTime": {
                    required: "请选择结束时间"
                },
                "dutyUserIds": {
                    required: "请选择值班人员"
                }
            },
            focusCleanup: true
        });
        
        function submitHandler() {
            if ($.validate.form()) {
                // 验证时间范围
                var startTime = $("input[name='startTime']").val();
                var endTime = $("input[name='endTime']").val();
                
                if (startTime && endTime) {
                    var start = new Date(startTime);
                    var end = new Date(endTime);
                    
                    if (start >= end) {
                        $.modal.alertWarning("开始时间必须早于结束时间");
                        return;
                    }
                }
                
                // 处理状态值（0停用 1启用）
                var status = $("#status").is(':checked') == true ? "1" : "0";
                
                // 处理值班人员 - 转换为逗号分隔的字符串
                var dutyUserIds = $("#dutyUserIds").val();
                if (dutyUserIds && dutyUserIds.length > 0) {
                    dutyUserIds = dutyUserIds.join(',');
                } else {
                    dutyUserIds = '';
                }
                
                // 移除表单中的 dutyUserIds（避免重复提交）
                var data = $("#form-dutyConfig-edit").serializeArray().filter(function(item) {
                    return item.name !== 'dutyUserIds';
                });
                
                data.push({"name": "status", "value": status});
                data.push({"name": "dutyUserIds", "value": dutyUserIds});
                
                $.operate.saveTab(prefix + "/edit", data);
            }
        }
        
        $(function() {
            // 初始化Select2
            $('.select2-multiple').select2({
                placeholder: "请选择值班人员",
                allowClear: true
            });
            
            // 初始化日期时间选择器
            $(".form-control[name='startTime']").datetimepicker({
                format: "yyyy-mm-dd hh:ii:ss",
                autoclose: true,
                todayBtn: true,
                language: 'zh-CN'
            });
            
            $(".form-control[name='endTime']").datetimepicker({
                format: "yyyy-mm-dd hh:ii:ss",
                autoclose: true,
                todayBtn: true,
                language: 'zh-CN'
            });
        });
	</script>
</body>
</html>