define(['jquery', 'underscore', 'bootstrap'], function($, _){
var coupon = {
'defaultoptions' : {
callback : null,
type : 'all',
multiple : true,
ignore : {
'local' : false,
'wechat' : false
}
},
'init' : function(callback, options) {
var $this = this;
$this.options = $.extend({}, $this.defaultoptions, options);
$this.options.callback = callback;
$('#counpon-Modal').remove();
$(document.body).append($this.buildHtml().mainDialog);
$this.modalobj = $('#counpon-Modal');
$this.modalobj.find('.modal-header .nav li a').click(function(){
var type = $(this).data('type');
$this.localPage(type, 1);
$(this).tab('show')
return false;
});
if (!$(this).data('init')) {
if($this.options.type && $this.options.type != 'all') {
$this.modalobj.find('.modal-header .nav li.' + $this.options.type + ' a').trigger('click');
} else {
$this.modalobj.find('.modal-header .nav li.show:first a').trigger('click');
}
}
$this.modalobj.modal('show');
return $this.modalobj;
},
'localPage' : function(type, page) {
var $this = this;
var $history = $this.modalobj.find('.coupon-content #' + type);
$history.html('
数据加载中
');
$.getJSON('./index.php?c=utility&a=coupon&do=' + type, {'page': page}, function(data){
data = data.message;
$this.modalobj.find('#coupon-list-pager').html('');
if(!_.isEmpty(data.items)) {
$this.modalobj.find('#btn-select').show();
$history.data('attachment', data.items);
$history.empty();
var Dialog = type + 'Dialog';
$history.html(_.template($this.buildHtml()[Dialog])(data));
$this.modalobj.find('#coupon-list-pager').html(data.page);
$this.modalobj.find('.pagination a').click(function(){
$this.localPage(type, $(this).attr('page'));
});
$history.find('.conpon-list').click(function(){
$this.selectCoupon(this);
});
} else {
$history.html(' 暂无数据
');
}
});
$this.modalobj.find('.modal-footer .btn-primary').unbind('click').click(function(){
var attachment = [];
$history.find('.btn-primary').each(function(){
attachment.push($history.data('attachment')[$(this).data('attachid')]);
$(this).removeClass('btn-primary');
});
$this.finish(attachment);
});
return false;
},
'selectCoupon' : function(obj) {
var $this = this;
$(obj).toggleClass('btn-primary');
if (!$this.options.multiple) {
$this.modalobj.find('.modal-footer .btn-primary').trigger('click');
}
},
'finish' : function(attachment) {
var $this = this;
if($.isFunction($this.options.callback)) {
if ($this.options.multiple == false) {
$this.options.callback(attachment[0]);
} else {
$this.options.callback(attachment);
}
$this.modalobj.modal('hide');
}
},
'buildHtml' : function() {
var dialog = {};
dialog['mainDialog'] = '\n' +
'
\n' +
'
\n' +
' \n' +
'
\n' +
' \n'+
'
\n' +
'
\n' +
'
';
dialog['wechatDialog'] ='\n'+
' \n'+
' \n'+
' 标题 | \n'+
' 类型 | \n'+
' 卡券有效期 | \n'+
' 库存/每人限领 | \n'+
' 操作 | \n'+
'
'+
' '+
' '+
' <%var items = _.sortBy(items, function(item) {return -item.id;});%>' +
' <%_.each(items, function(item) {%> \n' +
' ' +
' <%=item.title%> | ' +
' <%if(item.type == "1") {%><%=item.extra.discount%>折 折扣券<%} else if(item.type == "2") {%><%=item.extra.reduce_cost%>元 代金券<%} else if(item.type == "4") {%>礼品券<%} else if(item.type == "3") {%>团购券<%} else if(item.type == "5") {%>优惠券<%}%> | ' +
' <%if(item.date_info.time_type == 1) {%><%=item.date_info.time_limit_start%> ~ <%=item.date_info.time_limit_end%><%} else {%>领取后<%=item.date_info.date_info%>天后生效,<%=item.date_info.limit%>天有效期<%}%> | ' +
' <%=item.quantity%>/<%=item.get_limit%> | ' +
' 选取 | ' +
'
' +
' <%});%>' +
' '+
'
';
return dialog;
}
};
return coupon;
});