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

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.BaseMesBean;
import com.wd.workoffice.app.Config;
import com.wd.workoffice.app.WorkToolBarActivity;
import com.wd.workoffice.bean.event.AddTaskEvent;
import com.wd.workoffice.bean.mesBean.TaskPeopleBean;
import com.wd.workoffice.bean.mesBean.WorkTaskBean;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.adapter.WorkTruePeopleAdapter;
import com.wd.workoffice.util.WorkUtils;

import org.greenrobot.eventbus.EventBus;

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 WorkTrueChoosePeopleActivity 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<TaskPeopleBean.ListBean> clientList;
    private WorkTruePeopleAdapter clientAdapter;
    private int page = 1;
    private Map<String, Object> paramMap;

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

    }

    @Override
    protected void initData() {
        WorkTaskBean.ListBean info = JSON.parseObject(getIntent().getStringExtra("info"),
                WorkTaskBean.ListBean.class);
        clientList = new ArrayList<>();
        clientAdapter = new WorkTruePeopleAdapter(R.layout.item_fault_search, clientList);
        clientAdapter.bindToRecyclerView(rvClient);
        clientAdapter.setEmptyView(R.layout.view_empty_content, rvClient);
        param = WorkUtils.pageMesKey();
        paramMap = WorkUtils.simpleParam();
        paramMap.put("woId", info.getWoId());
        paramMap.put("actId", info.getId());
        param.put("paramMap", paramMap);
        param.put("pageNum", 1);
        getData();
    }

    private void getData() {
        RtfUtils.getMesRtf().pagePlanLabor("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<TaskPeopleBean.ListBean> getList = JSON.parseObject(data.getData().toString(), TaskPeopleBean.class).getList();
                if (page == 1) {
                    clientList.clear();
                    clientList.addAll(getList);
                    clientAdapter.notifyDataSetChanged();
                    clientAdapter.loadMoreComplete();
                } else {
                    clientAdapter.loadMoreComplete();
                    clientList.addAll(getList);
                    clientAdapter.notifyDataSetChanged();
                }
                if (getList.size() == 0|| getList.size()< Config.SIZE) {
                    clientAdapter.loadMoreEnd();
                } else {
                    page++;
                }
                toast("搜索成功");
            }
        });
    }

    @Override
    protected void initEvent() {
        clientAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                param.put("pageNum", page);
                getData();
            }
        }, rvClient);
        clientAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                TaskPeopleBean.ListBean listBean = clientList.get(position);
                listBean.setId(null);
                Observable<BaseMesBean> baseMesBeanObservable = RtfUtils.getMesRtf().saveLabor("aa8faf47c93311e98334fa163e1166bd",
                        WorkUtils.convertMapToBody(JSON.parseObject(JSON.toJSONString(listBean))));
                baseMesBeanObservable.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;
                        }
                        EventBus.getDefault().post(new AddTaskEvent());
                        finish();
                        toast("提交成功");
                    }
                });
            }
        });
    }

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


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