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

import UIKit

extension UpcomingListVC {
    /// 旧的获取列表
    func getMyUpcomingListHttpPostCmd(){
        let cmd:HttpCommand = MyUpcomingListHttpPostCmd.init(version: PHttpVersion_v1)
        let block:httpBlock = {[weak self] (result:RequestResult!,useInfo:Any!)->() in
            if result != nil {
                self?.httpMyUpcomingListHttpResultResponse(result: result)
            }else{
                print("空的")
            }
        }
        let dic:NSMutableDictionary = NSMutableDictionary()
        let attrDic:NSMutableDictionary = NSMutableDictionary()
        attrDic[kHttpMyUpcomingListHttpPostCmd_system] = judgmentStrNil(str: self.titleType)
        attrDic[kHttpMyUpcomingListHttpPostCmd_state] = "1"
        attrDic[kHttpMyUpcomingListHttpPostCmd_targetid] = judgmentStrNil(str: AccountInfo.sharedInstance().UserId)
        dic[kHttpMyUpcomingListHttpPostCmd_attr] = attrDic
        dic[kHttpMyUpcomingListHttpPostCmd_pageSize] = "20"
        dic[kHttpMyUpcomingListHttpPostCmd_pageNum] = self.page
        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 httpMyUpcomingListHttpResultResponse(result:RequestResult){
        let r:MyUpcomingListHttpResult = result as! MyUpcomingListHttpResult
        if r.isOk() {
            let array:NSMutableArray = r.getMyUpcomingListHttpResult() as NSMutableArray
            httpListData = array[0] as! UpcomingListModel
        }else{
            SMToastView.showMessage("请查看当前网络状态！")
        }
        group.leave()
    }
    
    
    
    
    func initSetData(httpArray:NSMutableArray){
        if self.page == 1 {
            self.dataSource = NSMutableArray()
            self.delegate.dataSource = self.dataSource
        }
        if httpArray.count != 1 {
            self.page = self.page+1
        }else if httpArray.count == 0 && self.page != 1 {
            SMToastView.showMessage("暂无更多数据！")
        }
        for index in 0..<httpArray.count {
            let httpModel:UpcomingListItemModel = httpArray[index] as! UpcomingListItemModel
            self.addHttpUpcomingListTableViewCellModel(httpModel: httpModel)
        }
        self.delegate.dataSource = self.dataSource
        //        self.myTableView .reloadData()
    }
    func addHttpUpcomingListTableViewCellModel(httpModel:UpcomingListItemModel){
        let model = UpcomingListTableViewCellModel()
        model.className = "UpcomingListTableViewCell"
        model.title = self.titleType
        model.subTitle = httpModel.title
//        model.dateTime = self.getDateFormatString(timeStamp: httpModel.msgtime!)
        model.dateTime = httpModel.datetime
        model.minuteTime = httpModel.dealtime
        model.upcomingType = self.upcomingType
        model.block = {[weak self] in
            self?.gotoDetails(model: httpModel)
        }
        self.dataSource.add(model)
    }
    func getDateFormatString(timeStamp:String) ->String{
        let interval:TimeInterval = TimeInterval.init(timeStamp)!
        let date = Date.init(timeIntervalSince1970: interval)
        let dateformatter = DateFormatter()
        dateformatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
        return dateformatter.string(from: date)
    }
    func gotoDetails(model:UpcomingListItemModel){
//        let vc = UpcomingDetailsVC()
//        vc.model = model
//        vc.upcomingType = self.upcomingType
//        vc.title = model.upcomingType.rawValue + "详情"
//        self.pushViewController(viewController: vc, animated: true)
        if model.url?.count == 0 {
            SMToastView.showMessage("跳转地址有误！")
            return
        }
        
//        model.url = "http://192.168.1.110:8081/myapply/view/index.html?sysUserId=a3230&taskId=11309225#/"
//        if (self.upcomingType == .DoneType) {
//            model.url = "http://mobileportal.shaphar.com:8020/SYBPM/appvoal/myapply/view/index1.html?" + self.getWebCookie(urlStr: model.url)
//        } else {
//            model.url = "http://mobileportal.shaphar.com:8020/SYBPM/appvoal/myappoval/view/index1.html?" + self.getWebCookie(urlStr: model.url)
//        }
        let url = judgmentStrNil(str: model.url!) + "&loginType=" + judgmentStrNil(str: AccountInfo.sharedInstance().loginType) + "&userId=" + judgmentStrNil(str: AccountInfo.sharedInstance().UserId) + "&IDMToken=" + judgmentStrNil(str: AccountInfo.sharedInstance().singlePoint_access_token)
        self.actionGotoTitleWeb(webViewUrl: url, title: model.title!, isHomePushType: .NOIsHomePushType, appid: self.appModel.appid!, isEncryptionType: .YesEncryptionType)
    }

}
