//
//  SetCompanyDepartmentViewController+initView.swift
//  MobilePortal
//
//  Created by xiejiangbo on 2019/4/1.
//  Copyright © 2019 yin chen. All rights reserved.
//

import UIKit

extension SetCompanyDepartmentViewController {
    func addInitTableView() {
        self.dataSource = NSMutableArray()
        self.delegate = SetCompanyDepartmentViewControllerDelegate()
        self.delegate.dataSource = self.dataSource
        self.view.addSubview(myTableView)
        myTableView.snp.makeConstraints { (make) in
            make.width.height.equalToSuperview()
            make.center.equalToSuperview()
        }
        self.myTableView.delegate = self.delegate
        self.myTableView.dataSource = self.delegate
    }
    func getData(){
        self.dataSource = NSMutableArray()
        let infoArray:NSMutableArray = self.getAllSelecCompanyDepartment()
        for index in 0..<infoArray.count {
            let detaModel:SelecCompanyDepartmentInfo = infoArray[index] as! SelecCompanyDepartmentInfo
            let cellModel = SetCompanyDepartmentTableViewCellModel()
            cellModel.className = "SetCompanyDepartmentTableViewCell"
            cellModel.tages = detaModel.tags+"（"+judgmentStrNil(str:detaModel.sysUserId)+"/"+judgmentStrNil(str:detaModel.partyOpcode)+"）"
            cellModel.companyName = detaModel.selecCompanyName
            cellModel.departmentName = detaModel.selecDepartmentName
            cellModel.partyOpcode = detaModel.partyOpcode
            cellModel.selecCompanyUserid = detaModel.sysUserId
            cellModel.block = {[weak self]in
                self?.gotoCompanyListVC(detaModel: detaModel)
            }
            
            self.dataSource.add(cellModel)
        }
        self.delegate.dataSource = self.dataSource
        self.myTableView.reloadData()
    }
    func gotoCompanyListVC(detaModel:SelecCompanyDepartmentInfo)  {
        AccountInfo.sharedInstance().selecCompanyName = detaModel.selecCompanyName
        AccountInfo.sharedInstance().sysUserId = detaModel.sysUserId
        AccountInfo.sharedInstance().selecDepartmentName = detaModel.selecDepartmentName
        let vc:SelectCompanyViewController=SelectCompanyViewController.createViewController(createArgs: nil) as! SelectCompanyViewController
        vc.selecClassModel = self.getselecClassModel(detaModel: detaModel)
        vc.block = {[weak self] (value:Any) in
            self?.getData()
        }
        self.pushViewController(viewController: vc, animated: true)
    }
    func getselecClassModel(detaModel:SelecCompanyDepartmentInfo) ->AdvisoryHttpListModel{
        let selecClassModel = AdvisoryHttpListModel()
        selecClassModel.tags = detaModel.tags
        selecClassModel.id = detaModel.docId
        return selecClassModel
    }

}
class SetCompanyDepartmentViewControllerDelegate: BaseOneTableViewDelegate {
    var cellNum:Double!
    override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0
    }
    
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        
        if (section==0){
            return 0
        }
        return 0
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let model:SetCompanyDepartmentTableViewCellModel = self.dataSource[indexPath.row] as! SetCompanyDepartmentTableViewCellModel
        let departmentNameh = getTextHeigh(textStr: model.departmentName!, font: UIFont.systemFont(ofSize: 17*WHIDTH_RATIO()), width: (SCREEN_WHIDTH()-25)/2)
        let companyNameh = getTextHeigh(textStr: model.companyName!, font: UIFont.systemFont(ofSize: 17*WHIDTH_RATIO()), width: (SCREEN_WHIDTH()-25)/2)
        let h = getTextHeigh(textStr: "12345", font: UIFont.systemFont(ofSize: 17*WHIDTH_RATIO()), width: (SCREEN_WHIDTH()-25)/2)
        if departmentNameh>h || companyNameh>h {
            return 68*HEIGHT_RATIO()
        }
        return 44
    }
}
