CNLiveShopCouponManagerViewController.swift 5.9 KB
//
//  CNLiveShopCouponManagerViewController.swift
//  metaCode
//
//  Created by open on 2023/11/20.
//

import Foundation
import EmptyDataSet_Swift

class CNLiveShopCouponManagerViewController: CNLiveBaseViewController, UITableViewDelegate, UITableViewDataSource, EmptyDataSetSource, EmptyDataSetDelegate {
    
    let kShopBodyCouponManagerTableViewIdentifier = "kShopBodyCouponManagerTableViewIdentifier"
    lazy var tableView : UITableView = {
        let tableView = UITableView(frame: .zero, style: .plain)
        tableView.dataSource = self
        tableView.delegate = self
        tableView.emptyDataSetSource = self
        tableView.emptyDataSetDelegate = self
        tableView.backgroundColor = HexColor("#f1f2f6")
        tableView.tableFooterView = UIView.init()
        tableView.separatorStyle = .none
        tableView.register(ShopBodyCouponManagerTableView.self, forCellReuseIdentifier: kShopBodyCouponManagerTableViewIdentifier)
        return tableView
    }()
    
    lazy var delectButton: UIButton = {
        let delectButton = UIButton.init(type: .custom)
        delectButton.frame = CGRect(x: KSreenWidth - 70, y: (get_system_nav_height() - (get_system_statusBar_height() + 25))/2 + get_system_statusBar_height(), width: 60, height: 25)
        delectButton.setTitle("删除", for: .normal)
        delectButton.titleLabel?.font = semibold_adapt_font(pointSize: 15)
        delectButton.setTitleColor(HexColor("#F5A623"), for: .normal)
        delectButton.backgroundColor = .clear
        delectButton.layer.masksToBounds  = true
        delectButton.layer.cornerRadius = 5
        delectButton.addTarget(self, action: #selector(delectBtnClick), for: .touchUpInside)
        delectButton.tag = 1000
        return delectButton
    }()
    
    lazy var dataArray: NSMutableArray = {
        let dataArray = NSMutableArray.init()
        return dataArray
    }()
    

    var network_type: CNLiveMineResponseType!
    var completerBlock: CNLiveShopOrderExpressFirmCompleterBlock!
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationBarHidden = false
        titleName = "优惠券"
        topNavView.addSubview(delectButton)
        
        loadDataSourceAction()
        
        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.top.equalTo(KNavigationHeight)
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview().offset(get_system_tabbar_height() - get_system_tabbar_ui_height())
        }
    }
    
    func loadDataSourceAction() {
        CNLiveShopViewModel.requestCouponList { [weak self] result, respStatue in
            if respStatue == .success {
                self!.dataArray.addObjects(from: (result as! NSMutableArray) as! [ShopOrderCouponListModel])
                self!.tableView.reloadData()
            }else if (respStatue == .noNetwork) {
                self!.network_type = .noNetwork
                self!.tableView.reloadEmptyDataSet()
            } else {
                self!.network_type = .failed
                self!.tableView.reloadEmptyDataSet()
            }
        }
    }
    
    @objc func delectBtnClick() {
        
    }
    
}
extension CNLiveShopCouponManagerViewController {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArray.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: kShopBodyCouponManagerTableViewIdentifier) as? ShopBodyCouponManagerTableView else {
            return ShopBodyCouponManagerTableView()
        }
        cell.setupWithDataSource(listModel: dataArray[indexPath.row] as! ShopOrderCouponListModel)
        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return adapt_length(length: 70)
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    }
}

extension CNLiveShopCouponManagerViewController {
    
    func image(forEmptyDataSet scrollView: UIScrollView) -> UIImage? {
        if network_type == .failed  || network_type == .unkonw {
            return UIImage(named: "mine_list_source_unusual_failed")
        }
        if dataArray.count == 0 {
            return UIImage(named: "mine_list_source_unusual_empty")
        }
        if network_type == .noNetwork {
            return UIImage(named: "mine_list_source_unusual_noNet")
        }
        return UIImage(named: "mine_list_source_unusual_empty")
    }
    
    func title(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
        var defult_string = ""
        if network_type == .failed  || network_type == .unkonw {
            defult_string = "接口请求错误,刷新试试~"
        }
        if network_type == .noNetwork {
            defult_string = "网络开小差了,刷新试试~"
        }
        if dataArray.count == 0 {
            defult_string = "暂时没有数据~"
        }
        return NSAttributedString(string: defult_string, attributes: [.font: Font_17,.foregroundColor: HexColor("#666666")!])
    }
    
    func buttonTitle(forEmptyDataSet scrollView: UIScrollView, for state: UIControl.State) -> NSAttributedString? {
        if network_type == .failed || network_type == .noNetwork {
            let attributes = [NSAttributedString.Key.font:Font_16_Fit,
                              NSAttributedString.Key.foregroundColor: HexColor("#609BFF")]
            return NSAttributedString(string: "刷一刷", attributes: attributes as [NSAttributedString.Key : Any])
        }
        return nil
    }
    
    func emptyDataSet(_ scrollView: UIScrollView, didTapView view: UIView) {
        if network_type == .failed || network_type == .noNetwork  {
            loadDataSourceAction()
        }
    }
}