<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增学校设施')" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-facilities-add">
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">学校名称：</label>
                <div class="col-sm-8">
                    <select id="schoolId" class="form-control select2-multiple" required>
                        <option>请选择学校</option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">设施类型：</label>
                <div class="col-sm-8">
                    <select id="facilitiesType" class="form-control select2-multiple" required>
                        <option>请选择设施类型</option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">设施：</label>
                <div class="col-sm-8">
                    <label class="check-box" id="ff" name="ff">

                    </label>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var prefix = ctx + "system/facilities"
        $("#form-facilities-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-facilities-add').serialize()+
                    "&facilitiesType="+$("#facilitiesType").val()+
                "&schoolId="+$("#schoolId").val()
                );
            }
        }

        $(document).ready(function() {
            <!--异步请求-->
            $.ajax({
                <!--请求路径，与后端对应-->
                url: ctx + "system/school/getList",
                <!--请求类型-->
                type: "get",
                <!--期待的返回数据类型-->
                dataType: "json",
                <!--前端接收数据成功是执行的该函数-->
                success: function (data) {
                    <!--清空下拉框中的缓存-->
                    // $("#city").empty();
                    <!--使用循环解析后端传来的数据，并用使用js动态拼接html语言-->
                    for (var i = 0; i < data.length; i++)
                        $("#schoolId").append("<option value='" + data[i].id + "'>" + data[i].schoolCnName + "</option>");
                }
            });

            $.ajax({
                url: ctx+"system/icon/getTypeList",
                type: "get",
                dataType: "json",
                success: function(data){
                    for(var i=0; i<data.length; i++){
                        $("#facilitiesType").append("<option value='" + data[i] + "'>" + data[i] + "</option>");
                    }
                }
            });

            $("#facilitiesType").change(function(){
                var type1=$("#facilitiesType").val();
                $.ajax({
                    url: ctx+"system/icon/getFacilitiesList?type="+type1,
                    type: "get",
                    dataType: "json",
                    success: function(data){
                        $("#ff").empty();
                        for(var i=0; i<data.length; i++){
                            $("#ff").append("<input id=\"facilities\" name=\"facilities\" type=\"checkbox\" value=\"" + data[i].id +"\">"+data[i].facilitiesName+"</input>");
                        }
                    }
                });
            });
        });
    </script>
</body>
</html>