package com.wd.workoffice.ui.fg;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.wd.workoffice.R;
import com.wd.workoffice.app.BaseBean;
import com.wd.workoffice.app.BaseMesBean;
import com.wd.workoffice.app.WorkBaseFg;
import com.wd.workoffice.bean.SendApplyBean;
import com.wd.workoffice.bean.event.CheckSendEvent;
import com.wd.workoffice.bean.mesBean.MesOrderBean;
import com.wd.workoffice.bean.mesBean.TakeOnBean;
import com.wd.workoffice.bean.workEnum.OrderType;
import com.wd.workoffice.bean.workEnum.PagePermissionType;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.activity.bat.order.SendApplyDetailActivity;
import com.wd.workoffice.ui.activity.mes.order.MesOrderDetailActivity;
import com.wd.workoffice.ui.adapter.MesOrderAdapter;
import com.wd.workoffice.ui.adapter.SendApplyAdapter;
import com.wd.workoffice.util.WorkUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

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

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
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 MesOrderFragment extends WorkBaseFg {

    @BindView(R.id.rv_data)
    RecyclerView rvData;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;
    private String state;
    private int page = 1;
    private Map<String, Object> param;
    private MesOrderAdapter dataAdapter;
    private List<MesOrderBean> dataList;

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


    @Override
    protected void initData() {
        param = WorkUtils.pageMesKey();
        page = 1;
        param.put("pageNum", page);
        Map<String ,Object> paramMap =new HashMap<>();
        param.put("paramMap", paramMap);
        paramMap.put("scheduleStatus",state);
        dataList = new ArrayList<>();
        dataAdapter = new MesOrderAdapter(R.layout.item_mes_order, 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) {
            state = arguments.getString("state");
        }
    }

    private void getData() {
//        RtfUtils.getMesRtf().orderList("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<MesOrderBean.ListBean> getList = JSON.parseObject(data.getData().toString(), MesOrderBean.class).getList();
//                if (page == 1) {
//                    dataList.clear();
//                    dataList.addAll(getList);
//                    dataAdapter.notifyDataSetChanged();
//                    dataAdapter.loadMoreComplete();
//                } 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(MesOrderDetailActivity.class,"id",dataList.get(position).getManufactureOrderNumber());
            }
        });
    }





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

}
