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

import android.view.View;

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.WorkToolBarActivity;
import com.wd.workoffice.bean.FinancialBean;
import com.wd.workoffice.bean.event.CheckFinancialEvent;
import com.wd.workoffice.bean.event.CheckLogisticEvent;
import com.wd.workoffice.bean.workEnum.PagePermissionType;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.adapter.FinancialAdapter;
import com.wd.workoffice.util.UserKeeper;
import com.wd.workoffice.util.WorkUtils;

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

import java.util.ArrayList;
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 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 StoreFinancialActivity extends WorkToolBarActivity {

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

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



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


    @Override
    protected void initData() {
        if (!WorkUtils.hasPermission(PagePermissionType.FINANCE.getPermission())) {
            toast(R.string.permission);
            return;
        }
        param = WorkUtils.pageKey();
        page = 1;
        param.put("current", page);
        param.put("payDept", UserKeeper.getInstance().getUserDepId()   );
        dataList = new ArrayList<>();
        dataAdapter = new FinancialAdapter(R.layout.item_financial, dataList);
        dataAdapter.bindToRecyclerView(rvData);
        dataAdapter.setEmptyView(R.layout.view_empty_content, rvData);
        getData();
    }


    private void getData() {
        RtfUtils.getRtf().storeFinancial(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<FinancialBean.RecordsBean> getList = JSON.parseObject(data.getData().toString(), FinancialBean.class).getRecords();

                if (page == 1) {
                    srlRefresh.finishRefresh();
                    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("current", page);
            getData();
        });
        dataAdapter.setOnLoadMoreListener(() -> {
            param.put("current", page);
            getData();
        }, rvData);
        dataAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (!WorkUtils.hasPermission(PagePermissionType.FINANCE_DETAIL.getPermission())) {
                    toast(R.string.permission);
                    return;
                }
                startActivity(StoreFinancialDetailActivity.class, "info",JSON.toJSONString(dataList.get(position)));
            }
        });
        dataAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()){
                    case R.id.tv_modify:
                        startActivity(StoreFinancialModifyActivity.class, "info",JSON.toJSONString(dataList.get(position)));
                        break;
                    case R.id.tv_delete:
                        delete(dataList.get(position).getId(),position);
                        break;
                }
            }
        });
    }

    private void delete(String id,int position) {
        RtfUtils.getRtf().storeFinancialCancel(id).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;
                }
                toast("作废成功");
                dataList.get(position).setStatus("9");
                dataAdapter.notifyItemChanged(position);
            }
        });

    }


    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

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

    @Subscribe
    public void refresh(CheckFinancialEvent event) {
        page = 1;
        param.put("current", page);
        getData();
    }
}
