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

import UIKit

extension CustomerServiceRootVC {
    /// 获取帮助中心类别
    func getCustomerServiewHttpCmd(){
        let cmd:HttpCommand = CustomerServiewHttpCmd.init(version: PHttpVersion_v1)
        let block:httpBlock = {[weak self] (result:RequestResult!,useInfo:Any!)->() in
            if result != nil {
                self?.httpCustomerServiewHttpResultResponse(result: result)
            }else{
                print("空的")
            }
        }
        let dic:NSMutableDictionary = NSMutableDictionary()
        dic[kHttpCustomerServiewHttpCmd_pageNum] = 1
        dic[kHttpCustomerServiewHttpCmd_pageSize] = 10
        let filtersArray = NSMutableArray()
        let filterDic:[String:Any] = ["key":"pid","opt":"EQ","type":"S","value":"1"]
        filtersArray.add(filterDic)
        let attrDic:[String:Any] = ["empid":AccountInfo.sharedInstance().UserId!]
        dic[kHttpCustomerServiewHttpCmd_filters] = filtersArray
        dic[kHttpCustomerServiewHttpCmd_attr] = attrDic
        cmd.requestInfo = dic as [NSObject:AnyObject]
        let completeDelegate = SMBaseHttpComplete.init(block: block , withUserInfo: nil)
        cmd.completeDelegate=completeDelegate
        print("url==%@",cmd.getUrl())
        cmd.execute()
        
    }
    func httpCustomerServiewHttpResultResponse(result:RequestResult){
        let r:CustomerServiewHttpResult = result as! CustomerServiewHttpResult
        if r.isOk() {
            httpArray = r.getCustomerServiewHttpResult()
                as NSMutableArray
        }else{
            SMToastView.showMessage("请查看当前网络状态！")
        }
        group.leave()
    }
    /// 处理数据
//    func setHttpData(){
//        pages = NSMutableArray()
//        for index in 0..<httpArray.count {
//            let model:CustomerServiewHttpModel = httpArray[index] as! CustomerServiewHttpModel
//            if (model.questions.count != 0) {
//                sectionTitles.add(model.orderType!)
//                pages.add(self.createHotPageVC(httpArray: model.questions))
//            }
//        }
//        self.setSegment()
//    }
    func setHttpData(){
        pages = NSMutableArray()
        for index in 0..<httpArray.count {
            let model:CustomerServiewHttpModel = httpArray[index] as! CustomerServiewHttpModel
            if (model.questions.count != 0) {
                sectionTitles.add(model.orderType!)
                pages.add(self.createHotPageVC(httpArray: model.questions))
            }
        }
        if pages.count != 0 {
            initData()
        }
    }
    /// 设置 segment
    func setSegment() {
        segment.frame = CGRect(x:0, y: collectionView.originY+collectionView.height+10, width:SCREEN_WHIDTH() , height:46 )
        segment.sectionTitles = (sectionTitles as! [String])
        segment.indexChangeBlock = {[weak self](index:NSInteger)->() in
            let index = self?.pages.index(of: self?.pageViewController.viewControllers?.last ?? NSInteger())
            let direction:UIPageViewControllerNavigationDirection = (self?.segment.selectedSegmentIndex)! > index! ? .forward:.reverse
            self?.pageViewController.setViewControllers([(self?.selectedController())!], direction: direction, animated: true, completion: nil)
        }
    }
    func createHotPageVC(httpArray:NSMutableArray) -> CustomerServiceVC{
        let vc:CustomerServiceVC = CustomerServiceVC()
        vc.httpArray = httpArray
        vc.block = {[weak self](value:Any) in
            self?.gotoDetalisVC(id: value as! String)
        }
        vc.title = title
        //        vc.type = type
        //        vc.sequenPageType = sequenPageType
        return vc
    }
    /// 获取自主服务
    func getAutonomousHttpPostCmd(){
        let cmd:HttpCommand = AutonomousHttpPostCmd.init(version: PHttpVersion_v1)
        let block:httpBlock = {[weak self] (result:RequestResult!,useInfo:Any!)->() in
            if result != nil {
                self?.httpAutonomousHttpPostResultResponse(result: result)
            }else{
                print("空的")
            }
        }
        let dic:NSMutableDictionary = NSMutableDictionary()
//        let filtersArray = NSMutableArray()
//        let filterDic:[String:Any] = ["value":"5"]
//        filtersArray.add(filterDic)
        let attrDic:[String:Any] = ["userid":AccountInfo.sharedInstance().UserId!,"value":"5"]
//        dic[kHttpCustomerServiewHttpCmd_filters] = filtersArray
        dic[kHttpCustomerServiewHttpCmd_attr] = attrDic
        cmd.requestInfo = dic as [NSObject:AnyObject]
        let completeDelegate = SMBaseHttpComplete.init(block: block , withUserInfo: nil)
        cmd.completeDelegate=completeDelegate
        print("url==%@",cmd.getUrl())
        cmd.execute()
    }
    func httpAutonomousHttpPostResultResponse(result:RequestResult){
        let r:AutonomousHttpPostResult = result as! AutonomousHttpPostResult
        if r.isOk() {
            let array = r.getAutonomousHttpPostResult() as NSMutableArray
            self.collectionViewData(array: array)
        }else{
            SMToastView.showMessage("请查看当前网络状态！")
        }
        group.leave()
    }
    
}
