package com.wd.workoffice.ui.activity.login;

import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;

import com.wd.workoffice.R;
import com.wd.workoffice.retrofit.ApiService;
import com.wd.workoffice.app.BaseBean;
import com.wd.workoffice.app.Config;
import com.wd.workoffice.app.WorkToolBarActivity;
import com.wd.workoffice.retrofit.WorkObserver;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import flexible.xd.android_base.network.rtfhttp.RtfHelper;
import flexible.xd.android_base.network.rtfhttp.Transformer;
import flexible.xd.android_base.network.rtfhttp.observer.BaseObserver;
import flexible.xd.android_base.utils.RegexUtils;
import io.reactivex.disposables.Disposable;

/*
 * 忘记密码第二步
 * */
public class ForgetTwoActivity extends WorkToolBarActivity {


    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.et_confirm_pwd)
    EditText etConfirmPwd;
    @BindView(R.id.btn_login)
    Button btnLogin;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected int layoutId() {
        return R.layout.activity_forget_pwd_two;
    }


    @OnClick(R.id.btn_login)
    public void onViewClicked() {
        String pwd = etPwd.getText().toString();
        String confirmPwd = etConfirmPwd.getText().toString();
        if (!RegexUtils.isMatch(Config.REGEX_PASSWORD, pwd)) {
            toast("设置密码数字或字母６-16位");
            return;
        }
        if (!TextUtils.equals(pwd, confirmPwd)) {
            toast("两次输入的密码不一致");
            return;
        }
        showLoading();
        RtfHelper.getInstance().getApiService(ApiService.class).
                forgetPwd(getIntent().getStringExtra("phone"),
                        getIntent().getStringExtra("code"), pwd).compose(Transformer.schedule())
                .subscribe(new WorkObserver<BaseBean>() {
            @Override
            public void doOnSubscribe(Disposable d) {
            }

            @Override
            public void onFail(String errorMsg) {
                hideLoading();
                toast(errorMsg);
            }

            @Override
            public void onSuccess(BaseBean data) {
                hideLoading();
                if (data.getCode() != 0) {
                    toast(data.getMessage());
                    return;
                }
                toast("修改密码成功");
                startActivity(LoginActivity.class);
            }
        });
    }
}
