//
//  UpcomingViewController+http.swift
//  MobilePortal
//
//  Created by xiejiangbo on 2020/2/10.
//  Copyright © 2020 yin chen. All rights reserved.
//

import UIKit

extension UpcomingViewController {
    //MARK：获取对应服务
    func getApplicationByBelongAction(applicationName:String)  {
        showHintView()
        let cmd:HttpCommand = ApplicationByBelongHttpGetCmd.init(version: PHttpVersion_v1)
        let block:httpBlock = {[weak self] (result:RequestResult!,useInfo:Any!)->() in
            if result != nil {
                self?.httpApplicationByBelongHttpResultResponse(result: result)
            }else{
                print("空的")
            }
        }
        let dic:NSMutableDictionary = NSMutableDictionary()
        dic[kHttpApplicationByBelongHttpGetCmd_applicationName] = applicationName
        cmd.requestInfo = dic as [NSObject:AnyObject]
        let completeDelegate = SMBaseHttpComplete.init(block: block , withUserInfo: nil)
        cmd.requestInfo = dic as? [AnyHashable : Any]
        cmd.completeDelegate=completeDelegate
        print("url==%@",cmd.getUrl())
        cmd.execute()
    }
    @objc func httpApplicationByBelongHttpResultResponse(result:RequestResult){
        let r:ApplicationByBelongHttpResult = result as! ApplicationByBelongHttpResult
        if r.isOk() {
            let array:NSMutableArray = r.getApplicationByBelongHttpResult()
            if array.count != 0{
                let httpDetailModel:ApplicationHttpListModel = array[0] as! ApplicationHttpListModel
                self.refreshTokenHttpAction(selecHttpModel: httpDetailModel)
            }
        }else{
            SMToastView.showMessage("请查看当前网络状态！")
        }
        self.hidenHintView()
    }
    //MARK：获取消息详情
    func getApplicationDetailsHttpActin(applicationName:String)  {
        showHintView()
        let cmd:HttpCommand = ApplicationDetailsHttpGetCmd.init(version: PHttpVersion_v1)
        let block:httpBlock = {[weak self] (result:RequestResult!,useInfo:Any!)->() in
            if result != nil {
                self?.httpApplicationDetailsHttpResultResponse(result: result)
            }else{
                print("空的")
            }
        }
        let dic:NSMutableDictionary = NSMutableDictionary()
        dic[kHttpApplicationDetailsHttpGetCmd_applicationName] = applicationName
        cmd.requestInfo = dic as [NSObject:AnyObject]
        let completeDelegate = SMBaseHttpComplete.init(block: block , withUserInfo: nil)
        cmd.requestInfo = dic as? [AnyHashable : Any]
        cmd.completeDelegate=completeDelegate
        print("url==%@",cmd.getUrl())
        cmd.execute()
    }
    @objc func httpApplicationDetailsHttpResultResponse(result:RequestResult){
        let r:ApplicationDetailsHttpResult = result as! ApplicationDetailsHttpResult
        if r.isOk() {
            let array:NSMutableArray = r.getApplicationDetailsHttpResult()
            if array.count != 0{
                let httpDetailModel:ApplicationHttpListModel = array[0] as! ApplicationHttpListModel
                self.refreshTokenHttpAction(selecHttpModel: httpDetailModel)
            }
        }else{
            SMToastView.showMessage("请查看当前网络状态！")
        }
        self.hidenHintView()
    }
    
