import React from 'react';
import ajax from 'jquery/src/ajax/xhr.js'
import $ from 'jquery/src/ajax';
import { Modal, Button, Icon, Input, message } from 'antd';
import '../account/set/changeModal.less';

const ChangePw = React.createClass({
    getInitialState() {
        return {
            forgetPw: false,
            setPw: false,
            getCodeButton: false,
            checkMobile: false
        };
    },
    showModal() {
        this.setState({
            forgetPw: true,
        });
    },
    forgetPwOk() {
        if (!this.state.checkMobile) {
            message.warning('请输入正确的手机号码！');
        } else if (this.state.checkMobile) {
            $.ajax({
                method: "POST",
                dataType: "json",
                crossDomain: false,
                url: globalConfig.context + "/api/user/checkMCode",
                data: {
                    "mobile": this.state.mobile,
                    "mobileCode": this.state.mcode
                }
            }).done(function (data) {
                if (!data.error.length) {
                    this.setState({
                        forgetPw: false,
                        setPw: true,
                    });
                } else {
                    message.warning(data.error[0].message);
                }
            }.bind(this));
        };
    },
    setPwOk() {
        if (this.state.newPw.length < 6 || this.state.newPw.length > 20) {
            message.warning('请使用长度为6-20位的密码！');
        } else if (this.state.confirmPw !== this.state.newPw) {
            message.warning('两次密码输入不相同！');
        } else if (this.state.confirmPw === this.state.newPw) {
            $.ajax({
                method: "POST",
                dataType: "json",
                crossDomain: false,
                url: globalConfig.context + "/api/user/pwd",
                data: {
                    "mobile": this.state.mobile,
                    "type": this.props.theType,
                    "newPwd": this.state.newPw,
                    "resetCode": this.state.uuid
                }
            }).done(function (data) {
                if (!data.error.length) {
                    message.success('修改成功！');
                    this.handleCancel;
                } else {
                    message.warning(data.error[0].message);
                }
            }.bind(this));
        };
    },
    handleCancel(e) {
        this.setState({
            forgetPw: false,
            setPw: false,
        });
    },
    newMobileChange(e) {
        this.state.mobile = e.target.value;
        if (/^1[34578]\d{9}$/.test(this.state.mobile)) {
            this.state.checkMobile = true
        }
    },
    mcodeChange(e) {
        this.state.mcode = e.target.value;
    },
    newPwChange(e) {
        this.state.newPw = e.target.value;
    },
    confirmPwChange(e) {
        this.state.confirmPw = e.target.value;
    },
    getMCode() {
        let _me = this;
        if (!this.state.checkMobile) {
            message.warning('请输入正确的手机号码！');
        } else if (this.state.checkMobile) {
            $.ajax({
                method: "get",
                dataType: "json",
                url: globalConfig.context + "/open/getMCode",
                data: {
                    "mobile": this.state.mobile,
                    "type": this.props.theType,
                    "sign": true
                },
                success: function (data) {
                    if (data.error.length == 0) {
                        message.success('验证码发送成功!');
                        this.state.uuid = data.data;
                        this.setState({
                            getCodeButton: true
                        });
                        let interval = setInterval(function () {
                            _me.setState({
                                getCodeButton: false
                            });
                            clearInterval(interval);
                        }, 60000);
                    } else {
                        message.warning(data.error[0].message);
                    }
                }.bind(this),
            });
        }
    },
    render() {
        return (
            <div style={{ 'float': 'right', 'marginTop': '0' }}>
                <a className="login-form-forgot" onClick={this.showModal}>忘记密码</a>
                <Modal key='1' className="change-modal" title="忘记密码" visible={this.state.forgetPw}
                    onOk={this.forgetPwOk} onCancel={this.handleCancel}
                    width={360}
                    footer={[
                        <Button key="submit" type="primary" size="large" loading={this.state.loading} onClick={this.forgetPwOk}>确认</Button>,
                        <Button key="back" type="ghost" size="large" onClick={this.handleCancel}>取消</Button>
                    ]}
                    >
                    <p><span>手机号码：</span><Input onChange={this.newMobileChange} /></p>
                    <p>
                        <span>验证码：</span>
                        <Input className="mixwidth" onChange={this.mcodeChange} />
                        <Button key="getCodeBtn" onClick={this.getMCode} disabled={this.state.getCodeButton}>获取验证码</Button>
                    </p>
                </Modal>
                <Modal key='2' className="change-modal" title="设置新密码" visible={this.state.setPw}
                    onOk={this.setPwOk} onCancel={this.handleCancel}
                    width={360}
                    footer={[
                        <Button key="submit" type="primary" size="large" loading={this.state.loading} onClick={this.setPwOk}>确认</Button>,
                        <Button key="back" type="ghost" size="large" onClick={this.handleCancel}>取消</Button>
                    ]}
                    >
                    <p className="modal-intro"><Icon type="info-circle" />密码长度为6-20个字符，区分大小写</p>
                    <p><span>新密码：</span><Input type="password" onChange={this.newPwChange} /></p>
                    <p><span>确认密码：</span><Input type="password" onChange={this.confirmPwChange} /></p>
                </Modal>
            </div>
        );
    },
});

export default ChangePw;