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

import UIKit

extension ChangePassWordViewController {
    func addInitTextView(){
        let textViewH = 55*HEIGHT_RATIO()
        textViewImage = UIImageView.init(frame: CGRect(x:15*WHIDTH_RATIO(), y: HEIGHT_RATIO()*28+STATUBAR_HEIGHT()+NAV_HEIGHT(), width:SCREEN_WHIDTH()-30*WHIDTH_RATIO() , height:HEIGHT_RATIO()*120 ))
        //textViewImage.image = UIImage.init(named: "textView_image")
        textViewImage.centerX = SCREEN_WHIDTH()/2
        //textViewImage.backgroundColor = UIColor.red
        self.view .addSubview(textViewImage)
        
        // 老密码
        oldViewImage = UIImageView.init(frame: CGRect(x:textViewImage.originX, y: textViewImage.originY+WHIDTH_RATIO()*17, width:WHIDTH_RATIO()*15 , height:WHIDTH_RATIO()*24 ))
        oldViewImage.contentMode = .scaleAspectFit
        oldViewImage.image = UIImage.init(named: "pass_image")
        self.view .addSubview(oldViewImage)
        oldTextView = self.getTextField(placeholder: "请输入原密码", tag: 100)
        oldTextView.frame = CGRect(x:oldViewImage.originX+oldViewImage.width+WHIDTH_RATIO()*15, y: textViewImage.originY+WHIDTH_RATIO()*5, width:textViewImage.width-(oldViewImage.width+WHIDTH_RATIO()*15) , height:textViewH )
        oldViewImage.centerY = oldTextView.centerY
        self.view .addSubview(oldTextView)
        textViewLineImage = self.getLinViewImage()
        textViewLineImage.frame = CGRect(x:oldViewImage.originX, y: oldViewImage.originY+oldViewImage.height+HEIGHT_RATIO()*17, width:textViewImage.width , height:HEIGHT_RATIO()*1 )
        self.view .addSubview(textViewLineImage)
        
        // 老密码
        newViewImage = UIImageView.init(frame: CGRect(x:textViewImage.originX, y: textViewLineImage.originY+WHIDTH_RATIO()*17, width:WHIDTH_RATIO()*15 , height:WHIDTH_RATIO()*24 ))
        newViewImage.contentMode = .scaleAspectFit
        newViewImage.image = UIImage.init(named: "pass_image")
        self.view .addSubview(newViewImage)
        newTextView = self.getTextField(placeholder: "请输入新密码", tag: 100)
        newTextView.frame = CGRect(x:newViewImage.originX+newViewImage.width+WHIDTH_RATIO()*15, y: textViewImage.originY+WHIDTH_RATIO()*5, width:textViewImage.width-(newViewImage.width+WHIDTH_RATIO()*15) , height:textViewH )
        newTextView.centerY = newViewImage.centerY
        self.view .addSubview(newTextView)
        newViewLineImage = self.getLinViewImage()
        newViewLineImage.frame = CGRect(x:newViewImage.originX, y: newViewImage.originY+newViewImage.height+HEIGHT_RATIO()*17, width:textViewImage.width , height:HEIGHT_RATIO()*1 )
        self.view .addSubview(newViewLineImage)
        
        
        
        //验证码
        verificationCodeViewImage = UIImageView.init(frame: CGRect(x:textViewImage.originX, y: newViewLineImage.originY+WHIDTH_RATIO()*17, width:WHIDTH_RATIO()*15 , height:WHIDTH_RATIO()*24 ))
        verificationCodeViewImage.image = UIImage.init(named: "pass_image")
        verificationCodeViewImage.contentMode = .scaleAspectFit
        self.view .addSubview(verificationCodeViewImage)
        confirmPassTextView = self.getTextField(placeholder: "请确认密码", tag: 101)
        confirmPassTextView.frame = CGRect(x:verificationCodeViewImage.originX+verificationCodeViewImage.width+WHIDTH_RATIO()*15, y: newViewLineImage.originY+HEIGHT_RATIO()*1, width:oldTextView.width , height:textViewH )
        //confirmPassTextView.backgroundColor = UIColor.yellow
        verificationCodeViewImage.centerY = confirmPassTextView.centerY
        self.view .addSubview(confirmPassTextView)
        
        verificationCodeLineImage = self.getLinViewImage()
        verificationCodeLineImage.frame = CGRect(x:verificationCodeViewImage.originX, y: confirmPassTextView.originY+confirmPassTextView.height+HEIGHT_RATIO()*1, width:textViewLineImage.width , height:HEIGHT_RATIO()*1 )
        self.view .addSubview(verificationCodeLineImage)
        
//        oldTextView.text = "P@ssw0rd"
//        newTextView.text = "P@ssw0rd"
//        confirmPassTextView.text = "P@ssw0rd"
    }
    // 添加登录按钮
    func addInitLoginButton() {
        changePassWordButton = UIButton.init(type: .custom)
        changePassWordButton.frame = CGRect(x:(SCREEN_WHIDTH()-verificationCodeLineImage.width)/2, y: verificationCodeLineImage.originY+HEIGHT_RATIO()*50, width:verificationCodeLineImage.width , height:WHIDTH_RATIO()*35)
        //loginButton.centerX = SCREEN_WHIDTH()/2
        changePassWordButton.setBackgroundImage(UIImage.init(named: "loginBtn_image"), for: .normal)
        changePassWordButton.setTitle("确认", for: .normal)
        changePassWordButton.titleLabel?.font = UIFont.systemFont(ofSize: 14.0)
        changePassWordButton.addTarget(self, action: #selector(actionChangePassWord), for: .touchUpInside)
        self.view .addSubview(changePassWordButton)
        
        //验证码
        redImage = UIImageView.init(frame: CGRect(x:changePassWordButton.originX, y: changePassWordButton.originY+changePassWordButton.height+WHIDTH_RATIO()*17, width:WHIDTH_RATIO()*15 , height:WHIDTH_RATIO()*24 ))
        redImage.image = UIImage.init(named: "red_image")
        redImage.contentMode = .scaleAspectFit
        self.view .addSubview(redImage)
        
        redLabel = UILabel.init(frame: CGRect(x:redImage.originX+redImage.width+10, y: redImage.originY, width:SCREEN_WHIDTH()-(redImage.originX+redImage.width+30) , height:30 ))
        redLabel.text = "用户密码必须包含字母、数字、特殊字符(列如：!@#%&*.)长度不得小于8位且不超过16位"
        redLabel.textColor = UIColor.red
        redLabel.font = UIFont.test_systemFont(ofSize: 11.33)
        redLabel.numberOfLines = 0
        self.view .addSubview(redLabel)
    }
    
    
    //获取分界线
    func getLinViewImage() -> UIImageView{
        let lineImageView = UIImageView.init()
        lineImageView.image = UIImage.init(named: "textViewLine_image")
        //lineImageView.centerX = SCREEN_WHIDTH()/2
        return lineImageView
    }
    func getTextField(placeholder:String,tag:NSInteger) -> UITextField {
        let text = UITextField.init()
        text.placeholder = placeholder
        text.tag = tag
        text.clearButtonMode = .whileEditing
        text.delegate = self
        text.backgroundColor = UIColor.clear
        text.isSecureTextEntry = true
        return text
    }
    //点击登录
    @objc func actionChangePassWord() {
        oldTextView.resignFirstResponder()
        confirmPassTextView.resignFirstResponder()
        if oldTextView.text == nil || oldTextView.text?.count == 0 {
            SMToastView.showMessage("原密码不能为空！")
            return
        }
        
        if newTextView.text == nil || newTextView.text?.count == 0 {
            SMToastView.showMessage("新密码为空！")
            return
        }
        if self.loginType == .OldLoginType && newTextView.text == "123456" {
            SMToastView.showMessage("新密码为初始密码！")
            return
        }
        if confirmPassTextView.text == nil || confirmPassTextView.text?.count == 0 {
            SMToastView.showMessage("确认密码为空！")
            return
        }
        if !(newTextView.text == confirmPassTextView.text){
            SMToastView.showMessage("确认密码有误！")
            return
        }
        if self.loginType == .OldLoginType {
            getChangePasswordHttpCmd()
        } else {
            getSinglePointPasswordHttpPostCmd()
        }
        
//        if AccountInfo.sharedInstance().loginType == "1" {
//            getSinglePointPasswordHttpPostCmd()
//        } else {
//            getChangePasswordHttpCmd()
//        }
        
    }
    //取消键盘响应
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        oldTextView.resignFirstResponder()
        confirmPassTextView.resignFirstResponder()
    }
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        oldTextView.resignFirstResponder()
        confirmPassTextView.resignFirstResponder()
    }
    // MARK:UITextField 代理方法
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
    func textFieldDidEndEditing(_ textField: UITextField) {
        print("textField.tag2=="+String(textField.tag))
        if textField.tag == 100 {
            //getDepartmentListHttp(str: textField.text!)
        }
    }
    
}
