//
//  LoginView.swift
//  MobilePortal
//
//  Created by xiejiangbo on 2019/3/25.
//  Copyright © 2019 yin chen. All rights reserved.
//

import UIKit

class LoginView: UIView,UITextFieldDelegate {
   let textViewH = 35*HEIGHT_RATIO()

    lazy var headTitleImage : UIImageView = {
        let headTitleImage = UIImageView.init(frame: CGRect(x:25*WHIDTH_RATIO(), y: HEIGHT_RATIO()*30+NAV_HEIGHT()+STATUBAR_HEIGHT(), width:SCREEN_WHIDTH()-50*WHIDTH_RATIO() , height:HEIGHT_RATIO()*38 ))
        headTitleImage.image = UIImage.init(named: "headTitle_image")
        headTitleImage.contentMode = .scaleAspectFit
        headTitleImage.centerX = SCREEN_WHIDTH()/2
        return headTitleImage
    }()

    lazy var textViewImage : UIImageView = {
        let textViewImage = UIImageView.init(frame: CGRect(x:30*WHIDTH_RATIO(), y: headTitleImage.originY+headTitleImage.height+WHIDTH_RATIO()*10, width:SCREEN_WHIDTH()-60*WHIDTH_RATIO() , height:HEIGHT_RATIO()*180 ))
        //textViewImage.image = UIImage.init(named: "textView_image")
        textViewImage.centerX = SCREEN_WHIDTH()/2
        return textViewImage
    }()

    lazy var accountViewImage : UIImageView = {
        let accountViewImage =  UIImageView.init(frame: CGRect(x:textViewImage.originX, y: textViewImage.originY+WHIDTH_RATIO()*20, width:WHIDTH_RATIO()*20 , height:WHIDTH_RATIO()*18 ))
        accountViewImage.image = UIImage.init(named: "account_image")
        accountViewImage.contentMode = .scaleAspectFit
        return accountViewImage
    }()

    lazy var passViewImage : UIImageView = {
        let passViewImage =  UIImageView.init(frame: CGRect(x:textViewImage.originX, y: textViewLineImage.originY+WHIDTH_RATIO()*13, width:WHIDTH_RATIO()*20 , height:WHIDTH_RATIO()*18 ))
        passViewImage.image = UIImage.init(named: "pass_image")
        passViewImage.centerY = textViewImage.centerY
        passViewImage.contentMode = .scaleAspectFit
        return passViewImage
    }()
    
    lazy var loginButton : UIButton = {
        let loginButton = UIButton.init(type: .custom)
        loginButton.frame = CGRect(x:headTitleImage.originX, y: passwordLineImage.originY+HEIGHT_RATIO()*32, width:headTitleImage.width , height:WHIDTH_RATIO()*40)
        //loginButton.centerX = SCREEN_WHIDTH()/2
        loginButton.setBackgroundImage(UIImage.init(named: "loginBtn_image"), for: .normal)
        loginButton.setTitle("登录", for: .normal)
        loginButton.titleLabel?.font = UIFont.test_systemFont(ofSize: 15.0)
        return loginButton
    }()
    lazy var weixinQQView : WeixinQQView = {
        let weixinQQView = WeixinQQView.init(frame: CGRect(x:0, y: SCREEN_HEIGHT()-150*HEIGHT_RATIO()-HEIGHT_BOTTOM_MARGIN(), width:SCREEN_WHIDTH() , height:HEIGHT_RATIO()*150))
//        weixinQQView.backgroundColor = UIColor.yellow
        return weixinQQView;
    }()
    
    var textViewLineImage:UIImageView!
    var passwordLineImage:UIImageView!
    var advertisingView:UIImageView!
   
