invoice.js
4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* 结算页发票
* @author: xuqi<qi.xu@yoho.cn>
* @date: 2016/7/13
*/
var $ = require('yoho-jquery'),
Dialog = require('../../plugins/dialog').Dialog;
var checkedInvoiceTpl = require('../../../tpl/shopping/checked-invoice-show.hbs');
var $invoiceContent = $('#invoice-content');
var $invoiceTitleInput;
// 发票信息验证
function validateInvoice($el) {
var pass = true;
var name = $.trim($el.find('.invoice-title-input').val());
// 发票抬头为单位
if ($el.invoiceTitleType === 2 && name === '') {
pass = false;
$el.find('.invoice-title-tip').removeClass('hide');
}
return pass;
}
// 发票弹窗Factory
function invoiceDialogFactory() {
var invoice = new Dialog({
className: 'invoice',
content: $('#invoice-dialog-tpl').html(),
btns: [
{
id: 'save-invoice',
btnClass: ['save-invoice'],
name: '保存发票信息',
cb: function() {
var $el = invoice.$el;
if (validateInvoice($el)) {
$invoiceContent.html(checkedInvoiceTpl({
invoiceTitle: $el.invoiceTitleType === 1 ? '个人' : $invoiceTitleInput.val(),
content: $el.invoiceContent,
invoiceContent: $el.find('.invoice-content-radio .checked').next('label').text(),
checked: true // 发票开具radio选中
}));
// 处理发票信息
invoice.close();
}
}
},
{
id: 'cancel-invoice',
btnClass: ['cancel-invoice', 'white'],
name: '取消',
cb: function() {
invoice.close();
}
}
]
});
return invoice;
}
function showInvoiceDialog() {
var $invoceDetail = $invoiceContent.find('.invoice-detail');
var dialog = invoiceDialogFactory();
var $invoiceTip = $('.yoho-dialog.invoice .input-tip');
var isEditInvoice = $invoceDetail.length > 0;
// radio默认值
var invoiceTitleType = 1,
invoiceContent = $('.invoice-content-radio').first().data('value');
var title;
$invoiceTitleInput = $('.yoho-dialog.invoice .invoice-title-input');
if (isEditInvoice) {
title = $invoceDetail.data('title');
invoiceTitleType = title === '个人' ? 1 : 2;
invoiceContent = $invoceDetail.data('content');
if (invoiceTitleType === 2) {
$invoiceTitleInput.removeClass('hide').val(title);
}
}
// 设置radio选中
$('[data-value=' + invoiceTitleType + '].invoice-title-radio').find('.radio').addClass('checked');
$('[data-value=' + invoiceContent + '].invoice-content-radio').find('.radio').addClass('checked');
// 存储发票参数
$.extend(dialog.$el, {
invoiceTitleType: invoiceTitleType,
invoiceContent: invoiceContent
});
// 初始化发票抬头radio
$('.invoice-title-radio').check({
type: 'radio',
group: 'invoice-title',
onChange: function(el, checked, value) {
// 只处理选中的change逻辑
if (checked) {
if ($(el).hasClass('personal')) {
$invoiceTitleInput.addClass('hide');
} else {
$invoiceTitleInput.removeClass('hide');
}
$invoiceTip.addClass('hide'); // 切换时隐藏提示
dialog.$el.invoiceTitleType = value; // 1-个人;2-单位
}
}
});
// 初始化发票内容radio
$('.invoice-content-radio').check({
type: 'radio',
group: 'invoice-content',
onChange: function(el, checked, value) {
if (checked) {
dialog.$el.invoiceContent = value;
}
}
});
dialog.show();
}
$invoiceContent.on('click', '.invoice-radio', function() {
if ($(this).find('.checked').length > 0) {
// 取消选中
$invoiceContent.html(checkedInvoiceTpl({
checked: false
}));
} else {
// 选中
showInvoiceDialog();
}
}).on('click', '.modify-invoice', function() {
showInvoiceDialog();
});