//
|
// 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
|
}
|
}
|
}
|