CNLiveDownShopDetailController.swift 11.9 KB
//
//  CNLiveDownShopDetailController.swift
//  metaCode
//
//  Created by zx on 2023/10/31.
//

import Foundation

class CNLiveDownShopDetailController:CNLiveBaseViewController, UICollectionViewDelegate, UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
    
    let kCNLiveShopDetailDefaultCellID = "kCNLiveShopDetailDefaultCellID"
    let kCNLiveShopDetailHeaderViewID = "kCNLiveShopDetailHeaderViewID"
    
    let kCNLiveShopDetailBannerCellID = "kCNLiveShopDetailBannerCellID"
    let kCNLiveShopDetailBtnsCellID = "kCNLiveShopDetailBtnsCellID"
    let kCNLiveShopDetailOrderAssetCellID = "kCNLiveShopDetailOrderAssetCellID"
    
    
    lazy var shopDetailModel: CNLiveShopDetailModel = {
        let shopDetailModel = CNLiveShopDetailModel()
        return shopDetailModel
    }()
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        let upSlideView = self.view.superview as! CNUpSlideView
        let scrollView = upSlideView.superview as! UIScrollView
        scrollView.setContentOffset(CGPoint(x: 0, y: scrollView.height), animated: true)
        currentViewController()?.tabBarController?.tabBar.isHidden = false
        
        let metaVC = currentViewController() as! CNMetaCodeHomePageController
        let index = metaVC.pageFlowView.currentPageIndex
        let customIndexView = metaVC.pageFlowView.viewWithTag(100+index) as! CNCustomIndexView
        customIndexView.superScrollView?.isScrollEnabled = true
    }
//    lazy var closeBtn: UIButton = {
//        let closeBtn = UIButton(type: .custom)
//        closeBtn.frame = CGRect(x: KSreenWidth-50, y: KStatusBarHeight, width: 50, height: KNavigationHeight-KStatusBarHeight)
//        closeBtn.setImage(UIImage(named: "mine_header_icon_close"), for: .normal)
//        closeBtn.addTarget(self, action: #selector(closeBtnAction), for: .touchUpInside)
//        closeBtn.isUserInteractionEnabled = false
//        return closeBtn
//    }()
    
//    lazy var navView: UIView = {
//        let closeBtn = UIView(frame: CGRect(x: 0, y: KStatusBarHeight, width: KSreenWidth-50, height: KNavigationHeight-KStatusBarHeight))
//        closeBtn.backgroundColor = .yellow
//        return closeBtn
//    }()
    lazy var closeBtn: UIImageView = {
        let closeBtn = UIImageView(frame: CGRect(x: KSreenWidth-35, y: KStatusBarHeight+(KNavigationHeight-KStatusBarHeight-20)*0.5, width: 20, height: 20))
        closeBtn.image = UIImage(named: "mine_header_icon_close")
//        closeBtn.isUserInteractionEnabled = true
//        let tap = UITapGestureRecognizer.init(target: self, action: #selector(closeBtnAction))
//        closeBtn.addGestureRecognizer(tap)
        return closeBtn
        }()
    
    lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout.init()
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.backgroundColor = .white
//        collectionView.register(CNLiveShopConsoleHeaderView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: kCNLiveShopConsoleCollectionReusableHeaderViewID)
        collectionView.register(CNLliveShopDetailBannerCell.self, forCellWithReuseIdentifier: kCNLiveShopDetailBannerCellID)
        collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: kCNLiveShopDetailDefaultCellID)
        collectionView.register(CNLiveShopDetailBtnsCell.self, forCellWithReuseIdentifier: kCNLiveShopDetailBtnsCellID)
        collectionView.register(CNLiveShopDetailOrderAssetCell.self, forCellWithReuseIdentifier: kCNLiveShopDetailOrderAssetCellID)
        
        return collectionView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.getShopDetailApi()
        self.view.addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(KNavigationHeight)
            make.left.bottom.right.equalToSuperview()
        }
        self.view.addSubview(closeBtn)
//        self.view.addSubview(navView)
        
    }
        
    // MARK: BtnAction
    @objc func closeBtnAction(){
        let upSlideView = self.view.superview as! CNUpSlideView
        let scrollView = upSlideView.superview as! UIScrollView
        scrollView.setContentOffset(CGPoint(x: 0, y: scrollView.height), animated: true)
        currentViewController()?.tabBarController?.tabBar.isHidden = false
        
        let metaVC = currentViewController() as! CNMetaCodeHomePageController
        let index = metaVC.pageFlowView.currentPageIndex
        let customIndexView = metaVC.pageFlowView.viewWithTag(100+index) as! CNCustomIndexView
        customIndexView.superScrollView?.isScrollEnabled = true
//        customIndexView.baseScrollView.setContentOffset(CGPoint(x: 0, y: customIndexView.baseScrollView.height), animated: true)
//        currentViewController()?.tabBarController?.tabBar.isHidden = false
    }
    
    // MARK: - UICollectionViewDelegate
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        if self.shopDetailModel.is_seller != nil{
            if self.shopDetailModel.is_seller == true{
                return 5
            }else{//非商家,不处理
                return 3
            }
        }else{
            return 1
        }
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if self.shopDetailModel.is_seller != nil {
            if (self.shopDetailModel.is_seller == true) {
                if (section == 4) {
                    let count = self.shopDetailModel.dynamic.count
                    return count == 0 ? 1 : count
                }else {
                    return 1
                }
            }else {
                return 1
            }
        }else{
            return 1
        }
    }