    /// 获取所有
    func getAllApplicationHttpListCmd(){
        let cmd:HttpCommand = AllApplicationHttpListCmd.init(version: PHttpVersion_v1)
        let block:httpBlock = {[weak self] (result:RequestResult!,useInfo:Any!)->() in
            if result != nil {
                self?.httpMoreApplicationsTwoHttpResultResponse(result: result)
            }else{
                print("空的")
            }
        }
        let dic:NSMutableDictionary = NSMutableDictionary()
        let attrDic:NSMutableDictionary = NSMutableDictionary()
        attrDic[kHttpAllApplicationHttpListCmd_userid] = judgmentStrNil(str: AccountInfo.sharedInstance().UserId)
        attrDic[kHttpAllApplicationHttpListCmd_empid] = judgmentStrNil(str: AccountInfo.sharedInstance().UserId)
        if self.businessType == .UpcomingType {
            attrDic[kHttpAllApplicationHttpListCmd_value] = "7"
        } else {
            attrDic[kHttpAllApplicationHttpListCmd_value] = "8"
        }
        
        dic[kHttpAllApplicationHttpListCmd_attr] = attrDic
        dic[kHttpAllApplicationHttpListCmd_orderby] = "createon"
        dic[kHttpAllApplicationHttpListCmd_pageNum] = "1"
        dic[kHttpAllApplicationHttpListCmd_pageSize] = ""
        dic[kHttpAllApplicationHttpListCmd_sort] = "desc"
        let filtersDic:NSMutableDictionary = NSMutableDictionary()
        let filtersArray = NSMutableArray()
        filtersArray.add(filtersDic)
        dic[kHttpAllApplicationHttpListCmd_filters] = filtersArray
        cmd.requestInfo = dic as [NSObject:AnyObject]
        let completeDelegate = SMBaseHttpComplete.init(block: block , withUserInfo: nil)
        cmd.completeDelegate=completeDelegate
        print("url==%@",cmd.getUrl())
        cmd.execute()
    }
    func httpMoreApplicationsTwoHttpResultResponse(result:RequestResult){
        let r:AllApplicationHttpListResult = result as! AllApplicationHttpListResult
        if r.isOk() {
            let array:NSMutableArray = r.getAllApplicationHttpListResult()
            self.httpArray = NSMutableArray()
            self.dataSource = NSMutableArray()
            self.settableViewCellModel(modelArray: array)
        }else{
            SMToastView.showMessage("请查看当前网络状态！")
            group.leave()
        }
    }
    //
    func settableViewCellModel(modelArray:NSMutableArray) {
        self.upcomingKey = ""
        for index in 0..<modelArray.count {
            let httpModel:ApplicationHttpListModel = modelArray[index] as! ApplicationHttpListModel
            if (index == 0) {
               self.upcomingKey = httpModel.todokey!
            } else {
                self.upcomingKey = self.upcomingKey + "," + httpModel.todokey!
            }
            self.httpArray.add(self.getServiceCollectionViewCellModelNew(httpModel:httpModel))
//            self.dataSource .add(self.getServiceCollectionViewCellModelNew(httpModel:httpModel))
        }
        /// 获取待办数目
        getUpcomingCountHttp()
    }
    
    
    
