宽窄优行-由【嘉易行】项目成品而来
无故事王国
2023-05-25 dc1998fc1ac124f6b9a0e434ccf91103dd936409
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
//
//  CouponAtPaymentVC.swift
//  OKProject
//
//  Created by 无故事王国 on 2022/2/17.
//  Copyright © 2022 yangwang. All rights reserved.
//
 
import UIKit
import RxCocoa
 
class CouponAtPaymentVC: YYViewController {
 
    @IBOutlet weak var unUseCouponView: UIView!
    @IBOutlet weak var unuseBtn: UIButton!
    @IBOutlet weak var tableView: UITableView!
 
    var orderType:OrderType = .taxi
    var orderId = 0
    var dataDict = Dictionary<String,[PaymentCouponModel]>()
    var couponItems = [PaymentCouponModel]()
    var maxDiscountM:PaymentCouponModel?
    private var keys = [String]()
    private var selectIndex:IndexPath?
 
    private var chooseDicountClouse:((PaymentCouponModel?)->Void)?
 
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "我的出行卡"
        view.backgroundColor = UIColor(hexString: "#F3F4F5")
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = UIColor(hexString: "#F3F4F5")
        tableView.separatorStyle = .none
        tableView.register(UINib(nibName: "TripPreferCardTCell", bundle: nil), forCellReuseIdentifier: "_TripPreferCardTCell")
        tableView.register(UINib(nibName: "MerchantCouponTCell", bundle: nil), forCellReuseIdentifier: "_MerchantCouponTCell")
 
        unuseBtn.isSelected = maxDiscountM == nil
 
        if couponItems.count > 0{
            filterData(couponItems)
        }else{
            APIManager.shared.provider.rx.request(.queryCouponList(orderType: orderType, orderId: orderId, pageNum: 1)).map(YYModel<[PaymentCouponModel]>.self).validate().subscribe(onSuccess: {data in
                self.filterData(data.data ?? [])
            }) { error in
 
            }.disposed(by: disposeBag)
        }
    }
 
    private func filterData(_ data:[PaymentCouponModel]){
        for item in data{
            if item.dataType == .coupon{
                if self.dataDict["b_coupon"] == nil{
                    self.keys.append("b_coupon")
                    self.dataDict["b_coupon"] = Array<PaymentCouponModel>()
                }
                self.dataDict["b_coupon"]?.append(item)
            }
 
            if item.dataType == .card{
                if self.dataDict["a_card"] == nil{
                    self.keys.append("a_card")
                    self.dataDict["a_card"] = Array<PaymentCouponModel>()
                }
                self.dataDict["a_card"]?.append(item)
            }
        }
        self.keys.sort()
        self.tableView.reloadData()
    }
 
    func chooseDiscount(_ clouse:@escaping (PaymentCouponModel?)->Void){
        self.chooseDicountClouse = clouse
    }
 
    @IBAction func unuseAction(_ sender: UIButton) {
        sender.isSelected = !sender.isSelected
        if sender.isSelected{
            selectIndex = nil
            tableView.reloadData()
            chooseDicountClouse?(nil)
            yy_pop()
        }
    }
}
 
extension CouponAtPaymentVC:UITableViewDelegate{
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        selectIndex = indexPath
        tableView.reloadData()
        let key = keys[indexPath.section]
        let m = dataDict[key]?[indexPath.row]
        chooseDicountClouse?(m)
        yy_pop()
    }
}
 
extension CouponAtPaymentVC:UITableViewDataSource{
 
    func numberOfSections(in tableView: UITableView) -> Int {
        return keys.count
    }
 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let key = keys[section]
        return dataDict[key]?.count ?? 0
    }
 
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 45
    }
 
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headView = UIView()
        headView.frame = CGRect(x: 0, y: 0, width: ScreenWidth, height: 45)
        headView.backgroundColor = UIColor(hexString: "#F3F4F5")
        let label = UILabel()
        label.text = section == 0 ? "优惠卡":"优惠券"
        label.font = UIFont.systemFont(ofSize: 13, weight: .medium)
        label.textColor = UIColor(hexString: "#666666")
        headView.addSubview(label)
        label.snp.makeConstraints { make in
            make.left.equalTo(14)
            make.centerY.equalToSuperview()
        }
        return headView
    }
 
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
 
        let key = keys[indexPath.section]
        let m = dataDict[key]![indexPath.row]
        if key == "a_card"{
            let cell = tableView.dequeueReusableCell(withIdentifier: "_TripPreferCardTCell") as! TripPreferCardTCell
            cell.isSelected = indexPath == selectIndex
            cell.isSelected = m.id == maxDiscountM?.id
            cell.paymentCouponModel = m
            cell.backgroundColor = .clear
            return cell
        }else{
            let cell = tableView.dequeueReusableCell(withIdentifier: "_MerchantCouponTCell") as! MerchantCouponTCell
            cell.isSelected = indexPath == selectIndex
            cell.isSelected = m.id == maxDiscountM?.id
            cell.paymentCouponModel = m
            cell.backgroundColor = .clear
            return cell
        }
    }
}