package com.wd.workoffice.ui.fg;

import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.wd.workoffice.R;
import com.wd.workoffice.app.WorkBaseFg;
import com.wd.workoffice.bean.OrderBean;
import com.wd.workoffice.bean.workEnum.PagePermissionType;
import com.wd.workoffice.contract.SaleSaleContract;
import com.wd.workoffice.presenter.SaleSalePresenter;
import com.wd.workoffice.ui.activity.bat.order.SupplierOrderDetailActivity;
import com.wd.workoffice.ui.adapter.ProSupplierAdapter;
import com.wd.workoffice.util.WorkUtils;

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

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;


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

    @BindView(R.id.rv_data)
    RecyclerView rvData;
    private String state;
    private SaleSalePresenter saleSalePresenter;
    private Map<String, Object> param;
    private List<OrderBean> orderList;
    private ProSupplierAdapter orderAdapter;
    private int page = 1;
    private String identity;
    @Override
    protected void initView() {
        ButterKnife.bind(this, getContentView());
        saleSalePresenter = new SaleSalePresenter();
        saleSalePresenter.onAttach(this);
        rvData.setLayoutManager(new LinearLayoutManager(getActivity(), RecyclerView.VERTICAL, false));
    }


    @Override
    protected void initData() {
        orderList = new ArrayList<>();
        orderAdapter = new ProSupplierAdapter(R.layout.item_sale_supplier, orderList);
        orderAdapter.bindToRecyclerView(rvData);
        param = WorkUtils.pageKey();
        param.put("orderStatus", state);
        param.put("current", page);
        param.put("identity",  identity);
        page = 1;
        param.put("current", page);
        saleSalePresenter.getSupplierOrder(param);
    }


    @Override
    protected void initEvent() {
        orderAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                param.put("current", page);
                saleSalePresenter.getSupplierOrder(param);
            }
        }, rvData);
        orderAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (!WorkUtils.hasPermission(PagePermissionType.FO_SUPPLIER_LIST.getPermission())){
                    toast(R.string.permission);
                    return;
                }
                startActivity(SupplierOrderDetailActivity.class,
                        "identity",identity,
                        "id", orderList.get(position).getId() + "",
                        "status", orderList.get(position).getStatus() + "");
            }
        });
    }

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

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        if (arguments != null) {
            state = arguments.getString("state");
            identity = arguments.getString("identity");
        }
    }

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

    }

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

    }

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

    }

    @Override
    public void supplierList(List<OrderBean> dataList) {
        if (page == 1) {
            orderList.clear();
            orderList.addAll(dataList);
            orderAdapter.notifyDataSetChanged();
            orderAdapter.loadMoreComplete();
        } else {
            orderAdapter.loadMoreComplete();
            orderList.addAll(dataList);
            orderAdapter.notifyDataSetChanged();
        }
        if (dataList.size() == 0) {
            orderAdapter.loadMoreEnd();
        } else {
            page++;
        }
    }
}
