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

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

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.WorkBaseFg;
import com.wd.workoffice.bean.mesBean.FaultBean;
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.WorkOrderFaultAdapter;
import com.wd.workoffice.ui.adapter.WorkOrderSonAdapter;
import com.wd.workoffice.ui.adapter.WorkOrderTaskAdapter;
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.disposables.Disposable;

/**
 * 故障
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class QualityWorkFaultFragment extends WorkBaseFg {
    @BindView(R.id.tv_no)
    TextView tvNo;
    @BindView(R.id.tv_desc)
    TextView tvDesc;
    @BindView(R.id.rv_data)
    RecyclerView rvData;
    private List<FaultBean.ListBean> taskList;
    private WorkOrderFaultAdapter taskAdapter;
    private WokrOrderBean.ListBean info;
    private int page = 1;
    private int taskPage = 1;
    private Map<String, Object> taskParam;
    private Map<String, Object> taskParamMap;

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

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


    @Override
    protected void initData() {
//        tvNo.setText(info.getWoCode());
//        tvDesc.setText(info.getDescription());
        taskList = new ArrayList<>();
        taskAdapter = new WorkOrderFaultAdapter(R.layout.item_work_order_fault, taskList);
        taskAdapter.bindToRecyclerView(rvData);

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

        getTask();
    }



    private void getTask() {
        RtfUtils.getMesRtf().pageFailure("1", WorkUtils.convertMapToBody(taskParam)).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<FaultBean.ListBean> getList = JSON.parseObject(data.getData().toString(), FaultBean.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);
//        taskAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                startActivity(WorkTaskDetailActivity.class,
//                        "plan", "0", "info", JSON.toJSONString(taskList.get(position)));
//            }
//        });
    }


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


}
