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

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

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.android.material.tabs.TabLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.wd.workoffice.R;
import com.wd.workoffice.app.WorkBaseActivity;
import com.wd.workoffice.bean.ContactsBean;
import com.wd.workoffice.bean.ContactsList;
import com.wd.workoffice.contract.WorkContactsContract;
import com.wd.workoffice.presenter.WorkContactsPresenter;
import com.wd.workoffice.ui.adapter.WorkContactsAdapter;
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 WorkContactsSearchActivity extends WorkBaseActivity implements WorkContactsContract.View {

    @BindView(R.id.et_key)
    EditText etKey;
    @BindView(R.id.tb_title)
    Toolbar tbTitle;
    @BindView(R.id.rv_data)
    RecyclerView rvData;
    @BindView(R.id.iv_search)
    TextView ivSearch;
    @BindView(R.id.tl_tab)
    TabLayout tlTab;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;
    private Map<String, Object> param;
    private WorkContactsPresenter workProPresenter;
    private WorkContactsAdapter workProductAdapter;
    private List<ContactsBean> dataList;
    private int page = 1;
    private String partTypeNames="供应商";
    private String tab[] = {"供应商", "客户", "物流公司"};
    private String ofKindArrays[] = {"供应商", "客户", "物流公司"};
    @Override
    protected void initView() {
        ButterKnife.bind(this);
        workProPresenter = new WorkContactsPresenter();
        workProPresenter.onAttach(this);
        setSupportActionBar(tbTitle);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        rvData.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        for (int i = 0; i < tab.length; i++) {
            TabLayout.Tab newTab = tlTab.newTab();
            tlTab.addTab(newTab.setText(tab[i]));
        }
        etKey.setHint("请输入成员名称");
    }

    @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() {
        param = WorkUtils.pageKey();
        param.put("partTypeNames", partTypeNames);
        page = 1;
        param.put("current", page);
        dataList = new ArrayList<>();
        workProductAdapter = new WorkContactsAdapter(R.layout.item_work_contacts, dataList);
        workProductAdapter.bindToRecyclerView(rvData);
        workProductAdapter.setEmptyView(R.layout.view_empty_content, rvData);
        workProPresenter.getData(param);
    }

    @Override
    protected void initEvent() {
        srlRefresh.setOnRefreshListener(refreshLayout -> {
            page = 1;
            param.put("current", page);
            workProPresenter.getData(param);
        });
        workProductAdapter.setOnLoadMoreListener(() -> {
            param.put("current", page);
            workProPresenter.getData(param);
        }, rvData);
        workProductAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                startActivity(WorkContactsInfoActivity.class, "id",dataList.get(position).getId()+"");
                startActivity(WorkContactsDetailActivity.class,
                        "id",dataList.get(position).getId()+"",
                        "partTypeNames", partTypeNames,"title",dataList.get(position).getName());
            }
        });
        tlTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                page=1;
                partTypeNames=ofKindArrays[tab.getPosition()];
                param.put("partTypeNames", partTypeNames);
                param.put("current", page);
                workProPresenter.getData(param);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

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


    @OnClick(R.id.iv_search)
    public void onViewClicked() {
        String key = etKey.getText().toString();
        if (TextUtils.isEmpty(key)) {
            toast("请输入搜索关键字");
            return;
        }
        page=1;
        param.put("current", page);
        param.put("name", key);
        workProPresenter.getData(param);
    }


    @Override
    public void getData(List<ContactsBean> data) {
        toast("加载完成");
        if (page == 1) {
            dataList.clear();
            dataList.addAll(data);
            workProductAdapter.notifyDataSetChanged();
            workProductAdapter.loadMoreComplete();
        } else {
            workProductAdapter.loadMoreComplete();
            dataList.addAll(data);
            workProductAdapter.notifyDataSetChanged();
        }
        if (data.size() == 0) {
            workProductAdapter.loadMoreEnd();
        } else {
            page++;
        }
    }
}
