package com.xxrv.invoice.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.frame.rv.config.RvFrameConfig;
import com.ruiwenliu.wrapper.base.BaseBean;
import com.ruiwenliu.wrapper.base.BaseStatusActivity;
import com.ruiwenliu.wrapper.weight.TitleView;
import com.ruiwenliu.wrapper.weight.refresh.SimpleRefreshLayout;
import com.ruiwenliu.wrapper.weight.refresh.SimpleRefreshView;
import com.xxrv.invoice.R;
import com.xxrv.invoice.R2;
import com.xxrv.invoice.adapter.CheckOrderInvoicingAdapter;
import com.xxrv.invoice.api.InvoiceApi;
import com.xxrv.invoice.bean.OrderInvoicingCarBean;
import com.xxrv.invoice.presenter.InvoicePresenter;
import com.yuyife.okgo.OkGoUtil;

import java.util.LinkedHashMap;
import java.util.Map;

import butterknife.BindView;

public class CheckOrderInvoicingActivity extends BaseStatusActivity<InvoicePresenter> implements BaseQuickAdapter.RequestLoadMoreListener, SimpleRefreshLayout.OnSimpleRefreshListener {


    @BindView(R2.id.rv_content)
    RecyclerView rvContent;
    @BindView(R2.id.refresh)
    SimpleRefreshLayout mSimpleRefreshLayout;

    private CheckOrderInvoicingAdapter adapter;
    private double totalPrice;
    private int mPage;
    private int countPage;
    private String type;
    private String orderIds;

    public static Intent getIntent(Context context, String type, String orderIds) {
        return new Intent(context, CheckOrderInvoicingActivity.class)
                .putExtra("type", type)
                .putExtra("orderIds", orderIds);
    }

    @Override
    protected int setLayout() {
        return R.layout.activity_check_order_invoicing;
    }

    @Override
    protected void initView(Bundle savedInstanceState, TitleView titleView, Intent intent) {
        titleView.setTitle("订单详情");

        type = intent.getStringExtra("type");
        orderIds = intent.getStringExtra("orderIds");

        adapter = new CheckOrderInvoicingAdapter();
        rvContent.setLayoutManager(new LinearLayoutManager(mActivity, LinearLayoutManager.VERTICAL, false));
        adapter.setOnLoadMoreListener(this, rvContent);
        mSimpleRefreshLayout.setHeaderView(new SimpleRefreshView(this));
        mSimpleRefreshLayout.setOnSimpleRefreshListener(this);
        rvContent.setAdapter(adapter);
    }

    @Override
    public void onShowResult(int requestType, BaseBean result) {
        mSimpleRefreshLayout.onRefreshComplete();
        switch (requestType) {
            case 0:
                processData((OrderInvoicingCarBean) result);
                break;
        }
    }


    @Override
    protected void loadData(Bundle savedInstanceState, Intent intent) {
        onRefresh();
    }

    @Override
    public void onRefresh() {
        mPage = 1;
        orderData(mPage);
    }

    private void orderData(int page) {
        Map<String, Object> headMap = new LinkedHashMap<>();
        if (OkGoUtil.getToken() != null)
            headMap.put("Authorization", OkGoUtil.getToken());
        Map<String, Object> mapData = new LinkedHashMap<>();
        mapData.put("page", page);
        mapData.put("limit", 10);
        mapData.put("orderIds", orderIds);
        mapData.put("multiStatus", "6");
        mapData.put("type", type);  //"订单类型 1--租车；2--旅游; 3--会员订单")
        mapData.put("invoiceStatus", "1");  //"开票状态，0未开票，1已开票
        mPresenter.getData(RvFrameConfig.VEHICLE_ORDER, 0, InvoiceApi.HTTP_URL_ORDER_LIST, OrderInvoicingCarBean.class, mapData, headMap, page == 1 ? true : false);
    }


    /**
     * 处理请求数据
     *
     * @param bean
     */
    private void processData(OrderInvoicingCarBean bean) {
        if (mPage == 1) {
            countPage = bean.getData().getTotalPage();
            adapter.setNewData(bean.getData().getData());
            mSimpleRefreshLayout.onRefreshComplete();
        } else {
            adapter.addData(bean.getData().getData());
            adapter.loadMoreComplete();
        }
    }

    @Override
    public void onLoadMoreRequested() {
        if (mPage >= countPage) {
            adapter.loadMoreEnd();

        } else {
            mPage++;
            orderData(mPage);
        }
    }
}
