// // ChooseCouponViewController.swift // MTP2_iOS // // Created by 曹晓亮 on 2020/12/16. // Copyright © 2020 Muchinfo. All rights reserved. // import UIKit import JXSegmentedView import WHToast import DeviceKit /// 优惠券选择视图容器控制类 class ChooseCouponViewController: BaseViewController { // MARK: - 属性列表 /// 分段控制器 @IBOutlet weak var segmentedView: JXSegmentedView! { didSet { segmentedView.delegate = self segmentedView.dataSource = dataSource segmentedView.indicators = [self.indicator] } } /// segmentedDataSource let dataSource: JXSegmentedTitleDataSource = { var items: [String] = ["可用优惠券", "不可用优惠券"] $0.titles = items $0.titleNormalColor = UIColorFromHex(rgbValue: 0x333333) $0.titleNormalFont = .font_14 $0.titleSelectedColor = UIColorFromHex(rgbValue: 0x60a1e3) $0.titleSelectedFont = .font_14 $0.isTitleColorGradientEnabled = true return $0 } (JXSegmentedTitleDataSource()) /// 列表 @IBOutlet weak var tableView: UITableView! /// 高度约束 @IBOutlet weak var heightLayoutConstraint: NSLayoutConstraint! { didSet { heightLayoutConstraint.constant = 0.0 } } /// CellIdentifier let CellIdentifier = "Coupon_Cell" /// 优惠券数据信息 var coupons: [MoMyCouponHold] = [] { didSet { /// 刷新列表数据 tableView.reloadData() /// 设备类型 if Device.current == .iPhone6 || Device.current == .iPhone6s || Device.current == .iPhone7 || Device.current == .iPhone8 { /// 高度约束 heightLayoutConstraint.constant = coupons.count == 0 ? 200.0 : (coupons.count <= 3 ? CGFloat(coupons.count)*130.0 : 400.0) } else { /// 高度约束 heightLayoutConstraint.constant = coupons.count == 0 ? 200.0 : (coupons.count <= 4 ? CGFloat(coupons.count)*130.0 : 400.0) } } } /// 商品数据 override var takeInfo: Any? { didSet { /// 异常 guard let _ = takeInfo as? MoGoodsInfo else { return } /// 查询数据 reloadData(segmentedView.selectedIndex) } } /// 付款金额 var amount: Double = 0.0 /// 优惠券使用回调 var usedBlock: ((_ coupon: MoMyCouponHold?, _ rowNum: Int?) -> Void)? /// 优惠券 var coupon: MoMyCouponHold? // MARK: - 生命周期 override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. /// UI界面初始化 buildView() } // MARK: - 初始化 /// UI界面初始化 fileprivate func buildView() { /// loding...... self.addLoadingView() } // MARK: - 交互相关 /// onTapGestureRecognizer /// - Parameter sender: segmentIndex @IBAction fileprivate func onTapGestureRecognizer(_ sender: UITapGestureRecognizer) { /// 查询我的优惠券 self.dismiss(animated: true, completion: {}) } // MARK: - 接口请求 /// 刷新数据 /// - Parameter segmentIndex: segmentIndex fileprivate func reloadData(_ segmentIndex: Int) { /// 查询我的优惠券 segmentIndex == 0 ? requestQueryMyCoupons() : requestQueryMyUnusedCoupons() } /// 查询我的优惠券 fileprivate func requestQueryMyCoupons() { /// 异常 guard let orderManager = MTP2BusinessCore.shared.orderManager, let moGoodsInfo = takeInfo as? MoGoodsInfo else { return } /// startAnimating _anim?.startAnimating() /// 查询数据 orderManager.requestQueryMyCouponHolds(moGoodsInfo.goodsid, amount, moGoodsInfo.sellUserID, nil) { (isComplete, error, objs) in DispatchQueue.main.async { /// stopAnimating self._anim?.stopAnimating() /// 查询成功 if isComplete { self.coupons = objs?.filter({ (obj) -> Bool in return obj.holdstatus == .executed }) ?? [] } else { self.coupons = [] /// showError WHToast.showError(withMessage: "查询失败,请稍候再试!", duration: 1.5, finishHandler: {}) } } } } /// 查询我的不可用优惠券 fileprivate func requestQueryMyUnusedCoupons() { /// 异常 guard let orderManager = MTP2BusinessCore.shared.orderManager, let moGoodsInfo = takeInfo as? MoGoodsInfo else { return } /// startAnimating _anim?.startAnimating() /// 查询数据 orderManager.requestQueryMyUnusedCouponHolds(moGoodsInfo.goodsid, amount, moGoodsInfo.sellUserID) { (isComplete, error, objs) in DispatchQueue.main.async { /// stopAnimating self._anim?.stopAnimating() /// 查询成功 if isComplete { self.coupons = objs ?? [] } else { self.coupons = [] /// showError WHToast.showError(withMessage: "查询失败,请稍候再试!", duration: 1.5, finishHandler: {}) } } } } } // MARK: - JXSegmentedViewDelegate extension ChooseCouponViewController: JXSegmentedViewDelegate { func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) { /// 刷新数据 self.reloadData(index) } } // MARK: - UITableViewDelegate, UITableViewDataSource extension ChooseCouponViewController: UITableViewDelegate, UITableViewDataSource { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return coupons.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier, for: indexPath) as! CouponCell if self.segmentedView.selectedIndex == 0 { cell.chooseBlock = { (_ coupon: MoMyCouponHold?, _ rowRum: Int?) in DispatchQueue.main.async { self.dismiss(animated: true) { /// 执行回调 if let block = self.usedBlock, self.coupons[indexPath.row].unusedresult == "" { block(self.coupons[indexPath.row], rowRum) } } } } } cell.rowNum = indexPath.row cell.model = coupons[indexPath.row] return cell } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 130.0 } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { if self.segmentedView.selectedIndex == 0 { /// 可用 /// 获取选中的优惠券 let cell = tableView.cellForRow(at: indexPath) as! CouponCell cell.choosed = !cell.choosed /// 执行回调 DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+0.2) { if let block = cell.chooseBlock, let obj = cell.model { block(obj, !cell.choosed ? nil : cell.rowNum) } } /// 先取消所有的勾选状态 for (index, obj) in tableView.visibleCells.enumerated() { if index != indexPath.row { (obj as! CouponCell).choosed = false } } } else { /// 不可用 DispatchQueue.main.async { self.dismiss(animated: true, completion: nil) } } } }