package com.unis.sxzcl.ui.grain_check;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.unis.model.DataCallback;
import com.unis.model.DepotModel;
import com.unis.model.GrainCheckModel;
import com.unis.model.HouseModel;
import com.unis.model.UserModel;
import com.unis.model.bean.grain_check.GrainCheckBean;
import com.unis.model.event.GrainCheckEvent;
import com.unis.sxzcl.R;
import com.unis.sxzcl.base.BaseListFragment;
import com.unis.sxzcl.databinding.FragmentBaseListBinding;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

/**
 * 包名: com.unis.sxzcl.ui.grain_check
 * 类名: GrainCheckListFragment
 * 作用描述:
 * 创建人: mQ
 * 创建时间: 2020/12/10 18:37
 * 更新人:
 * 更新时间: 2020/12/10 18:37
 * 更新说明:
 * 版本号: 1.0
 */
public class GrainCheckListFragment extends BaseListFragment<FragmentBaseListBinding> implements GrainCheckListAdapter.Callback {

    private GrainCheckListAdapter mAdapter;
    private String mTab;
    private Integer mUid;

    public static GrainCheckListFragment newFragment(String tab) {
        Bundle bundle = new Bundle();
        bundle.putString("tab", tab);
        GrainCheckListFragment fragment = new GrainCheckListFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

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

    @Override
    protected void init() {
        EventBus.getDefault().register(this);
        Bundle bundle = getArguments();
        mTab = bundle.getString("tab");

        mAdapter = new GrainCheckListAdapter();
        mAdapter.setCallback(this);
        initBaseListView(mBinding.refreshLayout, mBinding.recyclerView, mAdapter, true);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void onFirstShow() {
        mUid = UserModel.get().getUser().getUid();

        onRefresh();
    }

    @Override
    public void onRefreshData() {
        getData(true);
    }

    @Override
    public void onMoreData() {
        super.onMoreData();
        getData(false);
    }

    private int page = 1;

    public void getData(boolean refresh) {
        if (refresh)
            page = 1;
        else
            page++;

        GrainCheckModel.get().getList(mTab, page, mUid, new DataCallback<List<GrainCheckBean>>() {
            @Override
            public void onSuccess(List<GrainCheckBean> data) {
                if (!isActive()) return;

                if (page == 1) {
                    mAdapter.setList(data);
                } else {
                    mAdapter.addData(data);
                }
                if (data.size() == 0) {
                    loadEnd();
                } else {
                    loadComplete();
                }
            }

            @Override
            public void onError(String error) {
                super.onError(error);
                if (!isActive()) return;
                loadError();
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(GrainCheckEvent event) {
//        if (event.tab != null && event.tab == mTab)
        if (!isFirstLoad)// 页面已加载
            // 刷新页面
            onRefresh();
    }

    @Override
    public void onItemClickListener(GrainCheckBean item) {
        // 保管员记录 跳转编辑/详情
        Bundle bundle = new Bundle();
        bundle.putString("tab", mTab);
        // 0进行中(可编辑)  1已完成(不可编辑)
        if (item.recordStatus != null && item.recordStatus.equals("1")) {
            bundle.putString("pageType", "detail");
        } else {
            bundle.putString("pageType", "edit");
        }
        bundle.putString("data", getGson().toJson(item));
        navigation(R.id.grainCheckDetailFragment, bundle);
    }
}
