//
//  WeChatBindingVC+IdmHttp.swift
//  MobilePortal
//
//  Created by caiyang on 2020/12/9.
//  Copyright © 2020 yin chen. All rights reserved.
//

import UIKit

extension WeChatBindingVC {
    /// MARK: IDM 微信 绑定
    func setIDMWeChatBindingHttpPostCmd(){
        showHintView()
        let cmd:HttpCommand = IDMWeChatBindingHttpPostCmd.init(version: PHttpVersion_v1)
        let block:httpBlock = {[weak self] (result:RequestResult!,useInfo:Any!)->() in
            if result != nil {
                self?.getIDMWeChatBindingHttpResultResponse(result: result)
            }else{
                print("空的")
            }
        }
        let dic:NSMutableDictionary = NSMutableDictionary()
        dic[kHttpIDMWeChatBindingHttpPostCmd_appId] = AccountInfo.sharedInstance().singlePoint_appId
        dic[kHttpIDMWeChatBindingHttpPostCmd_openId] = judgmentStrNil(str: AccountInfo.sharedInstance().weChat_unionid)
//        dic[kHttpIDMWeChatBindingHttpPostCmd_openId] = "qwertyu"
        dic[kHttpIDMWeChatBindingHttpPostCmd_remoteIp] = AccountInfo.sharedInstance().singlePoint_remoteIp
        dic[kHttpIDMWeChatBindingHttpPostCmd_userName] = judgmentStrNil(str: self.loginView.userTextView.text)
        dic[kHttpIDMWeChatBindingHttpPostCmd_password] = judgmentStrNil(str: self.loginView.passTextView.text)
        dic[kHttpIDMWeChatBindingHttpPostCmd_equipmentId] = AccountInfo.sharedInstance().appUUID
        dic[kHttpIDMWeChatBindingHttpPostCmd_wechatname] = judgmentStrNil(str: AccountInfo.sharedInstance().weChatInfo_nickname)
        cmd.requestInfo = dic as [NSObject:AnyObject]
        let completeDelegate = SMBaseHttpComplete.init(block: block , withUserInfo: nil)
        cmd.completeDelegate=completeDelegate
        print("url==%@",cmd.getUrl())
        cmd.execute()
    }
    func getIDMWeChatBindingHttpResultResponse(result:RequestResult){
        let r:IDMWeChatBindingHttpResult = result as! IDMWeChatBindingHttpResult
        if r.isOk() {
            let array:NSMutableArray = r.getIDMWeChatBindingHttpResult()
            if array.count == 0{
               
                if (AccountInfo.sharedInstance().loginType != nil && AccountInfo.sharedInstance().loginType == "1") {
                   AccountInfo.sharedInstance().singlePoint_startTime = newDateStr()
                    let strMessage = "设备：" + judgmentStrNil(str: AccountInfo.sharedInstance().appUUID!) + ";IDM登录;" + newDateStr()
                    SinglePointWeChatHttp.sharedInstance().setIdmLogSaveHttpPostCmd(inputmessage: judgmentStrNil(str: self.loginView.userTextView.text), logmessage: strMessage, idmmessage: judgmentStrNil(str: AccountInfo.sharedInstance().UserId))
                }
                getLoginDeviceInformationHttpCmd()
            } else {
                self.hidenHintView()
                var content = array.firstObject as! String
                if content == "101" && array.count == 2 {
                    let lastObject = array.lastObject as! String
                    content = "(" + content + ")" + lastObject
                    SMToastView.showMessage(content)
                }
                
            }
        }else{
            hidenHintView()
            SMToastView.showMessage("请查看当前网络状态！")
        }
    }
}
