package com.genlot.ninglottery.module.function.user;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;


import com.blankj.utilcode.util.ToastUtils;
import com.genlot.ninglottery.R;
import com.genlot.ninglottery.base.BaseMVPFragment;
import com.genlot.ninglottery.base.InjectPresenter;
import com.genlot.ninglottery.game.GameManager;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author ZhuYongKang
 * @description 修改密码
 * @date 2019/8/2
 */
public class ChangePasswordFragment extends BaseMVPFragment implements IUserView{

    @BindView(R.id.et_user_id)
    EditText etUserId;
    @BindView(R.id.et_old_password)
    EditText etOldPassword;
    @BindView(R.id.et_new_password)
    EditText etNewPassword;
    @BindView(R.id.et_confirm_password)
    EditText etConfirmPassword;

    @InjectPresenter
    UserPresent mPresenter;

    private boolean isAdmin;

    @OnClick(R.id.bt_confirm)
    public void changePassword() {
        String userId = etUserId.getText().toString().trim();
        String oldPass = etOldPassword.getText().toString().trim();
        String newPass = etNewPassword.getText().toString().trim();
        String confirmPass = etConfirmPassword.getText().toString().trim();
        if (TextUtils.isEmpty(userId)) {
            etUserId.setError("工号不能为空");
            return;
        }
        if (TextUtils.isEmpty(oldPass)) {
            etOldPassword.setError("旧密码不能为空");
            return;
        }
        if (TextUtils.isEmpty(newPass)) {
            etNewPassword.setError("新密码不能为空");
            return;
        }
        if (TextUtils.isEmpty(confirmPass)) {
            etConfirmPassword.setError("确认密码不能为空");
            return;
        }
        if (!newPass.equals(confirmPass)) {
            etConfirmPassword.setError("输入密码不一致");
            return;
        }
        if (!userId.equals(GameManager.getUser()) && !isAdmin) {
            etUserId.setError("操作员没有权限修改他人密码");
            return;
        }
        if (!oldPass.equals(GameManager.getPassword()) && !isAdmin) {
            etOldPassword.setError("旧密码输入不正确");
            return;
        }
        mPresenter.changePassword(userId, oldPass, newPass);
    }

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_change_password;
    }

    @Override
    public void onCreateViewFinished(Bundle state) {
        super.onCreateViewFinished(state);
        if ("9".equals(GameManager.getUser()) || "0".equals(GameManager.getUser())) {
            isAdmin = true;
        }
        isAdmin = false;
    }

    @Override
    public void onSuccess(Object object) {
        dismissLoading();
        ToastUtils.showShort("修改成功!");
        GameManager.setPassword((String)object);
    }

    @Override
    public void onFailure(String message) {
        dismissLoading();
        ToastUtils.showShort(message);
    }
}
