CNLiveShopCouponViewController.swift
12 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
//
// CNLiveShopCouponViewController.swift
// metaCode
//
// Created by open on 2023/11/16.
//
import Foundation
import EmptyDataSet_Swift
import IQKeyboardManagerSwift
import APButton
enum CNLiveShopCouponShowType {
case create //创建
case edit //编辑
}
class CNLiveShopCouponViewController: CNLiveBaseViewController, UITableViewDelegate, UITableViewDataSource, EmptyDataSetSource, EmptyDataSetDelegate {
let kShopBodyCouponCreateTableViewCellIdentifier = "kShopBodyCouponCreateTableViewCellIdentifier"
let kUITableViewHeaderFooterViewIdentifier = "kUITableViewHeaderFooterViewIdentifier"
lazy var tableView : UITableView = {
let tableView = UITableView(frame: .zero, style: .plain)
tableView.dataSource = self
tableView.delegate = self
tableView.emptyDataSetSource = self
tableView.emptyDataSetDelegate = self
tableView.backgroundColor = HexColor("#f6f6f6")
tableView.tableFooterView = UIView.init()
tableView.separatorStyle = .none
tableView.register(ShopBodyCouponCreateTableViewCell.self, forCellReuseIdentifier: kShopBodyCouponCreateTableViewCellIdentifier)
tableView.register(UITableViewHeaderFooterView.self, forHeaderFooterViewReuseIdentifier: kUITableViewHeaderFooterViewIdentifier)
return tableView
}()
lazy var footerView: UIView = {
let footerView = UIView.init(frame: CGRect(x: 0, y: KSreenHeight - get_system_tabbar_height(), width: KSreenWidth, height: get_system_tabbar_height()))
footerView.backgroundColor = .white
footerView.addSubview(cancelBtn)
cancelBtn.snp.makeConstraints { make in
make.width.equalTo(adapt_length(length: 130))
make.height.equalTo(get_system_tabbar_ui_height())
make.left.equalToSuperview()
make.top.equalToSuperview()
}
footerView.addSubview(deliveryBtn)
deliveryBtn.snp.makeConstraints { make in
make.width.equalTo(KSreenWidth - adapt_length(length: 130))
make.height.equalTo(get_system_tabbar_ui_height())
make.right.equalToSuperview()
make.top.equalToSuperview()
}
return footerView
}()
lazy var cancelBtn: UIButton = {
let cancelBtn = UIButton.init(type: .custom)
cancelBtn.backgroundColor = .white
cancelBtn.setTitle("取消", for: .normal)
cancelBtn.setTitleColor(HexColor("#3A3A3A"), for: .normal)
cancelBtn.titleLabel?.font = medium_adapt_font(pointSize: 14)
cancelBtn.addTarget(self, action: #selector(cancelBtnClick), for: .touchUpInside)
return cancelBtn
}()
lazy var deliveryBtn: APButton = {
let deliveryBtn = APButton.init()
deliveryBtn.backgroundColor = HexColor("#65C368")
deliveryBtn.setTitle("保存", for: .normal)
deliveryBtn.activityIndicator.color = .white
deliveryBtn.titleLabel?.font = regular_adapt_font(pointSize: 15)
deliveryBtn.setTitleColor(HexColor("#FFFFFF"), for: .normal)
deliveryBtn.addTarget(self, action: #selector(sendBtnClick), for: .touchUpInside)
return deliveryBtn
}()
lazy var dataArray: NSMutableArray = {
let dataArray = NSMutableArray.init()
return dataArray
}()
var _listModel: ShopOrderCouponListModel!
var _show_type: CNLiveShopCouponShowType!
/// 创建优惠券使用的
init() {
_show_type = .create
_listModel = ShopOrderCouponListModel.init()
super.init(nibName:nil, bundle:nil)
}
/// 编辑优惠券需要调用
/// - Parameters:
/// - listModel: 数据源
/// - show_type: 显示类型
init(listModel: ShopOrderCouponListModel) {
_show_type = .edit
_listModel = listModel
super.init(nibName:nil, bundle:nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
var hiddenGoodsLine: Bool!
var network_type: CNLiveMineResponseType!
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = HexColor("#f6f6f6")
navigationBarHidden = false
titleName = "发布优惠券"
loadDataSourceAction()
view.addSubview(tableView)
tableView.snp.makeConstraints { make in
make.top.equalTo(KNavigationHeight)
make.left.right.equalToSuperview()
make.bottom.equalToSuperview().offset(-get_system_tabbar_height())
}
view.addSubview(footerView)
}
func loadDataSourceAction() {
CNLiveShopViewModel.requestCoupon(model: _listModel) { [weak self] result, respStatue in
if respStatue == .success {
self!.dataArray = result as! NSMutableArray
self!.tableView.reloadData()
let model = (self!.dataArray[1] as! NSArray)[0] as! ShopOrderCouponDataModel
self!.hiddenGoodsLine = model.section == "1"
}
}
}
@objc func cancelBtnClick() {
self.navigationController?.popViewController(animated: true)
}
var couponTitle: String! = ""
var totalNum: String! = ""
var maxMoney: String! = ""
var lessMoney: String! = ""
var startTime: String! = ""
var endTime: String! = ""
var goodsId: String! = ""
@objc func sendBtnClick(sender: APButton) {
let model01 = (self.dataArray[1] as! NSArray)[0] as! ShopOrderCouponDataModel
let model02 = (self.dataArray[1] as! NSArray)[2] as! ShopOrderCouponDataModel
let model03 = (self.dataArray[1] as! NSArray)[3] as! ShopOrderCouponDataModel
let model04 = (self.dataArray[1] as! NSArray)[4] as! ShopOrderCouponDataModel
if _show_type == .create {
showLoading(message: "提交中...")
sender.startAnimating()
CNLiveShopViewModel.requestCouponCreate(title: couponTitle, filled: maxMoney, discount: lessMoney, startTime: startTime, endTime: endTime, state: model02.section!, isShow: model04.section!, receiveNum: model03.section!, totalNum: totalNum, goodsType: model01.section!, goodsId: goodsId) { result, respStatue in
sender.stopAnimating()
if respStatue == .success {
showMessage(message: result as! String)
}
}
} else {
showLoading(message: "提交中...")
sender.startAnimating()
CNLiveShopViewModel.requestCouponEdit(model: _listModel, title: (couponTitle.isNotBlank() ? couponTitle : _listModel.title)!, state: model02.section!, isShow: model04.section!, receiveNum: model03.section!) { result, respStatue in
sender.stopAnimating()
if respStatue == .success {
showMessage(message: result as! String)
}
}
}
}
}
extension CNLiveShopCouponViewController {
func numberOfSections(in tableView: UITableView) -> Int {
return dataArray.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let temp_array = dataArray[section] as! NSArray
return temp_array.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: kShopBodyCouponCreateTableViewCellIdentifier) as? ShopBodyCouponCreateTableViewCell else {
return ShopBodyCouponCreateTableViewCell()
}
if _show_type == .edit {
if ((indexPath.section == 0 && indexPath.row == 0) || (indexPath.section == 1 && (indexPath.row == 2 || indexPath.row == 3 || indexPath.row == 4))) {
cell.editEnabled = true
} else {
cell.editEnabled = false
}
} else {
cell.editEnabled = true
}
cell.setupWithDataSource(couponModel: (dataArray[indexPath.section] as! NSArray)[indexPath.row] as! ShopOrderCouponDataModel)
cell.textFieldCallBack = { [self] section, number in
switch section {
case "1":
couponTitle = number
break
case "3":
totalNum = number
break
case "1001":
maxMoney = number
break
case "1002":
lessMoney = number
break
default:
break
}
}
cell.dateSelectCallBack = { [self] id, date in
let interval = date.timeIntervalSince1970
if id == "4" {
startTime = "\(interval)"
} else {
endTime = "\(interval + 3600 * 24 - 1)"
}
}
if (indexPath.row == 1 && indexPath.section == 1) {
if goodsId.isNotBlank() && _listModel.goods_id!.isNotBlank() && _listModel.goods_id != "0" {
cell.goodsLabel.text = ""
}
cell.isHidden = hiddenGoodsLine
}
cell.goodsHiddenCallBack = { [self] isHidden in
hiddenGoodsLine = isHidden
goodsId = ""
let indexPath = IndexPath(row: 1, section: 1)
tableView.reloadRows(at: [indexPath], with: .middle)
}
cell.goodsCallBack = { [weak self] goodsId in
self!.goodsId = goodsId
}
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.section == 1 && indexPath.row == 1 && hiddenGoodsLine {
return 0
}
return adapt_length(length: 50)
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if section == 0 {
return 0
}
return adapt_length(length: 5)
}
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
view.endEditing(true)
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = tableView.dequeueReusableHeaderFooterView(withIdentifier: kUITableViewHeaderFooterViewIdentifier)
view?.contentView.backgroundColor = HexColor("#f6f6f6")
return view
}
}
extension CNLiveShopCouponViewController {
func image(forEmptyDataSet scrollView: UIScrollView) -> UIImage? {
if network_type == .failed || network_type == .unkonw {
return UIImage(named: "mine_list_source_unusual_failed")
}
if dataArray.count == 0 {
return UIImage(named: "mine_list_source_unusual_empty")
}
if network_type == .noNetwork {
return UIImage(named: "mine_list_source_unusual_noNet")
}
return UIImage(named: "mine_list_source_unusual_empty")
}
func title(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
var defult_string = ""
if network_type == .failed || network_type == .unkonw {
defult_string = "接口请求错误,刷新试试~"
}
if network_type == .noNetwork {
defult_string = "网络开小差了,刷新试试~"
}
if dataArray.count == 0 {
defult_string = "暂时没有数据~"
}
return NSAttributedString(string: defult_string, attributes: [.font: Font_17,.foregroundColor: HexColor("#666666")!])
}
func buttonTitle(forEmptyDataSet scrollView: UIScrollView, for state: UIControl.State) -> NSAttributedString? {
if network_type == .failed || network_type == .noNetwork {
let attributes = [NSAttributedString.Key.font:Font_16_Fit,
NSAttributedString.Key.foregroundColor: HexColor("#609BFF")]
return NSAttributedString(string: "刷一刷", attributes: attributes as [NSAttributedString.Key : Any])
}
return nil
}
func emptyDataSet(_ scrollView: UIScrollView, didTapView view: UIView) {
if network_type == .failed || network_type == .noNetwork {
loadDataSourceAction()
}
}
}