<!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-mall-add">
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">商品名称：</label>
                <div class="col-sm-8">
                    <input name="goodsName" class="form-control" type="text" maxlength="15" required>
                </div>
            </div>
        </div>
<!--        <div class="col-xs-12">-->
<!--            <div class="form-group">-->
<!--                <label class="col-sm-3 control-label is-required" >商品条码：</label>-->
<!--                <div class="col-sm-8">-->
<!--                    <input name="goodsCode" class="form-control" type="text" required>-->
<!--                </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">
                    <select name="goodsType" class="form-control" th:with="type=${@dict.getType('goods_type')}">
                        <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-3 control-label is-required">计量单位：</label>-->
<!--                <div class="col-sm-8">-->
<!--                    <input name="goodsUnit" class="form-control" type="text" required>-->
<!--                </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">
                    <select name="goodsCurrentPrice" class="form-control" th:with="type=${@dict.getType('sys_yes_no')}">
                        <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-3 control-label is-required">销售单价：</label>
                <div class="col-sm-8">
                    <input name="salePrice" class="form-control" type="text" maxlength="15" required>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">建议售价：</label>
                <div class="col-sm-8">
                    <input name="suggestionPrice" class="form-control" type="text" required maxlength="15">
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">积分兑换：</label>
                <div class="col-sm-8">
                    <input name="goodsScore" class="form-control" type="text" required maxlength="15">
                </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">
                    <select name="goodsState" class="form-control" th:with="type=${@dict.getType('goods_state')}" disabled>
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:attr="selected=${dict.dictValue == '1'}"></option>
                    </select>
                </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="goodsImg">-->
                    <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">
                    <textarea name="goodsDescribe" class="form-control" type="text" maxlength="200"></textarea>
                </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 + "system/mall"
    $("#form-mall-add").validate({
        focusCleanup: true,
        rules:{
            salePrice:{
                double:true
            },
            suggestionPrice:{
                double:true
            },
        },
    });

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

    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-mall-add').serialize());
        }
    }


</script>
</body>
</html>