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

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
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.WorkToolBarActivity;
import com.wd.workoffice.bean.ClientBean;
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.StoreClientAdapter;
import com.wd.workoffice.util.WorkUtils;

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

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.Observable;
import io.reactivex.disposables.Disposable;

/**
 * 商店-销售-选择客户/供应商（回款选择客户）
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class StoreSaleClientActivity extends WorkToolBarActivity {

    @BindView(R.id.et_key)
    EditText etKey;
    @BindView(R.id.btn_search)
    Button btnSearch;
    @BindView(R.id.rv_client)
    RecyclerView rvClient;
    private Map<String, Object> param;
    private List<ClientBean.RecordsBean> clientList;
    private StoreClientAdapter clientAdapter;
    private int page = 1;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        rvClient.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));

    }

    @Override
    protected void initData() {
        if (!TextUtils.isEmpty(getIntent().getStringExtra("supplier"))) {
            setTitle(getIntent().getStringExtra("supplier"));
        }

        clientList = new ArrayList<>();
        clientAdapter = new StoreClientAdapter(R.layout.item_store_client, clientList);
        clientAdapter.bindToRecyclerView(rvClient);
        clientAdapter.setEmptyView(R.layout.view_empty_content, rvClient);
        param = WorkUtils.pageKey();
        if (TextUtils.isEmpty(getIntent().getStringExtra("flag"))) {
            param.put("partTypeCode", getIntent().getStringExtra("code"));
        } else {
            param.put("searchType", getIntent().getStringExtra("code"));
        }
        String depId = getIntent().getStringExtra("depId");
        if (!TextUtils.isEmpty(depId)) {
            param.put("deptId", depId);
        }
        getData();
    }

    private void getData() {
        Observable<BaseBean> observable;
        if (TextUtils.isEmpty(getIntent().getStringExtra("flag"))) {
            observable = RtfUtils.getRtf().getClient(param);
        } else {
            observable = RtfUtils.getRtf().getClientOut(param);
        }
        observable.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<ClientBean.RecordsBean> dataList = new ArrayList<>();
                if (TextUtils.isEmpty(getIntent().getStringExtra("flag"))) {
                    ClientBean clientBean = JSON.parseObject(data.getData().toString(), ClientBean.class);
                    dataList.addAll(clientBean.getRecords());
                    if (page == 1) {
                        clientList.clear();
                        clientList.addAll(dataList);
                        clientAdapter.notifyDataSetChanged();
                        clientAdapter.loadMoreComplete();
                    } else {
                        clientAdapter.loadMoreComplete();
                        clientList.addAll(dataList);
                        clientAdapter.notifyDataSetChanged();
                    }
                    if (dataList.size() == 0) {
                        clientAdapter.loadMoreEnd();
                    } else {
                        page++;
                    }
                } else {
                    dataList.addAll(JSON.parseArray(data.getData().toString(), ClientBean.RecordsBean.class));
                    clientAdapter.loadMoreEnd();
                    clientList.clear();
                    clientList.addAll(dataList);
                    clientAdapter.notifyDataSetChanged();
                }
                if (TextUtils.isEmpty(getIntent().getStringExtra("flag"))) {

                }
//                toast("搜索成功");
            }
        });
    }

    @Override
    protected void initEvent() {
        clientAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                param.put("current", page);
                getData();
            }
        }, rvClient);
        clientAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (TextUtils.equals(getIntent().getStringExtra("code"), "CUSTOMER")) {
                    Intent intent = new Intent();
                    intent.putExtra("clientName", clientList.get(position).getName());
                    intent.putExtra("id", clientList.get(position).getId());
                    setResult(10001, intent);
                } else if (TextUtils.equals(getIntent().getStringExtra("code"), "TRANSPORTERS")) {
                    Intent intent = new Intent();
                    intent.putExtra("clientName", clientList.get(position).getName());
                    intent.putExtra("id", clientList.get(position).getId());
                    setResult(10001, intent);
                } else {
                    Intent intent = new Intent();
                    intent.putExtra("clientName", clientList.get(position).getName());
                    intent.putExtra("id", clientList.get(position).getId());
                    setResult(10002, intent);
                }
                finish();
            }
        });
    }

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


    @OnClick(R.id.btn_search)
    public void onViewClicked() {
        param.put("name", etKey.getText().toString());
        param.put("current", 1);
        page = 1;
        getData();
    }
}
