<#assign base=request.contextPath />
<#import "../common/defaultLayout.ftl" as defaultLayout>
<@defaultLayout.layout>
    <link rel="stylesheet" href="${base}/assets/css/amazeui.switch.css"/>
    <script type="text/javascript">
        var base = '${base}';
    </script>
    <div class="admin-content">
        <div class="admin-content-body">
            <div class="am-cf am-padding">
                <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">配置管理</strong> /
                    <small>部门配置</small>
                </div>
            </div>

            <form method="post" class="am-form" id="tmpForm"
                  action="${base}/department/save">
                <input name="deptId" id="deptId" type="hidden" value="${deptId!}"/>
                <div class="am-tabs am-margin" data-am-tabs>
                    <ul class="am-tabs-nav am-nav am-nav-tabs">
                        <li class="am-active"><a href="#tab1">新增/编辑</a></li>
                    </ul>
                    <div class="am-tabs-bd">
                        <div class="am-tab-panel am-fade am-in am-active" id="tab1">
                            <div class="am-g am-form-group am-margin-top" style="display: flex;">
                                <div class="am-u-sm-4 am-u-md-2 am-text-right">
                                    <span style="color: red;">*</span>部门名称</div>
                                <div class="am-u-sm-6 am-u-md-6">
                                    <input type="text" class="am-input" data-validate-async data-validation-message="请输入项目名称（1000字符以内）"
                                           name="name" placeholder="请输入部门名称（20字符以内）" minlength="1" maxlength="20" id="name"
                                           value="${department.name!}" required/>
                                </div>
                                <div class="am-u-sm-2 am-u-md-4 input-msg"></div>
                            </div>

                            <div class="am-g am-form-group am-margin-top">
                                <div class="am-u-sm-4 am-u-md-2 am-text-right">
                                    <span style="color: red;">*</span>
                                    所属上级
                                </div>
                                <div class="am-u-sm-8 am-u-md-10">
                                    <select id="parentId" name="parentId"
                                            data-am-selected="{btnSize: 'sm',maxHeight: 200,searchBox: 1}" required>
                                        <option value="0">无</option>
                                        <#if parentList??>
                                            <#list parentList as parent>
                                                <option value="${parent.id!}" <#if department.parentId == parent.id >
                                                selected </#if>>${parent.name}</option>
                                            </#list>
                                        </#if>
                                    </select>
                                </div>
                                <div class="am-u-sm-2 am-u-md-4 input-msg"></div>
                            </div>

                            <div class="am-g am-form-group am-margin-top">
                                <div class="am-u-sm-4 am-u-md-2 am-text-right">
                                    <span style="color: red;">*</span>
                                    部门领导
                                </div>
                                <div class="am-u-sm-8 am-u-md-10">
                                    <select id="managerId" name="managerId"
                                            data-am-selected="{btnSize: 'sm',maxHeight: 200,searchBox: 1}" required>
                                        <option value="0">无</option>
                                        <#if userList??>
                                            <#list userList as user>
                                                <option value="${user.id!}" <#if department.managerId == user.id >
                                                selected </#if>>${user.realName}</option>
                                            </#list>
                                        </#if>
                                    </select>
                                </div>
                                <div class="am-u-sm-2 am-u-md-4 input-msg"></div>
                            </div>

                            <div class="am-g am-form-group am-margin-top">
                                <div class="am-u-sm-4 am-u-md-2 am-text-right">启用/禁用</div>

                                <div class="am-u-sm-12 am-u-md-4 switch-button" style="height: 25px;">
                                    <input id="switch" name="switch" type="checkbox" data-size='xs'
                                           data-am-switch data-off-text="禁用" data-on-text="启用"
                                           <#if department.enabled==1 >checked</#if>
                                    />
                                    <input type="hidden" class="am-input-sm" name="enabled" id="enabled"
                                           value="${department.enabled!1}"/>
                                </div>

                                <div class="am-hide-sm-only am-u-md-1" style="color: red;"></div>
                                <div class="am-u-sm-2 am-u-md-5 input-msg"></div>
                            </div>

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

                <!--选项卡（tabs）end-->
                <div class="am-margin">
                    <button type="button" id="saveApprove" class="am-btn am-btn-primary am-btn-xs">提交保存</button>
                    <button type="button" class="am-btn am-btn-warning am-btn-xs"
                            onclick="javascript:history.go(-1);">返回上一级
                    </button>
                </div>
            </form>
        </div>
    </div>

</@defaultLayout.layout>
<script src="${base}/assets/js/amazeui.switch.js"></script>

<script>
    var $mycheckbox = $('.switch-button');
    var result = 0;

    $mycheckbox.each(function () {
        $("#switch").on({
            'switchChange.bootstrapSwitch': function (event, state) {
                if (state.toString() == "true") {
                    $("#enabled").val("1");
                } else {
                    $("#enabled").val("0");
                }
            }
        });
    });

    $(function () {
        $("#saveApprove").click(function () {

            var name = $("#name").val();
            if (name.length == 0) {
                window.confirm('部门名称不能为空');
                return;
            }
            checkName();
            if (result == 1) {
                window.confirm('部门名称已存在');
                return;
            }
            $("#tmpForm").attr("action","${base}/department/save");
            $("#tmpForm").submit();
        });
    });

    function checkName () {
        var name = $("#name").val();
        var id = $("#deptId").val();
        $.ajax({
            url: "${base}/department/checkName",
            data: {name: name, id: id},
            type: "post",
            dataType: "json",
            async: false,
            success: function (data) {
                console.log("data: " + data.status);
                result = data.status;
            }
        });
    }

</script>