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

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

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.wd.workoffice.R;
import com.wd.workoffice.app.BaseBean;
import com.wd.workoffice.app.WorkToolBarActivity;
import com.wd.workoffice.bean.ContactsMoneyBean;
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.ContactsGetMoneyAdapter;
import com.wd.workoffice.util.UserKeeper;
import com.wd.workoffice.util.WorkUtils;

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

import androidx.annotation.NonNull;
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 WorkContactsMoneyActivity extends WorkToolBarActivity {

    @BindView(R.id.rv_data)
    RecyclerView rvData;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;
    @BindView(R.id.tv_process)
    TextView tvProcess;
    @BindView(R.id.tv_get)
    TextView tvGet;
    @BindView(R.id.tv_apply)
    TextView tvApply;
    @BindView(R.id.tv_back)
    TextView tvBack;
    @BindView(R.id.tv_dep)
    TextView tvDep;
    private Map<String, Object> param;
    private List<ContactsMoneyBean.RecordsBean> dataList;
    private ContactsGetMoneyAdapter dataAdapter;
    private int page = 1;
    private String type;

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


    @Override
    protected void initData() {
        type = getIntent().getStringExtra("type");
        dataList = new ArrayList<>();
        dataAdapter = new ContactsGetMoneyAdapter(R.layout.item_contacts_get_money, dataList, Integer.valueOf(type));
        dataAdapter.bindToRecyclerView(rvData);
        dataAdapter.setEmptyView(R.layout.view_empty_content, rvData);
        param = WorkUtils.pageKey();

//        param.put("financeDeptArApPartId", getIntent().getStringExtra("id"));
        if (TextUtils.equals("1", type)) {
            param.put("type", "AR");//AR：应收 AP应付(String)
            param.put("transitPartId", getIntent().getStringExtra("id"));
        } else if (TextUtils.equals("2", type)) {
            param.put("type", "AP");
            param.put("transitPartId", getIntent().getStringExtra("id"));
            tvGet.setText("应付款(万)");
            setTitle("应付账款");
        } else {//3
            tvDep.setVisibility(View.VISIBLE);
            tvGet.setText("应付款(万)");
            param.put("type", "AP");
            param.put("logisticsId", getIntent().getStringExtra("id"));
            param.put("receiveDeptId", UserKeeper.getInstance().getUserDepId());
            setTitle("应付账款");
        }
        param.put("current", page);
        getData();
    }

    private void getData() {
        Observable<BaseBean> observable;
        if (TextUtils.equals("3", type)) {
            observable = RtfUtils.getRtf().contactsMoneyLogistic(param);
        } else {
            observable = RtfUtils.getRtf().contactsMoney(param);
        }
        observable.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<ContactsMoneyBean.RecordsBean> getList = JSON.parseObject(data.getData().toString(), ContactsMoneyBean.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(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                param.put("current", page);
                getData();
            }
        });
        dataAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                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.LOGISTICS_FLOW_PAGE.getPermission())){
                    toast(R.string.permission);
                    return;
                }
                startActivity(WorkContactsMoneyFlowActivity.class, "type", type,
                        "id", dataList.get(position).getId() + "");
            }
        });
    }

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

}