    var userTextView:UITextField!
    var passTextView:UITextField!
    //var companyTextView:UITextField!
    
    
    var buttoSignUpNow:UIButton!
    //var buttonRemeberPassword:UIButton!
    //var labelRemeberPassword:UILabel!
    var buttonAutoLogin:UIButton!
    var labelAutoLogin:UILabel!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self .addSubview(headTitleImage)
        addInitTextView()
        self .addSubview(loginButton)
        addMemoryView()
        self.addSubview(weixinQQView)
        //setNamePassWord()
        //initReturnBtns()
        //initCompanyLabel()
    }
    
    override func layoutSubviews() {
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
extension LoginView {
    
    func addInitTextView(){
        
//        self .addSubview(textViewImage)
//        self .addSubview(accountViewImage)
        textViewLineImage = self.getLinViewImage()
        textViewLineImage.frame = CGRect(x:headTitleImage.originX, y: headTitleImage.originY+headTitleImage.height+91*HEIGHT_RATIO(), width:textViewImage.width , height:HEIGHT_RATIO()*1 )
        self .addSubview(textViewLineImage)
        
        print(textViewLineImage.originY)
        userTextView = self.getTextField(placeholder: "请输入账号名称", tag: 100)
        userTextView.frame = CGRect(x:headTitleImage.originX, y: textViewLineImage.originY-textViewH-5*HEIGHT_RATIO(), width:SCREEN_WHIDTH()-headTitleImage.originX*2 , height:textViewH )

        self .addSubview(userTextView)
        
//        self .addSubview(passViewImage)
        passwordLineImage = self.getLinViewImage()
        passwordLineImage.frame = CGRect(x:headTitleImage.originX, y: textViewLineImage.originY+52*HEIGHT_RATIO(), width:textViewLineImage.width , height:HEIGHT_RATIO()*1 )
        self .addSubview(passwordLineImage)
        passTextView = self.getTextField(placeholder: "请输入密码", tag: 101)
        passTextView.frame = CGRect(x:headTitleImage.originX, y: passwordLineImage.originY-textViewH-5*HEIGHT_RATIO(), width:SCREEN_WHIDTH()-headTitleImage.originX*2 , height:textViewH )
//        passTextView.centerY = textViewImage.centerY
        passTextView.isSecureTextEntry = true
        self .addSubview(passTextView)
        
    }
    
   
    func addMemoryView() {
        buttoSignUpNow = UIButton.init(type: .custom)
        buttoSignUpNow.contentMode = .scaleToFill
        buttoSignUpNow.frame = CGRect(x:loginButton.originX, y: loginButton.originY+loginButton.height+HEIGHT_RATIO()*23, width:WHIDTH_RATIO()*120 , height:WHIDTH_RATIO()*20)
        buttoSignUpNow.contentHorizontalAlignment = .left
        buttoSignUpNow.setTitle("注册", for: .normal)
        buttoSignUpNow.titleLabel?.font = UIFont.test_systemFont(ofSize: 12.0)
        buttoSignUpNow.setTitleColor(UIColorFromRGB(rgbValue:0x707985), for: .normal)
        //buttoSignUpNow.addTarget(self, action: #selector(signUpNow), for: .touchUpInside)
//        self .addSubview(buttoSignUpNow)
        
        
        
        buttonAutoLogin = UIButton.init(type: .custom)
        buttonAutoLogin.contentMode = .scaleToFill
        buttonAutoLogin.frame = CGRect(x:SCREEN_WHIDTH()-WHIDTH_RATIO()*120-loginButton.originX, y: buttoSignUpNow.originY, width:WHIDTH_RATIO()*120 , height:WHIDTH_RATIO()*20)
        buttonAutoLogin.contentHorizontalAlignment = .right
        buttonAutoLogin.setTitle("忘记密码", for: .normal)
        buttonAutoLogin.titleLabel?.font = UIFont.test_systemFont(ofSize: 12.0)
        buttonAutoLogin.setTitleColor(UIColorFromRGB(rgbValue:0x707985), for: .normal)
        //buttoSignUpNow.addTarget(self, action: #selector(signUpNow), for: .touchUpInside)
//        self .addSubview(buttonAutoLogin)
        
        
        //        buttonAutoLogin = self.getUIButton()
        //        buttonAutoLogin.frame = CGRect(x:WHIDTH_RATIO()*270, y: buttoSignUpNow.originY, width:WHIDTH_RATIO()*20 , height:WHIDTH_RATIO()*20)
        ////        buttonAutoLogin.addTarget(self, action: #selector(cliright), for: .touchUpInside)
        //        self .addSubview(buttonAutoLogin)
        //        labelAutoLogin = self.getUILabel(str: "忘记密码")
        //        labelAutoLogin.frame = CGRect(x:buttonAutoLogin.originX+WHIDTH_RATIO()*25, y: loginButton.originY+loginButton.height+WHIDTH_RATIO()*10, width:WHIDTH_RATIO()*120 , height:WHIDTH_RATIO()*20)
        //        labelAutoLogin.centerY = buttonAutoLogin.centerY;
        //        self .addSubview(labelAutoLogin)
    }
    
    
    
    func getBoolStr(bool:Bool) -> String {
        if bool {
            return "1"
        }
        return "0"
    }
    func getBool(str:String) -> Bool {
        if str == "1" {
            
            return true
        }
        return false
    }
    
    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.attributedPlaceholder = NSAttributedString.init(string:placeholder, attributes: [NSAttributedStringKey.font:UIFont.test_systemFont(ofSize: 14)])
        //字体颜色
        text.attributedPlaceholder = NSAttributedString.init(string:placeholder, attributes: [
                                                                NSAttributedStringKey.foregroundColor:placeholderColor])
//        text.attributedPlaceholder = NSAttributedString.init(string: placeholder, attributes: [])
        text.clearButtonMode = .whileEditing
        text.delegate = self
        text.font = UIFont.test_systemFont(ofSize: 14)
        //text.backgroundColor = UIColor.red
        return text
    }
    func getUIButton() -> UIButton{
        let btn = UIButton.init(type: .custom)
        btn.contentMode = .scaleToFill
        btn.setBackgroundImage(UIImage.init(named: "normal_image"), for: .normal)
        btn.setBackgroundImage(UIImage.init(named: "selec_image"), for: .selected)
        btn.setBackgroundImage(UIImage.init(named: "selec_image"), for: .highlighted)
        return btn
    }
    
    func getUILabel(str:String) -> UILabel {
        let label = UILabel.init()
        label.text = str
        label.centerY = buttoSignUpNow.centerY
        label.textColor = UIColor.darkGray
        label.font = UIFont.test_systemFont(ofSize: 12.0)
        return label
    }
    // 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 {
            
        }
    }
    
}
