CNLiveShopConsoleController.swift 6.53 KB
//
//  CNLiveShopConsoleController.swift
//  metaCode
//
//  Created by zx on 2023/10/27.
//

import Foundation

class CNLiveShopConsoleController: CNLiveBaseViewController, UICollectionViewDelegate, UICollectionViewDataSource {
    
    let kCNLiveShopConsoleCellKey = "kCNLiveShopConsoleCellKey"
    
    let kCNLiveShopConsoleCollectionReusableHeaderViewID = "kCNLiveShopConsoleCollectionReusableHeaderViewID"
    let kCNLiveShopConsoleCollectionReusableFooterViewID = "kCNLiveShopConsoleCollectionReusableFooterViewID"
    
    lazy var collectionView: UICollectionView = {
        let gap = CGFloat(15)
        let layout = UICollectionViewFlowLayout.init()
        layout.itemSize = CGSize(width: (KSreenWidth - gap*2-10)/2, height: 72)
        layout.sectionInset = .init(top: gap, left: gap, bottom: gap, right: gap)
        layout.headerReferenceSize = CGSize(width: KSreenWidth, height: 20)
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.backgroundColor = .clear
        collectionView.register(CNLiveShopConsoleHeaderView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: kCNLiveShopConsoleCollectionReusableHeaderViewID)
        collectionView.register(CNLiveShopConsoleCell.self, forCellWithReuseIdentifier: kCNLiveShopConsoleCellKey)
        return collectionView
    }()
    
    lazy var dataModel: CNLiveShopConsoleModel = {
        let dataModel = CNLiveShopConsoleModel()
        return dataModel
    }()
        
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .clear
        self.getPageApi()
        
        view.addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(KNavigationHeight)
            make.left.bottom.right.equalToSuperview()
        }
    }
    
    // MARK: - UICollectionViewDelegate
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return self.dataModel.list.count
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        let listMdl = self.dataModel.list[section]
        return listMdl.list.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kCNLiveShopConsoleCellKey, for: indexPath) as! CNLiveShopConsoleCell
        let listMdl = self.dataModel.list[indexPath.section]
        let model = listMdl.list[indexPath.item]
        cell.shopListContentModel = model
        return cell
    }
//    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
//        if (section == 0) {
//            return CGSizeMake(self.view.width, 33);
//        }else {
//            return CGSizeMake(self.view.width, 33);
//        }
//    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        if (kind == UICollectionView.elementKindSectionHeader){
            let sectionMdl = self.dataModel.list[indexPath.section]
            let headerView:CNLiveShopConsoleHeaderView = collectionView .dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: kCNLiveShopConsoleCollectionReusableHeaderViewID, for: indexPath) as! CNLiveShopConsoleHeaderView
            headerView.height = 24
            headerView.title = sectionMdl.title
            return headerView
        }else {
            let footer = collectionView .dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: kCNLiveShopConsoleCollectionReusableFooterViewID, for: indexPath)
            return footer
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let listMdl = self.dataModel.list[indexPath.section]
        let itemModel = listMdl.list[indexPath.item]
        if itemModel.type == CNLiveShopConsoleType.XZSP.rawValue{
            //商城新增商品
            
        }else if itemModel.type == CNLiveShopConsoleType.SPGL.rawValue{
            //商城商品管理
            
        }else if itemModel.type == CNLiveShopConsoleType.XZYHQ.rawValue{
            //新增优惠券
            
        }else if itemModel.type == CNLiveShopConsoleType.GLYHQ.rawValue{
            //优惠券管理
            
        }else if itemModel.type == CNLiveShopConsoleType.CreateShopClassify.rawValue{
            //创建店铺分类
            
        }else if itemModel.type == CNLiveShopConsoleType.ShopClassifyManager.rawValue{
            //店铺分类管理
            
        }else if itemModel.type == CNLiveShopConsoleType.FBDT.rawValue{
            //发布动态
            
        }else if itemModel.type == CNLiveShopConsoleType.DTGL.rawValue{
            //动态管理
            
        }else if itemModel.type == CNLiveShopConsoleType.DDGL.rawValue{
            //订单管理
            
        }
//        else if itemModel.type == CNLiveShopConsoleType.SH.rawValue{
//            //退款/售后
//            
//        }
        else if itemModel.type == CNLiveShopConsoleType.JYJL.rawValue{
            //交易记录
            
        }else if itemModel.type == CNLiveShopConsoleType.OnManagerment.rawValue{
            //直播管理
            
        }
    }
    
    //MARK: - 网络请求获取详情页
    func getPageApi()
    {
        showLoading(message: "")
        CNLiveShopConsoleViewModel.requestUserInfo() { result, status in
            hiddenLoading()
            
            if status == .success{
                let shopUserModel = result as! CNLiveShopUserInfoModel
                if shopUserModel.type != "3"{
                    //type 1商家或者8达人
                    let shopConsoleModel = CNLiveShopConsoleModel()
                    shopConsoleModel.initDataSource()
                    self.dataModel = shopConsoleModel
                    self.collectionView.reloadData()
                }
            }else if (status == .failed){
//                self.NetApiStatus = .apiError
            }else if (status == .noNetwork){
                
//                self.NetApiStatus = .notNetwork
            }
        }
//
    }
}