//    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
//        if (kind == UICollectionView.elementKindSectionHeader){
//            let headerView:CNLiveShopConsoleHeaderView = collectionView .dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: kCNLiveShopDetailHeaderViewID, for: indexPath) as! CNLiveShopConsoleHeaderView
//
//            return headerView
//        }else{
//             return
//    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        switch indexPath.section {
        case 0://banner轮播图
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kCNLiveShopDetailBannerCellID, for: indexPath) as! CNLliveShopDetailBannerCell
            
            cell.shopDetailModel = self.shopDetailModel
            return cell
        case 1://btn
            if (self.shopDetailModel.is_seller != nil && self.shopDetailModel.is_seller == true) {
                let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kCNLiveShopDetailBtnsCellID, for: indexPath) as! CNLiveShopDetailBtnsCell
                cell.shopDetailModel = self.shopDetailModel
                return cell
            }else{ 
                let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kCNLiveShopDetailDefaultCellID, for: indexPath)
                return cell
            }
        case 2://我的订单
            if (self.shopDetailModel.is_seller != nil && self.shopDetailModel.is_seller == true) {
                let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kCNLiveShopDetailOrderAssetCellID, for: indexPath) as! CNLiveShopDetailOrderAssetCell
                cell.displayType = .CNLiveShopDetailDisplayTypeOrder
                cell.shopDetailModel = self.shopDetailModel
                return cell
            }else{
                let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kCNLiveShopDetailDefaultCellID, for: indexPath)
                return cell
            }
        case 3://我的资产
            if (self.shopDetailModel.is_seller != nil && self.shopDetailModel.is_seller == true) {
                let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kCNLiveShopDetailOrderAssetCellID, for: indexPath) as! CNLiveShopDetailOrderAssetCell
                cell.displayType = .CNLiveShopDetailDisplayTypeAsset
                cell.shopDetailModel = self.shopDetailModel
                return cell
            }else{
                let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kCNLiveShopDetailDefaultCellID, for: indexPath)
                return cell
            }
//        case 4:
        default:
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kCNLiveShopDetailDefaultCellID, for: indexPath)
            return cell
        }
        
    }
    
    // MARK: - UICollectionViewDelegateFlowLayout
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if (self.shopDetailModel.is_seller != nil && self.shopDetailModel.is_seller == true) {
            switch (indexPath.section) {
                case 0://banner
                    return CGSize(width: KSreenWidth, height: CNLiveAdaptTool.adaptLength(length: 210))
                case 1://btn
                    return CGSize(width: KSreenWidth, height: 10+45+5+17+15)
                case 2://我的订单
                    return CGSize(width: KSreenWidth, height: 10+25+15+CNLiveAdaptTool.adaptLength(length: 128-55)+15)
                case 3://我的资产
                    return CGSize(width: KSreenWidth, height: 10+25+15+CNLiveAdaptTool.adaptLength(length: 128-55)+15)
                case 4://
                    return CGSize(width: KSreenWidth, height: 50)
                default:
                    return CGSizeZero
            }
        }else {
            return CGSizeZero
//            switch (indexPath.section) {
//                case 0:
//                {
//                    if (self.mainHPModel.is_show_btn) {
//                        return CGSizeMake(self.view.width, DS_adapt_length(262));
//                    }else {
//                        return CGSizeMake(self.view.width, DS_adapt_length(262) - DS_adapt_length(50));
//                    }
//                }
//                    break;
//                case 1:
//                {
//                    if (self.mainHPModel.is_show_btn) {
//                        return CGSizeMake(self.view.width, DS_adapt_length(145));
//                    }else {
//                        return CGSizeMake(self.view.width, 0.000001);
//                    }
//                }
//                    break;
//                case 2:
//                {
//                    if (self.mainHPModel.is_show_user) {
//                        return CGSizeMake(self.view.width, DS_adapt_length(191));
//                    }else {
//                        return CGSizeMake(self.view.width, 0.000001);
//                    }
//                }
//                    break;
//                default:
//                {
//                    return CGSizeZero;
//                }
//                    break;
//            }
        }
    }
    
    //MARK: - 网络请求获取店铺详情
    func getShopDetailApi()
    {
        showLoading(message: "")
        CNLiveShopDetailViewModel.requestShopDetailInfo(storeId: "") { result, status in
            hiddenLoading()
            if status == .success{
                let model = result as! CNLiveShopDetailModel
                self.shopDetailModel = model
                self.collectionView.reloadData()
            }else if (status == .failed){
//                self.NetApiStatus = .apiError
            }else if (status == .noNetwork){
                
//                self.NetApiStatus = .notNetwork
            }
        }
    }
}