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

import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
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.TicketBean;
import com.wd.workoffice.bean.event.CheckTicketEvent;
import com.wd.workoffice.bean.listener.ThreeMenuClickListener;
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.WorkFinancialTicketAdapter;
import com.wd.workoffice.util.WorkUtils;
import com.wd.workoffice.util.ddmfilter.FinancialTicketFilterData;
import com.wd.workoffice.widget.DropDownMenu;

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.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 WorkFinancialTicketActivity extends WorkToolBarActivity {

    @BindView(R.id.ddm_choose)
    DropDownMenu ddmChoose;
    private RecyclerView rvData;
    private int page = 1;
    private Map<String, Object> param;
    private SmartRefreshLayout srlRefresh;
    private List<TicketBean.RecordsBean> dataList;
    private WorkFinancialTicketAdapter dataAdapter;
    private String orderType = "销售订单";

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        FinancialTicketFilterData filterData = new FinancialTicketFilterData(this, ddmChoose, new ThreeMenuClickListener() {
            @Override
            public void clickOne(String value) {
                orderType = value;
                page = 1;
                param.put("current", page);
                getData();
            }

            @Override
            public void clickTwo(String value) {
                //0未审核      1已通过      2不通过
                if (TextUtils.equals(value, "全部状态")) {
                    param.put("status", "");
                } else if (TextUtils.equals(value, "待审核")) {
                    param.put("status", "0");
                } else if (TextUtils.equals(value, "已通过")) {
                    param.put("status", "1");
                } else {
                    param.put("status", "2");
                }
                page = 1;
                param.put("current", page);
                getData();
            }

            @Override
            public void clickThree(String value) {
            }
        });
        rvData = filterData.getRvData();
        srlRefresh = filterData.getSrlRefresh();
    }


    @Override
    protected void initData() {
        dataList = new ArrayList<>();
        dataAdapter = new WorkFinancialTicketAdapter(R.layout.item_work_ticket, dataList);
        dataAdapter.bindToRecyclerView(rvData);
        dataAdapter.setEmptyView(R.layout.view_empty_content, rvData);
        param = WorkUtils.pageKey();
        param.put("current", page);
        getData();
    }


    private void getData() {
        if (!hasPermission()) {
            toast(R.string.permission);
            return;
        }
        Observable<BaseBean> baseBeanObservable;
        if (TextUtils.equals(orderType, "销售订单")) {
            baseBeanObservable = RtfUtils.getRtf().openSoTicket(param);
        } else if (TextUtils.equals(orderType, "外采购订单")) {
            baseBeanObservable = RtfUtils.getRtf().openPoTicket(param);
        } else if (TextUtils.equals(orderType, "坩埚订单")) {
            baseBeanObservable = RtfUtils.getRtf().openCoTicket(param);
        } else {
            baseBeanObservable = RtfUtils.getRtf().openAoTicket(param);
        }
        baseBeanObservable.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<TicketBean.RecordsBean> getList = JSON.parseObject(data.getData().toString(), TicketBean.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) {
                startActivity(WorkFinancialTicketDetailActivity.class, "type", orderType, "info", JSON.toJSONString(dataList.get(position)));
            }
        });
    }


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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_add, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.add:
                startActivity(WorkFinancialTicketAddActivity.class);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

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

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

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

    private boolean hasPermission() {
        boolean hasPermission = true;
        if (TextUtils.equals(orderType, "销售订单")) {
            hasPermission = WorkUtils.hasPermission(PagePermissionType.SO_INVOICE_LIST.getPermission());
        } else if (TextUtils.equals(orderType, "外采购订单")) {
            hasPermission = WorkUtils.hasPermission(PagePermissionType.PO_INVOICE_LIST.getPermission());
        } else if (TextUtils.equals(orderType, "坩埚订单")) {
            hasPermission = WorkUtils.hasPermission(PagePermissionType.CO_INVOICE_LIST.getPermission());
        }
        return hasPermission;
    }


//    private void changePermission() {
//        boolean hasPermission = true;
//        if (TextUtils.equals(orderType, "销售订单")) {
//            hasPermission = WorkUtils.hasPermission(PagePermissionType.SO_INVOICE_AUDIT.getPermission());
//        } else if (TextUtils.equals(orderType, "外采购订单")) {
//            hasPermission = WorkUtils.hasPermission(PagePermissionType.PRODUCT.getPermission());
//        } else if (TextUtils.equals(orderType, "坩埚订单")) {
//            hasPermission = WorkUtils.hasPermission(PagePermissionType.PRODUCT.getPermission());
//        }
//        dataAdapter.setHasPermission(hasPermission);
//        dataAdapter.notifyDataSetChanged();
//    }
}
