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

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

import com.alibaba.fastjson.JSON;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.wd.workoffice.R;
import com.wd.workoffice.app.BaseBean;
import com.wd.workoffice.app.WorkToolBarActivity;
import com.wd.workoffice.bean.FlowBean;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.adapter.WorkFlowAdapter;
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.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 flexible.xd.android_base.utils.TimeUtils;
import io.reactivex.disposables.Disposable;

/**
 * 工作台 - 部门 - 流水
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class WorkAccountFlowActivity extends WorkToolBarActivity {

    @BindView(R.id.tv_start)
    TextView tvStart;
    @BindView(R.id.tv_end)
    TextView tvEnd;
    @BindView(R.id.rv_data)
    RecyclerView rvData;
    private Map<String, Object> param;
    int page;
    private List<FlowBean.RecordsBean> clientList;
    private WorkFlowAdapter clientAdapter;

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

    @Override
    protected void initData() {
        clientList = new ArrayList<>();
        clientAdapter = new WorkFlowAdapter(R.layout.item_work_flow, clientList);
        clientAdapter.bindToRecyclerView(rvData);
        clientAdapter.setEmptyView(R.layout.view_empty_content,rvData);
        param = WorkUtils.pageKey();
        param.put("deptId", getIntent().getStringExtra("id"));
        page = 1;
        param.put("current", page);
        getData();
    }

    private void getData() {
        RtfUtils.getRtf().getFlow(param).compose(Transformer.schedule()).subscribe(new WorkObserver<BaseBean>() {
            @Override
            public void doOnSubscribe(Disposable d) {
            }

            @Override
            public void onFail(String errorMsg) {
                hideLoading();
                toast(errorMsg);
            }

            @Override
            public void onSuccess(BaseBean data) {
                hideLoading();
                if (data.getCode() != 0) {
                    toast(data.getMessage());
                    return;
                }
                List<FlowBean.RecordsBean> dataList = JSON.parseObject(data.getData().toString(), FlowBean.class).getRecords();
                if (page == 1) {
                    clientList.clear();
                    clientList.addAll(dataList);
                    clientAdapter.notifyDataSetChanged();
                    clientAdapter.loadMoreComplete();
                } else {
                    clientAdapter.loadMoreComplete();
                    clientList.addAll(dataList);
                    clientAdapter.notifyDataSetChanged();
                }
                if (dataList.size() == 0) {
                    clientAdapter.loadMoreEnd();
                } else {
                    page++;
                }
            }
        });
    }

    @Override
    protected void initEvent() {
        clientAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                param.put("current", page);
                getData();
            }
        }, rvData);
        clientAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(WorkAccountFlowDetailActivity.class,"detail",JSON.toJSONString(clientList.get(position)));
            }
        });
    }

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


    @OnClick({R.id.tv_start, R.id.tv_end})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_start:
                PickTimeUtils.showPickThree(this, new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        String time = TimeUtils.date2String(date, new SimpleDateFormat("yyyy-MM-dd"));
                        param.put("searchStartTime",time+ " 00:00:00");
                        tvStart.setText(time);
                        page = 1;
                        param.put("current", page);
                        getData();
                    }
                }).show();
                break;
            case R.id.tv_end:
                PickTimeUtils.showPickThree(this, new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        String time = TimeUtils.date2String(date, new SimpleDateFormat("yyyy-MM-dd"));
                        param.put("searchEndTime", time+ " 23:59:59");
                        tvEnd.setText(time);
                        page = 1;
                        param.put("current", page);
                        getData();
                    }
                }).show();
                break;
        }
    }
}
