<!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 :: summernote-css"/>
    <th:block th:include="include :: bootstrap-fileinput-css"/>
    <!-- 自定义 CSS -->
    <style>
        /* 修改文本域的背景颜色和文字颜色 */
        .note-editable {
            background-color: black !important; /* 黑色背景 */
            color: white !important; /* 白色文字 */
        }
    </style>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content" >
    <form class="form-horizontal m" id="form-competition-add">
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-2 control-label is-required">赛事标题：</label>
                <div class="col-sm-4">
                    <input name="competitionTitle" class="form-control" type="text" placeholder="请输入赛事标题" maxlength="50"
                           required>
                </div>
                <label class="col-sm-2 control-label is-required">赛事类型：</label>
                <div class="col-sm-4">
                    <select name="competitionType" class="form-control"
                            th:with="type=${@dict.getType('competition_type')}" required>
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                th:value="${dict.dictValue}"></option>
                    </select>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-2 control-label is-required">最大人数/团队数：</label>
                <div class="col-sm-4">
                    <input name="teamMax" class="form-control" type="number" placeholder="请输入最大人数或团队数"
                           required>
                </div>
                <label class="col-sm-2 control-label">场地：</label>
                <div class="col-sm-4">
                    <input name="competitionPlace" class="form-control" type="text" placeholder="请输入场地名称" maxlength="50">
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-2 control-label is-required">报名费用：</label>
                <div class="col-sm-4">
                    <input name="competitionExpense" class="form-control" type="number" placeholder="请输入报名费用"
                           required>
                </div>
                <label class="col-sm-2 control-label is-required">观看费用：</label>
                <div class="col-sm-4">
                    <input name="viewingTicket" class="form-control" type="number" placeholder="请输入观看费用"
                           required>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-2 control-label is-required">赛事开始时间：</label>
                <div class="col-sm-4">
                    <div class="input-group date">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        <input name="applyStartTime" type="text" class="form-control" id="applyStartTime"
                               placeholder="年-月-日 时:分" required>
                    </div>
                </div>
                <label class="col-sm-2 control-label is-required">赛事结束时间：</label>
                <div class="col-sm-4">
                    <div class="input-group date">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        <input name="applyEndTime" type="text" class="form-control" id="applyEndTime"
                               placeholder="年-月-日 时:分" required>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-2 control-label is-required">取消报名截至时间：</label>
                <div class="col-sm-4">
                    <select name="applyBeforeTime" class="form-control" required>
                        <option value="1">赛事开始前1小时</option>
                        <option value="2">赛事开始前2小时</option>
                        <option value="3">赛事开始前3小时</option>
                        <option value="6">赛事开始前6小时</option>
                        <option value="12">赛事开始前12小时</option>
                        <option value="24">赛事开始前24小时</option>
                        <option value="48">赛事开始前48小时</option>
                    </select>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-2 control-label is-required">赛事详情：</label>
                <div class="col-sm-4">
                    <input type="hidden" class="form-control" name="competitionDetails" required>
                    <div class="summernote" id="competitionDetails"></div>
                </div>




                <label class="col-sm-2 control-label">报名须知：</label>
                <div class="col-sm-4">
                    <input type="hidden" class="form-control" name="registrationNotes">
                    <div class="summernote" id="registrationNotes"></div>
                </div>





            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">封面：</label>
            <div class="col-sm-10">
                <input type="hidden" name="competitionImg">
                <div class="file-loading">
                    <input class="form-control file-upload" id="competitionImg" name="file" type="file">
                </div>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: datetimepicker-js"/>
<th:block th:include="include :: summernote-js"/>
<th:block th:include="include :: bootstrap-fileinput-js"/>
<script th:inline="javascript">
    var prefix = ctx + "competition"
    $("#form-competition-add").validate({
        focusCleanup: true,
        rules: {
            teamMax: {
                digits: true
            },
            competitionExpense: {
                double: true
            },
            competitionExpense: {
                double: true
            }
        },
    });



    $(".file-upload").each(function (i) {
        var inputName = this.id;
        var val = $("input[name='" + inputName + "']").val();
        // 将已上传的图片路径分割成数组
        var initialPreview = val ? val.split(',') : [];

        $(this).fileinput({
            uploadUrl: ctx + 'common/upload',
            initialPreviewAsData: true,
            initialPreview: initialPreview,
            maxFileCount: 1,
            allowedFileExtensions: ['jpg', 'png'],
            maxFileSize: 10240,
            multiple: false,
            // required: true
        }).on('fileuploaded', function (event, data, previewId, index) {
            var inputName = event.currentTarget.id;
            var fullUrl = data.response.url; // 获取完整的URL
            var urlObject = new URL(fullUrl);
            var relativePath = urlObject.pathname; // 获取路径部分
            var newBaseUrl = "http://localhost"; // 新的URL
            var absoluteUrl = newBaseUrl + relativePath; // 拼接完整的URL

            // 直接设置输入框的值为新上传的图片URL
            $("input[name='" + inputName + "']").val(absoluteUrl);
        }).on('fileremoved', function (event, key, jqXHR, pd) {
            var inputName = event.currentTarget.id;
            // 移除文件后，清空输入框的值
            $("input[name='" + inputName + "']").val('');
        });

        $(this).fileinput('_initFileActions');
    });

    function submitHandler() {
        var applyStartTime = $('#applyStartTime').val()
        var applyEndTime = $('#applyEndTime').val()
        var currentTime = new Date().toISOString().slice(0, 16); // 当前时间，格式为 YYYY-MM-DD HH:MM


        if ($.validate.form()) {

            // 检查赛事开始时间不能小于当前时间
            if (applyStartTime < currentTime) {
                $.modal.alertWarning("赛事开始时间不能小于当前时间");
                return false;
            }

            // 检查赛事结束时间不能小于赛事开始时间
            if (applyEndTime < applyStartTime) {
                $.modal.alertWarning("赛事结束时间不能小于赛事开始时间");
                return false;
            }

            $.operate.save(prefix + "/add", $('#form-competition-add').serialize());
        }
    }

    $("input[name='applyStartTime']").datetimepicker({
        format: "yyyy-mm-dd hh:ii",
        autoclose: true
    });

    $("input[name='applyEndTime']").datetimepicker({
        format: "yyyy-mm-dd hh:ii",
        autoclose: true
    });

    $(function () {
        $('.summernote').summernote({
            lang: 'zh-CN',
            dialogsInBody: true,
            height: 300,
            callbacks: {
                onChange: function (contents, $edittable) {
                    $("input[name='" + this.id + "']").val(contents);
                },
                onImageUpload: function (files) {
                    var obj = this;
                    var data = new FormData();
                    data.append("file", files[0]);
                    $.ajax({
                        type: "post",
                        url: ctx + "common/upload",
                        data: data,
                        cache: false,
                        contentType: false,
                        processData: false,
                        dataType: 'json',
                        success: function (result) {
                            if (result.code == web_status.SUCCESS) {
                                $('#' + obj.id).summernote('insertImage', result.url);
                            } else {
                                $.modal.alertError(result.msg);
                            }
                        },
                        error: function (error) {
                            $.modal.alertWarning("图片上传失败。");
                        }
                    });
                }
            }
        });
    });
</script>
</body>
</html>