limitcode-edit.js
7.13 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
var $ = require('jquery');
var common = require('../common/common');
var editBean = require('./partials/limitcode-edit-bean');
var e = new common.edit2("#basicForm");
var type = $("#basicForm").attr("role"),
action = $("#basicForm").data("action");
var isFirst = true;
var limitDateFromObj;
var limitDateToObj;
var Bll = {
__render:function() {
editBean.currTime = common.util.__dateFormat(new Date(), "yyyy-MM-dd hh:mm:ss");
$("#basicForm").html(common.util.__template2($("#limitcodeEdit-template").html(), editBean));
e.init();
limitDateFromObj = $("#limitDateFrom").fdatepicker({
format: 'yyyy-mm-dd hh:ii:ss',
pickTime: true
}).data("datepicker");
limitDateToObj = $("#limitDateTo").fdatepicker({
format: 'yyyy-mm-dd hh:ii:ss',
pickTime: true
}).data("datepicker");
limitDateFromObj.setEndDate(editBean.limitDateTo);
if(editBean.limitDateFrom) {
limitDateToObj.setStartDate(editBean.currTime);
} else if(new Date(editBean.currTime).getTime() < new Date(editBean.limitDateFrom)){
limitDateToObj.setStartDate(editBean.limitDateFrom);
} else {
limitDateToObj.setStartDate(editBean.currTime);
}
if(type == "edit") {
$('input[name="skn_sku"]').prop('disabled', true);
$(".noEdit").prop("disabled", true);
$('input[name="userTypeLimit"]').prop('disabled', true);
if((new Date(editBean.limitDateFrom).getTime() <= new Date().getTime()) && isFirst) {
$("#limitDateFrom").prop("disabled", true);
}
isFirst = false;
}
},
__checkSkn:function(callback) {
var flag = false;
if(editBean.limitSkn) {
common.util.__ajax2({
async: false,
url: "/limitCode/checkSknValidity",
data: {
skn: editBean.limitSkn,
id: editBean.id
}
}, function (res) {
if (!res.data.isExist) {
flag = true;
common.util.__tip("SKN不存在");
} else if (res.data.isConflict) {
flag = true;
common.util.__tip("SKN与其他限购冲突");
} else if (res.data.isOnSales) {
flag = true;
common.util.__tip("SKN已上架,请先下架");
}
callback && callback(res);
}, function() {
flag = true;
common.util.__tip("网络请求异常");
});
}
return flag;
}
};
if(type != "add") {
var id = location.href.substring(location.href.lastIndexOf("/") + 1);
common.util.__ajax({
url: "/limitCode/getLimitCode",
data: {id: id}
}, function (res) {
editBean = res.data;
editBean.reqDepartment = editBean.reqDepartment.split("/");
if(!editBean.reqDepartment.length) {
editBean.reqDepartment = [
"零售部",
"营销策划"
];
}
editBean.selected = editBean.skus ? "SKU" : "SKN";
editBean.updateType = type == "edit" ? "1":"2";
editBean.skusInfo = [];
if(editBean.skus) {
var skuArr = editBean.skus.split(";");
var skuNumArr = editBean.skuTimes.split(";");
for(var i = 0; i < skuArr.length; i++) {
editBean.skusInfo.push({
"sku": skuArr[i],
"skuTimes": skuNumArr[i]
});
}
}
Bll.__render();
},true);
} else {
Bll.__render();
}
$(document).on("change", ".observe", function () {
var $this = $(this);
var name = $this.data("field");
editBean = common.util.__buildobj(name, '.', editBean, function (obj, name) {
obj[name] = $this.val();
});
if($this.prop("id") == "limitDateFrom" && new Date($this.val()).getTime() ) {
if(new Date(editBean.currTime).getTime() > new Date(editBean.limitDateFrom)) {
limitDateToObj.setStartDate(editBean.currTime);
} else {
limitDateToObj.setStartDate(editBean.limitDateFrom);
}
//console.log(editBean.limitDateFrom);
}
//console.log(editBean);
});
//输入限制
$(document).on("keyup", ".number", function() {
$(this).val($(this).val().replace(/\D/g, ''));
});
//单选框点击事件
$(document).on("click", ".limitProduct", function() {
editBean.limitCodeType = $(this).val();
$("#limitSkn").attr("required", !(editBean.limitCodeType == "N"));
Bll.__render();
//console.log(editBean);
});
$(document).on("click", ".delSku", function () {
var index = $(this).data("index");
editBean.skusInfo.splice(index, 1);
Bll.__render();
$(".addSku").attr("disabled", false);
});
$(document).on("click", ".addSku", function () {
editBean.skusInfo.push({
"sku": "",
"skuTimes": ""
});
Bll.__render();
});
$(document).on("blur", "#limitSkn", function() {
Bll.__checkSkn(function(res) {
if (res.data.skuList) {
editBean.skusInfo = [];
for (var i = 0; i < res.data.skuList.length; i++) {
editBean.skusInfo.push({
"sku": res.data.skuList[i],
"skuTimes": ""
});
}
Bll.__render();
$(".addSku").attr("disabled", false);
}
});
});
$(document).on("click","#save_brand", function() {
// 限购码状态0:待审核 1:审核通过 2:驳回 3:过期 4:作废
if(e.validate() && (type == "add" ? (!editBean.limitSkn || !Bll.__checkSkn()) : true)) {
if(editBean.limitCodeType == "U") {
for (var i in editBean.skusInfo) {
if(!editBean.skusInfo[i].skuTimes || editBean.skusInfo[i].skuTimes == "0") {
common.util.__tip("SKU数量不能为0");
return;
}
}
}
if(typeof editBean.reqDepartment=="string"){
editBean.reqDepartment=[editBean.reqDepartment];
}
editBean.reqDepartment =editBean.reqDepartment.join("/");
editBean.creatorId = $("#auth_id").val();
editBean.creatorName = $("#auth_name").val();
if (editBean.status != 1) {
editBean.status = 0;
}
if(editBean.limitCodeType == "U") {
editBean.skus = "";
editBean.skuTimes = "";
for(var item in editBean.skusInfo) {
if(item != 0) {
editBean.skus+=";";
editBean.skuTimes+=";";
}
editBean.skus += editBean.skusInfo[item].sku;
editBean.skuTimes+=editBean.skusInfo[item].skuTimes;
}
}
//console.log(JSON.stringify(editBean));
common.util.__ajax({
url: action,
data: editBean
}, function (res) {
location.href = "/market/limitcode/index";
});
}
return false;
});