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

import android.content.Intent;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.wd.workoffice.R;
import com.wd.workoffice.app.BaseMesBean;
import com.wd.workoffice.app.WorkToolBarActivity;
import com.wd.workoffice.bean.mesBean.ChooseMachineBean;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.adapter.MesChooseMachinerAdapter;
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 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 MesChooseMachineActivity extends WorkToolBarActivity {
    @BindView(R.id.rv_client)
    RecyclerView rvClient;
    private Map<String, Object> param;
    private List<ChooseMachineBean> clientList;
    private MesChooseMachinerAdapter clientAdapter;
    private Map<String, Object> paramMap;

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

    @Override
    protected void initData() {
        clientList = new ArrayList<>();
        clientAdapter = new MesChooseMachinerAdapter(R.layout.item_store_client, clientList);
        clientAdapter.bindToRecyclerView(rvClient);
        clientAdapter.setEmptyView(R.layout.view_empty_content, rvClient);
        param = WorkUtils.simpleParam();
        paramMap = WorkUtils.simpleParam();
        paramMap.put("custCode",getIntent().getStringExtra("code"));
        param.put("paramMap", paramMap);
        getData();
    }

    private void getData() {
        RtfUtils.getMesRtf().machineByUser("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<ChooseMachineBean> getList = JSON.parseArray(data.getData().toString(), ChooseMachineBean.class);
                clientList.clear();
                clientList.addAll(getList);
                clientAdapter.notifyDataSetChanged();
                toast("搜索成功");
            }
        });
    }

    @Override
    protected void initEvent() {
        clientAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent();
                intent.putExtra("data", JSON.toJSONString(clientList.get(position)));
                setResult(10001, intent);
                finish();
            }
        });
    }

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


}
