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.model.Constant;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author ZhuYongKang
 * @description 增加删除工号
 * @date 2019/8/2
 */
public class ChangeUserFragment extends BaseMVPFragment implements IUserView{

    public static final int TYPE_ADD = 0;
    public static final int TYPE_DEL = 1;
    private int mType;

    @InjectPresenter
    UserPresent mPresenter;

    @BindView(R.id.et_user)
    EditText etUser;

    public static ChangeUserFragment newInstance(int type) {
        Bundle args = new Bundle();
        args.putInt(Constant.KEY_TYPE, type);
        ChangeUserFragment fragment = new ChangeUserFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @OnClick(R.id.bt_confirm)
    public void changeUser() {
        String user = etUser.getText().toString().trim();
        if (TextUtils.isEmpty(user)) {
            etUser.setError("操作员工号不能为空");
            return;
        }
        if (mType == TYPE_ADD) {
            mPresenter.addUser(user);
        } else if (mType == TYPE_DEL) {
            mPresenter.delUser(user);
        }
    }

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

    @Override
    public void onCreateViewFinished(Bundle state) {
        super.onCreateViewFinished(state);
        if (getArguments() != null) {
            mType = getArguments().getInt(Constant.KEY_TYPE);
        }
    }

    @Override
    public void onSuccess(Object object) {
        dismissLoading();
        String message = (String) object;
        ToastUtils.showShort(message);
    }

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

}
