//
//  MineViewController+MyTableView.swift
//  MobilePortal
//
//  Created by xiejiangbo on 2019/3/15.
//  Copyright © 2019 yin chen. All rights reserved.
//

import UIKit

extension MineViewController {
    func addInitTableView() {
        self.dataSource = NSMutableArray()
        self.delegate = MineViewControllerDelegate()
        self.delegate.slidingBlock = {[weak self] in
            self?.scrollViewDidScroll(scrollView: (self?.myTableView)!)
        }
        self.delegate.dataSource = self.dataSource
        self.delegate.cellNum = self.cellNum
        
        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
//        addHeadImageModel(httpInfoModel: inforHttpModel)

    }
    func addHeadImageModel(httpInfoModel:MyInformationHttpModel) {
        headCellModel = MineHeadTableViewCellModel()
        headCellModel.className = "MineHeadTableViewCell"
        headCellModel.nameStr = judgmentStrNil(str: httpInfoModel.cnname)
        AccountInfo.sharedInstance().userName = judgmentStrNil(str: httpInfoModel.cnname)
        headCellModel.departementStr =  judgmentStrNil(str: httpInfoModel.deptnames)
        headCellModel.companyStr =  judgmentStrNil(str: httpInfoModel.companyname)
        AccountInfo.sharedInstance().infoCompanyName = judgmentStrNil(str: httpInfoModel.companyname)
        AccountInfo.sharedInstance().externalUser = judgmentStrNil(str: httpInfoModel.type)
        headCellModel.block = {[weak self] in
            self?.gotoMineInfoVC()
        }
        
        if judgmentStrNil(str: httpInfoModel.password) == md5(strs: "123456") {
            showOneAlert(title: "温馨提示", message: "您当前密码为初始密码，需要修改密码", titleSecondBtn: "确定", blockOtherBtn: {[weak self] in
                self?.mineGotoChangePassWordViewController(changePassWordType: .OrdinaryChangePassWordType)
            })
        }
        if judgmentStrNil(str: httpInfoModel.status) == "0" {
            self.logoutUserInfo(str: "您的账户已被禁用，请联系管理员！")
        }
        if judgmentStrNil(str: httpInfoModel.is_deleted) == "Y" {
            self.logoutUserInfo(str: "您的账户已被删除，请联系管理员！")
        }
        headArray .add(headCellModel)
//        self.dataSource .add(headArray)
    }
    func scrollViewDidScroll(scrollView: UIScrollView) {
        offsetY = scrollView.contentOffset.y
        if offsetY > 220-64*2 {
            let alpha = (offsetY-(220-64*2))/64
            self.wr_setNavBarBackgroundAlpha(alpha)
        } else {
            self.wr_setNavBarBackgroundAlpha(0)
            self.wr_setNavBarTintColor(UIColor.white)
            self.wr_setNavBarTitleColor(UIColor.white)
        }
    }
    //跳转 修改密码
    func mineGotoChangePassWordViewController(changePassWordType:ChangePassWordType){
        let vc:ChangePassWordViewController=ChangePassWordViewController()
        vc.changePassWordType = changePassWordType
        vc.signOutBlock = {[weak self] in
            self?.loginAction()
        }
        vc.hidesBottomBarWhenPushed = true
        self.pushViewController(viewController: vc, animated: true)
    }
}
class MineViewControllerDelegate: BaseTableViewDelegate {
    var cellNum:Double!
    var slidingBlock:selectBlock!
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        self.slidingBlock()
    }
    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 array:NSMutableArray  = self.dataSource[indexPath.section] as! NSMutableArray
        let cellModel:BaseCellModel = array[indexPath.row] as! BaseCellModel
        if cellModel.className == "MineHeadTableViewCell" {
            return (135/375)*SCREEN_WHIDTH()
        }else if cellModel.className == "MineTitleTableViewCell"{
            return 28*HEIGHT_RATIO()
        }else if cellModel.className == "MineDetailedTableViewCell"{
            let detailModel:MineDetailedTableViewCellModel = array[indexPath.row] as! MineDetailedTableViewCellModel
            if detailModel.detaileArray.count >= Int(self.cellNum){
                let index = lroundf(Float(ceil(Double(detailModel.detaileArray.count)/self.cellNum)))
                return (SCREEN_WHIDTH()/4)*CGFloat(index)*0.8
            }
        }else if cellModel.className == "MineWebTableViewCell"{
            return 250*HEIGHT_RATIO()
        }
        return (SCREEN_WHIDTH()/4)*0.8
    }
}
