package com.unis.sxzcl.ui.user;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.unis.model.DataCallback;
import com.unis.model.UserModel;
import com.unis.sxzcl.R;
import com.unis.sxzcl.base.BaseFragment;
import com.unis.sxzcl.databinding.FragmentChangePwdBinding;
import com.xyxsbj.mylibrary.tool.ETTool;
import com.xyxsbj.mylibrary.tool.StringTool;
import com.xyxsbj.mylibrary.tool.ToastTool;

/**
 * 修改密码
 * 创建人: mQ
 * 创建时间: 2021/3/18 9:44
 */
public class ChangePwdFragment extends BaseFragment<FragmentChangePwdBinding> {

    @Override
    protected FragmentChangePwdBinding getViewBinding(LayoutInflater inflater, ViewGroup container) {
        return FragmentChangePwdBinding.inflate(inflater, container, false);
    }

    @Override
    protected void init() {
        mBinding.bar.setTitle("修改密码")
                .setLeftClickListener(v -> onBack());
        mBinding.btnChange.setOnClickListener(v -> onChangePwd());
    }

    @Override
    protected void onFirstShow() {

    }

    private void onChangePwd() {
        String pwd1 = ETTool.getData(mBinding.etNewPwd1);
        if (StringTool.isEmpty(pwd1)) {
            ToastTool.showLong("请输入密码");
            return;
        }
        String pwd2 = ETTool.getData(mBinding.etNewPwd2);
        if (StringTool.isEmpty(pwd2)) {
            ToastTool.showLong("请再次输入密码");
            return;
        }
        if (!pwd1.equals(pwd2)) {
            ToastTool.showLong("两次密码不一致");
            return;
        }

        showProgress("正在修改密码");
        UserModel.get().updatePwd(pwd2, new DataCallback<String>() {
            @Override
            public void onSuccess(String data) {
                hideProgress();
                ToastTool.showLong("密码修改成功");
                ETTool.closeSoftKeybord(mBinding.etNewPwd1);
                popBackStack(R.id.userFragment, false);
            }

            @Override
            public void onError(String error) {
                super.onError(error);
                hideProgress();
            }
        });

    }
}
