CNLiveShopCouponsManagerViewController.swift
10.4 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
//
// CNLiveShopCouponsManagerViewController.swift
// metaCode
//
// Created by zx on 2023/12/22.
//
import Foundation
import MJRefresh
import SnapKit
class CNLiveShopCouponsManagerViewController:CNLiveBaseViewController,UITableViewDelegate,UITableViewDataSource{
let CNLiveCouponListTableViewCellID = "CNLiveCouponListTableViewCellID"
let CNLiveCouponListTableViewHeaderFooterViewID = "CNLiveCouponListTableViewHeaderFooterViewID"
lazy var listTableView: UITableView = {
let tableView = UITableView(frame: .zero, style: .plain)
tableView.separatorStyle = .none
tableView.backgroundColor = HexColor("#F6F6F6")
tableView.delegate = self
tableView.dataSource = self
tableView.allowsMultipleSelection = true
tableView.mj_header = MJRefreshNormalHeader.init(refreshingBlock: {[weak self] in
self!.requestData()
})
tableView.register(CNLiveCouponListTableViewCell.self, forCellReuseIdentifier: CNLiveCouponListTableViewCellID)
tableView.register(UITableViewHeaderFooterView.self, forHeaderFooterViewReuseIdentifier: CNLiveCouponListTableViewHeaderFooterViewID)
return tableView
}()
lazy var dataSource :Array<Any> = {
let dataSource = Array<Any>()
return dataSource
}()
lazy var createCouponBtn: UIButton = {
let button = UIButton(type: .custom)
button.setTitle("新增优惠券", for: .normal)
button.setTitleColor(HexColor("#FD862E"), for: .normal)
button.setImage(UIImage(named: "DS_coupon_create"), for: .normal)
button.titleLabel?.font = Font_15
let createLayer = CAShapeLayer()
createLayer.backgroundColor = HexColor("#E2E2E2")?.cgColor
createLayer.frame = CGRect(x: 0, y: 0, width: KSreenWidth, height: 0.5)
button.layer.addSublayer(createLayer)
button.addTarget(self, action: #selector(createCouponClick(_:)), for: .touchUpInside)
return button
}()
lazy var deleteDic: NSMutableDictionary = {
let dictionary = NSMutableDictionary()
return dictionary
}()
func requestData(){
showLoading(message: "")
let rightBar = self.rightNavBtns[0]
if rightBar.titleLabel?.text == "完成"{
self.tableViewEidting(edit: false)
}
CNLiveShopCouponViewModel.requestCouponListResponse { result, status in
if status == .success{
let dataArray = result as! [CNLiveShopCouponListModel]
self.listTableView.mj_header?.endRefreshing()
hiddenLoading()
if dataArray.count == 0{
if self.dataSource.count != 0{
self.dataSource = dataArray
self.listTableView.reloadData()
}
rightBar.isHidden = true
return
}
rightBar.isHidden = false
self.dataSource = dataArray
self.listTableView.reloadData()
}else{
hiddenLoading()
self.listTableView.mj_header?.endRefreshing()
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
navigationBarHidden = false
titleName = "优惠券"
self.view.backgroundColor = .white
let deleteNavBtn = UIButton(type: .custom)
deleteNavBtn.frame = CGRect(x: 0, y: KStatusBarHeight, width: KNavigationHeight-KStatusBarHeight, height: KNavigationHeight-KStatusBarHeight)
deleteNavBtn.backgroundColor = .clear
deleteNavBtn.setTitle("删除", for: .normal)
deleteNavBtn.setTitleColor(HexColor("#FC1541"), for: .normal)
deleteNavBtn.addTarget(self, action: #selector(deleteCoupons), for: .touchUpInside)
self.rightNavBtns = [deleteNavBtn]
self.view.addSubview(self.listTableView)
self.view.addSubview(self.createCouponBtn)
self.createCouponBtn.snp.makeConstraints { make in
make.left.equalTo(self.view.snp.left)
make.right.equalTo(self.view.snp.right)
make.bottom.equalTo(self.view.safeAreaLayoutGuide.snp.bottom)
make.height.equalTo(45)
}
self.listTableView.snp.makeConstraints { make in
make.left.equalTo(self.view.snp.left)
make.right.equalTo(self.view.snp.right)
make.bottom.equalTo(self.createCouponBtn.snp.top)
make.top.equalTo(self.view).offset(KNavigationHeight)
}
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.requestData()
}
// MARK: - Action
@objc func deleteCoupons(_ sender:UIButton){
if sender.titleLabel?.text == "删除"{
self.tableViewEidting(edit: true)
}else{
self.tableViewEidting(edit: false)
}
}
func tableViewEidting(edit:Bool){
let rightBar = self.rightNavBtns[0]
if edit{
rightBar.setTitle("完成", for: .normal)
rightBar.setTitleColor(HexColor("#FD862E"), for: .normal)
self.listTableView.setEditing(true, animated: true)
self.createCouponBtn.backgroundColor = HexColor("#FD862E")
self.view.backgroundColor = HexColor("#FD862E")
self.createCouponBtn.setTitleColor(.white, for: .normal)
self.createCouponBtn.setTitle("删除", for: .normal)
self.createCouponBtn.setImage(UIImage(named: "DS_coupon_edit_delete"), for: .normal)
}else{
self.createCouponBtn.backgroundColor = .white
self.view.backgroundColor = .white
self.createCouponBtn.setTitle("新增优惠券", for: .normal)
self.createCouponBtn.setTitleColor(HexColor("#FD862E"), for: .normal)
self.createCouponBtn.setImage(UIImage(named: "DS_coupon_create"), for: .normal)
rightBar.setTitle("删除", for: .normal)
rightBar.setTitleColor(HexColor("#666666"), for: .normal)
self.listTableView.setEditing(false, animated: true)
self.deleteDic.removeAllObjects()
}
if self.dataSource.count == 0{
rightBar.isHidden = true
}
}
@objc func createCouponClick(_ sender:UIButton){
if sender.titleLabel!.text == "删除"{
if self.deleteDic.count <= 0{
showMessage(message: "请选择需要删除的优惠券")
return
}
showLoading(message: "")
CNLiveShopCouponViewModel.requestDeleteCoupon(couponDic: self.deleteDic) { result, status in
hiddenLoading()
if status == .success{
let arr = NSMutableArray(array: self.dataSource)
let indexSet = NSMutableIndexSet()
for sectionString in self.deleteDic.allKeys{
let sectionStr = sectionString as! String
indexSet.add(Int(sectionStr) ?? 0)
}
arr.removeObjects(at: indexSet as IndexSet)
self.dataSource = Array(arr)
self.listTableView.reloadData()
self.deleteDic.removeAllObjects()
if self.dataSource.count == 0{
self.tableViewEidting(edit: false)
}
}else{
showMessage(message: "删除失败")
}
}
}else{
let vc = CNLiveShopCouponCreateViewController(couponListModel: nil, type: .create)
navigationViewController().pushViewController(vc, animated: pushAnimated)
// currentViewController()?.navigationController?.pushViewController(vc, animated: pushAnimated)
}
}
// MARK: - UITableViewDelegate
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 122
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 15
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = tableView.dequeueReusableHeaderFooterView(withIdentifier:CNLiveCouponListTableViewHeaderFooterViewID)
view?.contentView.backgroundColor = HexColor("#F6F6F6")
return view
}
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
return .none
}
// MARK: - UITableViewDataSource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func numberOfSections(in tableView: UITableView) -> Int {
return self.dataSource.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: CNLiveCouponListTableViewCellID) as? CNLiveCouponListTableViewCell
else {
let cell = CNLiveCouponListTableViewCell()
return cell
}
cell.model = self.dataSource[indexPath.section] as? CNLiveShopCouponListModel
cell.actionClickBlock = { index,model in
switch index {
case 0:
if model.id!.isEmpty{
return
}
// // 分享
// CNShareUserViewController *shareVC = [[CNShareUserViewController alloc] initWithCouponData:[model mj_JSONObject] shareType:CNTypeShareCoupon complete:^(BOOL success) {
//
// }];
// [self dsPushViewController:shareVC animated:YES animaterDirection:DSTransitionAnimaterDirectionTop];
break
case 1:
// 编辑
let vc = CNLiveShopCouponCreateViewController(couponListModel: model, type: .edit)
currentViewController()?.navigationController?.pushViewController(vc, animated: true)
break
default: break
}
}
cell.editBtnClickBlock = { selected,couponId in
if selected{
self.deleteDic.setObject(couponId, forKey: NSString(format: "%d", indexPath.section))
}else{
self.deleteDic.removeObject(forKey: NSString(format: "%d", indexPath.section))
}
}
return cell
}
}