import React from 'react';
import {connect,routerRedux} from 'dva';
import {Button, Input} from "antd";
import styles from './style.less';
import {ConnectState} from "@/models/connect";
import {router} from "umi";

class AccountReset extends React.Component {

  //构造
  constructor(props) {
    super(props);
    this.state = {
      pw: '',
      pw2: '',
      newPw: '',
    }
  }

  /*-----------------   方法集  ------------------*/
  //跳转到账户页面
  back = () => {
    this.props.dispatch(routerRedux.push({
      pathname: '/account',
    }))
  };
  //两次密码校对
  checkPw = () => {
    if (this.state.pw == '') {
      alert("未输入原密码!");
      return;
    }
    if (this.state.pw2 == '') {
      alert("未再次输入密码!");
      return;
    }
    if (this.state.pw != this.state.pw2) {
      alert("两次输入的原始密码不同!");
      return;
    } else if (this.state.newPw != ''){
      this.checkUserPw();
    }else{
      alert("未输入新密码!");
      return;
    }
  };
  //检测密码
  checkUserPw = () => {
    const params = {
      username: this.props.currentUser.name,
      password: this.state.pw,
    };
    this.props.dispatch({
      type: 'user/checkUserPw',
      payload: params,
      callback: (res) => {
        if (res) {
          if(res.error_code=="0000"){
            this.handleOk();
          }else if(res.error_code == "0001"){
            alert(res.error_msg);
          }else{
            alert("密码校验失败！未知错误！")
          }
        }
      }
    });
  };
  handleOk = () => {
    const params = {
      userStatus: 1,//暂时写死
      userID: this.props.currentUser.userid,
      userPassword: this.state.newPw,
      oneself: 1,
      // userAccount: this.props.currentUser.name,
    };
    this.props.dispatch({
      type: 'accountModel/saveAccount',
      payload: params,
      callback: (res) => {
        console.log(res);
        if (res.error_code == "0000") {
          alert(res.error_msg);
          this.back();
        } else if (res.error_code == "0001") {
          alert(res.error_msg);
          this.back();
        } else if (res.error_code == "0002"){
          alert(res.error_msg);
        } else if(res.error_code == "0008"){
          alert(res.error_msg);
          window.location.href = '/';
          router.replace('/');
        } else {
          alert("提交失败！未知错误!");
        }
      }
    });
  };
  //密码变更
  pwChange = (e) => {
    this.setState({pw: e.target.value});
  };
  //确认密码变更
  pwChange2 = (e) => {
    this.setState({pw2: e.target.value});
  };
  //新密码变更
  newPwChange = (e) => {
    this.setState({newPw: e.target.value});
  };
  //提交
  onSubmit = () => {
    this.checkPw();
  };
  //初始化
  componentDidMount() {}
  /*--------------------------------------*/

  //渲染
  render() {
    const {} = this.state;

    return (
      <div className={styles.body}>
        <div style={{marginTop: 48}}>
          <div className={styles.detailsBox}>
            <div>
              <span className={styles.detailsContent}>原始密码</span>
              <Input.Password className={styles.input}
                     onChange={(e) => this.pwChange(e)}/>
            </div>
            <div>
              <span className={styles.detailsContent}>新密码</span>
              <Input.Password className={styles.input}
                     onChange={(e) => this.newPwChange(e)}/>
            </div>
            <div>
              <span className={styles.detailsContent}>重复密码</span>
              <Input.Password className={styles.input}
                     onChange={(e) => this.pwChange2(e)}/>
            </div>
            <div>
              <Button className={styles.button1} onClick={() => this.onSubmit()}>提交</Button>
              <Button className={styles.button1} onClick={() => this.back()}>返回</Button>
            </div>
          </div>
        </div>
      </div>
    );
  }
}

export default connect(({user}: ConnectState) => ({
  currentUser: user.currentUser,
}))(AccountReset);
