//
//  AutonomousCompanyVC+http.swift
//  MobilePortal
//
//  Created by caiyang on 2020/4/26.
//  Copyright © 2020 yin chen. All rights reserved.
//

import UIKit

extension AutonomousCompanyVC {
    /// 获取自主服务公司
    func getAutonomousHttpPostCmd(){
        let cmd:HttpCommand = AutonomousCompanyHttpPostCmd.init(version: PHttpVersion_v1)
        let block:httpBlock = {[weak self] (result:RequestResult!,useInfo:Any!)->() in
            if result != nil {
                self?.httpAutonomousCompanyHttpPostResultResponse(result: result)
            }else{
                print("空的")
            }
        }
        let dic:NSMutableDictionary = NSMutableDictionary()
        dic[kHttpAutonomousCompanyHttpPostCmd_dbName] = ""
        dic[kHttpAutonomousCompanyHttpPostCmd_start] = "1"
        dic[kHttpAutonomousCompanyHttpPostCmd_length] = "100"
        let attr:NSMutableDictionary = NSMutableDictionary()
        attr[kHttpAutonomousCompanyHttpPostCmd_userid] = AccountInfo.sharedInstance().UserId
        attr[kHttpAutonomousCompanyHttpPostCmd_companyname] = ""
        attr[kHttpAutonomousCompanyHttpPostCmd_empid] = AccountInfo.sharedInstance().UserId
        attr[kHttpAutonomousCompanyHttpPostCmd_sysname] = judgmentStrNil(str: model.belong)
        dic[kHttpAutonomousCompanyHttpPostCmd_attr] = attr
        cmd.requestInfo = dic as [NSObject:AnyObject]
        let completeDelegate = SMBaseHttpComplete.init(block: block , withUserInfo: nil)
        cmd.completeDelegate=completeDelegate
        print("url==%@",cmd.getUrl())
        cmd.execute()
    }
    func httpAutonomousCompanyHttpPostResultResponse(result:RequestResult){
        let r:AutonomousCompanyHttpPostResult = result as! AutonomousCompanyHttpPostResult
        if r.isOk() {
            let array = r.getAutonomousCompanyHttpPostResult() as NSMutableArray
            self.setHttpViewData(array: array)
        }else{
            SMToastView.showMessage("请查看当前网络状态！")
        }
        group.leave()
    }
    func setHttpViewData(array:NSMutableArray){
        for index in 0..<array.count {
            let httpModel:AutonomousCompanyModel = array[index] as! AutonomousCompanyModel
            let cellModel = AutonomousCompanyTableViewCellModel()
            cellModel.className = "AutonomousCompanyTableViewCell"
            cellModel.titleStr = httpModel.companyname
            cellModel.block = {[weak self] in
                self?.block(httpModel)
                self?.arrowResponse()
            }
            if !self.companyArray.contains(httpModel.companyname!) {
                self.dataSource.add(cellModel)
                self.companyArray.add(httpModel.companyname!)
            }
        }
    }
}
