CNLiveShopClassilyManageViewController.swift 7.83 KB
//
//  CNLiveShopClassilyManageViewController.swift
//  metaCode
//
//  Created by zx on 2023/12/12.
//

import Foundation

///区头id
let kCNLiveShopClassifySectionHeaderViewID = "kCNLiveShopClassifySectionHeaderViewID"
///cell ID
let kCNLiveShopClassilyManageTableViewCellID = "kCNLiveShopClassilyManageTableViewCellID"

class CNLiveShopClassilyManageViewController:CNLiveBaseViewController, UITableViewDataSource, UITableViewDelegate{
    
    // MARK: - lazy
    lazy var tableView : UITableView = {
        let tableView = UITableView(frame: CGRect(x: 0, y: KNavigationHeight, width: KSreenWidth, height: KSreenHeight-KNavigationHeight-KTabBarHeight), style: .plain)
        tableView.backgroundColor = HexColor("#F6F6F6")
        tableView.dataSource = self
        tableView.delegate = self
        tableView.tableFooterView = UIView()
        tableView.separatorStyle = .none
        tableView.tableFooterView = UIView.init()
        tableView.tableHeaderView = self.headerView
        tableView.register(CNLiveShopClassifySectionHeaderView.self, forHeaderFooterViewReuseIdentifier: kCNLiveShopClassifySectionHeaderViewID)
        
        tableView.register(CNLiveShopClassilyManageTableViewCell.self, forCellReuseIdentifier:kCNLiveShopClassilyManageTableViewCellID )
        return tableView
    }()

    ///表头
    lazy var headerView :CNLiveShopClassilyManageHeaderView = {
        let headerView = CNLiveShopClassilyManageHeaderView(frame: CGRect(x: 0, y: 0, width: KSreenWidth, height: 114))
        return headerView
    }()
    
    lazy var bottomNavView :UIView = {
        let bottomNavView = UIView(frame: CGRect(x: 0, y: KSreenHeight-KTabBarHeight, width: KSreenWidth, height: KTabBarHeight))
        bottomNavView.backgroundColor = .white
        return bottomNavView
    }()

    
    ///分类列表详情数据
    var classifyListModel:CNLiveShopClassifyListModel?

    ///一级分类数据源,一级分类里面包含了二级分类
    var listArray:[CNLiveShopClassifyModel]?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.initUI()
        self.loadData()
    }
    // MARK: - 初始化UI
    func initUI(){
        self.view.addSubview(self.tableView)
        self.initTopNav()
        self.initBottomNav()
    }
    func initTopNav(){
        navigationBarHidden = false
        titleName = "店铺分类管理"
    }
    func initBottomNav(){
        self.view.addSubview(self.bottomNavView)
        
        let createBtn = UIButton(type: .custom)
        createBtn.frame = CGRect(x: 0, y: 0, width: KSreenWidth, height: KTabBar_UIHeight)
        createBtn.setTitle("创建店铺分类", for: .normal)
        createBtn.setImage(UIImage(named: "DS_WB_CJDP"), for: .normal)
        createBtn.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 8)
        createBtn.setTitleColor(HexColor("#FD862E"), for: .normal)
        createBtn.titleLabel?.font = Font_15
        createBtn.addTarget(self, action: #selector(createBtnClick), for: .touchUpInside)
        self.bottomNavView.addSubview(createBtn)

    }
    // MARK: - load data
    func loadData(){
        showLoading(message: "")
        CNLiveShopClassilyViewModel.getCateListWithShopId(shopId: DSUserInfoManager.shared.dsUserInfo.id!) { result, status in
            hiddenLoading()
            if status == .success{
                let model = result as! CNLiveShopClassifyListModel
                self.listArray = Array(model.list)
                self.headerView.classifyModel = model
                self.tableView.reloadData()
            }else{
                showMessage(message: "获取分类失败")
            }
        }
    }
    
    // MARK: - UITableViewDelegate,UITableViewDataSource
    func numberOfSections(in tableView: UITableView) -> Int {
        return self.listArray?.count ?? 0
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if self.listArray != nil{
            let model = self.listArray![section]
            if model.showChild{
                return model.child.count
            }
        }
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: kCNLiveShopClassilyManageTableViewCellID, for: indexPath) as? CNLiveShopClassilyManageTableViewCell
        else {
            let cell = CNLiveShopClassilyManageTableViewCell()
            return cell
        }
        let model = self.listArray![indexPath.section] 
        cell.classifyModel = model.child[indexPath.row]
        cell.editBtnBlock = { [weak self] classifyModel in
            self!.editBtnActionWithMModel(classifyModel: classifyModel, type: .twoLevelClassify)
        }
        return cell
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let sectionHeaderView = tableView.dequeueReusableHeaderFooterView(withIdentifier: kCNLiveShopClassifySectionHeaderViewID) as! CNLiveShopClassifySectionHeaderView
        sectionHeaderView.classifyModel = self.listArray![section]
        sectionHeaderView.section = section
        sectionHeaderView.showChildCalssifyBlock = { [weak self] section,classifyModel in
            self?.showChildCalssifyAction(section: section, classifyModel: classifyModel)
        }
        sectionHeaderView.addSubClassifyBtnBlock = { [weak self] classifyModel in
            self?.addSubClassifyBtnActionWithModel(classifyModel: classifyModel)
        }
        sectionHeaderView.editBtnBlock = { [weak self] classifyModel in
            self?.editBtnActionWithMModel(classifyModel: classifyModel, type: .oneLevelClassify)
        }
        return sectionHeaderView
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 50
    }
    // MARK: - response method
    @objc func createBtnClick(){
        let vc = CNLiveCreateShopClassifyViewController()
        vc.classifyType = .oneLevelClassify
        vc.successCompleteBlock = {[weak self] in
            self!.loadData()
        }
        navigationViewController().pushViewController(vc, animated: pushAnimated)
//        currentViewController()?.navigationController?.pushViewController(vc, animated: pushAnimated)
    }
    ///显示/隐藏子分类
    func showChildCalssifyAction(section:Int,classifyModel:CNLiveShopClassifyModel){
        if classifyModel.child.count == 0{
            showMessage(message: "当前分类没有子分类")
            return
        }
        classifyModel.showChild = !classifyModel.showChild
        self.tableView.reloadSections([section], with: .fade)
    }
    ///添加子分类
    func addSubClassifyBtnActionWithModel(classifyModel:CNLiveShopClassifyModel){
        let vc = CNLiveCreateShopClassifyViewController()
        vc.classifyType = .twoLevelClassify
        vc.superClassifyId = classifyModel.id!
        vc.successCompleteBlock = {[weak self] in
            self!.loadData()
        }
        navigationViewController().pushViewController(vc, animated: pushAnimated)
//        currentViewController()?.navigationController?.pushViewController(vc, animated: pushAnimated)
    }
    func editBtnActionWithMModel(classifyModel:CNLiveShopClassifyModel,type:CNLiveCreateType){
        let data = modelToDictionary(classifyModel) 
        ///重新拷贝一个对象
        var copyClassifyModel = newDictionaryToModel(data, CNLiveShopClassifyModel.self) as! CNLiveShopClassifyModel
        let vc = CNLiveCreateShopClassifyViewController(model: copyClassifyModel, isEdite: true)
        vc.classifyType = type
        vc.successCompleteBlock = {[weak self] in
            self!.loadData()
        }
        currentViewController()?.navigationController?.pushViewController(vc, animated: true)
    }
}