|  | @@ -265,378 +265,8 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function
 | 
	
		
			
				|  |  |                      area: ['95%', '90%'],
 | 
	
		
			
				|  |  |                      callback: function(data) {
 | 
	
		
			
				|  |  |                          if (data && data.length > 0) {
 | 
	
		
			
				|  |  | -                            var goodsIds = [];
 | 
	
		
			
				|  |  | -                            
 | 
	
		
			
				|  |  | -                            // 收集商品ID
 | 
	
		
			
				|  |  | -                            $.each(data, function(index, item) {
 | 
	
		
			
				|  |  | -                                goodsIds.push(item.id);
 | 
	
		
			
				|  |  | -                            });
 | 
	
		
			
				|  |  | -                            
 | 
	
		
			
				|  |  | -                            // 默认折扣值
 | 
	
		
			
				|  |  | -                            var defaultDiscount = 9;
 | 
	
		
			
				|  |  | -                            
 | 
	
		
			
				|  |  | -                            // 使用模板渲染表格
 | 
	
		
			
				|  |  | -                            var html = Template('goodsTableTpl', {
 | 
	
		
			
				|  |  | -                                list: data,
 | 
	
		
			
				|  |  | -                                discount: defaultDiscount
 | 
	
		
			
				|  |  | -                            });
 | 
	
		
			
				|  |  | -                            
 | 
	
		
			
				|  |  | -                            // 清空容器并显示表格
 | 
	
		
			
				|  |  | -                            $('#selected-goods-container').empty().append(html);
 | 
	
		
			
				|  |  | -                            
 | 
	
		
			
				|  |  | -                            // 初始化分页表格
 | 
	
		
			
				|  |  | -                            $('#selected-goods-table').bootstrapTable({
 | 
	
		
			
				|  |  | -                                data: data,
 | 
	
		
			
				|  |  | -                                pagination: true,
 | 
	
		
			
				|  |  | -                                pageSize: 5,
 | 
	
		
			
				|  |  | -                                pageList: [5, 10, 20],
 | 
	
		
			
				|  |  | -                                sortable: true,
 | 
	
		
			
				|  |  | -                                search: false,
 | 
	
		
			
				|  |  | -                                classes: 'table table-hover table-striped',
 | 
	
		
			
				|  |  | -                                onPostBody: function() {
 | 
	
		
			
				|  |  | -                                    // 表格渲染完成后处理行属性
 | 
	
		
			
				|  |  | -                                    $('#selected-goods-table tbody tr').each(function(index) {
 | 
	
		
			
				|  |  | -                                        // 确保每行有data-id属性
 | 
	
		
			
				|  |  | -                                        var rowData = data[index];
 | 
	
		
			
				|  |  | -                                        if(rowData && rowData.id) {
 | 
	
		
			
				|  |  | -                                            $(this).attr('data-id', rowData.id);
 | 
	
		
			
				|  |  | -                                            console.log('设置行data-id属性:', rowData.id);
 | 
	
		
			
				|  |  | -                                        }
 | 
	
		
			
				|  |  | -                                    });
 | 
	
		
			
				|  |  | -                                },
 | 
	
		
			
				|  |  | -                                columns: [
 | 
	
		
			
				|  |  | -                                    {
 | 
	
		
			
				|  |  | -                                        field: 'goods',
 | 
	
		
			
				|  |  | -                                        title: '商品',
 | 
	
		
			
				|  |  | -                                        width: '30%',
 | 
	
		
			
				|  |  | -                                        formatter: function(value, row) {
 | 
	
		
			
				|  |  | -                                            return '<div class="goods-item">' +
 | 
	
		
			
				|  |  | -                                                   '<img src="' + Fast.api.cdnurl(row.image) + '" style="width:60px;height:60px;margin-right:10px;" class="pull-left img-thumbnail">' +
 | 
	
		
			
				|  |  | -                                                   '<div style="margin-left:70px;">' +
 | 
	
		
			
				|  |  | -                                                   '<h5 style="margin-top:0;font-weight:600;">' + row.title + '</h5>' +
 | 
	
		
			
				|  |  | -                                                   '<p class="text-muted">分类:' + (row.category ? row.category.name : '') + ' | 规格:' + (row.spec_type == 0 ? '单规格' : '多规格') + '</p>' +
 | 
	
		
			
				|  |  | -                                                   '</div>' +
 | 
	
		
			
				|  |  | -                                                   '</div>'
 | 
	
		
			
				|  |  | -                                        }
 | 
	
		
			
				|  |  | -                                    },
 | 
	
		
			
				|  |  | -                                    {
 | 
	
		
			
				|  |  | -                                        field: 'price',
 | 
	
		
			
				|  |  | -                                        title: '价格',
 | 
	
		
			
				|  |  | -                                        width: '10%',
 | 
	
		
			
				|  |  | -                                        align: 'center',
 | 
	
		
			
				|  |  | -                                        formatter: function(value, row) {
 | 
	
		
			
				|  |  | -                                            return '<span class="text-danger">¥' + row.price + '</span>';
 | 
	
		
			
				|  |  | -                                        }
 | 
	
		
			
				|  |  | -                                    },
 | 
	
		
			
				|  |  | -                                    {
 | 
	
		
			
				|  |  | -                                        field: 'stocks',
 | 
	
		
			
				|  |  | -                                        title: '现库存',
 | 
	
		
			
				|  |  | -                                        width: '10%',
 | 
	
		
			
				|  |  | -                                        align: 'center'
 | 
	
		
			
				|  |  | -                                    },
 | 
	
		
			
				|  |  | -                                    {
 | 
	
		
			
				|  |  | -                                        field: 'discount_stocks',
 | 
	
		
			
				|  |  | -                                        title: '折扣库存',
 | 
	
		
			
				|  |  | -                                        width: '20%',
 | 
	
		
			
				|  |  | -                                        align: 'center',
 | 
	
		
			
				|  |  | -                                        formatter: function(value, row) {
 | 
	
		
			
				|  |  | -                                            if(row.spec_type == 0) { // 只有单规格商品显示输入框
 | 
	
		
			
				|  |  | -                                                var discountData = $('#discount-data').val() ? JSON.parse($('#discount-data').val()) : {};
 | 
	
		
			
				|  |  | -                                                console.log('渲染折扣库存输入框:', row.id, row.stocks);
 | 
	
		
			
				|  |  | -                                                // 确保折扣库存默认等于现库存
 | 
	
		
			
				|  |  | -                                                var stocks = parseInt(row.stocks);
 | 
	
		
			
				|  |  | -                                                // 移除max属性,仅通过JS验证限制最大值
 | 
	
		
			
				|  |  | -                                                return '<input type="number" class="form-control discount-stocks-input" value="' + stocks + '" min="0" data-id="' + row.id + '" data-max-stocks="' + stocks + '" style="width:100%;">';
 | 
	
		
			
				|  |  | -                                            } else {
 | 
	
		
			
				|  |  | -                                                return ''; // 多规格商品留空
 | 
	
		
			
				|  |  | -                                            }
 | 
	
		
			
				|  |  | -                                        }
 | 
	
		
			
				|  |  | -                                    },
 | 
	
		
			
				|  |  | -                                    {
 | 
	
		
			
				|  |  | -                                        field: 'discount',
 | 
	
		
			
				|  |  | -                                        title: '折扣',
 | 
	
		
			
				|  |  | -                                        width: '15%',
 | 
	
		
			
				|  |  | -                                        align: 'center',
 | 
	
		
			
				|  |  | -                                        formatter: function(value, row) {
 | 
	
		
			
				|  |  | -                                            if(row.spec_type == 1) {
 | 
	
		
			
				|  |  | -                                                var discountData = $('#discount-data').val() ? JSON.parse($('#discount-data').val()) : {};
 | 
	
		
			
				|  |  | -                                                var hasSpec = discountData[row.id] && discountData[row.id].specs;
 | 
	
		
			
				|  |  | -                                                var specBtnText = hasSpec ? '规格折扣设置 <span class="label label-success">已设置</span>' : '规格折扣设置';
 | 
	
		
			
				|  |  | -                                                var html = '<div class="text-center">' +
 | 
	
		
			
				|  |  | -                                                       '<a href="javascript:;" class="btn btn-info btn-sm btn-spec-discount" data-id="' + row.id + '">' + specBtnText + '</a>' +
 | 
	
		
			
				|  |  | -                                                       '<div class="text-muted small" style="margin-top:8px;">请点击设置各规格折扣</div>';
 | 
	
		
			
				|  |  | -                                                
 | 
	
		
			
				|  |  | -                                                // 如果已设置规格折扣,显示汇总信息
 | 
	
		
			
				|  |  | -                                                if (hasSpec && discountData[row.id].summary) {
 | 
	
		
			
				|  |  | -                                                    var summary = discountData[row.id].summary;
 | 
	
		
			
				|  |  | -                                                    var infoText = '已设置' + summary.participate_count + '个规格,平均' + summary.avg_discount + '折,共' + summary.total_stocks + '件';
 | 
	
		
			
				|  |  | -                                                    html += '<div class="spec-discount-info text-muted" style="margin-top:5px;">' + infoText + '</div>';
 | 
	
		
			
				|  |  | -                                                }
 | 
	
		
			
				|  |  | -                                                
 | 
	
		
			
				|  |  | -                                                html += '</div>';
 | 
	
		
			
				|  |  | -                                                return html;
 | 
	
		
			
				|  |  | -                                            } else {
 | 
	
		
			
				|  |  | -                                                return '<div class="input-group">' +
 | 
	
		
			
				|  |  | -                                                       '<input type="number" class="form-control discount-input" value="' + defaultDiscount + '" min="0.1" max="10" step="0.1" data-price="' + row.price + '" data-id="' + row.id + '">' +
 | 
	
		
			
				|  |  | -                                                       '<span class="input-group-addon">折</span>' +
 | 
	
		
			
				|  |  | -                                                       '</div>';
 | 
	
		
			
				|  |  | -                                            }
 | 
	
		
			
				|  |  | -                                        }
 | 
	
		
			
				|  |  | -                                    },
 | 
	
		
			
				|  |  | -                                    {
 | 
	
		
			
				|  |  | -                                        field: 'discount_price',
 | 
	
		
			
				|  |  | -                                        title: '折后价',
 | 
	
		
			
				|  |  | -                                        width: '20%',
 | 
	
		
			
				|  |  | -                                        align: 'center',
 | 
	
		
			
				|  |  | -                                        formatter: function(value, row) {
 | 
	
		
			
				|  |  | -                                            if(row.spec_type == 0) {
 | 
	
		
			
				|  |  | -                                                return '<span class="discount-price text-success" id="discount-price-' + row.id + '">¥' + (row.price * defaultDiscount / 10).toFixed(2) + '</span>';
 | 
	
		
			
				|  |  | -                                            } else {
 | 
	
		
			
				|  |  | -                                                return '';
 | 
	
		
			
				|  |  | -                                            }
 | 
	
		
			
				|  |  | -                                        }
 | 
	
		
			
				|  |  | -                                    },
 | 
	
		
			
				|  |  | -                                    {
 | 
	
		
			
				|  |  | -                                        field: 'operate',
 | 
	
		
			
				|  |  | -                                        title: '操作',
 | 
	
		
			
				|  |  | -                                        width: '15%',
 | 
	
		
			
				|  |  | -                                        align: 'center',
 | 
	
		
			
				|  |  | -                                        formatter: function(value, row) {
 | 
	
		
			
				|  |  | -                                            return '<a href="javascript:;" class="btn btn-xs btn-danger remove-goods" data-id="' + row.id + '"><i class="fa fa-trash"></i> 删除</a>';
 | 
	
		
			
				|  |  | -                                        },
 | 
	
		
			
				|  |  | -                                        events: {
 | 
	
		
			
				|  |  | -                                            'click .remove-goods': function (e, value, row, index) {
 | 
	
		
			
				|  |  | -                                                // 从表格中移除该行
 | 
	
		
			
				|  |  | -                                                $('#selected-goods-table').bootstrapTable('remove', {
 | 
	
		
			
				|  |  | -                                                    field: 'id',
 | 
	
		
			
				|  |  | -                                                    values: [row.id]
 | 
	
		
			
				|  |  | -                                                });
 | 
	
		
			
				|  |  | -                                                
 | 
	
		
			
				|  |  | -                                                // 更新隐藏字段
 | 
	
		
			
				|  |  | -                                                var currentIds = $('#goods-ids').val().split(',');
 | 
	
		
			
				|  |  | -                                                var newIds = [];
 | 
	
		
			
				|  |  | -                                                $.each(currentIds, function(i, val) {
 | 
	
		
			
				|  |  | -                                                    if (val != row.id && val) {
 | 
	
		
			
				|  |  | -                                                        newIds.push(val);
 | 
	
		
			
				|  |  | -                                                    }
 | 
	
		
			
				|  |  | -                                                });
 | 
	
		
			
				|  |  | -                                                $('#goods-ids').val(newIds.join(','));
 | 
	
		
			
				|  |  | -                                                
 | 
	
		
			
				|  |  | -                                                // 移除折扣数据
 | 
	
		
			
				|  |  | -                                                var discountData = $('#discount-data').val() ? JSON.parse($('#discount-data').val()) : {};
 | 
	
		
			
				|  |  | -                                                if(discountData[row.id]) {
 | 
	
		
			
				|  |  | -                                                    delete discountData[row.id];
 | 
	
		
			
				|  |  | -                                                    $('#discount-data').val(JSON.stringify(discountData));
 | 
	
		
			
				|  |  | -                                                }
 | 
	
		
			
				|  |  | -                                                
 | 
	
		
			
				|  |  | -                                                // 如果表格为空,则移除整个表格
 | 
	
		
			
				|  |  | -                                                if(newIds.length === 0) {
 | 
	
		
			
				|  |  | -                                                    $('#selected-goods-container').empty();
 | 
	
		
			
				|  |  | -                                                }
 | 
	
		
			
				|  |  | -                                            }
 | 
	
		
			
				|  |  | -                                        }
 | 
	
		
			
				|  |  | -                                    }
 | 
	
		
			
				|  |  | -                                ]
 | 
	
		
			
				|  |  | -                            });
 | 
	
		
			
				|  |  | -                            
 | 
	
		
			
				|  |  | -                            // 更新隐藏字段
 | 
	
		
			
				|  |  | -                            $('#goods-ids').val(goodsIds.join(','));
 | 
	
		
			
				|  |  | -                            
 | 
	
		
			
				|  |  | -                            // 初始化折扣数据
 | 
	
		
			
				|  |  | -                            var discountData = {};
 | 
	
		
			
				|  |  | -                            $.each(data, function(index, item) {
 | 
	
		
			
				|  |  | -                                if (item.spec_type == 0) { // 单规格商品
 | 
	
		
			
				|  |  | -                                    // 确保折扣库存默认值为现有库存
 | 
	
		
			
				|  |  | -                                    var currentStocks = parseInt(item.stocks) || 0;
 | 
	
		
			
				|  |  | -                                    
 | 
	
		
			
				|  |  | -                                    // 单规格商品需要获取其默认SKU ID
 | 
	
		
			
				|  |  | -                                    var skuId = item.sku_id || 0; // 如果返回数据中有sku_id就使用,否则用0
 | 
	
		
			
				|  |  | -                                    
 | 
	
		
			
				|  |  | -                                    discountData[item.id] = {
 | 
	
		
			
				|  |  | -                                        id: item.id,
 | 
	
		
			
				|  |  | -                                        sku_id: skuId, // 添加SKU ID
 | 
	
		
			
				|  |  | -                                        discount: defaultDiscount,
 | 
	
		
			
				|  |  | -                                        discount_price: (item.price * defaultDiscount / 10).toFixed(2),
 | 
	
		
			
				|  |  | -                                        stocks: currentStocks // 默认折扣库存等于商品库存
 | 
	
		
			
				|  |  | -                                    };
 | 
	
		
			
				|  |  | -                                    console.log('初始化单规格商品:', item.id, 'SKU ID:', skuId, '库存:', currentStocks);
 | 
	
		
			
				|  |  | -                                }
 | 
	
		
			
				|  |  | -                            });
 | 
	
		
			
				|  |  | -                            $('#discount-data').val(JSON.stringify(discountData));
 | 
	
		
			
				|  |  | -                            
 | 
	
		
			
				|  |  | -                            // 同步更新商品详情信息
 | 
	
		
			
				|  |  | -                            updateGoodsInfo();
 | 
	
		
			
				|  |  | -                            
 | 
	
		
			
				|  |  | -                            // 等待表格渲染完成后修复折扣库存输入框
 | 
	
		
			
				|  |  | -                            setTimeout(function() {
 | 
	
		
			
				|  |  | -                                $('.discount-stocks-input').each(function() {
 | 
	
		
			
				|  |  | -                                    var $input = $(this);
 | 
	
		
			
				|  |  | -                                    var goodsId = $input.data('id');
 | 
	
		
			
				|  |  | -                                    var maxStocks = parseInt($input.data('max-stocks')) || 0;
 | 
	
		
			
				|  |  | -                                    
 | 
	
		
			
				|  |  | -                                    // 从折扣数据中获取正确的库存值
 | 
	
		
			
				|  |  | -                                    var discountData = $('#discount-data').val() ? JSON.parse($('#discount-data').val()) : {};
 | 
	
		
			
				|  |  | -                                    var correctStocks = discountData[goodsId] && discountData[goodsId].stocks !== undefined ? 
 | 
	
		
			
				|  |  | -                                                       discountData[goodsId].stocks : maxStocks;
 | 
	
		
			
				|  |  | -                                    
 | 
	
		
			
				|  |  | -                                    // 直接设置正确的库存值
 | 
	
		
			
				|  |  | -                                    $input.val(correctStocks);
 | 
	
		
			
				|  |  | -                                    console.log('修复折扣库存输入框值:', goodsId, '设置为:', correctStocks);
 | 
	
		
			
				|  |  | -                                });
 | 
	
		
			
				|  |  | -                            }, 500);
 | 
	
		
			
				|  |  | -                            
 | 
	
		
			
				|  |  | -                            // 绑定折扣库存输入事件
 | 
	
		
			
				|  |  | -                            $(document).on('input', '.discount-stocks-input', function() {
 | 
	
		
			
				|  |  | -                                var stocks = parseInt($(this).val()) || 0;
 | 
	
		
			
				|  |  | -                                var goodsId = $(this).data('id');
 | 
	
		
			
				|  |  | -                                var maxStocks = parseInt($(this).data('max-stocks')) || 0;
 | 
	
		
			
				|  |  | -                                
 | 
	
		
			
				|  |  | -                                console.log('折扣库存输入事件:', goodsId, '输入值:', stocks, '最大值:', maxStocks);
 | 
	
		
			
				|  |  | -                                
 | 
	
		
			
				|  |  | -                                // 限制输入范围,确保不超过现库存
 | 
	
		
			
				|  |  | -                                if(stocks < 0) stocks = 0;
 | 
	
		
			
				|  |  | -                                if(stocks > maxStocks) stocks = maxStocks;
 | 
	
		
			
				|  |  | -                                $(this).val(stocks);
 | 
	
		
			
				|  |  | -                                
 | 
	
		
			
				|  |  | -                                // 更新商品折扣数据
 | 
	
		
			
				|  |  | -                                var discountData = $('#discount-data').val() ? JSON.parse($('#discount-data').val()) : {};
 | 
	
		
			
				|  |  | -                                
 | 
	
		
			
				|  |  | -                                if(discountData[goodsId]) {
 | 
	
		
			
				|  |  | -                                    discountData[goodsId].stocks = stocks;
 | 
	
		
			
				|  |  | -                                } else {
 | 
	
		
			
				|  |  | -                                    // 新建数据时需要包含SKU ID
 | 
	
		
			
				|  |  | -                                    discountData[goodsId] = {
 | 
	
		
			
				|  |  | -                                        id: goodsId,
 | 
	
		
			
				|  |  | -                                        sku_id: 0, // 单规格商品默认SKU ID为0,实际应从商品数据获取
 | 
	
		
			
				|  |  | -                                        stocks: stocks
 | 
	
		
			
				|  |  | -                                    };
 | 
	
		
			
				|  |  | -                                }
 | 
	
		
			
				|  |  | -                                
 | 
	
		
			
				|  |  | -                                $('#discount-data').val(JSON.stringify(discountData));
 | 
	
		
			
				|  |  | -                            });
 | 
	
		
			
				|  |  | -                            
 | 
	
		
			
				|  |  | -                            // 绑定折扣输入事件
 | 
	
		
			
				|  |  | -                            $(document).on('input', '.discount-input', function() {
 | 
	
		
			
				|  |  | -                                var discount = parseFloat($(this).val()) || 0;
 | 
	
		
			
				|  |  | -                                if(discount < 0.1) discount = 0.1;
 | 
	
		
			
				|  |  | -                                if(discount > 10) discount = 10;
 | 
	
		
			
				|  |  | -                                
 | 
	
		
			
				|  |  | -                                var price = parseFloat($(this).data('price')) || 0;
 | 
	
		
			
				|  |  | -                                var goodsId = $(this).data('id');
 | 
	
		
			
				|  |  | -                                var discountPrice = (price * discount / 10).toFixed(2);
 | 
	
		
			
				|  |  | -                                
 | 
	
		
			
				|  |  | -                                $('#discount-price-' + goodsId).text('¥' + discountPrice);
 | 
	
		
			
				|  |  | -                                
 | 
	
		
			
				|  |  | -                                // 更新商品折扣数据
 | 
	
		
			
				|  |  | -                                var discountData = $('#discount-data').val() ? JSON.parse($('#discount-data').val()) : {};
 | 
	
		
			
				|  |  | -                                
 | 
	
		
			
				|  |  | -                                // 保留折扣库存值和SKU ID,如果已存在
 | 
	
		
			
				|  |  | -                                var stocks = discountData[goodsId] && discountData[goodsId].stocks ? discountData[goodsId].stocks : null;
 | 
	
		
			
				|  |  | -                                var skuId = discountData[goodsId] && discountData[goodsId].sku_id ? discountData[goodsId].sku_id : 0;
 | 
	
		
			
				|  |  | -                                
 | 
	
		
			
				|  |  | -                                var goodsDiscount = {
 | 
	
		
			
				|  |  | -                                    id: goodsId,
 | 
	
		
			
				|  |  | -                                    sku_id: skuId, // 保留SKU ID
 | 
	
		
			
				|  |  | -                                    discount: discount,
 | 
	
		
			
				|  |  | -                                    discount_price: discountPrice
 | 
	
		
			
				|  |  | -                                };
 | 
	
		
			
				|  |  | -                                
 | 
	
		
			
				|  |  | -                                // 如果已有设置折扣库存,保留该值
 | 
	
		
			
				|  |  | -                                if (stocks !== null) {
 | 
	
		
			
				|  |  | -                                    goodsDiscount.stocks = stocks;
 | 
	
		
			
				|  |  | -                                }
 | 
	
		
			
				|  |  | -                                
 | 
	
		
			
				|  |  | -                                // 存储折扣数据
 | 
	
		
			
				|  |  | -                                discountData[goodsId] = goodsDiscount;
 | 
	
		
			
				|  |  | -                                $('#discount-data').val(JSON.stringify(discountData));
 | 
	
		
			
				|  |  | -                                
 | 
	
		
			
				|  |  | -                                // 更新商品详情信息
 | 
	
		
			
				|  |  | -                                updateGoodsInfo();
 | 
	
		
			
				|  |  | -                            });
 | 
	
		
			
				|  |  | -                            
 | 
	
		
			
				|  |  | -                            // 处理规格设置按钮点击事件
 | 
	
		
			
				|  |  | -                            $(document).on('click', '.btn-spec-discount', function() {
 | 
	
		
			
				|  |  | -                                var goodsId = $(this).data('id');
 | 
	
		
			
				|  |  | -                                var $row = $(this).closest('tr');
 | 
	
		
			
				|  |  | -                                var hasDiscountSetting = $row.attr('data-has-discount-setting');
 | 
	
		
			
				|  |  | -                                var specData = '';
 | 
	
		
			
				|  |  | -                                
 | 
	
		
			
				|  |  | -                                console.log('点击规格折扣设置按钮:', goodsId, '当前设置状态:', hasDiscountSetting);
 | 
	
		
			
				|  |  | -                                
 | 
	
		
			
				|  |  | -                                // 如果已经有设置,则传递已有的数据
 | 
	
		
			
				|  |  | -                                if (hasDiscountSetting && $row.attr('data-discount-data')) {
 | 
	
		
			
				|  |  | -                                    specData = encodeURIComponent($row.attr('data-discount-data'));
 | 
	
		
			
				|  |  | -                                    console.log('已有折扣数据:', $row.attr('data-discount-data'));
 | 
	
		
			
				|  |  | -                                }
 | 
	
		
			
				|  |  | -                                
 | 
	
		
			
				|  |  | -                                Fast.api.open('marketing/discount/spec_discount?goods_id=' + goodsId + '&spec_data=' + specData, '规格折扣设置', {
 | 
	
		
			
				|  |  | -                                    area: ['90%', '90%'],
 | 
	
		
			
				|  |  | -                                    callback: function(data) {
 | 
	
		
			
				|  |  | -                                        if (data && data.goodsId && data.specs) {
 | 
	
		
			
				|  |  | -                                            console.log('规格折扣设置返回数据:', data);
 | 
	
		
			
				|  |  | -                                            
 | 
	
		
			
				|  |  | -                                            // 保存数据到行属性
 | 
	
		
			
				|  |  | -                                            $row.attr('data-has-discount-setting', 'true');
 | 
	
		
			
				|  |  | -                                            $row.attr('data-discount-data', JSON.stringify(data));
 | 
	
		
			
				|  |  | -                                            
 | 
	
		
			
				|  |  | -                                            // 调试确认设置成功
 | 
	
		
			
				|  |  | -                                            console.log('设置后的data-discount-data属性:', $row.attr('data-discount-data'));
 | 
	
		
			
				|  |  | -                                            
 | 
	
		
			
				|  |  | -                                            // 更新按钮文字
 | 
	
		
			
				|  |  | -                                            $row.find('.btn-spec-discount').html('规格折扣设置 <span class="label label-success">已设置</span>');
 | 
	
		
			
				|  |  | -                                            
 | 
	
		
			
				|  |  | -                                            // 更新状态和折扣显示
 | 
	
		
			
				|  |  | -                                            $row.find('.discount-status').text('已设置');
 | 
	
		
			
				|  |  | -                                            
 | 
	
		
			
				|  |  | -                                            // 计算平均折扣和参与规格数量
 | 
	
		
			
				|  |  | -                                            var totalDiscount = 0;
 | 
	
		
			
				|  |  | -                                            var count = 0;
 | 
	
		
			
				|  |  | -                                            var totalStocks = 0;
 | 
	
		
			
				|  |  | -                                            
 | 
	
		
			
				|  |  | -                                            $.each(data.specs, function(id, item) {
 | 
	
		
			
				|  |  | -                                                if (item.participate) {
 | 
	
		
			
				|  |  | -                                                    totalDiscount += parseFloat(item.discount);
 | 
	
		
			
				|  |  | -                                                    totalStocks += parseInt(item.stocks);
 | 
	
		
			
				|  |  | -                                                    count++;
 | 
	
		
			
				|  |  | -                                                }
 | 
	
		
			
				|  |  | -                                            });
 | 
	
		
			
				|  |  | -                                            
 | 
	
		
			
				|  |  | -                                            var avgDiscount = count > 0 ? (totalDiscount / count).toFixed(1) : '-';
 | 
	
		
			
				|  |  | -                                            
 | 
	
		
			
				|  |  | -                                            // 添加汇总信息
 | 
	
		
			
				|  |  | -                                            var summary = {
 | 
	
		
			
				|  |  | -                                                participate_count: count,
 | 
	
		
			
				|  |  | -                                                avg_discount: avgDiscount,
 | 
	
		
			
				|  |  | -                                                total_stocks: totalStocks
 | 
	
		
			
				|  |  | -                                            };
 | 
	
		
			
				|  |  | -                                            
 | 
	
		
			
				|  |  | -                                            // 更新显示
 | 
	
		
			
				|  |  | -                                            var infoText = '已设置' + summary.participate_count + '个规格,平均' + summary.avg_discount + '折,共' + summary.total_stocks + '件';
 | 
	
		
			
				|  |  | -                                            
 | 
	
		
			
				|  |  | -                                            // 如果已有汇总信息区域则更新,否则创建
 | 
	
		
			
				|  |  | -                                            var $infoArea = $row.find('.spec-discount-info');
 | 
	
		
			
				|  |  | -                                            if ($infoArea.length > 0) {
 | 
	
		
			
				|  |  | -                                                $infoArea.text(infoText);
 | 
	
		
			
				|  |  | -                                            } else {
 | 
	
		
			
				|  |  | -                                                $row.find('.btn-spec-discount').parent().append('<div class="spec-discount-info text-muted" style="margin-top:5px;">' + infoText + '</div>');
 | 
	
		
			
				|  |  | -                                            }
 | 
	
		
			
				|  |  | -                                            
 | 
	
		
			
				|  |  | -                                            // 将汇总信息添加到数据中
 | 
	
		
			
				|  |  | -                                            data.summary = summary;
 | 
	
		
			
				|  |  | -                                            
 | 
	
		
			
				|  |  | -                                            // 将折扣数据保存到隐藏字段
 | 
	
		
			
				|  |  | -                                            var discountData = $('#discount-data').val() ? JSON.parse($('#discount-data').val()) : {};
 | 
	
		
			
				|  |  | -                                            discountData[goodsId] = data;
 | 
	
		
			
				|  |  | -                                            $('#discount-data').val(JSON.stringify(discountData));
 | 
	
		
			
				|  |  | -                                            
 | 
	
		
			
				|  |  | -                                            // 更新商品详情信息
 | 
	
		
			
				|  |  | -                                            updateGoodsInfo();
 | 
	
		
			
				|  |  | -                                            
 | 
	
		
			
				|  |  | -                                            console.log('多规格商品设置后的折扣数据:', $('#discount-data').val());
 | 
	
		
			
				|  |  | -                                        }
 | 
	
		
			
				|  |  | -                                    }
 | 
	
		
			
				|  |  | -                                });
 | 
	
		
			
				|  |  | -                            });
 | 
	
		
			
				|  |  | +                            // 使用共享的商品渲染方法
 | 
	
		
			
				|  |  | +                            Controller.renderSelectedGoods(data, false);
 | 
	
		
			
				|  |  |                          }
 | 
	
		
			
				|  |  |                      }
 | 
	
		
			
				|  |  |                  });
 | 
	
	
		
			
				|  | @@ -656,173 +286,215 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function
 | 
	
		
			
				|  |  |              
 | 
	
		
			
				|  |  |              // 初始化已选择的商品
 | 
	
		
			
				|  |  |              var goodsIds = $('#goods-ids').val();
 | 
	
		
			
				|  |  | -            var discountData = $('#discount-data').val() ? JSON.parse($('#discount-data').val()) : {};
 | 
	
		
			
				|  |  | +            var goodsInfo = $('#goods-info').val() ? JSON.parse($('#goods-info').val()) : [];
 | 
	
		
			
				|  |  |              
 | 
	
		
			
				|  |  | -            if(goodsIds) {
 | 
	
		
			
				|  |  | -                // 加载已选择的商品数据
 | 
	
		
			
				|  |  | -                Fast.api.ajax({
 | 
	
		
			
				|  |  | -                    url: 'shop/goods/get_goods_by_ids',
 | 
	
		
			
				|  |  | -                    data: {ids: goodsIds}
 | 
	
		
			
				|  |  | -                }, function(data) {
 | 
	
		
			
				|  |  | -                    if(data && data.length > 0) {
 | 
	
		
			
				|  |  | -                        // 使用模板渲染表格
 | 
	
		
			
				|  |  | -                        var html = Template('goodsTableTpl', {
 | 
	
		
			
				|  |  | -                            list: data
 | 
	
		
			
				|  |  | -                        });
 | 
	
		
			
				|  |  | +            // 从goods_info构建折扣数据映射
 | 
	
		
			
				|  |  | +            var discountData = {};
 | 
	
		
			
				|  |  | +            if (goodsInfo && goodsInfo.length > 0) {
 | 
	
		
			
				|  |  | +                $.each(goodsInfo, function(index, item) {
 | 
	
		
			
				|  |  | +                    if (item.goods_id) {
 | 
	
		
			
				|  |  | +                        discountData[item.goods_id] = {
 | 
	
		
			
				|  |  | +                            id: item.goods_id,
 | 
	
		
			
				|  |  | +                            sku_id: item.sku_id || 0,
 | 
	
		
			
				|  |  | +                            discount: item.discount || 9,
 | 
	
		
			
				|  |  | +                            discount_price: item.discount_price || '',
 | 
	
		
			
				|  |  | +                            stocks: item.discount_stocks || item.stock || 0
 | 
	
		
			
				|  |  | +                        };
 | 
	
		
			
				|  |  |                          
 | 
	
		
			
				|  |  | -                        // 清空容器并显示表格
 | 
	
		
			
				|  |  | -                        $('#selected-goods-container').empty().append(html);
 | 
	
		
			
				|  |  | -                        
 | 
	
		
			
				|  |  | -                        // 初始化分页表格
 | 
	
		
			
				|  |  | -                        $('#selected-goods-table').bootstrapTable({
 | 
	
		
			
				|  |  | -                            data: data,
 | 
	
		
			
				|  |  | -                            pagination: true,
 | 
	
		
			
				|  |  | -                            pageSize: 5,
 | 
	
		
			
				|  |  | -                            pageList: [5, 10, 20],
 | 
	
		
			
				|  |  | -                            sortable: true,
 | 
	
		
			
				|  |  | -                            search: false,
 | 
	
		
			
				|  |  | -                            classes: 'table table-hover table-striped',
 | 
	
		
			
				|  |  | -                            columns: [
 | 
	
		
			
				|  |  | -                                {
 | 
	
		
			
				|  |  | -                                    field: 'goods',
 | 
	
		
			
				|  |  | -                                    title: '商品',
 | 
	
		
			
				|  |  | -                                    width: '30%',
 | 
	
		
			
				|  |  | -                                    formatter: function(value, row) {
 | 
	
		
			
				|  |  | -                                        return '<div class="goods-item">' +
 | 
	
		
			
				|  |  | -                                               '<img src="' + Fast.api.cdnurl(row.image) + '" style="width:60px;height:60px;margin-right:10px;" class="pull-left img-thumbnail">' +
 | 
	
		
			
				|  |  | -                                               '<div style="margin-left:70px;">' +
 | 
	
		
			
				|  |  | -                                               '<h5 style="margin-top:0;font-weight:600;">' + row.title + '</h5>' +
 | 
	
		
			
				|  |  | -                                               '<p class="text-muted">分类:' + (row.category ? row.category.name : '') + ' | 规格:' + (row.spec_type == 0 ? '单规格' : '多规格') + '</p>' +
 | 
	
		
			
				|  |  | -                                               '</div>' +
 | 
	
		
			
				|  |  | -                                               '</div>'
 | 
	
		
			
				|  |  | -                                    }
 | 
	
		
			
				|  |  | -                                },
 | 
	
		
			
				|  |  | -                                {
 | 
	
		
			
				|  |  | -                                    field: 'price',
 | 
	
		
			
				|  |  | -                                    title: '价格',
 | 
	
		
			
				|  |  | -                                    width: '10%',
 | 
	
		
			
				|  |  | -                                    align: 'center',
 | 
	
		
			
				|  |  | -                                    formatter: function(value, row) {
 | 
	
		
			
				|  |  | -                                        return '<span class="text-danger">¥' + row.price + '</span>';
 | 
	
		
			
				|  |  | -                                    }
 | 
	
		
			
				|  |  | -                                },
 | 
	
		
			
				|  |  | -                                {
 | 
	
		
			
				|  |  | -                                    field: 'stocks',
 | 
	
		
			
				|  |  | -                                    title: '现库存',
 | 
	
		
			
				|  |  | -                                    width: '10%',
 | 
	
		
			
				|  |  | -                                    align: 'center'
 | 
	
		
			
				|  |  | -                                },
 | 
	
		
			
				|  |  | -                                {
 | 
	
		
			
				|  |  | -                                    field: 'discount_stocks',
 | 
	
		
			
				|  |  | -                                    title: '折扣库存',
 | 
	
		
			
				|  |  | -                                    width: '10%',
 | 
	
		
			
				|  |  | -                                    align: 'center',
 | 
	
		
			
				|  |  | -                                    formatter: function(value, row) {
 | 
	
		
			
				|  |  | -                                                                                if(row.spec_type == 0) { // 只有单规格商品显示输入框
 | 
	
		
			
				|  |  | -                                            // 从折扣数据中获取库存值,如果没有则使用商品库存
 | 
	
		
			
				|  |  | -                                            var currentStocks = discountData[row.id] && discountData[row.id].stocks !== undefined ? 
 | 
	
		
			
				|  |  | -                                                               discountData[row.id].stocks : parseInt(row.stocks);
 | 
	
		
			
				|  |  | -                                            return '<input type="number" class="form-control discount-stocks-input" value="' + currentStocks + '" min="0" data-id="' + row.id + '" data-max-stocks="' + parseInt(row.stocks) + '" style="width:100%;">';
 | 
	
		
			
				|  |  | -                                        } else {
 | 
	
		
			
				|  |  | -                                            return ''; // 多规格商品留空
 | 
	
		
			
				|  |  | -                                        }
 | 
	
		
			
				|  |  | -                                    }
 | 
	
		
			
				|  |  | -                                },
 | 
	
		
			
				|  |  | -                                {
 | 
	
		
			
				|  |  | -                                    field: 'discount',
 | 
	
		
			
				|  |  | -                                    title: '折扣',
 | 
	
		
			
				|  |  | -                                    width: '15%',
 | 
	
		
			
				|  |  | -                                    align: 'center',
 | 
	
		
			
				|  |  | -                                    formatter: function(value, row) {
 | 
	
		
			
				|  |  | -                                        if(row.spec_type == 1) {
 | 
	
		
			
				|  |  | -                                            var hasSpec = discountData[row.id] && discountData[row.id].specs;
 | 
	
		
			
				|  |  | -                                            var specBtnText = hasSpec ? '规格折扣设置 <span class="label label-success">已设置</span>' : '规格折扣设置';
 | 
	
		
			
				|  |  | -                                            var html = '<div class="text-center">' +
 | 
	
		
			
				|  |  | -                                                   '<a href="javascript:;" class="btn btn-info btn-sm btn-spec-discount" data-id="' + row.id + '">' + specBtnText + '</a>' +
 | 
	
		
			
				|  |  | -                                                   '<div class="text-muted small" style="margin-top:8px;">请点击设置各规格折扣</div>';
 | 
	
		
			
				|  |  | -                                            
 | 
	
		
			
				|  |  | -                                            // 如果已设置规格折扣,显示汇总信息
 | 
	
		
			
				|  |  | -                                            if (hasSpec && discountData[row.id].summary) {
 | 
	
		
			
				|  |  | -                                                var summary = discountData[row.id].summary;
 | 
	
		
			
				|  |  | -                                                var infoText = '已设置' + summary.participate_count + '个规格,平均' + summary.avg_discount + '折,共' + summary.total_stocks + '件';
 | 
	
		
			
				|  |  | -                                                html += '<div class="spec-discount-info text-muted" style="margin-top:5px;">' + infoText + '</div>';
 | 
	
		
			
				|  |  | -                                            }
 | 
	
		
			
				|  |  | -                                            
 | 
	
		
			
				|  |  | -                                            html += '</div>';
 | 
	
		
			
				|  |  | -                                            return html;
 | 
	
		
			
				|  |  | -                                        } else {
 | 
	
		
			
				|  |  | -                                            var discount = (discountData[row.id] && discountData[row.id].discount) ? discountData[row.id].discount : 9;
 | 
	
		
			
				|  |  | -                                            return '<div class="input-group">' +
 | 
	
		
			
				|  |  | -                                                   '<input type="number" class="form-control discount-input" value="' + discount + '" min="0.1" max="10" step="0.1" data-price="' + row.price + '" data-id="' + row.id + '">' +
 | 
	
		
			
				|  |  | -                                                   '<span class="input-group-addon">折</span>' +
 | 
	
		
			
				|  |  | -                                                   '</div>';
 | 
	
		
			
				|  |  | -                                        }
 | 
	
		
			
				|  |  | -                                    }
 | 
	
		
			
				|  |  | -                                },
 | 
	
		
			
				|  |  | -                                {
 | 
	
		
			
				|  |  | -                                    field: 'discount_price',
 | 
	
		
			
				|  |  | -                                    title: '折后价',
 | 
	
		
			
				|  |  | -                                    width: '20%',
 | 
	
		
			
				|  |  | -                                    align: 'center',
 | 
	
		
			
				|  |  | -                                    formatter: function(value, row) {
 | 
	
		
			
				|  |  | -                                        if(row.spec_type == 0) {
 | 
	
		
			
				|  |  | -                                            var discount = (discountData[row.id] && discountData[row.id].discount) ? discountData[row.id].discount : 9;
 | 
	
		
			
				|  |  | -                                            var discountPrice = (row.price * discount / 10).toFixed(2);
 | 
	
		
			
				|  |  | -                                            return '<span class="discount-price text-success" id="discount-price-' + row.id + '">¥' + discountPrice + '</span>';
 | 
	
		
			
				|  |  | -                                        } else {
 | 
	
		
			
				|  |  | -                                            return '';
 | 
	
		
			
				|  |  | -                                        }
 | 
	
		
			
				|  |  | -                                    }
 | 
	
		
			
				|  |  | -                                },
 | 
	
		
			
				|  |  | -                                {
 | 
	
		
			
				|  |  | -                                    field: 'operate',
 | 
	
		
			
				|  |  | -                                    title: '操作',
 | 
	
		
			
				|  |  | -                                    width: '15%',
 | 
	
		
			
				|  |  | -                                    align: 'center',
 | 
	
		
			
				|  |  | -                                    formatter: function(value, row) {
 | 
	
		
			
				|  |  | -                                        return '<a href="javascript:;" class="btn btn-xs btn-danger remove-goods" data-id="' + row.id + '"><i class="fa fa-trash"></i> 删除</a>';
 | 
	
		
			
				|  |  | -                                    },
 | 
	
		
			
				|  |  | -                                    events: {
 | 
	
		
			
				|  |  | -                                        'click .remove-goods': function (e, value, row, index) {
 | 
	
		
			
				|  |  | -                                            // 从表格中移除该行
 | 
	
		
			
				|  |  | -                                            $('#selected-goods-table').bootstrapTable('remove', {
 | 
	
		
			
				|  |  | -                                                field: 'id',
 | 
	
		
			
				|  |  | -                                                values: [row.id]
 | 
	
		
			
				|  |  | -                                            });
 | 
	
		
			
				|  |  | -                                            
 | 
	
		
			
				|  |  | -                                            // 更新隐藏字段
 | 
	
		
			
				|  |  | -                                            var currentIds = $('#goods-ids').val().split(',');
 | 
	
		
			
				|  |  | -                                            var newIds = [];
 | 
	
		
			
				|  |  | -                                            $.each(currentIds, function(i, val) {
 | 
	
		
			
				|  |  | -                                                if (val != row.id && val) {
 | 
	
		
			
				|  |  | -                                                    newIds.push(val);
 | 
	
		
			
				|  |  | -                                                }
 | 
	
		
			
				|  |  | -                                            });
 | 
	
		
			
				|  |  | -                                            $('#goods-ids').val(newIds.join(','));
 | 
	
		
			
				|  |  | -                                            
 | 
	
		
			
				|  |  | -                                            // 移除折扣数据
 | 
	
		
			
				|  |  | -                                            var discountData = $('#discount-data').val() ? JSON.parse($('#discount-data').val()) : {};
 | 
	
		
			
				|  |  | -                                            if(discountData[row.id]) {
 | 
	
		
			
				|  |  | -                                                delete discountData[row.id];
 | 
	
		
			
				|  |  | -                                                $('#discount-data').val(JSON.stringify(discountData));
 | 
	
		
			
				|  |  | -                                            }
 | 
	
		
			
				|  |  | -                                            
 | 
	
		
			
				|  |  | -                                            // 如果表格为空,则移除整个表格
 | 
	
		
			
				|  |  | -                                            if(newIds.length === 0) {
 | 
	
		
			
				|  |  | -                                                $('#selected-goods-container').empty();
 | 
	
		
			
				|  |  | -                                            }
 | 
	
		
			
				|  |  | -                                        }
 | 
	
		
			
				|  |  | -                                    }
 | 
	
		
			
				|  |  | -                                }
 | 
	
		
			
				|  |  | -                            ]
 | 
	
		
			
				|  |  | -                        });
 | 
	
		
			
				|  |  | +                        // 处理多规格商品
 | 
	
		
			
				|  |  | +                        if (item.spec && item.spec.length > 0) {
 | 
	
		
			
				|  |  | +                            var specs = {};
 | 
	
		
			
				|  |  | +                            var summary = {
 | 
	
		
			
				|  |  | +                                participate_count: 0,
 | 
	
		
			
				|  |  | +                                avg_discount: 0,
 | 
	
		
			
				|  |  | +                                total_stocks: 0
 | 
	
		
			
				|  |  | +                            };
 | 
	
		
			
				|  |  | +                            
 | 
	
		
			
				|  |  | +                            var totalDiscount = 0;
 | 
	
		
			
				|  |  | +                            $.each(item.spec, function(specIndex, spec) {
 | 
	
		
			
				|  |  | +                                specs[spec.sku_id] = {
 | 
	
		
			
				|  |  | +                                    id: spec.sku_id,
 | 
	
		
			
				|  |  | +                                    participate: true,
 | 
	
		
			
				|  |  | +                                    discount: spec.discount,
 | 
	
		
			
				|  |  | +                                    discount_price: spec.discount_price,
 | 
	
		
			
				|  |  | +                                    stocks: spec.discount_stocks
 | 
	
		
			
				|  |  | +                                };
 | 
	
		
			
				|  |  | +                                
 | 
	
		
			
				|  |  | +                                summary.participate_count++;
 | 
	
		
			
				|  |  | +                                totalDiscount += parseFloat(spec.discount);
 | 
	
		
			
				|  |  | +                                summary.total_stocks += parseInt(spec.discount_stocks);
 | 
	
		
			
				|  |  | +                            });
 | 
	
		
			
				|  |  | +                            
 | 
	
		
			
				|  |  | +                            summary.avg_discount = summary.participate_count > 0 ? 
 | 
	
		
			
				|  |  | +                                (totalDiscount / summary.participate_count).toFixed(1) : '0';
 | 
	
		
			
				|  |  | +                            
 | 
	
		
			
				|  |  | +                            discountData[item.goods_id].specs = specs;
 | 
	
		
			
				|  |  | +                            discountData[item.goods_id].summary = summary;
 | 
	
		
			
				|  |  | +                        }
 | 
	
		
			
				|  |  |                      }
 | 
	
		
			
				|  |  | -                    return false;
 | 
	
		
			
				|  |  |                  });
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |              
 | 
	
		
			
				|  |  | -            // 选择商品功能和删除商品功能,与add方法相同
 | 
	
		
			
				|  |  | -            Controller.add();
 | 
	
		
			
				|  |  | +            console.log('编辑页面初始化:', {
 | 
	
		
			
				|  |  | +                goodsIds: goodsIds,
 | 
	
		
			
				|  |  | +                goodsInfo: goodsInfo,
 | 
	
		
			
				|  |  | +                discountData: discountData
 | 
	
		
			
				|  |  | +            });
 | 
	
		
			
				|  |  | +            
 | 
	
		
			
				|  |  | +            // 设置discount-data隐藏字段以便共享方法使用
 | 
	
		
			
				|  |  | +            if (!$('#discount-data').length) {
 | 
	
		
			
				|  |  | +                $('#goods-info').after('<input type="hidden" id="discount-data" value="">');
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            $('#discount-data').val(JSON.stringify(discountData));
 | 
	
		
			
				|  |  | +            
 | 
	
		
			
				|  |  | +            if(goodsIds) {
 | 
	
		
			
				|  |  | +                console.log('开始加载商品数据,商品IDs:', goodsIds);
 | 
	
		
			
				|  |  | +                
 | 
	
		
			
				|  |  | +                // 加载已选择的商品数据
 | 
	
		
			
				|  |  | +                                 $.ajax({
 | 
	
		
			
				|  |  | +                     url: Fast.api.fixurl('shop/goods/get_goods_by_ids'),
 | 
	
		
			
				|  |  | +                     type: 'POST',
 | 
	
		
			
				|  |  | +                     dataType: 'json',
 | 
	
		
			
				|  |  | +                     data: {ids: goodsIds},
 | 
	
		
			
				|  |  | +                     headers: {
 | 
	
		
			
				|  |  | +                         'X-Requested-With': 'XMLHttpRequest'
 | 
	
		
			
				|  |  | +                     },
 | 
	
		
			
				|  |  | +                                         success: function(ret, textStatus, jqXHR) {
 | 
	
		
			
				|  |  | +                         console.log('商品数据加载结果:', {
 | 
	
		
			
				|  |  | +                             ret: ret,
 | 
	
		
			
				|  |  | +                             retType: typeof ret,
 | 
	
		
			
				|  |  | +                             retIsArray: Array.isArray(ret),
 | 
	
		
			
				|  |  | +                             retCode: ret ? ret.code : null,
 | 
	
		
			
				|  |  | +                             retData: ret ? ret.data : null,
 | 
	
		
			
				|  |  | +                             retDataIsArray: ret && ret.data ? Array.isArray(ret.data) : false
 | 
	
		
			
				|  |  | +                         });
 | 
	
		
			
				|  |  | +                         
 | 
	
		
			
				|  |  | +                         // 根据实际返回的数据结构来处理
 | 
	
		
			
				|  |  | +                         var goodsData = [];
 | 
	
		
			
				|  |  | +                         
 | 
	
		
			
				|  |  | +                         // 情况1: 标准FastAdmin格式,数据在ret.data中
 | 
	
		
			
				|  |  | +                         if (ret && ret.code === 1 && ret.data && Array.isArray(ret.data)) {
 | 
	
		
			
				|  |  | +                             goodsData = ret.data;
 | 
	
		
			
				|  |  | +                             console.log('使用标准格式数据,数量:', goodsData.length);
 | 
	
		
			
				|  |  | +                         }
 | 
	
		
			
				|  |  | +                         // 情况2: 直接返回商品数组
 | 
	
		
			
				|  |  | +                         else if (Array.isArray(ret)) {
 | 
	
		
			
				|  |  | +                             goodsData = ret;
 | 
	
		
			
				|  |  | +                             console.log('使用直接数组数据,数量:', goodsData.length);
 | 
	
		
			
				|  |  | +                         }
 | 
	
		
			
				|  |  | +                         // 情况3: 如果ret.code不等于1,说明有错误
 | 
	
		
			
				|  |  | +                         else if (ret && ret.code !== undefined && ret.code !== 1) {
 | 
	
		
			
				|  |  | +                             console.error('接口返回错误:', ret.msg || '未知错误');
 | 
	
		
			
				|  |  | +                             Layer.msg(ret.msg || '加载商品数据失败', {icon: 2});
 | 
	
		
			
				|  |  | +                             return;
 | 
	
		
			
				|  |  | +                         }
 | 
	
		
			
				|  |  | +                         
 | 
	
		
			
				|  |  | +                         if(goodsData && goodsData.length > 0) {
 | 
	
		
			
				|  |  | +                             console.log('接口返回商品数据成功,商品数量:', goodsData.length);
 | 
	
		
			
				|  |  | +                             
 | 
	
		
			
				|  |  | +                             // 如果有goods_info数据,则合并折扣信息
 | 
	
		
			
				|  |  | +                             if (goodsInfo && goodsInfo.length > 0) {
 | 
	
		
			
				|  |  | +                                 console.log('合并goods_info中的折扣数据');
 | 
	
		
			
				|  |  | +                                 // 将goods_info中的折扣数据合并到接口返回的商品数据中
 | 
	
		
			
				|  |  | +                                 $.each(goodsData, function(index, goods) {
 | 
	
		
			
				|  |  | +                                     var matchedInfo = null;
 | 
	
		
			
				|  |  | +                                     $.each(goodsInfo, function(infoIndex, info) {
 | 
	
		
			
				|  |  | +                                         if (info.goods_id == goods.id) {
 | 
	
		
			
				|  |  | +                                             matchedInfo = info;
 | 
	
		
			
				|  |  | +                                             return false; // 跳出循环
 | 
	
		
			
				|  |  | +                                         }
 | 
	
		
			
				|  |  | +                                     });
 | 
	
		
			
				|  |  | +                                     
 | 
	
		
			
				|  |  | +                                     if (matchedInfo) {
 | 
	
		
			
				|  |  | +                                         // 合并折扣信息到商品数据
 | 
	
		
			
				|  |  | +                                         goods.discount_info = matchedInfo;
 | 
	
		
			
				|  |  | +                                         console.log('商品', goods.id, '合并折扣信息:', matchedInfo);
 | 
	
		
			
				|  |  | +                                     }
 | 
	
		
			
				|  |  | +                                 });
 | 
	
		
			
				|  |  | +                             }
 | 
	
		
			
				|  |  | +                             
 | 
	
		
			
				|  |  | +                             // 使用共享的商品渲染方法,传入编辑模式标识
 | 
	
		
			
				|  |  | +                             Controller.renderSelectedGoods(goodsData, true);
 | 
	
		
			
				|  |  | +                         } else {
 | 
	
		
			
				|  |  | +                             console.warn('没有获取到商品数据或数据为空');
 | 
	
		
			
				|  |  | +                             
 | 
	
		
			
				|  |  | +                             // 如果接口没有返回数据,但有goods_info,尝试直接渲染
 | 
	
		
			
				|  |  | +                             if (goodsInfo && goodsInfo.length > 0) {
 | 
	
		
			
				|  |  | +                                 console.log('尝试从goods_info构造商品数据进行渲染');
 | 
	
		
			
				|  |  | +                                 // 这里可以实现从goods_info重构商品数据的逻辑
 | 
	
		
			
				|  |  | +                                 // 不过通常情况下,如果有goods_ids,接口应该能返回商品基础数据
 | 
	
		
			
				|  |  | +                             }
 | 
	
		
			
				|  |  | +                         }
 | 
	
		
			
				|  |  | +                     },
 | 
	
		
			
				|  |  | +                     error: function(jqXHR, textStatus, errorThrown) {
 | 
	
		
			
				|  |  | +                         console.log('Ajax error回调触发,尝试解析响应:', {
 | 
	
		
			
				|  |  | +                             status: jqXHR.status,
 | 
	
		
			
				|  |  | +                             statusText: jqXHR.statusText,
 | 
	
		
			
				|  |  | +                             textStatus: textStatus,
 | 
	
		
			
				|  |  | +                             errorThrown: errorThrown,
 | 
	
		
			
				|  |  | +                             responseText: jqXHR.responseText
 | 
	
		
			
				|  |  | +                         });
 | 
	
		
			
				|  |  | +                         
 | 
	
		
			
				|  |  | +                         // 尝试解析响应文本,可能是有效的JSON但格式不符合预期
 | 
	
		
			
				|  |  | +                         try {
 | 
	
		
			
				|  |  | +                             var responseData = JSON.parse(jqXHR.responseText);
 | 
	
		
			
				|  |  | +                             console.log('解析响应数据成功:', responseData);
 | 
	
		
			
				|  |  | +                             
 | 
	
		
			
				|  |  | +                             if (Array.isArray(responseData)) {
 | 
	
		
			
				|  |  | +                                 console.log('响应是商品数组,尝试渲染:', responseData.length);
 | 
	
		
			
				|  |  | +                                 
 | 
	
		
			
				|  |  | +                                 // 如果有goods_info数据,则合并折扣信息
 | 
	
		
			
				|  |  | +                                 if (goodsInfo && goodsInfo.length > 0) {
 | 
	
		
			
				|  |  | +                                     console.log('合并goods_info中的折扣数据');
 | 
	
		
			
				|  |  | +                                     $.each(responseData, function(index, goods) {
 | 
	
		
			
				|  |  | +                                         var matchedInfo = null;
 | 
	
		
			
				|  |  | +                                         $.each(goodsInfo, function(infoIndex, info) {
 | 
	
		
			
				|  |  | +                                             if (info.goods_id == goods.id) {
 | 
	
		
			
				|  |  | +                                                 matchedInfo = info;
 | 
	
		
			
				|  |  | +                                                 return false;
 | 
	
		
			
				|  |  | +                                             }
 | 
	
		
			
				|  |  | +                                         });
 | 
	
		
			
				|  |  | +                                         
 | 
	
		
			
				|  |  | +                                         if (matchedInfo) {
 | 
	
		
			
				|  |  | +                                             goods.discount_info = matchedInfo;
 | 
	
		
			
				|  |  | +                                             console.log('商品', goods.id, '合并折扣信息:', matchedInfo);
 | 
	
		
			
				|  |  | +                                         }
 | 
	
		
			
				|  |  | +                                     });
 | 
	
		
			
				|  |  | +                                 }
 | 
	
		
			
				|  |  | +                                 
 | 
	
		
			
				|  |  | +                                 // 使用共享的商品渲染方法
 | 
	
		
			
				|  |  | +                                 Controller.renderSelectedGoods(responseData, true);
 | 
	
		
			
				|  |  | +                                 return; // 成功处理,不显示错误信息
 | 
	
		
			
				|  |  | +                             }
 | 
	
		
			
				|  |  | +                         } catch (e) {
 | 
	
		
			
				|  |  | +                             console.error('解析响应数据失败:', e);
 | 
	
		
			
				|  |  | +                         }
 | 
	
		
			
				|  |  | +                         
 | 
	
		
			
				|  |  | +                         // 真正的错误情况
 | 
	
		
			
				|  |  | +                         console.error('加载商品数据失败:', {
 | 
	
		
			
				|  |  | +                             status: jqXHR.status,
 | 
	
		
			
				|  |  | +                             statusText: jqXHR.statusText,
 | 
	
		
			
				|  |  | +                             textStatus: textStatus,
 | 
	
		
			
				|  |  | +                             errorThrown: errorThrown
 | 
	
		
			
				|  |  | +                         });
 | 
	
		
			
				|  |  | +                         Layer.msg('加载商品数据失败', {icon: 2});
 | 
	
		
			
				|  |  | +                     }
 | 
	
		
			
				|  |  | +                 });
 | 
	
		
			
				|  |  | +            } else {
 | 
	
		
			
				|  |  | +                console.log('没有商品IDs,跳过商品数据加载');
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            
 | 
	
		
			
				|  |  | +            // 绑定选择商品按钮事件 - 使用共享方法
 | 
	
		
			
				|  |  | +            $('#select-goods').off('click').on('click', function() {
 | 
	
		
			
				|  |  | +                Fast.api.open('shop/goods/select', '选择商品', {
 | 
	
		
			
				|  |  | +                    area: ['95%', '90%'],
 | 
	
		
			
				|  |  | +                    callback: function(data) {
 | 
	
		
			
				|  |  | +                        if (data && data.length > 0) {
 | 
	
		
			
				|  |  | +                            Controller.renderSelectedGoods(data, true);
 | 
	
		
			
				|  |  | +                        }
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                });
 | 
	
		
			
				|  |  | +            });
 | 
	
		
			
				|  |  |          },
 | 
	
		
			
				|  |  |          
 | 
	
		
			
				|  |  |          // 规格折扣设置页面
 | 
	
	
		
			
				|  | @@ -1091,6 +763,481 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function
 | 
	
		
			
				|  |  |                  Fast.api.close(returnData);
 | 
	
		
			
				|  |  |              });
 | 
	
		
			
				|  |  |          },
 | 
	
		
			
				|  |  | +        
 | 
	
		
			
				|  |  | +        // 渲染已选择的商品表格 - 共享方法
 | 
	
		
			
				|  |  | +        renderSelectedGoods: function(data, isEdit) {
 | 
	
		
			
				|  |  | +            var defaultDiscount = 9;
 | 
	
		
			
				|  |  | +            var existingDiscountData = $('#discount-data').val() ? JSON.parse($('#discount-data').val()) : {};
 | 
	
		
			
				|  |  | +            
 | 
	
		
			
				|  |  | +            // 如果是编辑模式且有现有数据,合并数据
 | 
	
		
			
				|  |  | +            if (isEdit && Object.keys(existingDiscountData).length > 0) {
 | 
	
		
			
				|  |  | +                console.log('编辑模式,使用现有折扣数据:', existingDiscountData);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            
 | 
	
		
			
				|  |  | +            console.log('renderSelectedGoods调用:', {
 | 
	
		
			
				|  |  | +                dataLength: data ? data.length : 0,
 | 
	
		
			
				|  |  | +                isEdit: isEdit,
 | 
	
		
			
				|  |  | +                existingDataCount: Object.keys(existingDiscountData).length
 | 
	
		
			
				|  |  | +            });
 | 
	
		
			
				|  |  | +            
 | 
	
		
			
				|  |  | +            // 收集商品ID
 | 
	
		
			
				|  |  | +            var goodsIds = [];
 | 
	
		
			
				|  |  | +            $.each(data, function(index, item) {
 | 
	
		
			
				|  |  | +                goodsIds.push(item.id);
 | 
	
		
			
				|  |  | +            });
 | 
	
		
			
				|  |  | +            
 | 
	
		
			
				|  |  | +            // 使用模板渲染表格
 | 
	
		
			
				|  |  | +            var html = Template('goodsTableTpl', {
 | 
	
		
			
				|  |  | +                list: data,
 | 
	
		
			
				|  |  | +                discount: defaultDiscount
 | 
	
		
			
				|  |  | +            });
 | 
	
		
			
				|  |  | +            
 | 
	
		
			
				|  |  | +            // 清空容器并显示表格
 | 
	
		
			
				|  |  | +            $('#selected-goods-container').empty().append(html);
 | 
	
		
			
				|  |  | +            
 | 
	
		
			
				|  |  | +            // 初始化分页表格
 | 
	
		
			
				|  |  | +            $('#selected-goods-table').bootstrapTable({
 | 
	
		
			
				|  |  | +                data: data,
 | 
	
		
			
				|  |  | +                pagination: true,
 | 
	
		
			
				|  |  | +                pageSize: 5,
 | 
	
		
			
				|  |  | +                pageList: [5, 10, 20],
 | 
	
		
			
				|  |  | +                sortable: true,
 | 
	
		
			
				|  |  | +                search: false,
 | 
	
		
			
				|  |  | +                classes: 'table table-hover table-striped',
 | 
	
		
			
				|  |  | +                onPostBody: function() {
 | 
	
		
			
				|  |  | +                    // 表格渲染完成后处理行属性
 | 
	
		
			
				|  |  | +                    $('#selected-goods-table tbody tr').each(function(index) {
 | 
	
		
			
				|  |  | +                        var rowData = data[index];
 | 
	
		
			
				|  |  | +                        if(rowData && rowData.id) {
 | 
	
		
			
				|  |  | +                            $(this).attr('data-id', rowData.id);
 | 
	
		
			
				|  |  | +                            console.log('设置行data-id属性:', rowData.id);
 | 
	
		
			
				|  |  | +                        }
 | 
	
		
			
				|  |  | +                    });
 | 
	
		
			
				|  |  | +                },
 | 
	
		
			
				|  |  | +                columns: [
 | 
	
		
			
				|  |  | +                    {
 | 
	
		
			
				|  |  | +                        field: 'goods',
 | 
	
		
			
				|  |  | +                        title: '商品',
 | 
	
		
			
				|  |  | +                        width: '30%',
 | 
	
		
			
				|  |  | +                        formatter: function(value, row) {
 | 
	
		
			
				|  |  | +                            return '<div class="goods-item">' +
 | 
	
		
			
				|  |  | +                                   '<img src="' + Fast.api.cdnurl(row.image) + '" style="width:60px;height:60px;margin-right:10px;" class="pull-left img-thumbnail">' +
 | 
	
		
			
				|  |  | +                                   '<div style="margin-left:70px;">' +
 | 
	
		
			
				|  |  | +                                   '<h5 style="margin-top:0;font-weight:600;">' + row.title + '</h5>' +
 | 
	
		
			
				|  |  | +                                   '<p class="text-muted">分类:' + (row.category ? row.category.name : '') + ' | 规格:' + (row.spec_type == 0 ? '单规格' : '多规格') + '</p>' +
 | 
	
		
			
				|  |  | +                                   '</div>' +
 | 
	
		
			
				|  |  | +                                   '</div>'
 | 
	
		
			
				|  |  | +                        }
 | 
	
		
			
				|  |  | +                    },
 | 
	
		
			
				|  |  | +                    {
 | 
	
		
			
				|  |  | +                        field: 'price',
 | 
	
		
			
				|  |  | +                        title: '价格',
 | 
	
		
			
				|  |  | +                        width: '10%',
 | 
	
		
			
				|  |  | +                        align: 'center',
 | 
	
		
			
				|  |  | +                        formatter: function(value, row) {
 | 
	
		
			
				|  |  | +                            return '<span class="text-danger">¥' + row.price + '</span>';
 | 
	
		
			
				|  |  | +                        }
 | 
	
		
			
				|  |  | +                    },
 | 
	
		
			
				|  |  | +                    {
 | 
	
		
			
				|  |  | +                        field: 'stocks',
 | 
	
		
			
				|  |  | +                        title: '现库存',
 | 
	
		
			
				|  |  | +                        width: '10%',
 | 
	
		
			
				|  |  | +                        align: 'center'
 | 
	
		
			
				|  |  | +                    },
 | 
	
		
			
				|  |  | +                    {
 | 
	
		
			
				|  |  | +                        field: 'discount_stocks',
 | 
	
		
			
				|  |  | +                        title: '折扣库存',
 | 
	
		
			
				|  |  | +                        width: '20%',
 | 
	
		
			
				|  |  | +                        align: 'center',
 | 
	
		
			
				|  |  | +                        formatter: function(value, row) {
 | 
	
		
			
				|  |  | +                            if(row.spec_type == 0) {
 | 
	
		
			
				|  |  | +                                // 从折扣数据中获取库存值,如果没有则使用商品库存
 | 
	
		
			
				|  |  | +                                var currentStocks = existingDiscountData[row.id] && existingDiscountData[row.id].stocks !== undefined ? 
 | 
	
		
			
				|  |  | +                                                   existingDiscountData[row.id].stocks : parseInt(row.stocks);
 | 
	
		
			
				|  |  | +                                return '<input type="number" class="form-control discount-stocks-input" value="' + currentStocks + '" min="0" data-id="' + row.id + '" data-max-stocks="' + parseInt(row.stocks) + '" style="width:100%;">';
 | 
	
		
			
				|  |  | +                            } else {
 | 
	
		
			
				|  |  | +                                return '';
 | 
	
		
			
				|  |  | +                            }
 | 
	
		
			
				|  |  | +                        }
 | 
	
		
			
				|  |  | +                    },
 | 
	
		
			
				|  |  | +                    {
 | 
	
		
			
				|  |  | +                        field: 'discount',
 | 
	
		
			
				|  |  | +                        title: '折扣',
 | 
	
		
			
				|  |  | +                        width: '15%',
 | 
	
		
			
				|  |  | +                        align: 'center',
 | 
	
		
			
				|  |  | +                        formatter: function(value, row) {
 | 
	
		
			
				|  |  | +                            if(row.spec_type == 1) {
 | 
	
		
			
				|  |  | +                                // 检查是否有已保存的规格折扣数据
 | 
	
		
			
				|  |  | +                                var hasSpec = false;
 | 
	
		
			
				|  |  | +                                var specBtnText = '规格折扣设置';
 | 
	
		
			
				|  |  | +                                var summaryInfo = '';
 | 
	
		
			
				|  |  | +                                
 | 
	
		
			
				|  |  | +                                // 先检查合并的折扣信息
 | 
	
		
			
				|  |  | +                                if (row.discount_info && row.discount_info.spec && row.discount_info.spec.length > 0) {
 | 
	
		
			
				|  |  | +                                    hasSpec = true;
 | 
	
		
			
				|  |  | +                                    specBtnText = '规格折扣设置 <span class="label label-success">已设置</span>';
 | 
	
		
			
				|  |  | +                                    
 | 
	
		
			
				|  |  | +                                    // 计算汇总信息
 | 
	
		
			
				|  |  | +                                    var totalDiscount = 0;
 | 
	
		
			
				|  |  | +                                    var count = 0;
 | 
	
		
			
				|  |  | +                                    var totalStocks = 0;
 | 
	
		
			
				|  |  | +                                    $.each(row.discount_info.spec, function(index, spec) {
 | 
	
		
			
				|  |  | +                                        totalDiscount += parseFloat(spec.discount || 0);
 | 
	
		
			
				|  |  | +                                        totalStocks += parseInt(spec.discount_stocks || 0);
 | 
	
		
			
				|  |  | +                                        count++;
 | 
	
		
			
				|  |  | +                                    });
 | 
	
		
			
				|  |  | +                                    var avgDiscount = count > 0 ? (totalDiscount / count).toFixed(1) : '0';
 | 
	
		
			
				|  |  | +                                    summaryInfo = '<div class="spec-discount-info text-muted" style="margin-top:5px;">已设置' + count + '个规格,平均' + avgDiscount + '折,共' + totalStocks + '件</div>';
 | 
	
		
			
				|  |  | +                                }
 | 
	
		
			
				|  |  | +                                // 再检查existingDiscountData
 | 
	
		
			
				|  |  | +                                else if (existingDiscountData[row.id] && existingDiscountData[row.id].specs) {
 | 
	
		
			
				|  |  | +                                    hasSpec = true;
 | 
	
		
			
				|  |  | +                                    specBtnText = '规格折扣设置 <span class="label label-success">已设置</span>';
 | 
	
		
			
				|  |  | +                                    
 | 
	
		
			
				|  |  | +                                    if (existingDiscountData[row.id].summary) {
 | 
	
		
			
				|  |  | +                                        var summary = existingDiscountData[row.id].summary;
 | 
	
		
			
				|  |  | +                                        summaryInfo = '<div class="spec-discount-info text-muted" style="margin-top:5px;">已设置' + summary.participate_count + '个规格,平均' + summary.avg_discount + '折,共' + summary.total_stocks + '件</div>';
 | 
	
		
			
				|  |  | +                                    }
 | 
	
		
			
				|  |  | +                                }
 | 
	
		
			
				|  |  | +                                
 | 
	
		
			
				|  |  | +                                var html = '<div class="text-center">' +
 | 
	
		
			
				|  |  | +                                       '<a href="javascript:;" class="btn btn-info btn-sm btn-spec-discount" data-id="' + row.id + '">' + specBtnText + '</a>' +
 | 
	
		
			
				|  |  | +                                       '<div class="text-muted small" style="margin-top:8px;">请点击设置各规格折扣</div>' +
 | 
	
		
			
				|  |  | +                                       summaryInfo +
 | 
	
		
			
				|  |  | +                                       '</div>';
 | 
	
		
			
				|  |  | +                                
 | 
	
		
			
				|  |  | +                                return html;
 | 
	
		
			
				|  |  | +                            } else {
 | 
	
		
			
				|  |  | +                                var discount = (existingDiscountData[row.id] && existingDiscountData[row.id].discount) ? existingDiscountData[row.id].discount : defaultDiscount;
 | 
	
		
			
				|  |  | +                                return '<div class="input-group">' +
 | 
	
		
			
				|  |  | +                                       '<input type="number" class="form-control discount-input" value="' + discount + '" min="0.1" max="10" step="0.1" data-price="' + row.price + '" data-id="' + row.id + '">' +
 | 
	
		
			
				|  |  | +                                       '<span class="input-group-addon">折</span>' +
 | 
	
		
			
				|  |  | +                                       '</div>';
 | 
	
		
			
				|  |  | +                            }
 | 
	
		
			
				|  |  | +                        }
 | 
	
		
			
				|  |  | +                    },
 | 
	
		
			
				|  |  | +                    {
 | 
	
		
			
				|  |  | +                        field: 'discount_price',
 | 
	
		
			
				|  |  | +                        title: '折后价',
 | 
	
		
			
				|  |  | +                        width: '20%',
 | 
	
		
			
				|  |  | +                        align: 'center',
 | 
	
		
			
				|  |  | +                        formatter: function(value, row) {
 | 
	
		
			
				|  |  | +                            if(row.spec_type == 0) {
 | 
	
		
			
				|  |  | +                                var discount = (existingDiscountData[row.id] && existingDiscountData[row.id].discount) ? existingDiscountData[row.id].discount : defaultDiscount;
 | 
	
		
			
				|  |  | +                                var discountPrice = (row.price * discount / 10).toFixed(2);
 | 
	
		
			
				|  |  | +                                return '<span class="discount-price text-success" id="discount-price-' + row.id + '">¥' + discountPrice + '</span>';
 | 
	
		
			
				|  |  | +                            } else {
 | 
	
		
			
				|  |  | +                                return '';
 | 
	
		
			
				|  |  | +                            }
 | 
	
		
			
				|  |  | +                        }
 | 
	
		
			
				|  |  | +                    },
 | 
	
		
			
				|  |  | +                    {
 | 
	
		
			
				|  |  | +                        field: 'operate',
 | 
	
		
			
				|  |  | +                        title: '操作',
 | 
	
		
			
				|  |  | +                        width: '15%',
 | 
	
		
			
				|  |  | +                        align: 'center',
 | 
	
		
			
				|  |  | +                        formatter: function(value, row) {
 | 
	
		
			
				|  |  | +                            return '<a href="javascript:;" class="btn btn-xs btn-danger remove-goods" data-id="' + row.id + '"><i class="fa fa-trash"></i> 删除</a>';
 | 
	
		
			
				|  |  | +                        },
 | 
	
		
			
				|  |  | +                        events: {
 | 
	
		
			
				|  |  | +                            'click .remove-goods': function (e, value, row, index) {
 | 
	
		
			
				|  |  | +                                // 从表格中移除该行
 | 
	
		
			
				|  |  | +                                $('#selected-goods-table').bootstrapTable('remove', {
 | 
	
		
			
				|  |  | +                                    field: 'id',
 | 
	
		
			
				|  |  | +                                    values: [row.id]
 | 
	
		
			
				|  |  | +                                });
 | 
	
		
			
				|  |  | +                                
 | 
	
		
			
				|  |  | +                                // 更新隐藏字段
 | 
	
		
			
				|  |  | +                                var currentIds = $('#goods-ids').val().split(',');
 | 
	
		
			
				|  |  | +                                var newIds = [];
 | 
	
		
			
				|  |  | +                                $.each(currentIds, function(i, val) {
 | 
	
		
			
				|  |  | +                                    if (val != row.id && val) {
 | 
	
		
			
				|  |  | +                                        newIds.push(val);
 | 
	
		
			
				|  |  | +                                    }
 | 
	
		
			
				|  |  | +                                });
 | 
	
		
			
				|  |  | +                                $('#goods-ids').val(newIds.join(','));
 | 
	
		
			
				|  |  | +                                
 | 
	
		
			
				|  |  | +                                // 移除折扣数据
 | 
	
		
			
				|  |  | +                                var discountData = $('#discount-data').val() ? JSON.parse($('#discount-data').val()) : {};
 | 
	
		
			
				|  |  | +                                if(discountData[row.id]) {
 | 
	
		
			
				|  |  | +                                    delete discountData[row.id];
 | 
	
		
			
				|  |  | +                                    $('#discount-data').val(JSON.stringify(discountData));
 | 
	
		
			
				|  |  | +                                }
 | 
	
		
			
				|  |  | +                                
 | 
	
		
			
				|  |  | +                                // 如果表格为空,则移除整个表格
 | 
	
		
			
				|  |  | +                                if(newIds.length === 0) {
 | 
	
		
			
				|  |  | +                                    $('#selected-goods-container').empty();
 | 
	
		
			
				|  |  | +                                }
 | 
	
		
			
				|  |  | +                                
 | 
	
		
			
				|  |  | +                                updateGoodsInfo();
 | 
	
		
			
				|  |  | +                            }
 | 
	
		
			
				|  |  | +                        }
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                ]
 | 
	
		
			
				|  |  | +            });
 | 
	
		
			
				|  |  | +            
 | 
	
		
			
				|  |  | +            // 更新隐藏字段
 | 
	
		
			
				|  |  | +            $('#goods-ids').val(goodsIds.join(','));
 | 
	
		
			
				|  |  | +            
 | 
	
		
			
				|  |  | +            // 初始化折扣数据
 | 
	
		
			
				|  |  | +            var discountData = existingDiscountData;
 | 
	
		
			
				|  |  | +            $.each(data, function(index, item) {
 | 
	
		
			
				|  |  | +                // 检查是否有合并的折扣信息
 | 
	
		
			
				|  |  | +                var hasDiscountInfo = item.discount_info;
 | 
	
		
			
				|  |  | +                
 | 
	
		
			
				|  |  | +                if (item.spec_type == 0) {
 | 
	
		
			
				|  |  | +                    if (hasDiscountInfo) {
 | 
	
		
			
				|  |  | +                        // 使用合并的折扣信息
 | 
	
		
			
				|  |  | +                        discountData[item.id] = {
 | 
	
		
			
				|  |  | +                            id: item.id,
 | 
	
		
			
				|  |  | +                            sku_id: hasDiscountInfo.sku_id || 0,
 | 
	
		
			
				|  |  | +                            discount: hasDiscountInfo.discount || defaultDiscount,
 | 
	
		
			
				|  |  | +                            discount_price: hasDiscountInfo.discount_price || (item.price * defaultDiscount / 10).toFixed(2),
 | 
	
		
			
				|  |  | +                            stocks: hasDiscountInfo.discount_stocks || parseInt(item.stocks) || 0
 | 
	
		
			
				|  |  | +                        };
 | 
	
		
			
				|  |  | +                        console.log('使用已保存的单规格商品折扣信息:', item.id, discountData[item.id]);
 | 
	
		
			
				|  |  | +                    } else if (!discountData[item.id]) {
 | 
	
		
			
				|  |  | +                        // 单规格商品,只有在没有现有数据时才初始化
 | 
	
		
			
				|  |  | +                        var currentStocks = parseInt(item.stocks) || 0;
 | 
	
		
			
				|  |  | +                        var skuId = item.sku_id || 0;
 | 
	
		
			
				|  |  | +                        
 | 
	
		
			
				|  |  | +                        discountData[item.id] = {
 | 
	
		
			
				|  |  | +                            id: item.id,
 | 
	
		
			
				|  |  | +                            sku_id: skuId,
 | 
	
		
			
				|  |  | +                            discount: defaultDiscount,
 | 
	
		
			
				|  |  | +                            discount_price: (item.price * defaultDiscount / 10).toFixed(2),
 | 
	
		
			
				|  |  | +                            stocks: currentStocks
 | 
	
		
			
				|  |  | +                        };
 | 
	
		
			
				|  |  | +                        console.log('初始化单规格商品:', item.id, 'SKU ID:', skuId, '库存:', currentStocks);
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                } else if (item.spec_type == 1 && hasDiscountInfo && hasDiscountInfo.spec) {
 | 
	
		
			
				|  |  | +                    // 多规格商品,处理规格折扣信息
 | 
	
		
			
				|  |  | +                    var specs = {};
 | 
	
		
			
				|  |  | +                    var summary = {
 | 
	
		
			
				|  |  | +                        participate_count: 0,
 | 
	
		
			
				|  |  | +                        avg_discount: 0,
 | 
	
		
			
				|  |  | +                        total_stocks: 0
 | 
	
		
			
				|  |  | +                    };
 | 
	
		
			
				|  |  | +                    
 | 
	
		
			
				|  |  | +                    var totalDiscount = 0;
 | 
	
		
			
				|  |  | +                    $.each(hasDiscountInfo.spec, function(specIndex, spec) {
 | 
	
		
			
				|  |  | +                        specs[spec.sku_id] = {
 | 
	
		
			
				|  |  | +                            id: spec.sku_id,
 | 
	
		
			
				|  |  | +                            participate: true,
 | 
	
		
			
				|  |  | +                            discount: spec.discount,
 | 
	
		
			
				|  |  | +                            discount_price: spec.discount_price,
 | 
	
		
			
				|  |  | +                            stocks: spec.discount_stocks
 | 
	
		
			
				|  |  | +                        };
 | 
	
		
			
				|  |  | +                        
 | 
	
		
			
				|  |  | +                        summary.participate_count++;
 | 
	
		
			
				|  |  | +                        totalDiscount += parseFloat(spec.discount);
 | 
	
		
			
				|  |  | +                        summary.total_stocks += parseInt(spec.discount_stocks);
 | 
	
		
			
				|  |  | +                    });
 | 
	
		
			
				|  |  | +                    
 | 
	
		
			
				|  |  | +                    summary.avg_discount = summary.participate_count > 0 ? 
 | 
	
		
			
				|  |  | +                        (totalDiscount / summary.participate_count).toFixed(1) : '0';
 | 
	
		
			
				|  |  | +                    
 | 
	
		
			
				|  |  | +                    discountData[item.id] = {
 | 
	
		
			
				|  |  | +                        id: item.id,
 | 
	
		
			
				|  |  | +                        specs: specs,
 | 
	
		
			
				|  |  | +                        summary: summary
 | 
	
		
			
				|  |  | +                    };
 | 
	
		
			
				|  |  | +                    
 | 
	
		
			
				|  |  | +                    console.log('使用已保存的多规格商品折扣信息:', item.id, discountData[item.id]);
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            });
 | 
	
		
			
				|  |  | +            
 | 
	
		
			
				|  |  | +            // 确保discount-data字段存在(兼容性)
 | 
	
		
			
				|  |  | +            if (!$('#discount-data').length) {
 | 
	
		
			
				|  |  | +                $('#goods-info').after('<input type="hidden" id="discount-data" value="">');
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            $('#discount-data').val(JSON.stringify(discountData));
 | 
	
		
			
				|  |  | +            
 | 
	
		
			
				|  |  | +            // 延迟处理事件绑定和数据回显
 | 
	
		
			
				|  |  | +            setTimeout(function() {
 | 
	
		
			
				|  |  | +                // 处理多规格商品的数据回显
 | 
	
		
			
				|  |  | +                $('#selected-goods-table tbody tr').each(function() {
 | 
	
		
			
				|  |  | +                    var $row = $(this);
 | 
	
		
			
				|  |  | +                    var goodsId = $row.data('id');
 | 
	
		
			
				|  |  | +                    
 | 
	
		
			
				|  |  | +                    console.log('检查商品行:', goodsId, '折扣数据:', discountData[goodsId]);
 | 
	
		
			
				|  |  | +                    
 | 
	
		
			
				|  |  | +                    if (goodsId && discountData[goodsId]) {
 | 
	
		
			
				|  |  | +                        // 检查是否是多规格商品且有规格数据
 | 
	
		
			
				|  |  | +                        if (discountData[goodsId].specs) {
 | 
	
		
			
				|  |  | +                            console.log('回显多规格商品数据:', goodsId, discountData[goodsId]);
 | 
	
		
			
				|  |  | +                            
 | 
	
		
			
				|  |  | +                            // 设置多规格商品的数据属性
 | 
	
		
			
				|  |  | +                            $row.attr('data-has-discount-setting', 'true');
 | 
	
		
			
				|  |  | +                            $row.attr('data-discount-data', JSON.stringify(discountData[goodsId]));
 | 
	
		
			
				|  |  | +                            
 | 
	
		
			
				|  |  | +                            // 更新按钮显示为"已设置"状态
 | 
	
		
			
				|  |  | +                            var $btn = $row.find('.btn-spec-discount');
 | 
	
		
			
				|  |  | +                            if ($btn.length > 0) {
 | 
	
		
			
				|  |  | +                                $btn.html('规格折扣设置 <span class="label label-success">已设置</span>');
 | 
	
		
			
				|  |  | +                                
 | 
	
		
			
				|  |  | +                                // 显示汇总信息
 | 
	
		
			
				|  |  | +                                if (discountData[goodsId].summary) {
 | 
	
		
			
				|  |  | +                                    var summary = discountData[goodsId].summary;
 | 
	
		
			
				|  |  | +                                    var infoText = '已设置' + summary.participate_count + '个规格,平均' + summary.avg_discount + '折,共' + summary.total_stocks + '件';
 | 
	
		
			
				|  |  | +                                    
 | 
	
		
			
				|  |  | +                                    // 检查是否已有汇总信息区域
 | 
	
		
			
				|  |  | +                                    var $infoArea = $row.find('.spec-discount-info');
 | 
	
		
			
				|  |  | +                                    if ($infoArea.length > 0) {
 | 
	
		
			
				|  |  | +                                        $infoArea.text(infoText);
 | 
	
		
			
				|  |  | +                                    } else {
 | 
	
		
			
				|  |  | +                                        $btn.parent().append('<div class="spec-discount-info text-muted" style="margin-top:5px;">' + infoText + '</div>');
 | 
	
		
			
				|  |  | +                                    }
 | 
	
		
			
				|  |  | +                                }
 | 
	
		
			
				|  |  | +                            }
 | 
	
		
			
				|  |  | +                        }
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                });
 | 
	
		
			
				|  |  | +                
 | 
	
		
			
				|  |  | +                // 绑定事件处理器
 | 
	
		
			
				|  |  | +                Controller.bindGoodsEvents();
 | 
	
		
			
				|  |  | +                
 | 
	
		
			
				|  |  | +                // 同步更新商品详情信息
 | 
	
		
			
				|  |  | +                updateGoodsInfo();
 | 
	
		
			
				|  |  | +            }, 300);
 | 
	
		
			
				|  |  | +        },
 | 
	
		
			
				|  |  | +        
 | 
	
		
			
				|  |  | +        // 绑定商品相关事件处理器
 | 
	
		
			
				|  |  | +        bindGoodsEvents: function() {
 | 
	
		
			
				|  |  | +            // 绑定折扣库存输入事件
 | 
	
		
			
				|  |  | +            $(document).off('input', '.discount-stocks-input').on('input', '.discount-stocks-input', function() {
 | 
	
		
			
				|  |  | +                var stocks = parseInt($(this).val()) || 0;
 | 
	
		
			
				|  |  | +                var goodsId = $(this).data('id');
 | 
	
		
			
				|  |  | +                var maxStocks = parseInt($(this).data('max-stocks')) || 0;
 | 
	
		
			
				|  |  | +                
 | 
	
		
			
				|  |  | +                console.log('折扣库存输入事件:', goodsId, '输入值:', stocks, '最大值:', maxStocks);
 | 
	
		
			
				|  |  | +                
 | 
	
		
			
				|  |  | +                // 限制输入范围,确保不超过现库存
 | 
	
		
			
				|  |  | +                if(stocks < 0) stocks = 0;
 | 
	
		
			
				|  |  | +                if(stocks > maxStocks) stocks = maxStocks;
 | 
	
		
			
				|  |  | +                $(this).val(stocks);
 | 
	
		
			
				|  |  | +                
 | 
	
		
			
				|  |  | +                // 更新商品折扣数据
 | 
	
		
			
				|  |  | +                var discountData = $('#discount-data').val() ? JSON.parse($('#discount-data').val()) : {};
 | 
	
		
			
				|  |  | +                
 | 
	
		
			
				|  |  | +                if(discountData[goodsId]) {
 | 
	
		
			
				|  |  | +                    discountData[goodsId].stocks = stocks;
 | 
	
		
			
				|  |  | +                } else {
 | 
	
		
			
				|  |  | +                    discountData[goodsId] = {
 | 
	
		
			
				|  |  | +                        id: goodsId,
 | 
	
		
			
				|  |  | +                        sku_id: 0,
 | 
	
		
			
				|  |  | +                        stocks: stocks
 | 
	
		
			
				|  |  | +                    };
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +                
 | 
	
		
			
				|  |  | +                $('#discount-data').val(JSON.stringify(discountData));
 | 
	
		
			
				|  |  | +                updateGoodsInfo();
 | 
	
		
			
				|  |  | +            });
 | 
	
		
			
				|  |  | +            
 | 
	
		
			
				|  |  | +            // 绑定折扣输入事件
 | 
	
		
			
				|  |  | +            $(document).off('input', '.discount-input').on('input', '.discount-input', function() {
 | 
	
		
			
				|  |  | +                var discount = parseFloat($(this).val()) || 0;
 | 
	
		
			
				|  |  | +                if(discount < 0.1) discount = 0.1;
 | 
	
		
			
				|  |  | +                if(discount > 10) discount = 10;
 | 
	
		
			
				|  |  | +                
 | 
	
		
			
				|  |  | +                var price = parseFloat($(this).data('price')) || 0;
 | 
	
		
			
				|  |  | +                var goodsId = $(this).data('id');
 | 
	
		
			
				|  |  | +                var discountPrice = (price * discount / 10).toFixed(2);
 | 
	
		
			
				|  |  | +                
 | 
	
		
			
				|  |  | +                $('#discount-price-' + goodsId).text('¥' + discountPrice);
 | 
	
		
			
				|  |  | +                
 | 
	
		
			
				|  |  | +                // 更新商品折扣数据
 | 
	
		
			
				|  |  | +                var discountData = $('#discount-data').val() ? JSON.parse($('#discount-data').val()) : {};
 | 
	
		
			
				|  |  | +                
 | 
	
		
			
				|  |  | +                // 保留已有的数据结构
 | 
	
		
			
				|  |  | +                if (!discountData[goodsId]) {
 | 
	
		
			
				|  |  | +                    discountData[goodsId] = {
 | 
	
		
			
				|  |  | +                        id: goodsId,
 | 
	
		
			
				|  |  | +                        sku_id: 0
 | 
	
		
			
				|  |  | +                    };
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +                
 | 
	
		
			
				|  |  | +                discountData[goodsId].discount = discount;
 | 
	
		
			
				|  |  | +                discountData[goodsId].discount_price = discountPrice;
 | 
	
		
			
				|  |  | +                
 | 
	
		
			
				|  |  | +                $('#discount-data').val(JSON.stringify(discountData));
 | 
	
		
			
				|  |  | +                updateGoodsInfo();
 | 
	
		
			
				|  |  | +            });
 | 
	
		
			
				|  |  | +            
 | 
	
		
			
				|  |  | +            // 处理规格设置按钮点击事件
 | 
	
		
			
				|  |  | +            $(document).off('click', '.btn-spec-discount').on('click', '.btn-spec-discount', function() {
 | 
	
		
			
				|  |  | +                var goodsId = $(this).data('id');
 | 
	
		
			
				|  |  | +                var $row = $(this).closest('tr');
 | 
	
		
			
				|  |  | +                var hasDiscountSetting = $row.attr('data-has-discount-setting');
 | 
	
		
			
				|  |  | +                var specData = '';
 | 
	
		
			
				|  |  | +                
 | 
	
		
			
				|  |  | +                console.log('点击规格折扣设置按钮:', goodsId, '当前设置状态:', hasDiscountSetting);
 | 
	
		
			
				|  |  | +                
 | 
	
		
			
				|  |  | +                // 如果已经有设置,则传递已有的数据
 | 
	
		
			
				|  |  | +                if (hasDiscountSetting && $row.attr('data-discount-data')) {
 | 
	
		
			
				|  |  | +                    specData = encodeURIComponent($row.attr('data-discount-data'));
 | 
	
		
			
				|  |  | +                    console.log('已有折扣数据:', $row.attr('data-discount-data'));
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +                
 | 
	
		
			
				|  |  | +                Fast.api.open('marketing/discount/spec_discount?goods_id=' + goodsId + '&spec_data=' + specData, '规格折扣设置', {
 | 
	
		
			
				|  |  | +                    area: ['90%', '90%'],
 | 
	
		
			
				|  |  | +                    callback: function(data) {
 | 
	
		
			
				|  |  | +                        if (data && data.goodsId && data.specs) {
 | 
	
		
			
				|  |  | +                            console.log('规格折扣设置返回数据:', data);
 | 
	
		
			
				|  |  | +                            
 | 
	
		
			
				|  |  | +                            // 保存数据到行属性
 | 
	
		
			
				|  |  | +                            $row.attr('data-has-discount-setting', 'true');
 | 
	
		
			
				|  |  | +                            $row.attr('data-discount-data', JSON.stringify(data));
 | 
	
		
			
				|  |  | +                            
 | 
	
		
			
				|  |  | +                            // 更新按钮文字
 | 
	
		
			
				|  |  | +                            $row.find('.btn-spec-discount').html('规格折扣设置 <span class="label label-success">已设置</span>');
 | 
	
		
			
				|  |  | +                            
 | 
	
		
			
				|  |  | +                            // 计算并显示汇总信息
 | 
	
		
			
				|  |  | +                            var totalDiscount = 0;
 | 
	
		
			
				|  |  | +                            var count = 0;
 | 
	
		
			
				|  |  | +                            var totalStocks = 0;
 | 
	
		
			
				|  |  | +                            
 | 
	
		
			
				|  |  | +                            $.each(data.specs, function(id, item) {
 | 
	
		
			
				|  |  | +                                if (item.participate) {
 | 
	
		
			
				|  |  | +                                    totalDiscount += parseFloat(item.discount);
 | 
	
		
			
				|  |  | +                                    totalStocks += parseInt(item.stocks);
 | 
	
		
			
				|  |  | +                                    count++;
 | 
	
		
			
				|  |  | +                                }
 | 
	
		
			
				|  |  | +                            });
 | 
	
		
			
				|  |  | +                            
 | 
	
		
			
				|  |  | +                            var avgDiscount = count > 0 ? (totalDiscount / count).toFixed(1) : '-';
 | 
	
		
			
				|  |  | +                            
 | 
	
		
			
				|  |  | +                            var summary = {
 | 
	
		
			
				|  |  | +                                participate_count: count,
 | 
	
		
			
				|  |  | +                                avg_discount: avgDiscount,
 | 
	
		
			
				|  |  | +                                total_stocks: totalStocks
 | 
	
		
			
				|  |  | +                            };
 | 
	
		
			
				|  |  | +                            
 | 
	
		
			
				|  |  | +                            var infoText = '已设置' + summary.participate_count + '个规格,平均' + summary.avg_discount + '折,共' + summary.total_stocks + '件';
 | 
	
		
			
				|  |  | +                            
 | 
	
		
			
				|  |  | +                            var $infoArea = $row.find('.spec-discount-info');
 | 
	
		
			
				|  |  | +                            if ($infoArea.length > 0) {
 | 
	
		
			
				|  |  | +                                $infoArea.text(infoText);
 | 
	
		
			
				|  |  | +                            } else {
 | 
	
		
			
				|  |  | +                                $row.find('.btn-spec-discount').parent().append('<div class="spec-discount-info text-muted" style="margin-top:5px;">' + infoText + '</div>');
 | 
	
		
			
				|  |  | +                            }
 | 
	
		
			
				|  |  | +                            
 | 
	
		
			
				|  |  | +                            data.summary = summary;
 | 
	
		
			
				|  |  | +                            
 | 
	
		
			
				|  |  | +                            // 将折扣数据保存到隐藏字段
 | 
	
		
			
				|  |  | +                            var discountData = $('#discount-data').val() ? JSON.parse($('#discount-data').val()) : {};
 | 
	
		
			
				|  |  | +                            discountData[goodsId] = data;
 | 
	
		
			
				|  |  | +                            $('#discount-data').val(JSON.stringify(discountData));
 | 
	
		
			
				|  |  | +                            
 | 
	
		
			
				|  |  | +                            updateGoodsInfo();
 | 
	
		
			
				|  |  | +                            
 | 
	
		
			
				|  |  | +                            console.log('多规格商品设置后的折扣数据:', $('#discount-data').val());
 | 
	
		
			
				|  |  | +                        }
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                });
 | 
	
		
			
				|  |  | +            });
 | 
	
		
			
				|  |  | +        },
 | 
	
		
			
				|  |  | +        
 | 
	
		
			
				|  |  |          api: {
 | 
	
		
			
				|  |  |              // 解析Config中的JSON字符串的辅助函数
 | 
	
		
			
				|  |  |              parseConfigJson: function(configKey, defaultValue) {
 |