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.SaleInsideAdapter;
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 InsideOrderSearchActivity 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 SaleInsideAdapter orderAdapter;
    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<>();
        orderAdapter = new SaleInsideAdapter(R.layout.item_sale_inside, orderList);
        orderAdapter.bindToRecyclerView(rvData);
        orderAdapter.setEmptyView(R.layout.view_empty_order, rvData);
        param = WorkUtils.pageKey();
        param.put("current", page);
        param.put("identity", identity);
        if (!WorkUtils.hasPermission(PagePermissionType.IO_INSIDE_LIST.getPermission())){
            toast(R.string.permission);
        }
    }

    @Override
    protected void initEvent() {
        orderAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                param.put("current", page);
                saleSalePresenter.getInsideOrder(param);
            }
        }, rvData);
        orderAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (!WorkUtils.hasPermission(PagePermissionType.IO_INSIDE_DETAILS.getPermission())){
                    toast(R.string.permission);
                    return;
                }
                startActivity(InsideOrderDetailActivity.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.IO_INSIDE_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.getInsideOrder(param);
    }

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

    }

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

    }

    @Override
    public void insideList(List<OrderBean> dataList) {
        toast("加载成功");
        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++;
        }
    }

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

    }
}
