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

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.genlot.nativesdk.model.OpBean;
import com.genlot.ninglottery.R;
import com.genlot.ninglottery.base.BaseMVPFragment;
import com.genlot.ninglottery.base.InjectPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author ZhuYongKang
 * @description 查询工号
 * @date 2019/8/2
 */
public class QueryUserFragment extends BaseMVPFragment implements IUserView{

    private QueryUserAdapter mAdapter;
    ArrayList<OpBean> mList;

    @InjectPresenter
    UserPresent mPresenter;

    @BindView(R.id.rv_users)
    RecyclerView rvUsers;

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

    @Override
    public void onCreateViewFinished(Bundle state) {
        super.onCreateViewFinished(state);
        mList = new ArrayList<>();
        rvUsers.setLayoutManager(new LinearLayoutManager(getActivity()));
        mAdapter = new QueryUserAdapter(R.layout.item_query_user, mList);
        rvUsers.setAdapter(mAdapter);
        mPresenter.queryUsers();
    }

    @Override
    @SuppressWarnings("unchecked")
    public void onSuccess(Object object) {
        dismissLoading();
        mList = (ArrayList<OpBean>) object;
        activity.runOnUiThread(() -> mAdapter.replaceData(mList));
    }

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

    class QueryUserAdapter extends BaseQuickAdapter<OpBean, BaseViewHolder> {

        QueryUserAdapter(int layoutResId, @Nullable List<OpBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(@NonNull BaseViewHolder helper, OpBean item) {
            helper.setText(R.id.user_number, item.OpID);
            helper.setText(R.id.user_level, item.OpLevel);
            helper.setText(R.id.user_password, item.OpPassword);
            helper.setText(R.id.user_name, item.OpName);
        }
    }
}
