<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('新增轮播')"/>
    <th:block th:include="include :: image-upload-css"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-carousel-add">
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label">图片名称：</label>
                <div class="col-sm-8">
                    <input name="carouselName" class="form-control" type="text">
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label">图片：</label>
                <div class="col-sm-8">
<!--                    <input type="hidden" name="carouselImg">-->
                    <div id="image-upload-container"></div>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label">排序：</label>
                <div class="col-sm-8">
                    <input name="sort" class="form-control" type="text">
                </div>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: image-upload-js"/>
<script th:inline="javascript">
    var prefix = ctx + "carousel"
    $("#form-carousel-add").validate({
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            var uploadInstance = $('#image-upload-container').data('imageUpload');

            // 检查是否有上传组件实例
            if (uploadInstance) {
                var uploadedFiles = uploadInstance.getFiles();
                console.log('已上传的图片:', uploadedFiles);

                // 如果需要封面必填
                if (uploadedFiles.length === 0) {
                    $.modal.alertWarning("请上传图片");
                    return false;
                }

                // 将图片URL数组转换为逗号分隔的字符串
                var imageUrls = uploadedFiles.join(',');
                console.log('图片URL字符串:', imageUrls);

                // 注意：图片上传组件已经通过 addHiddenField 方法添加了隐藏字段
                // 我们不需要再次添加，只需要确保所有图片都已被收集
                // 组件会自动处理隐藏字段
            } else {
                // 如果没有上传组件实例，直接检查是否有隐藏字段
                var existingImages = $('#form-competition-add input[name="competitionImg"]').map(function() {
                    return $(this).val();
                }).get().filter(function(val) {
                    return val && val.trim() !== '';
                });

                if (existingImages.length === 0) {
                    $.modal.alertWarning("请上传图片");
                    return false;
                }
            }
            $.operate.save(prefix + "/add", $('#form-carousel-add').serialize());
        }
    }

    // 初始化图片上传组件
    $('#image-upload-container').imageUpload({
        uploadUrl: ctx + "common/upload",
        maxFiles: 1,
        maxSize: 5 * 1024 * 1024,
        allowedTypes: ['image/jpeg', 'image/png'],
        previewWidth: 200,
        previewHeight: 150,
        inputName: 'carouselImg'
    });
</script>
</body>
</html>