宽窄优行-由【嘉易行】项目成品而来
younger_times
2023-07-05 0d8f5fc8a516bfd07e425909e4a4432600572ee7
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
//
//  MineCouponsListVC.swift
//  OKProject
//
//  Created by alvin_y on 2020/6/15.
//  Copyright © 2020 yangwang. All rights reserved.
//
 
import UIKit
import HandyJSON
/// 优惠券状态
enum CouponsState:Int,HandyJSONEnum{
    /// 未使用
    case unused = 1
    /// 已使用
    case used = 2
    /// 已过期
    case exceed = 3
    
    func parame() -> Int {
        switch self {
        case .unused:
            return 1
        case .used:
            return 2
        case .exceed:
            return 3
        }
    }
}
 
 
/// 优惠券列表
class MineCouponsListVC: YYTableViewController {
 
    let viewModel = MineCouponsListViewModel()
//    private var avaibleBtn:UIButton!
//    private var unavaibleBtn:UIButton!
//    private var presentBtn:UIButton!
    
    init(state: CouponsState) {
        super.init(nibName: nil, bundle: nil)
        viewModel.state.accept(state.parame())
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
 
        // Do any additional setup after loading the view.
    }
 
    //MARK: - UI
    override func setupViews() {
        super.setupViews()
        view.backgroundColor = UIColor(hexString: "#F3F4F5")
        tableView.register(cellName: "TravelCouponsCell", identifier: "item")
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = .none
        viewModel.configure(tableView: tableView)
        tableView.mj_header?.beginRefreshing()
 
//        avaibleBtn = UIButton(type: .custom)
//        avaibleBtn.setTitle("可使用", for: .normal)
//        avaibleBtn.isSelected = true
//        avaibleBtn.titleLabel?.font = UIFont.systemFont(ofSize: 12, weight: .medium)
//        avaibleBtn.cornerRadius = 15.5
//        avaibleBtn.maskToBounds = true
//        avaibleBtn.tag = 10
//        avaibleBtn.addTarget(self, action: #selector(sequeeceAction(_:)), for: .touchUpInside)
//        avaibleBtn.setTitleColor(UIColor.white, for: .selected)
//        avaibleBtn.setTitleColor(UIColor(hexString: "#8C9097"), for: .normal)
//        avaibleBtn.setBackgroundImage(UIImage(color: UIColor(hexString: "#00BF30")!), for: .selected)
//        avaibleBtn.setBackgroundImage(UIImage(color: UIColor.white), for: .normal)
//
//        unavaibleBtn = UIButton(type: .custom)
//        unavaibleBtn.setTitle("已失效", for: .normal)
//        unavaibleBtn.isSelected = false
//        unavaibleBtn.titleLabel?.font = UIFont.systemFont(ofSize: 12, weight: .medium)
//        unavaibleBtn.cornerRadius = 15.5
//        unavaibleBtn.maskToBounds = true
//        unavaibleBtn.tag = 11
//        unavaibleBtn.addTarget(self, action: #selector(sequeeceAction(_:)), for: .touchUpInside)
//        unavaibleBtn.setTitleColor(UIColor.white, for: .selected)
//        unavaibleBtn.setTitleColor(UIColor(hexString: "#8C9097"), for: .normal)
//        unavaibleBtn.setBackgroundImage(UIImage(color: UIColor(hexString: "#00BF30")!), for: .selected)
//        unavaibleBtn.setBackgroundImage(UIImage(color: UIColor.white), for: .normal)
//
//
//        presentBtn = UIButton(type: .custom)
//        presentBtn.setTitle("赠送", for: .normal)
//        presentBtn.setTitleColor(UIColor(hexString: "#FB692A"), for: .normal)
//        presentBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)
//        presentBtn.setImage(UIImage(named: "icon_arrow_left-1"), for: .normal)
//        presentBtn.adjustImage(position: .right, spacing: 9)
//        presentBtn.addTarget(self, action: #selector(give), for: .touchUpInside)
//
//        view.addSubview(avaibleBtn)
//        view.addSubview(unavaibleBtn)
//        view.addSubview(presentBtn)
    }
    
    
    //MARK: - Layouts
    override func defineLayouts() {
        super.defineLayouts()
        tableView.snp.remakeConstraints { (make) in
            make.top.equalTo(49)
            make.left.right.bottom.equalToSuperview()
        }
 
//        avaibleBtn.snp.makeConstraints { make in
//            make.top.equalTo(13)
//            make.left.equalTo(12)
//            make.width.equalTo(62)
//            make.height.equalTo(31)
//        }
//
//        unavaibleBtn.snp.makeConstraints { make in
//            make.top.equalTo(13)
//            make.left.equalTo(avaibleBtn.snp.right).offset(29)
//            make.width.equalTo(62)
//            make.height.equalTo(31)
//        }
//
//        presentBtn.snp.makeConstraints { make in
//            make.right.equalToSuperview().offset(-14)
//            make.centerY.equalTo(avaibleBtn)
//            make.height.equalTo(30)
//        }
    }
 
//    @objc func sequeeceAction(_ btn:UIButton){
//        var state:CouponsState = .unused
//        avaibleBtn.isSelected = btn.tag == 10
//        unavaibleBtn.isSelected = btn.tag == 11
//        state = btn.tag == 10 ? CouponsState.unused : CouponsState.used
//        viewModel.state.accept(state.parame())
//        tableView.mj_header?.beginRefreshing()
//    }
 
//    /// 赠送
//    @objc func give()  {
//        let vc = MineGiveCouponsListVC()
//        self.yy_push(vc: vc)
//    }
    
    //MARK: - Rx
    override func bindRx() {
        super.bindRx()
        viewModel.delMyCouponSubject
            .subscribe(onNext: { (state) in
                switch state{
                case .loading:
                    self.show()
                    break
                case .success(_):
                    self.hide()
                    var data = self.viewModel.dataSource.value
                    data.remove(at: self.viewModel.row.value)
                    self.viewModel.dataSource.accept(data)
                    self.tableView.reloadData()
                    break
                case .error(let error):
                    self.hide()
                    alert(text: error.localizedDescription)
                    break
                }
            }).disposed(by: disposeBag)
    }
}
 
 
// MARK: - UITableViewDelegate
extension MineCouponsListVC:UITableViewDelegate{
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
    
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
         return true
     }
     
     // 设置确认删除按钮的文字
     func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
         return "删除"
     }
     
    func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
         return .delete
     }
     
     
     
     func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
         self.viewModel.row.accept(indexPath.row)
         self.viewModel.id.accept(viewModel.dataSource.value[indexPath.row].id)
         self.viewModel.delMyCoupon()
     }
}
 
// MARK: - UITableViewDelegate
extension MineCouponsListVC:UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel.dataSource.value.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "item", for: indexPath) as! TravelCouponsCell
        cell.myConfigure(model: viewModel.dataSource.value[indexPath.row])
        cell.button_select.isHidden = true
        if viewModel.state.value == 2{
            cell.image_front_bg.image = UIImage.init(named: "bg_gray_front")
            cell.image_expired.isHidden = false
        }
        cell.selectionStyle = .none
        return cell
    }
    
    
}