//
//  SelectCompanyViewController+http.swift
//  MobilePortal
//
//  Created by xiejiangbo on 2019/3/28.
//  Copyright © 2019 yin chen. All rights reserved.
//

import UIKit

extension SelectCompanyViewController {
    // MARK:获取公司列表
    func getSelectCompanyHttpCmd(){
        let cmd:HttpCommand = SelectCompanyHttpCmd.init(version: PHttpVersion_v1)
        let block:httpBlock = {[weak self] (result:RequestResult!,useInfo:Any!)->() in
            if result != nil {
                self?.httpSelectCompanyHttpResultResponse(result: result)
            }else{
                print("空的")
            }
        }
        let dic:NSMutableDictionary = NSMutableDictionary()
        let attrDic:NSMutableDictionary = NSMutableDictionary()
        attrDic[kHttpSelectCompanyHttpCmd_companyname] = ""
        attrDic[kHttpSelectCompanyHttpCmd_sysname] = judgmentStrNil(str: self.selecClassModel.belong)
        attrDic[kHttpSelectCompanyHttpCmd_empid] = AccountInfo.sharedInstance().UserId!+",1"
//        attrDic[kHttpSelectCompanyHttpCmd_empid] = judgmentStrNil(str: AccountInfo.sharedInstance().UserId)
        dic[kHttpSelectCompanyHttpCmd_attr] = attrDic
        dic[kHttpSelectCompanyHttpCmd_dbName] = ""
        dic[kHttpSelectCompanyHttpCmd_length] = "100"
        dic[kHttpSelectCompanyHttpCmd_start] = "1"
        let completeDelegate = SMBaseHttpComplete.init(block: block , withUserInfo: nil)
        cmd.requestInfo = dic as? [AnyHashable : Any]
        cmd.completeDelegate=completeDelegate
        print("url==%@",cmd.getUrl())
        cmd.execute()
    }
    func httpSelectCompanyHttpResultResponse(result:RequestResult){
        let r:SelectCompanyHttpResult = result as! SelectCompanyHttpResult
        if r.isOk() {
            let array:NSMutableArray = r.getSelectCompanyHttpResult()
            let selectCompanyModel:SelectCompanyHttpModel = array[0] as! SelectCompanyHttpModel
            self.initCompanyDataSource(selectCompanyModel:selectCompanyModel)
        }else{
            SMToastView.showMessage("请查看当前网络状态！")
        }
        group.leave()
    }
    func initCompanyDataSource(selectCompanyModel:SelectCompanyHttpModel)  {
        self.dataSource = NSMutableArray()
        self.depDataSource = NSMutableArray()
        for index in 0..<selectCompanyModel.sysCompany.data.count {
            let companyHttpModel:SelectCompanyHttpCompanyDataModel = selectCompanyModel.sysCompany.data[index] as! SelectCompanyHttpCompanyDataModel
            if index == 0 {
                selecCompanyModel = companyHttpModel
            }
            let model = CompanyListTableViewCellModel()
            model.className = "CompanyListTableViewCell"
            model.titleValue = companyHttpModel.companyname
            model.selecCompanyUserid = companyHttpModel.userid
            model.titleValueSelecCompanyUserid = companyHttpModel.companyname! + "(" + companyHttpModel.userid! + ")"
            model.isShowSelec = true
            model.block = {[weak self] (value:Any) in
                self?.actionCompany(companyHttpModel: companyHttpModel)
            }
            self.dataSource .add(model)
        }
        
        for i in 0..<selectCompanyModel.orgList.data.count {
            let deHttpMolde:SelectCompanyHttpOrgListDataModel = selectCompanyModel.orgList.data[i] as! SelectCompanyHttpOrgListDataModel
            let model = CompanyListTableViewCellModel()
            if i == 0{
                self.selecDepartmentModel = deHttpMolde
            }
            model.className = "CompanyListTableViewCell"
            model.titleValue = deHttpMolde.partyName
            model.isShowSelec = true
            model.block = {[weak self] (value:Any) in
                self?.actionDepartmentModel(deHttpMolde: deHttpMolde)
            }
            self.depDataSource.add(model)
        }
        
        if selectCompanyModel.orgList.data.count == 0 {
            addConfirmBtn()
            self.departmentTableView.isScrollEnabled = false
        }else{
            self.departmentTableView.isScrollEnabled = true
        }
        if self.selecCompanyModel == nil{
            if self.selecDepartmentModel == nil{
                self.selecDepartmentModel = SelectCompanyHttpOrgListDataModel()
            }
//            setVcTitle(userid: judgmentStrNil(str: self.selecCompanyModel.userid), partyOpcode: judgmentStrNil(str: self.selecDepartmentModel.partyOpcode))
        }
    }
    func addConfirmBtn(){
        self.selecDepartmentModel = SelectCompanyHttpOrgListDataModel()
        //        self.selecCompanyModel =  selecOneCompanyModel
        let model = CompanyListTableViewCellModel()
        model.className = "CompanyListTableViewCell"
        model.titleValue = "确认"
        model.isShowSelec = true
        model.block = {[weak self] (value:Any) in
            //self?.setWebUrl()
            self?.actionDepartmentModel(deHttpMolde: SelectCompanyHttpOrgListDataModel())
        }
        self.depDataSource.add(model)
    }
    //点击公司
    func actionCompany(companyHttpModel:SelectCompanyHttpCompanyDataModel){
        
        self.selecCompanyModel = companyHttpModel
        AccountInfo.sharedInstance().sysUserId = self.selecCompanyModel.userid
        AccountInfo.sharedInstance().selecCompanyName = self.selecCompanyModel.companyname
        AccountInfo.sharedInstance().selecCompanyId = self.selecCompanyModel.id
        AccountInfo.sharedInstance().sysOrgService = self.selecCompanyModel.org_service
        AccountInfo.sharedInstance().sysType = self.selecCompanyModel.sysname
        AccountInfo.sharedInstance().docId = self.selecClassModel.applicationid
        AccountInfo.sharedInstance().db_source = self.selecCompanyModel.db_source
        AccountInfo.sharedInstance().cmsRealDb = self.selecCompanyModel.cmsRealDb
        AccountInfo.sharedInstance().cmsTestDb = self.selecCompanyModel.cmsTestDb
        AccountInfo.sharedInstance().omsRealDb = self.selecCompanyModel.omsRealDb
        AccountInfo.sharedInstance().omsTestDb = self.selecCompanyModel.omsTestDb
        AccountInfo.sharedInstance().tmsRealDb = self.selecCompanyModel.tmsRealDb
        AccountInfo.sharedInstance().tmsTestDb = self.selecCompanyModel.tmsTestDb
        AccountInfo.sharedInstance().appRealDb = self.selecCompanyModel.appRealDb
        AccountInfo.sharedInstance().appTestDb = self.selecCompanyModel.appTestDb
        AccountInfo.sharedInstance().tms_version = self.selecCompanyModel.tms_version
//        self.getSelectDepartmentListHttpCmd()
        self.actiondepartmentHttpGCD()
    }
    //点击部门
    func actionDepartmentModel(deHttpMolde:SelectCompanyHttpOrgListDataModel){
        self.selecDepartmentModel = deHttpMolde
        AccountInfo.sharedInstance().partyOpcode = judgmentStrNil(str: self.selecDepartmentModel.partyOpcode)
        AccountInfo.sharedInstance().sysOrg = judgmentStrNil(str: self.selecDepartmentModel.org)
        AccountInfo.sharedInstance().docId = judgmentStrNil(str: self.selecClassModel.applicationid)
        AccountInfo.sharedInstance().selecDepartmentName = judgmentStrNil(str: self.selecDepartmentModel.partyName)
       
        AccountInfo.sharedInstance().selecCompanyName = judgmentStrNil(str: self.selecCompanyModel.companyname)
        AccountInfo.sharedInstance().selecCompanyId = judgmentStrNil(str: self.selecCompanyModel.id)
        AccountInfo.sharedInstance().sysOrgService = judgmentStrNil(str: self.selecCompanyModel.org_service)
        AccountInfo.sharedInstance().sysType = judgmentStrNil(str: self.selecCompanyModel.sysname)
        AccountInfo.sharedInstance().db_source = judgmentStrNil(str: self.selecCompanyModel.db_source)
        AccountInfo.sharedInstance().sysUserId = judgmentStrNil(str: self.selecCompanyModel.userid)
        AccountInfo.sharedInstance().cmsRealDb = judgmentStrNil(str: self.selecCompanyModel.cmsRealDb)
        AccountInfo.sharedInstance().cmsTestDb = judgmentStrNil(str: self.selecCompanyModel.cmsTestDb)
        AccountInfo.sharedInstance().omsRealDb = judgmentStrNil(str: self.selecCompanyModel.omsRealDb)
        AccountInfo.sharedInstance().omsTestDb = judgmentStrNil(str: self.selecCompanyModel.omsTestDb)
        AccountInfo.sharedInstance().tmsRealDb = judgmentStrNil(str: self.selecCompanyModel.tmsRealDb)
        AccountInfo.sharedInstance().tmsTestDb = judgmentStrNil(str: self.selecCompanyModel.tmsTestDb)
        AccountInfo.sharedInstance().appRealDb = judgmentStrNil(str: self.selecCompanyModel.appRealDb)
        AccountInfo.sharedInstance().appTestDb = judgmentStrNil(str: self.selecCompanyModel.appTestDb)
        setVcTitle(userid: judgmentStrNil(str: self.selecCompanyModel.userid), partyOpcode: judgmentStrNil(str: self.selecDepartmentModel.partyOpcode))
        self.setWebUrl()
    }
    func setWebUrl(){
        
        let companyInfo = SelecCompanyDepartmentInfo()
        companyInfo.tags = judgmentStrNil(str: self.selecClassModel.sysid)
        companyInfo.selecCompanyName = judgmentStrNil(str:AccountInfo.sharedInstance().selecCompanyName)
        companyInfo.selecCompanyId = judgmentStrNil(str: AccountInfo.sharedInstance().selecCompanyId)
        companyInfo.selecDepartmentName = judgmentStrNil(str:AccountInfo.sharedInstance().selecDepartmentName)
        companyInfo.docId = judgmentStrNil(str: AccountInfo.sharedInstance().selecCompanyId)
        companyInfo.db_source = judgmentStrNil(str:  AccountInfo.sharedInstance().db_source)
        companyInfo.sysOrg = judgmentStrNil(str: AccountInfo.sharedInstance().sysOrg)
        companyInfo.sysUserId = judgmentStrNil(str: AccountInfo.sharedInstance().sysUserId)
        companyInfo.sysType = judgmentStrNil(str: AccountInfo.sharedInstance().sysType)
        companyInfo.org_service = judgmentStrNil(str: AccountInfo.sharedInstance().sysOrgService)
        companyInfo.partyOpcode = judgmentStrNil(str: AccountInfo.sharedInstance().partyOpcode)
        companyInfo.cmsRealDb = judgmentStrNil(str: AccountInfo.sharedInstance().cmsRealDb)
        companyInfo.cmsTestDb = judgmentStrNil(str: AccountInfo.sharedInstance().cmsTestDb)
        companyInfo.omsRealDb = judgmentStrNil(str: AccountInfo.sharedInstance().omsRealDb)
        companyInfo.omsTestDb = judgmentStrNil(str: AccountInfo.sharedInstance().omsTestDb)
        companyInfo.tmsRealDb = judgmentStrNil(str: AccountInfo.sharedInstance().tmsRealDb)
        companyInfo.tmsTestDb = judgmentStrNil(str: AccountInfo.sharedInstance().tmsTestDb)
        companyInfo.appRealDb = judgmentStrNil(str: AccountInfo.sharedInstance().appRealDb)
        companyInfo.appTestDb = judgmentStrNil(str: AccountInfo.sharedInstance().appTestDb)
        companyInfo.tms_version = judgmentStrNil(str: AccountInfo.sharedInstance().tms_version)
        companyInfo.userId = AccountInfo.sharedInstance().UserId
        if FGDBHelper.sharedInstance().hasCompanyDepartmentInfo(AccountInfo.sharedInstance().UserId, andTages: companyInfo.tags) {
            FGDBHelper.sharedInstance().update(companyInfo) { [weak self](bool:Bool) in
                if bool {
                    self?.navigationController?.popViewController(animated: true)
                    self?.block("123" as AnyObject)
                }else{
                    SMToastView.showMessage("存储失败！")
                }
            }
        }else{
            FGDBHelper.sharedInstance().save(companyInfo) {[weak self](bool:Bool) in
                if bool {
                    self?.navigationController?.popViewController(animated: true)
                    self?.block("123" as AnyObject)
                }else{
                    SMToastView.showMessage("存储失败！")
                }
            }
        }
    }
    // MARK:获取部门列表
    func getSelectDepartmentListHttpCmd(){
        let cmd:HttpCommand = SelectCompanyHttpCmd.init(version: PHttpVersion_v1)
        let block:httpBlock = {[weak self] (result:RequestResult!,useInfo:Any!)->() in
            if result != nil {
                //self?.httpSelectCompanyHttpResultResponse(result: result)
                self?.httpSelectDepartmentHttpResultResponse(result: result)
            }else{
                print("空的")
            }
        }
        let dic:NSMutableDictionary = NSMutableDictionary()
        let attrDic:NSMutableDictionary = NSMutableDictionary()
        attrDic[kHttpSelectCompanyHttpCmd_companyname] = self.selecCompanyModel.companyname
        attrDic[kHttpSelectCompanyHttpCmd_sysname] = judgmentStrNil(str: self.selecClassModel.belong)
        
        attrDic[kHttpSelectCompanyHttpCmd_empid] = AccountInfo.sharedInstance().UserId!+",1"
//        attrDic[kHttpSelectCompanyHttpCmd_empid] = judgmentStrNil(str: AccountInfo.sharedInstance().UserId)
        attrDic[kHttpSelectCompanyHttpCmd_userid] = self.selecCompanyModel.userid
        dic[kHttpSelectCompanyHttpCmd_attr] = attrDic
        dic[kHttpSelectCompanyHttpCmd_dbName] = ""
        dic[kHttpSelectCompanyHttpCmd_length] = "100"
        dic[kHttpSelectCompanyHttpCmd_start] = "1"
        let completeDelegate = SMBaseHttpComplete.init(block: block , withUserInfo: nil)
        cmd.requestInfo = dic as? [AnyHashable : Any]
        cmd.completeDelegate=completeDelegate
        print("url==%@",cmd.getUrl())
        cmd.execute()
    }
    func httpSelectDepartmentHttpResultResponse(result:RequestResult){
        let r:SelectCompanyHttpResult = result as! SelectCompanyHttpResult
        if r.isOk() {
            let array:NSMutableArray = r.getSelectCompanyHttpResult()
            let selectCompanyModel:SelectCompanyHttpModel = array[0] as! SelectCompanyHttpModel
            self.initSelectDepartmentListDataSource(selectCompanyModel:selectCompanyModel)
        }else{
            SMToastView.showMessage("请查看当前网络状态！")
        }
        group.leave()
    }
    func initSelectDepartmentListDataSource(selectCompanyModel:SelectCompanyHttpModel)  {
        self.depDataSource = NSMutableArray()
        for i in 0..<selectCompanyModel.orgList.data.count {
            let deHttpMolde:SelectCompanyHttpOrgListDataModel = selectCompanyModel.orgList.data[i] as! SelectCompanyHttpOrgListDataModel
            let model = CompanyListTableViewCellModel()
            if i == 0{
                self.selecDepartmentModel = deHttpMolde
            }
            model.className = "CompanyListTableViewCell"
            model.titleValue = deHttpMolde.partyName
            model.isShowSelec = true
            model.block = {[weak self] (value:Any) in
                self?.actionDepartmentModel(deHttpMolde: deHttpMolde)
            }
            self.depDataSource.add(model)
        }
        if selectCompanyModel.orgList.data.count == 0 {
            addConfirmBtn()
            self.selecDepartmentModel = SelectCompanyHttpOrgListDataModel()
            self.departmentTableView.isScrollEnabled = false
        }else{
            self.departmentTableView.isScrollEnabled = true
        }
        if selecDepartmentModel == nil   {
            selecDepartmentModel = SelectCompanyHttpOrgListDataModel()
        }
        setVcTitle(userid: AccountInfo.sharedInstance().sysUserId, partyOpcode: judgmentStrNil(str: self.selecDepartmentModel.partyOpcode))
        
    }
}
