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

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.wd.workoffice.R;
import com.wd.workoffice.app.BaseBean;
import com.wd.workoffice.app.WorkToolBarActivity;
import com.wd.workoffice.bean.LogisticBean;
import com.wd.workoffice.bean.LogisticDetailBean;
import com.wd.workoffice.bean.event.CheckLogisticEvent;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.adapter.WorkAddLogisticProAdapter;
import com.wd.workoffice.ui.adapter.WorkLogisticDetailAdapter;
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.appcompat.app.AlertDialog;
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 io.reactivex.disposables.Disposable;

/**
 * 工作台 - 财务 -  物流- 选择详情
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class WorkFinancialLogisticsProActivity extends WorkToolBarActivity {
    @BindView(R.id.tv_bottom)
    TextView tvBottom;
    @BindView(R.id.rv_data)
    RecyclerView rvData;
    private List<LogisticDetailBean.RecordsBean> dataList;
    private WorkAddLogisticProAdapter dataAdapter;
    private Map<String, Object> param;
    private int page = 1;

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

    @Override
    protected void initData() {
        dataList = new ArrayList<>();
        dataAdapter = new WorkAddLogisticProAdapter(R.layout.item_logistic_add_pro, dataList);
        dataAdapter.bindToRecyclerView(rvData);
        param = new HashMap<>();
        param.put("current", 1);
        param.put("status", 0);
        param.put("receiveDeptId", getIntent().getStringExtra("depId"));
        param.put("logisticsId", getIntent().getStringExtra("logisticsId"));
        if (TextUtils.equals("销售订单", getIntent().getStringExtra("orderType"))) {
            param.put("processCode", "SO");
        } else {
            param.put("processCode", "PO");
        }
        getData();
    }

    private void getData() {
        RtfUtils.getRtf().contactsLogistic(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<LogisticDetailBean.RecordsBean> dataBean = JSON.parseObject(data.getData().toString(), LogisticDetailBean.class).getRecords();
                if (page == 1) {
                    dataList.clear();
                    dataList.addAll(dataBean);
                    dataAdapter.notifyDataSetChanged();
                    dataAdapter.loadMoreComplete();
                } else {
                    dataAdapter.loadMoreComplete();
                    dataList.addAll(dataBean);
                    dataAdapter.notifyDataSetChanged();
                }
                if (dataBean.size() == 0) {
                    dataAdapter.loadMoreEnd();
                } else {
                    page++;
                }
            }
        });
    }

    @Override
    protected void initEvent() {
        dataAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                param.put("current", page);
                getData();
            }
        }, rvData);
    }

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


    @OnClick(R.id.tv_bottom)
    public void onViewClicked() {
        Map<Integer, Boolean> isChoose = dataAdapter.getIsChoose();
        List<LogisticDetailBean.RecordsBean> info = new ArrayList<>();
        for (Map.Entry<Integer, Boolean> entry : isChoose.entrySet()) {
            if (entry.getValue()) {
                LogisticDetailBean.RecordsBean recordsBean = dataList.get(entry.getKey());
                info.add(recordsBean);
            }
        }
        if (info.size() == 0) {
            toast("请选择货品");
            return;
        }
        startActivity(WorkFinancialLogisticsSubmitActivity.class, "info", JSON.toJSONString(info),
                "receiveDeptId", getIntent().getStringExtra("depId"),
                "logisticsId", getIntent().getStringExtra("logisticsId"),
                "processCode", TextUtils.equals("销售订单", getIntent().getStringExtra("orderType")) ? "SO" : "PO");
    }
    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe
    public void refresh(CheckLogisticEvent event) {
        finish();
    }
}
