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

import android.os.Bundle;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.wd.workoffice.R;
import com.wd.workoffice.app.BaseMesBean;
import com.wd.workoffice.app.WorkBaseFg;
import com.wd.workoffice.bean.mesBean.TaskPeopleBean;
import com.wd.workoffice.bean.mesBean.WokrOrderBean;
import com.wd.workoffice.bean.mesBean.WorkOrderSon;
import com.wd.workoffice.bean.mesBean.WorkTaskBean;
import com.wd.workoffice.bean.workEnum.WorkOrder;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.adapter.WorkOrderSonAdapter;
import com.wd.workoffice.ui.adapter.WorkOrderTaskAdapter;
import com.wd.workoffice.ui.adapter.WorkTaskPeopleAdapter;
import com.wd.workoffice.util.WorkUtils;

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

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

/**
 * 员工
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class WorkTaskPeopleFragment extends WorkBaseFg {
    @BindView(R.id.rv_data)
    RecyclerView rvData;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;
    private Map<String, Object> param;
    private List<TaskPeopleBean.ListBean> taskList;
    private WorkTaskPeopleAdapter taskAdapter;
    private WorkTaskBean.ListBean info;
    private int page = 1;
    private int taskPage = 1;
    private Map<String, Object> paramMap;
    private Map<String, Object> taskParam;
    private Map<String, Object> taskParamMap;
    private String plan;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        if (arguments != null) {
            info = JSON.parseObject(arguments.getString("info"), WorkTaskBean.ListBean.class);
            plan = arguments.getString("plan");
        }
    }

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


    @Override
    protected void initData() {
        taskList = new ArrayList<>();
        taskAdapter = new WorkTaskPeopleAdapter(R.layout.item_work_task_people, taskList);
        taskAdapter.setEmptyView(R.layout.view_empty_content, rvData);
        taskAdapter.bindToRecyclerView(rvData);

        taskParam = WorkUtils.pageMesKey();
        taskParam.put("pageNum", 1);
        taskParamMap = WorkUtils.simpleParam();
        taskParam.put("paramMap", taskParamMap);
        taskParamMap.put("woId", info.getWoId());
        taskParamMap.put("actId", info.getId());
        getTask();
    }



    private void getTask() {
        Observable<BaseMesBean> baseMesBeanObservable =
                TextUtils.equals("1",plan)?
                        RtfUtils.getMesRtf().pagePlanLabor("1", WorkUtils.convertMapToBody(taskParam))
                        :RtfUtils.getMesRtf().pageLabor("1", WorkUtils.convertMapToBody(taskParam));
        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;
                }
                List<TaskPeopleBean.ListBean> getList = JSON.parseObject(data.getData().toString(), TaskPeopleBean.class).getList();

                if (taskPage == 1) {
                    taskList.clear();
                    taskList.addAll(getList);
                    taskAdapter.notifyDataSetChanged();
                    taskAdapter.loadMoreComplete();
                } else {
                    taskAdapter.loadMoreComplete();
                    taskList.addAll(getList);
                    taskAdapter.notifyDataSetChanged();
                }
                if (getList.size() == 0) {
                    taskAdapter.loadMoreEnd(true);
                } else {
                    taskPage++;
                }
            }
        });

    }

    @Override
    protected void initEvent() {
        taskAdapter.setOnLoadMoreListener(() -> {
            taskParam.put("pageNum", taskPage);
            getTask();
        }, rvData);
    }


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


}
