|
@@ -1,4 +1,4 @@
|
|
|
-define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
|
|
|
+define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function ($, undefined, Backend, Table, Form, Template) {
|
|
|
|
|
|
var Controller = {
|
|
|
index: function () {
|
|
@@ -117,86 +117,147 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
|
|
|
|
|
|
add: function () {
|
|
|
Controller.api.bindevent();
|
|
|
-
|
|
|
// 选择商品功能
|
|
|
$('#select-goods').on('click', function() {
|
|
|
Fast.api.open('shop/goods/select', '选择商品', {
|
|
|
+ area: ['95%', '90%'],
|
|
|
callback: function(data) {
|
|
|
if (data && data.length > 0) {
|
|
|
var goodsIds = [];
|
|
|
|
|
|
- // 清空容器
|
|
|
- $('#selected-goods-container').empty();
|
|
|
-
|
|
|
- // 创建表格结构
|
|
|
- var tableHtml =
|
|
|
- '<table class="table table-striped table-bordered table-hover">' +
|
|
|
- '<thead>' +
|
|
|
- '<tr>' +
|
|
|
- '<th width="30%">商品</th>' +
|
|
|
- '<th width="10%">价格</th>' +
|
|
|
- '<th width="10%">现库存</th>' +
|
|
|
- '<th width="15%">折扣</th>' +
|
|
|
- '<th>折后价</th>' +
|
|
|
- '<th width="10%">操作</th>' +
|
|
|
- '</tr>' +
|
|
|
- '</thead>' +
|
|
|
- '<tbody>';
|
|
|
-
|
|
|
- // 添加商品行
|
|
|
+ // 收集商品ID
|
|
|
$.each(data, function(index, item) {
|
|
|
goodsIds.push(item.id);
|
|
|
- var specTypeText = item.spectype == 0 ? "单规格" : "多规格";
|
|
|
-
|
|
|
- // 计算默认折扣价(默认9折)
|
|
|
- var defaultDiscount = 9;
|
|
|
- var discountPrice = (item.price * defaultDiscount / 10).toFixed(2);
|
|
|
-
|
|
|
- tableHtml += '<tr data-id="' + item.id + '" data-spectype="' + item.spectype + '">' +
|
|
|
- '<td>' +
|
|
|
- '<div class="goods-item">' +
|
|
|
- '<img src="' + item.image + '" style="width:60px;height:60px;margin-right:10px;" class="pull-left img-thumbnail">' +
|
|
|
- '<div style="margin-left:70px;">' +
|
|
|
- '<h5 style="margin-top:0;">' + item.title + '</h5>' +
|
|
|
- '<p class="text-muted">分类:' + (item.category ? item.category.name : '') + ' | 规格:' + specTypeText + '</p>' +
|
|
|
- '</div>' +
|
|
|
- '</div>' +
|
|
|
- '</td>' +
|
|
|
- '<td>¥' + item.price + '</td>' +
|
|
|
- '<td>' + item.stocks + '</td>' +
|
|
|
- '<td>' +
|
|
|
- '<div class="input-group">' +
|
|
|
- '<input type="number" class="form-control discount-input" value="' + defaultDiscount + '" min="0.1" max="10" step="0.1" data-price="' + item.price + '" style="width:60px;">' +
|
|
|
- '<span class="input-group-addon">折</span>' +
|
|
|
- '</div>' +
|
|
|
- (item.spectype == 1 ? '<a href="javascript:;" class="btn btn-xs btn-info btn-spec-discount" data-id="' + item.id + '" style="margin-top:5px;">规格折扣设置</a>' : '') +
|
|
|
- '</td>' +
|
|
|
- '<td><span class="discount-price">¥' + discountPrice + '</span></td>' +
|
|
|
- '<td><a href="javascript:;" class="btn btn-xs btn-danger remove-goods" data-id="' + item.id + '">删除</a></td>' +
|
|
|
- '</tr>';
|
|
|
});
|
|
|
|
|
|
- tableHtml += '</tbody></table>';
|
|
|
+ // 默认折扣值
|
|
|
+ var defaultDiscount = 9;
|
|
|
+
|
|
|
+ // 使用模板渲染表格
|
|
|
+ var html = Template('goodsTableTpl', {
|
|
|
+ list: data,
|
|
|
+ discount: defaultDiscount
|
|
|
+ });
|
|
|
+
|
|
|
+ // 清空容器并显示表格
|
|
|
+ $('#selected-goods-container').empty().append(html);
|
|
|
|
|
|
- // 显示表格
|
|
|
- $('#selected-goods-container').append(tableHtml);
|
|
|
+ // 初始化分页表格
|
|
|
+ $('#selected-goods-table').bootstrapTable({
|
|
|
+ data: data,
|
|
|
+ pagination: true,
|
|
|
+ pageSize: 5,
|
|
|
+ pageList: [5, 10, 20],
|
|
|
+ sortable: true,
|
|
|
+ search: false,
|
|
|
+ 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;">' + row.title + '</h5>' +
|
|
|
+ '<p class="text-muted">分类:' + (row.category ? row.category.name : '') + ' | 规格:' + (row.spectype == 0 ? '单规格' : '多规格') + '</p>' +
|
|
|
+ '</div>' +
|
|
|
+ '</div>'
|
|
|
+ }
|
|
|
+ },
|
|
|
+ {
|
|
|
+ field: 'price',
|
|
|
+ title: '价格',
|
|
|
+ width: '10%',
|
|
|
+ formatter: function(value, row) {
|
|
|
+ return '¥' + row.price;
|
|
|
+ }
|
|
|
+ },
|
|
|
+ {
|
|
|
+ field: 'stocks',
|
|
|
+ title: '现库存',
|
|
|
+ width: '10%'
|
|
|
+ },
|
|
|
+ {
|
|
|
+ field: 'discount',
|
|
|
+ title: '折扣',
|
|
|
+ width: '15%',
|
|
|
+ formatter: function(value, row) {
|
|
|
+ if(row.spectype == 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 = '<a href="javascript:;" class="btn btn-info btn-spec-discount" data-id="' + row.id + '">' + specBtnText + '</a>' +
|
|
|
+ '<span class="text-muted" style="margin-left:10px;">请点击设置各规格折扣</span>';
|
|
|
+
|
|
|
+ // 如果已设置规格折扣,显示汇总信息
|
|
|
+ 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>';
|
|
|
+ }
|
|
|
+
|
|
|
+ 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%',
|
|
|
+ formatter: function(value, row) {
|
|
|
+ if(row.spectype == 0) {
|
|
|
+ return '<span class="discount-price" id="discount-price-' + row.id + '">¥' + (row.price * defaultDiscount / 10).toFixed(2) + '</span>';
|
|
|
+ } else {
|
|
|
+ return '';
|
|
|
+ }
|
|
|
+ }
|
|
|
+ },
|
|
|
+ {
|
|
|
+ field: 'operate',
|
|
|
+ title: '操作',
|
|
|
+ width: '15%',
|
|
|
+ formatter: function(value, row) {
|
|
|
+ return '<a href="javascript:;" class="btn btn-xs btn-danger remove-goods" data-id="' + row.id + '">删除</a>';
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ]
|
|
|
+ });
|
|
|
|
|
|
// 更新隐藏字段
|
|
|
$('#goods-ids').val(goodsIds.join(','));
|
|
|
|
|
|
+ // 初始化折扣数据
|
|
|
+ var discountData = {};
|
|
|
+ $.each(data, function(index, item) {
|
|
|
+ if (item.spectype == 0) { // 单规格商品
|
|
|
+ discountData[item.id] = {
|
|
|
+ id: item.id,
|
|
|
+ discount: defaultDiscount,
|
|
|
+ discount_price: (item.price * defaultDiscount / 10).toFixed(2)
|
|
|
+ };
|
|
|
+ }
|
|
|
+ });
|
|
|
+ $('#discount-data').val(JSON.stringify(discountData));
|
|
|
+
|
|
|
// 绑定折扣输入事件
|
|
|
- $('.discount-input').on('input', function() {
|
|
|
+ $(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);
|
|
|
|
|
|
- $(this).closest('tr').find('.discount-price').text('¥' + discountPrice);
|
|
|
+ $('#discount-price-' + goodsId).text('¥' + discountPrice);
|
|
|
|
|
|
// 更新商品折扣数据
|
|
|
- var goodsId = $(this).closest('tr').data('id');
|
|
|
var goodsDiscount = {
|
|
|
id: goodsId,
|
|
|
discount: discount,
|
|
@@ -209,24 +270,100 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
|
|
|
$('#discount-data').val(JSON.stringify(discountData));
|
|
|
});
|
|
|
|
|
|
- // 绑定规格折扣按钮点击事件
|
|
|
- $('.btn-spec-discount').on('click', function() {
|
|
|
+ // 处理规格设置按钮点击事件
|
|
|
+ $(document).on('click', '.btn-spec-discount', function() {
|
|
|
var goodsId = $(this).data('id');
|
|
|
- Fast.api.open('marketing/discount/spec_discount?goods_id=' + goodsId, '规格折扣设置');
|
|
|
+ var $row = $(this).closest('tr');
|
|
|
+ var hasDiscountSetting = $row.data('has-discount-setting');
|
|
|
+ var specData = '';
|
|
|
+
|
|
|
+ // 如果已经有设置,则传递已有的数据
|
|
|
+ if (hasDiscountSetting && $row.data('discount-data')) {
|
|
|
+ specData = encodeURIComponent(JSON.stringify($row.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) {
|
|
|
+ // 保存数据到行属性
|
|
|
+ $row.data('discount-data', data);
|
|
|
+ $row.data('has-discount-setting', true);
|
|
|
+
|
|
|
+ // 更新按钮文字
|
|
|
+ $row.find('.btn-spec-discount').text('已设置(可点击修改)');
|
|
|
+
|
|
|
+ // 更新状态和折扣显示
|
|
|
+ $row.find('.discount-status').text('已设置');
|
|
|
+
|
|
|
+ // 计算平均折扣
|
|
|
+ var totalDiscount = 0;
|
|
|
+ var count = 0;
|
|
|
+ $.each(data.specs, function(id, item) {
|
|
|
+ if (item.participate) {
|
|
|
+ totalDiscount += parseFloat(item.discount);
|
|
|
+ count++;
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ var avgDiscount = count > 0 ? (totalDiscount / count).toFixed(1) : '-';
|
|
|
+ $row.find('.discount-value').text(avgDiscount + '折');
|
|
|
+
|
|
|
+ // 将折扣数据保存到隐藏字段
|
|
|
+ updateDiscountData();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
});
|
|
|
|
|
|
+ // 更新所有折扣数据到隐藏字段
|
|
|
+ function updateDiscountData() {
|
|
|
+ var discountData = {};
|
|
|
+ $('#selected-goods-table tr[data-id]').each(function() {
|
|
|
+ var $row = $(this);
|
|
|
+ var goodsId = $row.data('id');
|
|
|
+ if ($row.data('has-discount-setting') && $row.data('discount-data')) {
|
|
|
+ discountData[goodsId] = $row.data('discount-data').specs;
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ $('#discount-data').val(JSON.stringify(discountData));
|
|
|
+ }
|
|
|
+
|
|
|
// 删除已选商品
|
|
|
- $(document).on('click', '.remove-goods', function() {
|
|
|
+ $(document).on('click', '.btn-remove', function() {
|
|
|
var id = $(this).data('id');
|
|
|
- $(this).closest('tr').remove();
|
|
|
|
|
|
- // 如果表格为空,则移除整个表格
|
|
|
- if($('#selected-goods-container tbody tr').length === 0) {
|
|
|
- $('#selected-goods-container').empty();
|
|
|
+ // 从表格中移除该行
|
|
|
+ var $table = $('#selected-goods-table');
|
|
|
+ if($table.length > 0) {
|
|
|
+ // 如果使用了bootstrap-table
|
|
|
+ var rows = $table.bootstrapTable('getData');
|
|
|
+ var newRows = [];
|
|
|
+
|
|
|
+ for(var i = 0; i < rows.length; i++) {
|
|
|
+ if(rows[i].id != id) {
|
|
|
+ newRows.push(rows[i]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if(newRows.length === 0) {
|
|
|
+ $('#selected-goods-container').empty();
|
|
|
+ } else {
|
|
|
+ $table.bootstrapTable('load', newRows);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 如果使用了传统表格
|
|
|
+ $(this).closest('tr').remove();
|
|
|
+
|
|
|
+ // 如果表格为空,则移除整个表格
|
|
|
+ if($('#selected-goods-container tbody tr').length === 0) {
|
|
|
+ $('#selected-goods-container').empty();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// 更新隐藏字段
|
|
@@ -250,9 +387,268 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
|
|
|
edit: function () {
|
|
|
Controller.api.bindevent();
|
|
|
|
|
|
+ // 初始化已选择的商品
|
|
|
+ var goodsIds = $('#goods-ids').val();
|
|
|
+ var discountData = $('#discount-data').val() ? JSON.parse($('#discount-data').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
|
|
|
+ });
|
|
|
+
|
|
|
+ // 清空容器并显示表格
|
|
|
+ $('#selected-goods-container').empty().append(html);
|
|
|
+
|
|
|
+ // 初始化分页表格
|
|
|
+ $('#selected-goods-table').bootstrapTable({
|
|
|
+ data: data,
|
|
|
+ pagination: true,
|
|
|
+ pageSize: 5,
|
|
|
+ pageList: [5, 10, 20],
|
|
|
+ sortable: true,
|
|
|
+ search: false,
|
|
|
+ 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;">' + row.title + '</h5>' +
|
|
|
+ '<p class="text-muted">分类:' + (row.category ? row.category.name : '') + ' | 规格:' + (row.spectype == 0 ? '单规格' : '多规格') + '</p>' +
|
|
|
+ '</div>' +
|
|
|
+ '</div>'
|
|
|
+ }
|
|
|
+ },
|
|
|
+ {
|
|
|
+ field: 'price',
|
|
|
+ title: '价格',
|
|
|
+ width: '10%',
|
|
|
+ formatter: function(value, row) {
|
|
|
+ return '¥' + row.price;
|
|
|
+ }
|
|
|
+ },
|
|
|
+ {
|
|
|
+ field: 'stocks',
|
|
|
+ title: '现库存',
|
|
|
+ width: '10%'
|
|
|
+ },
|
|
|
+ {
|
|
|
+ field: 'discount',
|
|
|
+ title: '折扣',
|
|
|
+ width: '15%',
|
|
|
+ formatter: function(value, row) {
|
|
|
+ if(row.spectype == 1) {
|
|
|
+ var hasSpec = discountData[row.id] && discountData[row.id].specs;
|
|
|
+ var specBtnText = hasSpec ? '规格折扣设置 <span class="label label-success">已设置</span>' : '规格折扣设置';
|
|
|
+ var html = '<a href="javascript:;" class="btn btn-info btn-spec-discount" data-id="' + row.id + '">' + specBtnText + '</a>' +
|
|
|
+ '<span class="text-muted" style="margin-left:10px;">请点击设置各规格折扣</span>';
|
|
|
+
|
|
|
+ // 如果已设置规格折扣,显示汇总信息
|
|
|
+ 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>';
|
|
|
+ }
|
|
|
+
|
|
|
+ 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%',
|
|
|
+ formatter: function(value, row) {
|
|
|
+ if(row.spectype == 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" id="discount-price-' + row.id + '">¥' + discountPrice + '</span>';
|
|
|
+ } else {
|
|
|
+ return '';
|
|
|
+ }
|
|
|
+ }
|
|
|
+ },
|
|
|
+ {
|
|
|
+ field: 'operate',
|
|
|
+ title: '操作',
|
|
|
+ width: '15%',
|
|
|
+ formatter: function(value, row) {
|
|
|
+ return '<a href="javascript:;" class="btn btn-xs btn-danger remove-goods" data-id="' + row.id + '">删除</a>';
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ]
|
|
|
+ });
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
// 选择商品功能和删除商品功能,与add方法相同
|
|
|
Controller.add();
|
|
|
},
|
|
|
+
|
|
|
+ // 规格折扣设置页面
|
|
|
+ spec_discount: function () {
|
|
|
+ // 获取URL参数中的goods_id和spec_data
|
|
|
+ var goodsId = Fast.api.query('goods_id');
|
|
|
+ var specData = Fast.api.query('spec_data');
|
|
|
+
|
|
|
+ // 如果有规格数据,则进行回显
|
|
|
+ if (specData) {
|
|
|
+ try {
|
|
|
+ var data = JSON.parse(decodeURIComponent(specData));
|
|
|
+ if (data && data.specs) {
|
|
|
+ $.each(data.specs, function(id, item) {
|
|
|
+ var $row = $('tr[data-id="' + id + '"]');
|
|
|
+ if ($row.length > 0) {
|
|
|
+ // 设置参与折扣
|
|
|
+ if (item.participate) {
|
|
|
+ $row.find('.participate-checkbox').prop('checked', true).trigger('change');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 设置折扣库存
|
|
|
+ if (item.stocks) {
|
|
|
+ $row.find('.sku-stocks').val(item.stocks);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 设置折扣
|
|
|
+ if (item.discount) {
|
|
|
+ $row.find('.sku-discount').val(item.discount).trigger('input');
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ } catch (e) {
|
|
|
+ console.error('解析规格数据失败', e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 参与折扣复选框事件
|
|
|
+ $('.participate-checkbox').on('change', function() {
|
|
|
+ var checked = $(this).prop('checked');
|
|
|
+ var $row = $(this).closest('tr');
|
|
|
+
|
|
|
+ // 启用/禁用库存和折扣输入框
|
|
|
+ $row.find('.sku-stocks').prop('disabled', !checked);
|
|
|
+ $row.find('.sku-discount').prop('disabled', !checked);
|
|
|
+
|
|
|
+ // 如果勾选,设置默认值
|
|
|
+ if (checked) {
|
|
|
+ var stocks = $row.data('stocks');
|
|
|
+ $row.find('.sku-stocks').val(stocks);
|
|
|
+ $row.find('.sku-discount').trigger('input');
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ // 批量参与复选框事件
|
|
|
+ $('#batch-participate').on('change', function() {
|
|
|
+ var checked = $(this).prop('checked');
|
|
|
+ $('.participate-checkbox').prop('checked', checked).trigger('change');
|
|
|
+ });
|
|
|
+
|
|
|
+ // 批量折扣变化事件
|
|
|
+ $('#batch-discount').on('input', function() {
|
|
|
+ var discount = parseFloat($(this).val()) || 0;
|
|
|
+ if (discount < 0.1) discount = 0.1;
|
|
|
+ if (discount > 10) discount = 10;
|
|
|
+
|
|
|
+ $(this).val(discount);
|
|
|
+
|
|
|
+ // 更新批量价格信息
|
|
|
+ Controller.api.updateBatchPriceInfo();
|
|
|
+ });
|
|
|
+
|
|
|
+ // 初始化批量价格信息
|
|
|
+ Controller.api.updateBatchPriceInfo();
|
|
|
+
|
|
|
+ // 应用批量设置按钮事件
|
|
|
+ $('#apply-batch').on('click', function() {
|
|
|
+ var discount = parseFloat($('#batch-discount').val()) || 9;
|
|
|
+
|
|
|
+ // 应用到所有已勾选的行
|
|
|
+ $('.participate-checkbox:checked').each(function() {
|
|
|
+ var $row = $(this).closest('tr');
|
|
|
+ $row.find('.sku-discount').val(discount).trigger('input');
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ // 折扣输入事件
|
|
|
+ $('.sku-discount').on('input', function() {
|
|
|
+ var discount = parseFloat($(this).val()) || 0;
|
|
|
+ if(discount < 0.1) discount = 0.1;
|
|
|
+ if(discount > 10) discount = 10;
|
|
|
+
|
|
|
+ $(this).val(discount);
|
|
|
+
|
|
|
+ var price = parseFloat($(this).data('price')) || 0;
|
|
|
+ var discountPrice = (price * discount / 10).toFixed(2);
|
|
|
+
|
|
|
+ $(this).closest('tr').find('.discount-price').text('¥' + discountPrice);
|
|
|
+ });
|
|
|
+
|
|
|
+ // 库存输入验证
|
|
|
+ $('.sku-stocks').on('input', function() {
|
|
|
+ var stocks = parseInt($(this).val()) || 0;
|
|
|
+ var maxStocks = parseInt($(this).closest('tr').data('stocks')) || 0;
|
|
|
+
|
|
|
+ if (stocks < 0) stocks = 0;
|
|
|
+ if (stocks > maxStocks) stocks = maxStocks;
|
|
|
+
|
|
|
+ $(this).val(stocks);
|
|
|
+ });
|
|
|
+
|
|
|
+ // 确认按钮事件
|
|
|
+ $('.btn-confirm').on('click', function() {
|
|
|
+ var specs = {};
|
|
|
+ var hasParticipate = false;
|
|
|
+
|
|
|
+ // 收集所有参与的规格数据
|
|
|
+ $('.participate-checkbox:checked').each(function() {
|
|
|
+ var $row = $(this).closest('tr');
|
|
|
+ var id = $row.data('id');
|
|
|
+ var price = $row.data('price');
|
|
|
+ var discount = parseFloat($row.find('.sku-discount').val());
|
|
|
+ var stocks = parseInt($row.find('.sku-stocks').val());
|
|
|
+ var discountPrice = (price * discount / 10).toFixed(2);
|
|
|
+
|
|
|
+ specs[id] = {
|
|
|
+ id: id,
|
|
|
+ participate: true,
|
|
|
+ discount: discount,
|
|
|
+ price: price,
|
|
|
+ stocks: stocks,
|
|
|
+ discount_price: discountPrice
|
|
|
+ };
|
|
|
+
|
|
|
+ hasParticipate = true;
|
|
|
+ });
|
|
|
+
|
|
|
+ // 验证是否有选择参与的规格
|
|
|
+ if (!hasParticipate) {
|
|
|
+ Layer.alert('请至少选择一个参与折扣的规格', {icon: 2});
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 返回数据到add.html页面
|
|
|
+ Fast.api.close({goodsId: goodsId, specs: specs});
|
|
|
+ });
|
|
|
+ },
|
|
|
api: {
|
|
|
bindevent: function () {
|
|
|
Form.api.bindevent($("form[role=form]"));
|
|
@@ -294,6 +690,24 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
|
|
|
$('input[name="row[purchase_limit_daily]"]').prop('disabled', false).focus();
|
|
|
}
|
|
|
});
|
|
|
+ },
|
|
|
+
|
|
|
+ // 更新批量价格信息
|
|
|
+ updateBatchPriceInfo: function() {
|
|
|
+ var discount = parseFloat($('#batch-discount').val()) || 0;
|
|
|
+ var totalPrice = 0;
|
|
|
+ var totalCount = 0;
|
|
|
+
|
|
|
+ $('tr[data-price]').each(function() {
|
|
|
+ var price = parseFloat($(this).data('price')) || 0;
|
|
|
+ totalPrice += price;
|
|
|
+ totalCount++;
|
|
|
+ });
|
|
|
+
|
|
|
+ var avgPrice = totalCount > 0 ? totalPrice / totalCount : 0;
|
|
|
+ var discountPrice = (avgPrice * discount / 10).toFixed(2);
|
|
|
+
|
|
|
+ $('.batch-price-info').text('平均折后价:¥' + discountPrice);
|
|
|
}
|
|
|
}
|
|
|
};
|