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

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
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.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.adapter.ProductOrderDetailProductAdapter;
import com.wd.workoffice.util.MathUtils;

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

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 ProductOrderDetailActivity extends WorkToolBarActivity {
    @BindView(R.id.ll_bottom)
    LinearLayout llBottom;
    @BindView(R.id.tv_order_no)
    TextView tvOrderNo;
    @BindView(R.id.tv_stock)
    TextView tvStock;
    @BindView(R.id.tv_place_name)
    TextView tvPlaceName;
    @BindView(R.id.tv_dep)
    TextView tvDep;
    @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_pro)
    RecyclerView rvPro;
    private OrderDetail dataBean;
    private ProductOrderDetailProductAdapter proAdapter;
    private List<OrderDetail.OrderItemsBean> proList;
    private String identity;
    private Integer status;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        rvPro.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        status = TextUtils.isEmpty(getIntent().getStringExtra("status"))?0:Integer.valueOf(getIntent().getStringExtra("status"));
        identity = getIntent().getStringExtra("identity");
    }

    @Override
    protected void initData() {
        getData();
        proList = new ArrayList<>();
        proAdapter = new ProductOrderDetailProductAdapter(R.layout.item_order_product, proList, status);
        proAdapter.bindToRecyclerView(rvPro);
    }

    private void getData() {
        RtfUtils.getRtf().productOrderDetail(getIntent().getStringExtra("id"), "3").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;
                }
                dataBean = JSON.parseObject(data.getData().toString(), OrderDetail.class);
                proList.addAll(dataBean.getOrderItems());
                proAdapter.setStatus(dataBean.getStatus());
                proAdapter.notifyDataSetChanged();
                tvStock.setText(dataBean.getExpandInfo().getOuterStoreName());
                tvDep.setText(dataBean.getLaunchDeptName());
                tvPlaceName.setText(dataBean.getLaunchUserName());
                tvOrderNo.setText(dataBean.getId());
                tvTime.setText(dataBean.getCreatedTime());
                tvOrderPrice.setText(MathUtils.converData(dataBean.getLaunchOrderAmount(),3));
            }
        });
    }

    @Override
    protected void initEvent() {
        proAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.tv_get:
                        startActivity(UseApplyActivity.class,
                                "status", dataBean.getStatus() + "",
                                "orderType", dataBean.getExpandInfo().getOrderType(),
                                "from", "1",
                                "customerId", dataBean.getCustomerId()+"",
                                "num", proList.get(position).getCanUseProductSum() + "",
                                "product", proList.get(position).getProductName(),
                                "simpleCode", proList.get(position).getProductSimpleCode(),
                                "orderId", getIntent().getStringExtra("id"),
                                "orderItemId", proList.get(position).getId(),
                                "productId", proList.get(position).getProductId() + "");
                        break;
                    case R.id.tv_add:
                        startActivity(AddStockApplyActivity.class,
                                "status", dataBean.getStatus() + "",
                                "num", proList.get(position).getCanInStoreProductSum() + "",
                                "product", proList.get(position).getProductName(),
                                "simpleCode", proList.get(position).getProductSimpleCode(),
                                "orderId", getIntent().getStringExtra("id"),
                                "orderItemId", proList.get(position).getId(),
                                "productId", proList.get(position).getProductId() + "");
                        break;
                    case R.id.tv_return:
                        startActivity(ReturnApplyActivity.class,
                                "identity", identity,
                                "status", dataBean.getStatus() + "",
                                "num", proList.get(position).getCanUseProductSum() + "",
                                "enterStockType", dataBean.getEnterStockType(),
                                "deptId",dataBean.getLaunchDeptId()+"",
                                "returnNum", TextUtils.equals(dataBean.getEnterStockType(), "1") ?
                                        proList.get(position).getCanOutReturnSum() : proList.get(position).getCanUsedReturnSum(),
                                "product", proList.get(position).getProductName(),
                                "simpleCode", proList.get(position).getProductSimpleCode(),
                                "orderId", getIntent().getStringExtra("id"),
                                "orderItemId", proList.get(position).getId(),
                                "productId", proList.get(position).getProductId() + "");
                        break;
                    case R.id.tv_flow:
                        startActivity(ProFlowActivity.class,
                                "status", dataBean.getStatus() + "",
                                "num", proList.get(position).getStoreSum() + "",
                                "product", proList.get(position).getProductName(),
                                "simpleCode", proList.get(position).getProductSimpleCode(),
                                "orderId", getIntent().getStringExtra("id"),
                                "productId", proList.get(position).getProductId() + "",
                                "orderItemId", proList.get(position).getId(),
                                "info", JSON.toJSONString(proList.get(position)));
                        break;
                }
            }
        });
    }

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

}
