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

import android.text.TextUtils;
import android.widget.LinearLayout;
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.OrderDetail;
import com.wd.workoffice.bean.ProPriceBean;
import com.wd.workoffice.bean.ProfitBean;
import com.wd.workoffice.bean.workEnum.OrderType;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.adapter.ProfitProductAdapter;
import com.wd.workoffice.util.MathUtils;
import com.wd.workoffice.util.WorkUtils;

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.Observable;
import io.reactivex.disposables.Disposable;

/**
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class WorkFinancialProfitDetailActivity extends WorkToolBarActivity {

    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_people)
    TextView tvPeople;
    @BindView(R.id.tv_dep)
    TextView tvDep;
    @BindView(R.id.tv_order_no)
    TextView tvOrderNo;
    @BindView(R.id.tv_order_type)
    TextView tvOrderType;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_order_price)
    TextView tvOrderPrice;
    @BindView(R.id.ll_sale)
    LinearLayout llSale;
    @BindView(R.id.rv_data)
    RecyclerView rvData;
    private List<OrderDetail.OrderItemsBean> dataList;
    private ProfitProductAdapter dataAdapter;
    private String orderType;
    private ProfitBean.PageBean.RecordsBean info;

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


    @Override
    protected void initData() {
        info = JSON.parseObject(getIntent().getStringExtra("info"), ProfitBean.PageBean.RecordsBean.class);
        orderType = info.getOrderType();
        dataList = new ArrayList<>();
        dataAdapter = new ProfitProductAdapter(R.layout.item_profit_product, dataList);
        dataAdapter.bindToRecyclerView(rvData);
        dataAdapter.setEmptyView(R.layout.view_empty_content, rvData);
        getData();
    }

    private void getData() {
            Observable<BaseBean> observable;
            if (TextUtils.equals(orderType, OrderType.SALE.getCode())) {
                observable = RtfUtils.getRtf().saleOrderDetail(info.getOrderId(), "1");
            } else if (TextUtils.equals(orderType, OrderType.OUT_BUY.getCode())) {
                observable = RtfUtils.getRtf().purchaseOrderDetail(info.getOrderId(), "1");
            } else if (TextUtils.equals(orderType, OrderType.SUPPLIER.getCode())) {
                observable = RtfUtils.getRtf().supplierOrderDetail(info.getOrderId(), "1");
            } else if (TextUtils.equals(orderType, OrderType.BUILD.getCode())) {
                observable = RtfUtils.getRtf().productOrderDetail(info.getOrderId(), "1");
            } else if (TextUtils.equals(orderType, OrderType.IN.getCode())) {
                observable = RtfUtils.getRtf().insideOrderDetail(info.getOrderId(), "1");
            } else if (TextUtils.equals(orderType, OrderType.CRUCIBLE.getCode())) {
                observable = RtfUtils.getRtf().crucibleSaleOrderDetail(info.getOrderId(), "1");
            } else {
                observable = RtfUtils.getRtf().saleOrderDetail(info.getOrderId(), "1");
            }
            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;
                    }
                    OrderDetail dataBean = JSON.parseObject(data.getData().toString(), OrderDetail.class);
                    tvName.setText(dataBean.getCustomerName());
                    tvOrderPrice.setText(MathUtils.converData(dataBean.getLaunchOrderAmount(),3));
                    tvPeople.setText(dataBean.getLaunchUserName());
                    tvDep.setText(dataBean.getLaunchDeptName());
                    tvOrderType.setText(WorkUtils.getOrderName(dataBean.getOrderType()));
                    tvOrderNo.setText(dataBean.getId());
                    tvTime.setText(dataBean.getCreatedTime());

                    dataList.addAll(dataBean.getOrderItems());
                    dataAdapter.notifyDataSetChanged();

                }
            });
        }


    @Override
    protected void initEvent() {
    }

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

}
