package com.wd.workoffice.ui.fg;

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

import com.alibaba.fastjson.JSON;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.chad.library.adapter.base.BaseQuickAdapter;
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.ServiceCheckBean;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.activity.mes.repair.ServiceCheckDetailActivity;
import com.wd.workoffice.ui.adapter.MesCheckAdapter;
import com.wd.workoffice.util.PickTimeUtils;
import com.wd.workoffice.util.WorkUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
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 butterknife.OnClick;
import flexible.xd.android_base.network.rtfhttp.Transformer;
import flexible.xd.android_base.utils.TimeUtils;
import io.reactivex.disposables.Disposable;


/**
 * 点检
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class MesCheckFragment extends WorkBaseFg {

    @BindView(R.id.rv_data)
    RecyclerView rvData;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;
    @BindView(R.id.tv_desc1)
    TextView tvDesc1;
    @BindView(R.id.tv_desc2)
    TextView tvDesc2;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.ll_time)
    LinearLayout llTime;
    private int page = 1;
    private Map<String, Object> param;
    private MesCheckAdapter dataAdapter;
    private List<ServiceCheckBean.ListBean> dataList;
    private Map<String, Object> paramMap;
    @Override
    protected void initView() {
        ButterKnife.bind(this, getContentView());
        tvDesc1.setText("点检单号");
        tvDesc2.setText("检查人");
        rvData.setLayoutManager(new LinearLayoutManager(getActivity(), RecyclerView.VERTICAL, false));
    }


    @Override
    protected void initData() {
        param = WorkUtils.pageMesKey();
        page = 1;
        param.put("pageNum", page);
        paramMap = new HashMap<>();
        param.put("paramMap", paramMap);
        dataList = new ArrayList<>();
        dataAdapter = new MesCheckAdapter(R.layout.item_mes_process, dataList);
        dataAdapter.bindToRecyclerView(rvData);
        dataAdapter.setEmptyView(R.layout.view_empty_content, rvData);
        dataAdapter.notifyDataSetChanged();
        getData();
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        if (arguments != null) {
        }
    }

    private void getData() {
        RtfUtils.getMesRtf().spotCheckBillListForApp("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<ServiceCheckBean.ListBean> getList = JSON.parseObject(data.getData().toString(), ServiceCheckBean.class).getList();
                if (page == 1) {
                    dataList.clear();
                    dataList.addAll(getList);
                    dataAdapter.notifyDataSetChanged();
                    dataAdapter.loadMoreComplete();
                    srlRefresh.finishRefresh();
                } else {
                    dataAdapter.loadMoreComplete();
                    dataList.addAll(getList);
                    dataAdapter.notifyDataSetChanged();
                }
                if (getList.size() == 0) {
                    dataAdapter.loadMoreEnd();
                } else {
                    page++;
                }
            }
        });
    }

    @Override
    protected void initEvent() {
        srlRefresh.setOnRefreshListener(refreshLayout -> {
            page = 1;
            param.put("pageNum", page);
            getData();
        });
        dataAdapter.setOnLoadMoreListener(() -> {
            param.put("pageNum", page);
            getData();
        }, rvData);
        dataAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(ServiceCheckDetailActivity.class,"info",JSON.toJSONString(dataList.get(position)));
            }
        });
    }

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

    @OnClick(R.id.ll_time)
    public void onViewClicked() {
        PickTimeUtils.showPickThree(getActivity(), new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View view) {
                tvTime.setText(TimeUtils.date2String(date, new SimpleDateFormat("yyyy-MM-dd")));
                paramMap.put("createDateTime", TimeUtils.date2String(date, new SimpleDateFormat("yyyy-MM-dd")));
                page = 1;
                param.put("pageNum", page);
                getData();
            }
        }, false).show();
    }
}
