<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增学校图册')" />
    <th:block th:include="include :: bootstrap-fileinput-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-atlas-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">
                        <option>请选择学校</option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">图册名称：</label>
                <div class="col-sm-8">
                    <input name="atlasName" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">图册封面：</label>
                <input id="atlasCover" type="hidden" name="atlasCover">
                <div class="col-sm-8">
                    <div class="file-loading">
                        <input id="singleFile" name="file" type="file">
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">图册内容：</label>
                <input id="atlasContent" type="hidden" name="atlasContent">
                <div class="col-sm-8">
                    <div class="file-loading">
                        <input id="multipleFile" name="files" type="file" multiple>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: bootstrap-fileinput-js" />
    <script th:inline="javascript">
        var prefix = ctx + "system/atlas"
        $("#form-atlas-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($('#atlasCover').val() == null) {
                $.modal.alertWarning("请先选择文件");
                return false;
            }
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-atlas-add').serialize()+
                    "&schoolId="+$("#schoolId")[0].value);
            }
        }

        $(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>");
                }
            });

            // 单图上传
            $("#singleFile").fileinput({
                uploadUrl: ctx + 'common/upload',
                maxFileCount: 1,
                autoReplace: true
            }).on('fileuploaded', function (event, data, previewId, index) {
                var rsp = data.response;
                $("#atlasCover").val(rsp.fileName);
            }).on('fileremoved', function (event, id, index) {
                $("input[name='" + event.currentTarget.id + "']").val('')
            })

            // 多图上传
            $("#multipleFile").fileinput({
                uploadUrl: ctx + 'common/uploads',
                uploadAsync: false
            }).on('filebatchuploadsuccess', function (event, data, previewId, index) {
                var rsp = data.response;
                $("#atlasContent").val(rsp.fileNames);
            }).on('fileremoved', function (event, id, index) {
                $("input[name='" + event.currentTarget.id + "']").val('')
            })
        });
    </script>
</body>
</html>