package com.wd.workoffice.ui.activity.bat.order;

import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.wd.workoffice.R;
import com.wd.workoffice.app.WorkBaseActivity;
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.adapter.ProSupplierAdapter;
import com.wd.workoffice.ui.adapter.SaleSupplierAdapter;
import com.wd.workoffice.util.WorkUtils;

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

import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 销售订单搜索
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class SupplierOrderSearchActivity extends WorkBaseActivity implements SaleSaleContract.View {

    @BindView(R.id.et_key)
    EditText etKey;
    @BindView(R.id.tb_title)
    Toolbar tbTitle;
    @BindView(R.id.rv_data)
    RecyclerView rvData;
    private SaleSalePresenter saleSalePresenter;
    private Map<String, Object> param;
    private List<OrderBean> orderList;
    private SaleSupplierAdapter saleAdapter;
    private ProSupplierAdapter proAdapter;
    private int page = 1;
    private String identity;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        saleSalePresenter = new SaleSalePresenter();
        saleSalePresenter.onAttach(this);
        setSupportActionBar(tbTitle);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        rvData.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void initData() {
        identity = getIntent().getStringExtra("identity");
        orderList = new ArrayList<>();
        if (TextUtils.equals(identity, "1")) {
            saleAdapter = new SaleSupplierAdapter(R.layout.item_sale_supplier, orderList);
            saleAdapter.bindToRecyclerView(rvData);
            saleAdapter.setEmptyView(R.layout.view_empty_order,rvData);
        } else {
            proAdapter = new ProSupplierAdapter(R.layout.item_sale_supplier, orderList);
            proAdapter.bindToRecyclerView(rvData);
            proAdapter.setEmptyView(R.layout.view_empty_order,rvData);
        }

        param = WorkUtils.pageKey();
        param.put("current", page);
        param.put("identity", identity);
        param.put("searchKey", "orderId");
        if (!WorkUtils.hasPermission(PagePermissionType.FO_SUPPLIER_LIST.getPermission())){
            toast(R.string.permission);
        }
    }

    @Override
    protected void initEvent() {
        if (TextUtils.equals(identity, "1")) {
            saleAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
                @Override
                public void onLoadMoreRequested() {
                    param.put("current", page);
                    saleSalePresenter.getSupplierOrder(param);
                }
            }, rvData);
            saleAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    startActivity(SupplierOrderDetailActivity.class,
                            "identity",identity,
                            "id", orderList.get(position).getId() + "",
                            "status", orderList.get(position).getStatus() + "");
                }
            });
        } else {
            proAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
                @Override
                public void onLoadMoreRequested() {
                    param.put("current", page);
                    saleSalePresenter.getSupplierOrder(param);
                }
            }, rvData);
            proAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    startActivity(SupplierOrderDetailActivity.class,
                            "identity",identity,
                            "id", orderList.get(position).getId() + "",
                            "status", orderList.get(position).getStatus() + "");
                }
            });
        }
    }

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


    @OnClick(R.id.iv_search)
    public void onViewClicked() {
        if (!WorkUtils.hasPermission(PagePermissionType.FO_SUPPLIER_LIST.getPermission())){
            toast(R.string.permission);
            return;
        }
        String key = etKey.getText().toString();
        if (TextUtils.isEmpty(key)) {
            toast("请输入搜索关键字");
            return;
        }
        page=1;
        param.put("current", page);
        param.put("searchValue", key);
        saleSalePresenter.getSupplierOrder(param);
    }

    @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) {
        toast("加载成功");
        if (page == 1) {
            orderList.clear();
            orderList.addAll(dataList);
            if (TextUtils.equals(identity, "1")) {
                saleAdapter.notifyDataSetChanged();
                saleAdapter.loadMoreComplete();
            } else {
                proAdapter.notifyDataSetChanged();
                proAdapter.loadMoreComplete();
            }
        } else {
            if (TextUtils.equals(identity, "1")) {
                saleAdapter.loadMoreComplete();
                orderList.addAll(dataList);
                saleAdapter.notifyDataSetChanged();
            } else {
                proAdapter.loadMoreComplete();
                orderList.addAll(dataList);
                proAdapter.notifyDataSetChanged();
            }
        }
        if (dataList.size() == 0) {
            if (TextUtils.equals(identity, "1")) {
                saleAdapter.loadMoreEnd();
            } else {
                proAdapter.loadMoreEnd();
            }
        } else {
            page++;
        }
    }
}
