define(['jquery', 'table', 'form'], function($, Table, Form) {
Yzn.config.openArea = ['90%', '90%'];
var Controller = {
index: function() {
Table.init = {
table_elem: '#currentTable',
table_render_id: 'currentTable',
add_url: 'emailbroadcast/add',
edit_url: 'emailbroadcast/edit',
delete_url: 'emailbroadcast/del'
};
Table.render({
init: Table.init,
toolbar: ['refresh', 'add'],
url: 'emailbroadcast/index',
cols: [[
{type:'checkbox', width:50},
{field:'id', width:80, title:'ID', sort:true},
{field:'name', minWidth:220, title:'任务名称'},
{field:'lang', width:100, title:'语言'},
{field:'template_name', minWidth:160, title:'模板'},
{field:'recipient_count', width:110, title:'目标人数'},
{field:'success_count', width:110, title:'成功数'},
{field:'fail_count', width:110, title:'失败数'},
{field:'status_text', width:110, title:'状态'},
{field:'start_time', width:170, title:'开始时间', templet:function(d){ return d.start_time ? Table.formatter.datetime.call(this, d.start_time) : '-'; }},
{field:'finish_time', width:170, title:'完成时间', templet:function(d){ return d.finish_time ? Table.formatter.datetime.call(this, d.finish_time) : '-'; }},
{field:'createtime', width:170, title:'创建时间', templet: Table.formatter.datetime},
{field:'operate', title:'操作', width:360, fixed:'right', templet:function(d){
var status = parseInt(d.status, 10);
var sendText = [0,3,4].indexOf(status) > -1 ? '执行发送' : '查看状态';
var html = '明细';
if (status === 0) {
html += '编辑';
}
html += ''+sendText+'';
html += '删除';
return html;
}}
]],
page: {}
});
$('#searchBtn').on('click', function() {
layui.table.reload('currentTable', { where: {
name: $('#name').val(), lang: $('#lang').val(), status: $('#status').val(), template_id: $('#template_id').val()
}, page: {curr: 1} });
});
$('#resetBtn').on('click', function() {
$('#name,#lang,#status,#template_id').val('');
layui.form.render('select');
layui.table.reload('currentTable', { where: {}, page: {curr: 1} });
});
$(document).on('click', '[data-event="items"]', function() {
location.href = Backend.api.fixurl('emailbroadcast/items') + '?id=' + $(this).data('id');
});
$(document).on('click', '[data-event="edit"]', function() {
location.href = Backend.api.fixurl('emailbroadcast/edit') + '?id=' + $(this).data('id');
});
$(document).on('click', '[data-event="send"]', function() {
var id = $(this).data('id');
var status = parseInt($(this).data('status'), 10);
if ([0,3,4].indexOf(status) === -1) {
var tips = {
1: '该任务当前处于发送中,请稍后刷新查看结果。',
2: '该任务已经全部发送完成,无需再次执行。'
};
layer.msg(tips[status] || '当前状态不允许执行发送');
return;
}
var batchSize = parseInt($('#batch_size').val(), 10) || 100;
if (batchSize < 1) batchSize = 1;
if (batchSize > 500) batchSize = 500;
layer.confirm('确认立即执行该群发任务吗?发送后将逐条写入邮件日志。', {icon:3, title:'提示'}, function(index) {
$.post('emailbroadcast/send', {id:id, batch_size:batchSize}, function(res) {
layer.msg(res.msg || (res.code === 1 ? '发送完成' : '发送失败'));
if (res.code === 1) {
layui.table.reload('currentTable');
}
}, 'json').fail(function(){layer.msg('发送请求失败');});
layer.close(index);
});
});
$(document).on('click', '[data-event="delete"]', function() {
var id = $(this).data('id');
layer.confirm('确认删除该群发任务及其明细吗?', {icon:3, title:'警告'}, function(index) {
$.post('emailbroadcast/del', {ids:[id]}, function(res) {
layer.msg(res.msg || (res.code === 1 ? '删除成功' : '删除失败'));
if (res.code === 1) {
layui.table.reload('currentTable');
}
}, 'json').fail(function(){layer.msg('删除请求失败');});
layer.close(index);
});
});
Table.api.bindevent();
Form.api.bindevent($('form.layui-form'));
}
};
return Controller;
});