package com.wd.workoffice.ui.fg;

import android.text.TextUtils;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.wd.workoffice.R;
import com.wd.workoffice.app.WorkBaseFg;
import com.wd.workoffice.bean.OrderBean;
import com.wd.workoffice.bean.WarehoustBean;
import com.wd.workoffice.bean.listener.WarehouseFilterClickListener;
import com.wd.workoffice.contract.WorkWarehouseContract;
import com.wd.workoffice.presenter.WorkWarehousePresenter;
import com.wd.workoffice.ui.activity.bat.work.WorkProDetailActivity;
import com.wd.workoffice.ui.activity.bat.work.WorkWarehouseStockActivity;
import com.wd.workoffice.ui.adapter.WorkWarehouseAdapter;
import com.wd.workoffice.util.WorkUtils;
import com.wd.workoffice.util.ddmfilter.WarehouseFilterData;
import com.wd.workoffice.widget.DropDownMenu;

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

import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class WorkWarehouse1Fragment extends WorkBaseFg implements WorkWarehouseContract.View {

    @BindView(R.id.ddm_choose)
    DropDownMenu ddmChoose;
    private RecyclerView rvData;
    private int page = 1;
    private Map<String, Object> param;
    private SmartRefreshLayout srlRefresh;
    private WorkWarehousePresenter workProPresenter;
    private WorkWarehouseAdapter workProductAdapter;
    private List<WarehoustBean.RecordsBean> dataList;

    @Override
    protected void initView() {
        ButterKnife.bind(this, getContentView());
        workProPresenter = new WorkWarehousePresenter();
        workProPresenter.onAttach(this);
        WarehouseFilterData filterData = new WarehouseFilterData(getActivity(),ddmChoose, new WarehouseFilterClickListener() {
            @Override
            public void clickType(String value) {
                if (TextUtils.equals(value, "全部类型")) {
                    param.put("siteType", "");
                } else if (TextUtils.equals(value, "厂内库")) {
                    param.put("siteType", "IN");
                } else {
                    param.put("siteType", "OUT");
                }
                page = 1;
                param.put("current", page);
                workProPresenter.getWarehouse(param);
            }

            @Override
            public void clickStatus(String value) {
                if (TextUtils.equals(value, "全部状态")) {
                    param.put("status", "");
                } else if (TextUtils.equals(value, "未启用")) {
                    param.put("status", "0");
                } else {
                    param.put("status", "1");
                }
                page = 1;
                param.put("current", page);
                workProPresenter.getWarehouse(param);
            }

        });
        rvData = filterData.getRvData();
        srlRefresh = filterData.getSrlRefresh();
    }


    @Override
    protected void initData() {
        param = WorkUtils.pageKey();
        page = 1;
        param.put("current", page);
        dataList = new ArrayList<>();
        workProductAdapter = new WorkWarehouseAdapter(R.layout.item_work_warehouse, dataList);
        workProductAdapter.bindToRecyclerView(rvData);
        workProductAdapter.setEmptyView(R.layout.view_empty_content, rvData);
        workProPresenter.getWarehouse(param);
    }


    @Override
    protected void initEvent() {
        srlRefresh.setOnRefreshListener(refreshLayout -> {
            page = 1;
            param.put("current", page);
            workProPresenter.getWarehouse(param);
        });
        workProductAdapter.setOnLoadMoreListener(() -> {
            param.put("current", page);
            workProPresenter.getWarehouse(param);
        }, rvData);
        workProductAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(WorkWarehouseStockActivity.class,
                        "id", dataList.get(position).getId()+"",
                        "name",dataList.get(position).getName());
            }
        });
    }


    @Override
    protected int layoutId() {
        return R.layout.fg_work_warehouse;
    }



    @Override
    public void getWarehouse(List<WarehoustBean.RecordsBean> data) {
        if (page == 1) {
            dataList.clear();
            dataList.addAll(data);
            workProductAdapter.notifyDataSetChanged();
            workProductAdapter.loadMoreComplete();
            srlRefresh.finishRefresh();
        } else {
            workProductAdapter.loadMoreComplete();
            dataList.addAll(data);
            workProductAdapter.notifyDataSetChanged();
        }
        if (data.size() == 0) {
            workProductAdapter.loadMoreEnd();
        } else {
            page++;
        }
    }

    @Override
    public void getOrder(List<OrderBean> dataList) {

    }
}
