<!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-edit" th:object="${qmjsShoppingMall}">
        <input name="id" th:field="*{id}" type="hidden">
        <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" th:field="*{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">商品条码：</label>-->
<!--                <div class="col-sm-8">-->
<!--                    <input name="goodsCode" th:field="*{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}" th:field="*{goodsType}"></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 name="goodsUnit" th:field="*{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}" th:field="*{goodsCurrentPrice}"></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" th:field="*{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" th:field="*{suggestionPrice}" 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" th:field="*{goodsScore}" 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">商品库存：</label>-->
<!--                <div class="col-sm-8">-->
<!--                    <input name="goodsInventory" th:field="*{goodsInventory}" 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">-->
<!--                    <input name="inventoryCost" th:field="*{inventoryCost}" 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="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:field="*{goodsState}"></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">
                    <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">[[*{goodsDescribe}]]</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-edit").validate({
        focusCleanup: true,
        rules:{
            salePrice:{
                double:true
            },
            suggestionPrice:{
                double: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;
                }
                // 组件会自动更新隐藏字段，我们不需要额外处理
            } else {
                // 如果没有上传组件实例，检查是否有隐藏字段
                var existingImages = $('#form-competition-edit input[name="goodsImg"]').map(function() {
                    return $(this).val();
                }).get().filter(function(val) {
                    return val && val.trim() !== '';
                });

                if (existingImages.length === 0) {
                    $.modal.alertWarning("请上传封面图片");
                    return false;
                }
            }


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

   $(function(){
       // ============ 修复：初始化图片上传组件并回显已有图片 ============
       // 获取已有的封面图片数据（逗号分隔的字符串）
       var imgStr = [[${qmjsShoppingMall.goodsImg}]];
       var existingImages = [];

       console.log('原始图片字符串:', imgStr);

       if (imgStr && imgStr.trim() !== '') {
           // 分割逗号分隔的图片字符串
           existingImages = imgStr.split(',').map(function(img) {
               return img.trim();
           }).filter(function(img) {
               // 过滤掉空字符串和无效的URL
               return img !== '' && img !== null && img !== undefined;
           });

           console.log('解析后的图片数组:', existingImages);
           console.log('图片数量:', existingImages.length);
       }

       // 初始化图片上传组件
       var imageUploadConfig = {
           uploadUrl: ctx + "common/upload",
           maxFiles: 3,
           maxSize: 5 * 1024 * 1024,
           allowedTypes: ['image/jpeg', 'image/png'],
           previewWidth: 200,
           previewHeight: 150,
           inputName: 'goodsImg'
       };

       // 只有在有图片时才传递 initialFiles
       if (existingImages.length > 0) {
           imageUploadConfig.initialFiles = existingImages;
       }

       console.log('图片上传配置:', imageUploadConfig);

       $('#image-upload-container').imageUpload(imageUploadConfig);

       // 监听事件（可选）
       $('#image-upload-container').on('upload.success', function(e, data) {
           console.log('上传成功:', data.url);
       });

       $('#image-upload-container').on('upload.error', function(e, data) {
           console.error('上传失败:', data.error);
       });

       $('#image-upload-container').on('files.set', function(e, data) {
           console.log('图片已设置:', data.urls);
       });
       // ============ 修复结束 ============
   })
</script>
</body>
</html>