package com.unis.sxzcl.ui.quantity_quality;

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.HouseModel;
import com.unis.model.PermitConst;
import com.unis.model.QuantityQualityModel;
import com.unis.model.UserModel;
import com.unis.model.bean.QuantityQualityBean;
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;


/**
 * 库存质量 出库质量 列表页
 * 创建人: mQ
 * 时间: 2021/2/1 15:46
 */
public class QuantityQualityListFragment extends BaseListFragment<FragmentBaseListBinding> implements QuantityQualityListAdapter.Callback {

    /*********************** 传递参数的key **************************/
    // 检查类型id
    public static final String key_check_type = "check_type";
    // 业务类型
    public static final String key_work_type = "work_type";

    private String mCheckType;
    private String mWorkType;

    private QuantityQualityListAdapter mAdapter;


    public static QuantityQualityListFragment newFragment(String checkType, String workType) {
        Bundle bundle = new Bundle();
        bundle.putString(key_check_type, checkType);
        bundle.putString(key_work_type, workType);
        QuantityQualityListFragment fragment = new QuantityQualityListFragment();
        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();
        mCheckType = bundle.getString(key_check_type);
        mWorkType = bundle.getString(key_work_type);

        // 初始化列表适配器
        mAdapter = new QuantityQualityListAdapter();
        mAdapter.setCallback(this);
        initBaseListView(mBinding.refreshLayout, mBinding.recyclerView, mAdapter, true);
    }

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

    @Override
    protected void onFirstShow() {
        // 首次加载刷新数据
        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++;
        QuantityQualityModel.get().getList(mWorkType,
                DepotModel.get().getSelectDepot().depotCode,
                HouseModel.get().getSelectHouse().houseCode,
                mCheckType, page, mDataCallback);
    }

    /**
     * 获取数据回调
     */
    private DataCallback mDataCallback = new DataCallback<List<QuantityQualityBean>>() {
        @Override
        public void onSuccess(List<QuantityQualityBean> 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();
        }
    };

    /**
     * 接收eventbus 刷新数据
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(QuantityQualityBean event) {
        if (!isFirstLoad)// 页面已加载
            // 刷新页面
            onRefresh();
    }

    /**
     * 列表点击事件
     */
    @Override
    public void onItemClickListener(QuantityQualityBean item) {
        // 检测查看权限
        if (mWorkType.equals("0") && !UserModel.get().containPermit(PermitConst.quantity_read_single))
            return;
        if (mWorkType.equals("1") && !UserModel.get().containPermit(PermitConst.out_read_single))
            return;

        // 状态为1时 可修改
        String pageType = item.getSaveState() != null || item.getSaveState().equals("1") ?
                "edit" : "detail";
        if (mWorkType.equals("0") && !UserModel.get().containPermit(PermitConst.quantity_update)) {
            // 库存无权限修改 只能查看
            pageType = "detail";
        }
        if (mWorkType.equals("1") && !UserModel.get().containPermit(PermitConst.out_update)) {
            // 出库无权限修改 只能查看
            pageType = "detail";
        }
        Bundle bundle = new Bundle();
        bundle.putString(QuantityQualityDetailFragment.key_page_type, pageType);
        bundle.putString(QuantityQualityDetailFragment.key_info, getGson().toJson(item));
        navigation(R.id.quantityQualityDetailFragment, bundle);
    }
}
