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.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.wd.workoffice.R;
import com.wd.workoffice.app.BaseBean;
import com.wd.workoffice.app.WorkBaseActivity;
import com.wd.workoffice.bean.OrderBean;
import com.wd.workoffice.bean.workEnum.PagePermissionType;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.adapter.SaleProductAdapter;
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 ProductOrderSearchActivity extends WorkBaseActivity {

    @BindView(R.id.et_key)
    EditText etKey;
    @BindView(R.id.tb_title)
    Toolbar tbTitle;
    @BindView(R.id.rv_data)
    RecyclerView rvData;
    private int page = 1;
    private Map<String, Object> param;
    private SaleProductAdapter dataAdapter;
    private List<OrderBean> dataList;
    private String identity;

    @Override
    protected void initView() {
        ButterKnife.bind(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");
        param = WorkUtils.pageKey();
        param.put("identity", identity);
        page = 1;
        param.put("current", page);
        dataList = new ArrayList<>();
        dataAdapter = new SaleProductAdapter(R.layout.item_sale_product, dataList);
        dataAdapter.bindToRecyclerView(rvData);
        dataAdapter.setEmptyView(R.layout.view_empty_content, rvData);
    }

    @Override
    protected void initEvent() {
        dataAdapter.setOnLoadMoreListener(() -> {
            param.put("current", page);
            getData();
        }, rvData);
        dataAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(ProductOrderDetailActivity.class,
                        "identity",identity,
                        "id", dataList.get(position).getId() + "",
                        "status", dataList.get(position).getStatus() + "");
            }
        });
    }

    private void getData() {
        RtfUtils.getRtf().productList(param).compose(Transformer.schedule()).subscribe(new WorkObserver<BaseBean>() {
            @Override
            public void doOnSubscribe(Disposable d) {
            }

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

            @Override
            public void onSuccess(BaseBean data) {
                hideLoading();
                if (data.getCode() != 0) {
                    toast(data.getMessage());
                    return;
                }
                List<OrderBean> getList = JSON.parseArray(JSON.parseObject(data.getData().toString()).getString("records"), OrderBean.class);
                if (page == 1) {
                    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++;
                }
                toast("加载成功");
            }
        });
    }

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


    @OnClick(R.id.iv_search)
    public void onViewClicked() {
        if (!WorkUtils.hasPermission(PagePermissionType.AO_PACKAGE_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);
        getData();
    }
}
