package com.wd.workoffice.ui.activity.mes;

import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
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.BaseBean;
import com.wd.workoffice.app.BaseMesBean;
import com.wd.workoffice.app.WorkBaseActivity;
import com.wd.workoffice.bean.WarehouseStockBean;
import com.wd.workoffice.bean.mesBean.QualityTraceabilityBean;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.activity.bat.work.WorkWarehouseStockDetailActivity;
import com.wd.workoffice.ui.activity.bat.work.WorkWarehouseStockFlowActivity;
import com.wd.workoffice.ui.activity.bat.work.WorkWarehouseStockNumActivity;
import com.wd.workoffice.ui.adapter.QualityTraceabilityAdapter;
import com.wd.workoffice.ui.adapter.WorkWarhosutStockAdapter;
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;
import flexible.xd.android_base.network.rtfhttp.Transformer;
import io.reactivex.disposables.Disposable;

/**
 * 质量追溯
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class QualityTraceabilityActivity extends WorkBaseActivity {

    @BindView(R.id.et_key)
    EditText etKey;
    @BindView(R.id.tv_search)
    TextView tvSearch;
    @BindView(R.id.tb_title)
    Toolbar tbTitle;
    @BindView(R.id.rv_data)
    RecyclerView rvData;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;
    private int page = 1;
    private Map<String, Object> param;
    private List<QualityTraceabilityBean.ListBean> dataList;
    private QualityTraceabilityAdapter dataAdapter;
    private Map<String, Object> paramMap;

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


    @Override
    protected void initData() {
        dataList = new ArrayList<>();
        dataAdapter = new QualityTraceabilityAdapter(R.layout.item_quality_traceability, dataList);
        dataAdapter.bindToRecyclerView(rvData);
        dataAdapter.setEmptyView(R.layout.view_empty_content, rvData);
        param = WorkUtils.pageMesKey();
        param.put("pageNum", 1);
        paramMap = WorkUtils.simpleParam();
        param.put("paramMap", paramMap);
        getData();
    }

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

    private void getData() {
        RtfUtils.getMesRtf().getMpsProductByBasket("1",WorkUtils.convertMapToBody(param)).compose(Transformer.schedule()).subscribe(new WorkObserver<BaseMesBean>() {
            @Override
            public void doOnSubscribe(Disposable d) {
            }

            @Override
            public void onFail(String errorMsg) {
                hideLoading();
                toast(errorMsg);
            }

            @Override
            public void onSuccess(BaseMesBean data) {
                hideLoading();
                if (!data.isSuccess()) {
                    toast(data.getMessage());
                    return;
                }
                List<QualityTraceabilityBean.ListBean> getList = JSON.parseObject(data.getData().toString(), QualityTraceabilityBean.class).getList();
                if (page == 1) {
                    srlRefresh.finishRefresh();
                    dataList.clear();
                    dataList.addAll(getList);
                    dataAdapter.notifyDataSetChanged();
                    dataAdapter.loadMoreComplete();
                } else {
                    dataAdapter.loadMoreComplete();
                    dataList.addAll(getList);
                    dataAdapter.notifyDataSetChanged();
                }
                if (getList.size() == 0) {
                    dataAdapter.loadMoreEnd();
                } else {
                    page++;
                }
            }
        });
    }

    @Override
    protected void initEvent() {
        srlRefresh.setOnRefreshListener(refreshLayout -> {
            page = 1;
            param.put("pageNum", page);
            getData();
        });
        dataAdapter.setOnLoadMoreListener(() -> {
            param.put("pageNum", page);
            getData();
        }, rvData);
        dataAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(QualityTraceabilityDetailActivity.class, "info",
                        JSON.toJSONString(dataList.get(position)));
            }
        });
    }


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


    @OnClick(R.id.tv_search)
    public void onViewClicked() {
        page = 1;
        param.put("pageNum", page);
        paramMap.put("serialNumber", etKey.getText().toString());
        getData();
    }

}
