import React, { Component } from "react";
import { Input, Button, Card, message, Form, Icon, Row, Col } from "antd";
import "./AccountLogin.css";
import request from "../../utils/request";
const { TextArea } = Input;
@Form.create()
export default class PhoneRetrieval extends Component {
  state = {
    sequence: "",
    loading: false,
    listData: {},
    duanxin: false,
    Sent: false,
    mailAddress: "",
    yztime: 60,
  };
  onChange = (e) => {
    const { value } = e.target;
    console.log(value);
    this.setState({ sequence: value });
  };

  goMail = () => {
    request
      .post("yszh-login/auth/getMailCaptchCode", {
        mailAddress: this.state.mailAddress,
      })
      .then((res) => {
        console.log(res);
        this.setState({ loading: false });
        if (res.data.code === 200) {
          this.count();
          message.success(res.data.data);
          
        } else {
          message.error(res.data.message);
        }
      })
      .catch((err) => {
        this.setState({ loading: false });
        console.error(err);
      });
  };
  getValueMail = (e) => {
    const { value } = e.target;
    var regExp = new RegExp("^[a-zA-Z0-9]+([-_.][A-Za-zd]+)*@([a-zA-Z0-9]+[-.])+[A-Za-zd]{2,5}$");

    if (regExp.test(value)) {
      console.log(regExp.test(value));
      this.setState({
        duanxin: true,
      });
      this.setState({
        mailAddress: value,
      });
    } else {
      this.setState({
        duanxin: false,
      });
    }
  };
  count = () => {
    this.setState({
      duanxin: false,
      Sent: true,
    });
    let { yztime } = this.state;
    let siv = setInterval(() => {
      this.setState({ yztime: yztime-- }, () => {
        if (yztime <= -1) {
          clearInterval(siv); //倒计时( setInterval() 函数会每秒执行一次函数)，用 clearInterval() 来停止执行:
          this.setState({ duanxin: true, Sent: false, yztime: 59 });
        }
      });
    }, 1000);
  };
  handleSubmit = (e) => {
    e.preventDefault();
    this.setState({
      focusItem: -1,
    });
    this.props.form.validateFields((err, values) => {
      console.log(values);
      const {mailAddress,pwd,checkPwd,captcha}=values

      let data={
        mailAddress:mailAddress,
        pwd:pwd,
        checkPwd:checkPwd,
        captcha: captcha

      }
      if (!err) {
        request
          .post("yszh-login/auth/checkMailCaptcha", data)
          .then((res) => {
            console.log(res);
            this.setState({ loading: false });
            if (res.data.code === 200) {
              
              message.success("修改成功");
              this.props.gobackLogin();
             
            } else {
              message.error(res.data.message);
            }
          })
          .catch((err) => {
            this.setState({ loading: false });
            console.error(err);
          });
      }
    });
  };
  render() {
    const { listData, loading } = this.state;

    const { getFieldDecorator, getFieldError, getFieldValue } = this.props.form;
    const formItemLayout = {
        labelCol: { span: 7 },
        wrapperCol: { span: 17 },
      };

    return (
      <Form onSubmit={this.handleSubmit} layout="vertical">
        <Form.Item label="邮箱地址"{...formItemLayout}>
        {getFieldDecorator("mailAddress", {
              rules: [
                { required: true, message: "邮箱不能为空" },
                { type: "email", message: "请输入正确邮箱地址！" },
              ],
            })(<Input onChange={this.getValueMail} placeholder="请输入邮箱" />)}
        </Form.Item>
        <Form.Item label="新密码" {...formItemLayout}>
            {getFieldDecorator("pwd", {
              validateFirst: true,
              rules: [
                { required: true, message: "密码不能为空" },
                { pattern: "^[^ ]+$", message: "密码不能有空格" },
              ],
            })(<Input type="password" maxLength={16} placeholder="密码" />)}
          </Form.Item>
          <Form.Item label="确认密码" {...formItemLayout}>
            {getFieldDecorator("checkPwd", {
              validateFirst: true,
              rules: [
                { required: true, message: "请确认密码" },
                {
                  validator: (rule, value, callback) => {
                    if (value && value !== getFieldValue("checkPwd")) {
                      callback("两次输入不一致！");
                    }
                    callback();
                  },
                },
              ],
            })(<Input type="password" maxLength={16} placeholder="确认密码" />)}
          </Form.Item>
          <Form.Item label="验证码" {...formItemLayout}>
            {getFieldDecorator("captcha", {
              validateFirst: true,
              rules: [{ required: true, message: "请输入验证码" }],
            })(
              <Row>
                <Col span={11}>
                  <Input maxLength={6} placeholder="验证码" />
                </Col>
                <Col span={13}>
                  <div className="Verification-code">
                    <Button
                      disabled={!this.state.duanxin}
                      type="primary"
                      onClick={this.goMail}
                      style={{ marginLeft: "5px" }}
                    >
                      {this.state.Sent ? "已发送" : "发送邮件"}
                      {this.state.Sent && this.state.yztime}
                    </Button>
                  </div>
                </Col>
              </Row>
            )}
          </Form.Item>
          <Form.Item>
          <Button
            type="primary"
            style={{ width: "100%" }}
            htmlType="submit"
            className="login-form-button"
          >
            确定
          </Button>
        </Form.Item>
   
      </Form>
    );
  }
}
