CNLiveShopCouponCreateViewController.swift
8.75 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
//
// CNLiveShopCouponCreateViewController.swift
// metaCode
//
// Created by zx on 2023/12/12.
//
import Foundation
import SnapKit
///跳转类型
enum CNLivePushCreateCouponType:Int {
//创建优惠券
case create
///编辑优惠券
case edit
}
class CNLiveShopCouponCreateViewController:CNLiveBaseViewController,UITableViewDelegate,UITableViewDataSource, CNLiveShopCouponButtonClickDelegate{
let kCNLiveCreateCouponCellIdentifier = "kCNLiveCreateCouponCellIdentifier"
let kCNLiveCreateCouponTableViewHeaderFooterViewID = "kCNLiveCreateCouponTableViewHeaderFooterViewID"
lazy var bottomView :CNLiveShopCouponCreateBottomView = {
let bottomView = CNLiveShopCouponCreateBottomView()
bottomView.delegate = self
return bottomView
}()
lazy var tableView : UITableView = {
let tableView = UITableView(frame: .zero, style: .plain)
tableView.dataSource = self
tableView.delegate = self
// tableView.emptyDataSetSource = self
// tableView.emptyDataSetDelegate = self
tableView.separatorStyle = .none
tableView.backgroundColor = HexColor("#F6F6F6")
tableView.register(CNLiveCreateCouponTableViewCell.self, forCellReuseIdentifier: kCNLiveCreateCouponCellIdentifier)
tableView.register(UITableViewHeaderFooterView.self, forHeaderFooterViewReuseIdentifier: kCNLiveCreateCouponTableViewHeaderFooterViewID)
return tableView
}()
var dataSource:Array<Array<CNLiveShopCouponModel>>?
var couponTitle:String?
var totalNum:String?
var maxMoney:String?
var lessMoney:String?
var startTime:String?
var endTime:String?
var goodsId:String?
var hiddenGoodsLine:Bool = false
var couponModel:CNLiveShopCouponListModel?
var type:CNLivePushCreateCouponType = .create
init(couponListModel:CNLiveShopCouponListModel?,type:CNLivePushCreateCouponType){
super.init(nibName: nil, bundle: nil)
self.couponModel = couponListModel
self.type = type
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
self.dataSource = CNLiveShopCouponViewModel.responseCouponCreateWithModel(listModel: self.couponModel)
let model = self.dataSource![1][0]
if model.section == 1{
self.hiddenGoodsLine = true
}else{
self.hiddenGoodsLine = false
}
self.view.backgroundColor = HexColor("#F6F6F6")
navigationBarHidden = false
titleName = "发布优惠券"
self.view.addSubview(self.tableView)
self.view.addSubview(self.bottomView)
self.bottomView.snp.makeConstraints { make in
make.left.right.equalTo(self.view)
make.bottom.equalTo(self.view.safeAreaLayoutGuide.snp.bottom)
make.height.equalTo(50)
}
self.tableView.snp.makeConstraints { make in
make.left.right.equalTo(self.view)
make.top.equalTo(self.view).offset(KNavigationHeight)
make.bottom.equalTo(self.bottomView.snp.top)
}
}
// MARK: - DSShopCouponButtonClickDelegate
func selectedItemButton(section:Int){
switch section{
case 0:
self.navigationController?.popViewController(animated: true)
break
case 1:
let model1 = self.dataSource![1][0]
let model2 = self.dataSource![1][2]
let model3 = self.dataSource![1][3]
let model4 = self.dataSource![1][4]
if self.type == .create{
CNLiveShopCouponViewModel.requestCreateCouponWithTitle(title: self.couponTitle ?? "", filled: self.maxMoney ?? "", discount: self.lessMoney ?? "", startTime: self.startTime ?? "", endTime: self.endTime ?? "", state: String(format: "%d", model2.section), isShow: String(format: "%d", model4.section), receiveNum: String(format: "%d", model3.section), totalNum: self.totalNum ?? "", type: model1.section, goodsId: self.goodsId ?? "") { result, status in
if status == .success{
if let ret = result as? Dictionary<String, Any> {
if ret["code"] as! String != "0" && ret["code"] as! String != "200"{
showMessage(message: ret["message"] as! String)
return
}
}
showMessage(message: "发布成功")
self.navigationController?.popViewController(animated: true)
}else{
showMessage(message: "发布失败")
}
}
}else{
CNLiveShopCouponViewModel.requestEditCouponWithModel(model: self.couponModel!, title: (self.couponTitle?.count ?? 0>0 ? self.couponTitle! : self.couponModel!.title)!, state: String(format: "%d", model2.section), isShow: String(format: "%d", model4.section), receiveNum: String(format: "%d", model3.section)) { result, status in
if status == .success{
showMessage(message: "发布成功")
self.navigationController?.popViewController(animated: true)
}else{
showMessage(message: "发布失败")
}
}
}
break
default:
break
}
}
// MARK: - UITableViewDelegate
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.section == 1 && indexPath.row == 1 && self.hiddenGoodsLine{
return 0
}
return 50
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if section == 0{
return 0
}
return 5
}
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
self.view.endEditing(true)
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = tableView.dequeueReusableHeaderFooterView(withIdentifier: kCNLiveCreateCouponTableViewHeaderFooterViewID)
view?.contentView.backgroundColor = HexColor("#F6F6F6")
return view
}
// MARK: - UITableViewDataSource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let listArr = self.dataSource![section]
return listArr.count
}
func numberOfSections(in tableView: UITableView) -> Int {
return self.dataSource!.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: kCNLiveCreateCouponCellIdentifier) as? CNLiveCreateCouponTableViewCell
else {
let cell = CNLiveCreateCouponTableViewCell()
return cell
}
if self.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.model = self.dataSource![indexPath.section][indexPath.row]
cell.textFieldChangeBlock = {section, number in
switch section {
case 1:
self.couponTitle = number
break
case 3:
self.totalNum = number
break
case 1001:
self.maxMoney = number
break
case 1002:
self.lessMoney = number
break
default:
break
}
}
cell.dateSelectBlock = { id,date in
let interval = date.timeIntervalSince1970
if id == 4{
self.startTime = String(format: "%f", interval)
}else{
self.endTime = String(format: "%f", interval+3600*24-1)
}
}
if indexPath.row == 1 && indexPath.section == 1{
if self.goodsId == nil && (self.couponModel?.goods_id == nil || self.couponModel?.goods_id == "0"){
cell.goodsLabel.text = ""
}
cell.isHidden = self.hiddenGoodsLine
}
cell.couponGoodsHiddenBlock = { isHidden in
self.hiddenGoodsLine = isHidden
self.goodsId = nil
tableView.reloadRows(at: [IndexPath(row: 1, section: 1)], with: .middle)
}
cell.couponGoodsBlock = { goodsId in
self.goodsId = goodsId
}
return cell
}
}