    // MARK:待办已办总数
    func getUpcomingCountHttpPostCmd(){
        /// 获取d统一待办 总数目
        UpcomingNewCountHttpAction.sharedInstance().getAllApplicationHttpListCmd {[weak self] in
            self?.tabBarController?.tabBar.showBadge(onItemIndex: 1, angNum: numBigData(numStr: judgmentStrNil(str: AccountInfo.sharedInstance().upcomingCount))
            )
        }
        return
        let cmd:HttpCommand = UpcomingCountHttpPostCmd.init(version: PHttpVersion_v1)
        let block:httpBlock = {[weak self] (result:RequestResult!,useInfo:Any!)->() in
            if result != nil {
                self?.httpUpcomingCountHttpResultResponse(result: result)
            }else{
                print("空的")
            }
        }
        let dic:NSMutableDictionary = NSMutableDictionary()
        dic[kHttpUpcomingCountHttpPostCmd_targetid] = judgmentStrNil(str: AccountInfo.sharedInstance().UserId)
        dic[kHttpUpcomingCountHttpPostCmd_empuid] = judgmentStrNil(str:AccountInfo.sharedInstance().sysUserId)
        let completeDelegate = SMBaseHttpComplete.init(block: block , withUserInfo: nil)
        cmd.requestInfo = dic as? [AnyHashable : Any]
        cmd.completeDelegate=completeDelegate
        print("url==%@",cmd.getUrl())
        cmd.execute()

    }
    @objc func httpUpcomingCountHttpResultResponse(result:RequestResult){
        let r:UpcomingCountHttpResult = result as! UpcomingCountHttpResult
        if r.isOk() {
            let array:NSMutableArray = r.getUpcomingCountHttpResult()
            if array.count != 0{
                let model:UpcomingCountModel = array[0] as! UpcomingCountModel
                AccountInfo.sharedInstance().upcomingCount = model.ytotal
                AccountInfo.sharedInstance().doneCount = model.dtotal
                self.tabBarController?.tabBar.showBadge(onItemIndex: 1, angNum: judgmentStrNil(str: AccountInfo.sharedInstance().doneCount))
            }
        }else{
            //            SMToastView.showMessage("请查看当前网络状态！")
        }
    }
    
    
//    func getUpcomingSystemHttpPostCmd(){
//        let cmd:HttpCommand = UpcomingSystemHttpPostCmd.init(version: PHttpVersion_v1)
//        let block:httpBlock = {[weak self] (result:RequestResult!,useInfo:Any!)->() in
//            if result != nil {
//                self?.httpUpcomingSystemHttpResultResponse(result: result)
//            }else{
//                print("空的")
//            }
//        }
//        let dic:NSMutableDictionary = NSMutableDictionary()
//        dic[kHttpMyUpcomingListHttpPostCmd_targetid] = judgmentStrNil(str: AccountInfo.sharedInstance().UserId)
//        dic[kHttpMyUpcomingListHttpPostCmd_empuid] = judgmentStrNil(str:AccountInfo.sharedInstance().sysUserId)
//        dic[kHttpMyUpcomingListHttpPostCmd_state] = "1"
//        cmd.requestInfo = dic as [NSObject:AnyObject]
//        let completeDelegate = SMBaseHttpComplete.init(block: block , withUserInfo: nil)
//        cmd.requestInfo = dic as? [AnyHashable : Any]
//        cmd.completeDelegate=completeDelegate
//        print("url==%@",cmd.getUrl())
//        cmd.execute()
//    }
//    func httpUpcomingSystemHttpResultResponse(result:RequestResult){
//        let r:UpcomingSystemHttpResult = result as! UpcomingSystemHttpResult
//        if r.isOk() {
//            let array:NSMutableArray = r.getUpcomingSystemHttpResult() as NSMutableArray
////            httpListData = array[0] as! ServiceHttpListModelData
//            self.dataSource = NSMutableArray()
//            if array.count == 0 {
////                SMToastView.showMessage("暂无更多数据！")
//            } else {
//                self.initSetData(httpArray: array)
//            }
//        }else{
////            SMToastView.showMessage("请查看当前网络状态！")
//        }
//        group.leave()
//    }
//    func initSetData(httpArray:NSMutableArray){
//        for index in 0..<httpArray.count {
//            let httpModel:UpcomingSystemModel = httpArray[index] as! UpcomingSystemModel
////            self.dataSource .add(self.getServiceCollectionViewCellModelNew(httpModel:httpModel))
//        }
//    }
    func gotoListVC(model:ApplicationHttpListModel){
        let vc = UpcomingListVC()
        vc.titleType = judgmentStrNil(str: model.name)
        vc.upcomingType = self.businessType
        vc.appModel = model
//        vc.title = judgmentStrNil(str: model.name) + self.businessType.rawValue
        vc.title = judgmentStrNil(str: model.name)
        vc.hidesBottomBarWhenPushed = true
        self.pushViewController(viewController: vc, animated: true)
    }
    /// 获取统一待办 数量
    func getUpcomingCountHttp(){
        let type = self.businessType == .DoneType ? UnifiedAgencyType.DoneUnifiedAgencyType.rawValue:UnifiedAgencyType.UpcomingUnifiedAgencyType.rawValue
        UpcomingNewCountHttpAction.sharedInstance().getUpcomingCountCmdAction(type: type, key: self.upcomingKey, resultBlock: { [weak self] (value:Any) in
            self?.setUpcomingCountDataAction(array: value as! NSMutableArray)
            self?.group.leave()
        }) { [weak self] (value:Any) in
            self?.group.leave()
        }
    }
    // 处理待办数目
    func setUpcomingCountDataAction(array:NSMutableArray) {
        self.upcomingCountData = array
    }
}
