define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) { var Controller = { index: function () { // 初始化表格参数配置 Table.api.init({ extend: { index_url: 'shop/coupon/index' + location.search, add_url: 'shop/coupon/add', edit_url: 'shop/coupon/edit', del_url: 'shop/coupon/del', multi_url: 'shop/coupon/multi', import_url: 'shop/coupon/import', table: 'shop_coupon', } }); var table = $("#table"); // 初始化表格 table.bootstrapTable({ url: $.fn.bootstrapTable.defaults.extend.index_url, pk: 'id', sortName: 'id', fixedColumns: true, fixedRightNumber: 1, columns: [ [{ checkbox: true }, { field: 'id', title: __('Id') }, { field: 'name', title: __('Name'), operate: 'LIKE' }, { field: 'condition_name', title: __('Condition'), operate: false }, { field: 'result', title: __('Result'), visible: false, searchList: { "0": __('Result 0'), "1": __('Result 1') }, formatter: Table.api.formatter.normal }, { field: 'result_data', title: __('Result'), operate: false, formatter: function (value, row) { if (row.result == 0) { return '订单满' + row.result_data.money + '元,打' + row.result_data.number + '折'; } else { return '订单满' + row.result_data.money + '元,减' + row.result_data.number + '元'; } } }, // { // field: 'result_data', // title: __('Result_data'), // operate: 'LIKE' // }, { field: 'give_num', title: __('Give_num'), operate: 'BETWEEN' }, { field: 'allow_num', title: __('Allow_num'), operate: 'BETWEEN' }, { field: 'received_num', title: __('Received_num'), operate: 'BETWEEN' }, { field: 'is_private', title: __('Is_private'), searchList: { "yes": __('Is_private yes'), "no": __('Is_private no') }, formatter: Table.api.formatter.normal }, { field: 'is_open', title: __('Is_open'), searchList: { "0": __('Is_open 0'), "1": __('Is_open 1') }, formatter: Table.api.formatter.normal }, { field: 'url', title: __('Url'), operate: false, formatter: function (value, row, index) { return ''; } }, { field: 'status', title: __('Status'), operate: false, formatter: function (value, row, index) { return row.expired ? "已过期" : "正常"; } }, { field: 'receive_times', title: __('Receive_times'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false }, { field: 'use_times', title: __('Use_times'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: function (value, row) { if (row.mode == 'fixation') { return value + '天'; } else { return value; } } }, { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, visible: false, formatter: Table.api.formatter.datetime }, { field: 'updatetime', title: __('Updatetime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, visible: false, operate: false, formatter: Table.api.formatter.datetime }, { field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate } ] ] }); // 为表格绑定事件 Table.api.bindevent(table); }, select: function () { // 从URL参数中获取选择模式,默认为单选(优惠券通常单选) var urlParams = new URLSearchParams(window.location.search); var selectMode = urlParams.get('selectMode') || 'single'; // 'single' 或 'multiple' var maxSelect = parseInt(urlParams.get('maxSelect')) || 0; // 最大选择数量,0表示无限制 // 初始化表格参数配置 Table.api.init({ extend: { index_url: 'shop/coupon/index' + location.search, table: 'shop_coupon', } }); Table.config.dragsortfield = ''; var table = $("#table"); // 根据选择模式配置表格列 var columns = [ {field: 'id', title: 'ID', width: 60}, { field: 'name', title: '优惠券信息', operate: 'LIKE', formatter: function (value, row, index) { var html = '
'; html += '
' + value + '
'; // 优惠券条件和结果描述 if (row.result_data) { html += '
'; if (row.result == 0) { html += '满' + row.result_data.money + '元,打' + row.result_data.number + '折'; } else { html += '满' + row.result_data.money + '元,减' + row.result_data.number + '元'; } html += '
'; } // 使用时间和状态 html += '
'; if (row.receive_times) { html += '领取: ' + row.receive_times; } if (row.use_times) { if (row.receive_times) html += ' | '; html += '使用: ' + row.use_times; if (row.mode == 'fixation') { html += '天'; } } html += '
'; return html; } }, { field: 'condition_name', title: '使用条件', width: 120, operate: false, formatter: function (value, row, index) { return '' + (value || '-') + ''; } }, { field: 'give_num', title: '发放/已领', width: 100, operate: false, formatter: function (value, row, index) { return '
' + '
发放: ' + (row.give_num || 0) + '
' + '
已领: ' + (row.received_num || 0) + '
' + '
'; } }, { field: 'status', title: '状态', width: 80, operate: false, formatter: function (value, row, index) { if (row.expired) { return '已过期'; } else if (row.is_open == 1) { return '正常'; } else { return '停用'; } } }, // 操作列 - 添加选择按钮 { field: 'operate', title: selectMode === 'single' ? '选择' : '操作', width: selectMode === 'single' ? 80 : 100, operate: false, formatter: function (value, row, index) { // 检查优惠券是否可选(未过期且开启) var disabled = row.expired || row.is_open != 1; var disabledClass = disabled ? ' disabled' : ''; var disabledAttr = disabled ? ' disabled="disabled"' : ''; var html = ''; if (selectMode === 'single') { // 单选模式:蓝色选择按钮 html = ''; } else { // 多选模式:绿色选择按钮 html = ''; } return html; } } ]; // 初始化表格 table.bootstrapTable({ url: $.fn.bootstrapTable.defaults.extend.index_url, pk: 'id', sortName: 'id', clickToSelect: false, // 禁用点击行选择 columns: [columns] }); // 存储选中的优惠券(用于按钮模式) var selectedCoupons = []; // 单选按钮点击事件 $(document).on('click', '.btn-select-single:not(.disabled)', function () { var $btn = $(this); var id = $btn.data('id'); var index = $btn.data('index'); var rowData = table.bootstrapTable('getData')[index]; // 直接选择优惠券并关闭弹窗 Fast.api.close([rowData]); }); // 多选按钮点击事件 $(document).on('click', '.btn-select-multi:not(.disabled)', function () { var $btn = $(this); var id = $btn.data('id'); var index = $btn.data('index'); var isSelected = $btn.data('selected'); var rowData = table.bootstrapTable('getData')[index]; if (!isSelected) { // 检查最大选择限制 if (maxSelect > 0 && selectedCoupons.length >= maxSelect) { Toastr.warning('最多只能选择 ' + maxSelect + ' 个优惠券'); return; } // 添加到选中列表 selectedCoupons.push(rowData); $btn.removeClass('btn-success').addClass('btn-danger') .html(' 取消选择') .attr('title', '点击从选择列表中移除') .data('selected', true); } else { // 从选中列表移除 selectedCoupons = selectedCoupons.filter(function(item) { return item.id != id; }); $btn.removeClass('btn-danger').addClass('btn-success') .html(' 选择') .attr('title', '点击添加到选择列表') .data('selected', false); } // 更新选中数量显示 updateSelectedCount(); }); // 更新选中数量显示 function updateSelectedCount() { var count = selectedCoupons.length; var countText = count > 0 ? '已选择 ' + count + ' 个优惠券' : '请选择优惠券'; // 更新确认按钮文本 $('.btn-coupon-select').text(countText === '请选择优惠券' ? '确认选择' : '确认选择 (' + count + ')'); // 如果达到最大选择数量,禁用其他选择按钮 if (maxSelect > 0 && count >= maxSelect) { $('.btn-select-multi[data-selected="false"]:not(.disabled)').prop('disabled', true).addClass('disabled'); } else { $('.btn-select-multi[data-selected="false"]:not(.disabled)').prop('disabled', false).removeClass('disabled'); } } // 确认按钮绑定事件(仅多选模式使用) $(document).on('click', '.btn-coupon-select', function () { var selectedRows = []; if (selectMode === 'single') { // 单选模式:不应该走到这里,因为单选时直接选择 Layer.alert('单选模式请直接点击优惠券的选择按钮'); return; } else { // 多选模式:使用按钮选择的优惠券 selectedRows = selectedCoupons; } if (selectedRows.length === 0) { Layer.alert('请选择优惠券'); return; } // 调用父窗口回调函数 Fast.api.close(selectedRows); }); // 在页面标题中显示选择模式 var modeText = selectMode === 'single' ? '单选' : '多选'; if (selectMode === 'multiple' && maxSelect > 0) { modeText += '(最多' + maxSelect + '个)'; } $('.panel-heading, .layui-layer-title').each(function() { var currentTitle = $(this).text(); if (currentTitle.indexOf('选择优惠券') !== -1 && currentTitle.indexOf(modeText) === -1) { $(this).text(currentTitle + ' - ' + modeText); } else if (currentTitle.indexOf('选择') !== -1 && currentTitle.indexOf('优惠券') === -1 && currentTitle.indexOf(modeText) === -1) { $(this).text(currentTitle.replace('选择', '选择优惠券') + ' - ' + modeText); } }); // 显示选择模式提示信息 $('#selection-info').show(); $('#selection-mode-text').text(modeText); if (selectMode === 'single') { $('#selection-tip').html('单击任意优惠券的"选择"按钮即可完成选择'); // 单选模式隐藏确认按钮 $('#confirm-select-btn').hide(); } else { var tipText = '点击优惠券的"选择"按钮添加到选择列表,绿色表示可选择,红色表示已选择'; if (maxSelect > 0) { tipText += ',最多可选择' + maxSelect + '个优惠券'; } $('#selection-tip').text(tipText); // 多选模式显示确认按钮 $('#confirm-select-btn').show(); } // 表格刷新后恢复选择状态 table.on('refresh.bs.table load-success.bs.table', function () { if (selectMode === 'multiple') { // 恢复多选按钮状态 setTimeout(function() { $('.btn-select-multi').each(function() { var $btn = $(this); var id = $btn.data('id'); var isSelected = selectedCoupons.some(function(item) { return item.id == id; }); if (isSelected && !$btn.hasClass('disabled')) { $btn.removeClass('btn-success').addClass('btn-danger') .html(' 取消选择') .attr('title', '点击从选择列表中移除') .data('selected', true); } else if (!$btn.hasClass('disabled')) { $btn.removeClass('btn-danger').addClass('btn-success') .html(' 选择') .attr('title', '点击添加到选择列表') .data('selected', false); } }); // 更新选中数量显示 updateSelectedCount(); }, 100); } }); // 为表格绑定事件 Table.api.bindevent(table); }, result: function () { //优惠模式切换 $('#c-result').on('change', function () { if ($(this).val() == 0) { $('#text-a').html('打'); $('#text-b').html('折'); } else { $('#text-a').html('减'); $('#text-b').html('元'); } }) //去掉日期的选项 $('#c-receive_times').data('ranges', 1); $('#c-use_times').data('ranges', 1); let receive_times1 = ''; let receive_times2 = ''; let use_times1 = ''; let use_times2 = ''; //有效期模式切换 $('.modes').click(function () { var value = $(this).val() //获取选中的radio的值 if (value != 'fixation') {//固定日期 receive_times2 = $('#c-receive_times').val(); use_times2 = $('#c-use_times').val(); $('#modetpl').html(Template('datestpl', {receive_times: receive_times1, use_times: use_times1})); //去掉日期的选项 $('#c-receive_times').data('ranges', 1); $('#c-use_times').data('ranges', 1); Form.events.daterangepicker($("form[role=form]")); } else {//天数 receive_times1 = $('#c-receive_times').val(); use_times1 = $('#c-use_times').val(); $('#modetpl').html(Template('fixationtpl', {receive_times: receive_times2, use_times: use_times2})); } }); }, add: function () { this.result(); Controller.api.bindevent(); }, edit: function () { this.result(); Controller.api.bindevent(); }, api: { bindevent: function () { Form.api.bindevent($("form[role=form]")); } } }; return Controller; });