//
//  ChangePassWordViewController+http.swift
//  MobilePortal
//
//  Created by xiejiangbo on 2019/6/28.
//  Copyright © 2019 yin chen. All rights reserved.
//

import UIKit

extension ChangePassWordViewController {
    // MARK:根据公司获取登录方式
    func getLoginTypeHttpAction(){
        //       SinglePointWeChatHttp.sharedInstance().getlogin
        var userId = judgmentStrNil(str: AccountInfo.sharedInstance().UserId)
        if self.changePassWordType == .LoginChangePassWordType {
            userId = judgmentStrNil(str: AccountInfo.sharedInstance().ChangPassWordUserId)
        }
        SinglePointWeChatHttp.sharedInstance().getLoginTypeHttpGetCmd(userid: userId, resultBlock: {[weak self] (value:Any) in
            self?.loginTypeHttpGetCmdTypeAction(array: value as! NSMutableArray)
        }) { (value:Any) in
            
        }
    }
    func loginTypeHttpGetCmdTypeAction(array:NSMutableArray) {
        for index in 0..<array.count {
            let item:String = array[index] as! String
            /// 1.自带登录；2.老单点；3.集团单点
            if (index == 0) {
                if (item == "3") {
                    ///集团单点
                    self.loginType = .SinglePointLoginType
                } else {
                    /// 自带登录
                    self.loginType = .OldLoginType
                }
//                AccountInfo.sharedInstance().loginType = self.loginType.rawValue
            }
        }
    }
    
    func getChangePasswordHttpCmd(){
        let cmd:HttpCommand = ChangePasswordHttpCmd.init(version: PHttpVersion_v1)
        let block:httpBlock = {[weak self] (result:RequestResult!,useInfo:Any!)->() in
            if result != nil {
                self?.httpChangePasswordHttpResultResponse(result: result)
            }else{
                print("空的")
            }
        }
        let attrDic:NSMutableDictionary = NSMutableDictionary()
        let dic:NSMutableDictionary = NSMutableDictionary()
        dic[kHttpChangePasswordHttpPostCmd_oldPsw] = oldTextView.text
        dic[kHttpChangePasswordHttpPostCmd_password] = newTextView.text
        if self.changePassWordType == .OrdinaryChangePassWordType {
            dic[kHttpChangePasswordHttpPostCmd_userid] = AccountInfo.sharedInstance().UserId
        } else {
            dic[kHttpChangePasswordHttpPostCmd_userid] = AccountInfo.sharedInstance().ChangPassWordUserId
        }
        
        attrDic[kHttpChangePasswordHttpPostCmd_attr] = dic
        cmd.requestInfo = attrDic as [NSObject:AnyObject]
        let completeDelegate = SMBaseHttpComplete.init(block: block , withUserInfo: nil)
        cmd.completeDelegate=completeDelegate
        print("url==%@",cmd.getUrl())
        cmd.execute()
    }
    func httpChangePasswordHttpResultResponse(result:RequestResult){
        let r:ChangePasswordHttpResult = result as! ChangePasswordHttpResult
        if r.isOk() {
            let array:NSMutableArray = r.getChangePasswordHttpResult()
            if array.count == 0{
                SMToastView.showMessage("密码修改成功！")
                AccountInfo.sharedInstance().clearAccount()
                if self.signOutBlock != nil{
                    signOutBlock()
                }
                if self.changePassWordType == .OrdinaryChangePassWordType {
                    self.deAllSelecCompanyDepartment()
                    DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
                        NotificationCenter.default.post(name: Notification.Name(rawValue: "setLoginWindow"), object: "0")
                    }
                } else {
                    arrowResponse()
                }
            }else{
                SMToastView.showMessage((array[0] as! String))
            }
        }else{
            SMToastView.showMessage("请查看当前网络状态！")
        }
    }
    /// 单点登录 修改密码
    func getSinglePointPasswordHttpPostCmd(){
        let cmd:HttpCommand = SinglePointPasswordHttpPostCmd.init(version: PHttpVersion_v1)
        let block:httpBlock = {[weak self] (result:RequestResult!,useInfo:Any!)->() in
            if result != nil {
                self?.httpSinglePointPasswordHttpResultResponse(result: result)
            }else{
                print("空的")
            }
        }
        let dic:NSMutableDictionary = NSMutableDictionary()
        
        dic[kHttpSinglePointPasswordHttpPostCmd_token] = "IDM:P@ssw0rd"
        if self.changePassWordType == .OrdinaryChangePassWordType {
            dic[kHttpSinglePointPasswordHttpPostCmd_username] = AccountInfo.sharedInstance().UserId
        } else {
            dic[kHttpSinglePointPasswordHttpPostCmd_username] = AccountInfo.sharedInstance().ChangPassWordUserId
        }
        dic[kHttpSinglePointPasswordHttpPostCmd_oldPassword] = oldTextView.text
        dic[kHttpSinglePointPasswordHttpPostCmd_newPassword] = newTextView.text
        cmd.requestInfo = dic as [NSObject:AnyObject]
        let completeDelegate = SMBaseHttpComplete.init(block: block , withUserInfo: nil)
        cmd.completeDelegate=completeDelegate
        print("url==%@",cmd.getUrl())
        cmd.execute()
    }
    func httpSinglePointPasswordHttpResultResponse(result:RequestResult){
        let r:SinglePointPasswordHttpResult = result as! SinglePointPasswordHttpResult
        if r.isOk() {
            let array:NSMutableArray = r.getSinglePointPasswordHttpResult()
            if array.count == 0{
                SMToastView.showMessage("密码修改成功！")
                AccountInfo.sharedInstance().clearAccount()
                if self.signOutBlock != nil{
                    signOutBlock()
                }
                if self.changePassWordType == .OrdinaryChangePassWordType {
                    self.deAllSelecCompanyDepartment()
                    DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
                        NotificationCenter.default.post(name: Notification.Name(rawValue: "setLoginWindow"), object: "0")
                    }
                } else {
                    arrowResponse()
                }
            }else{
//                SMToastView.showMessage((array[0] as! String))
            }
        }else{
            SMToastView.showMessage("请查看当前网络状态！")
        }
    }
}
