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

import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.wd.workoffice.R;
import com.wd.workoffice.app.BaseBean;
import com.wd.workoffice.app.WorkToolBarActivity;
import com.wd.workoffice.bean.OrderBean;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.adapter.WorkWarehouseOrderDetailAdapter;

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 WorkWarehouseOrderDetailActivity extends WorkToolBarActivity {
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_order_type)
    TextView tvOrderType;
    @BindView(R.id.tv_order_dep)
    TextView tvOrderDep;
    @BindView(R.id.tv_receive_order_dep)
    TextView tvReceiveOrderDep;
    @BindView(R.id.tv_place_name)
    TextView tvPlaceName;
    @BindView(R.id.tv_receive_person)
    TextView tvReceivePerson;
    @BindView(R.id.tv_client)
    TextView tvClient;
    @BindView(R.id.tv_process)
    TextView tvProcess;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.rv_data)
    RecyclerView rvData;
    private OrderBean dataBean;
    private List<OrderBean.OrderItemsBean> dataList;
    private WorkWarehouseOrderDetailAdapter workWarehouseOrderDetailAdapter;

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

    @Override
    protected void initData() {
        dataList = new ArrayList<>();
        workWarehouseOrderDetailAdapter = new WorkWarehouseOrderDetailAdapter(R.layout.item_warehoust_order, dataList);
        workWarehouseOrderDetailAdapter.bindToRecyclerView(rvData);
        getData();
    }

    private void getData() {
        RtfUtils.getRtf().stockDetailFlow(getIntent().getStringExtra("id"), getIntent().getStringExtra("type")).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(), OrderBean.class);
                tvName.setText(dataBean.getId());
                tvProcess.setText(dataBean.getSubProcessName());
                tvClient.setText(dataBean.getCustomerName());
                tvOrderDep.setText(dataBean.getLaunchDeptName());
                tvOrderType.setText(dataBean.getOrderType());
                tvPlaceName.setText(dataBean.getLaunchUserName());
                tvPrice.setText(String.valueOf(dataBean.getLaunchOrderAmount()));
                tvReceiveOrderDep.setText(dataBean.getReceiveDeptName());
                tvReceivePerson.setText(dataBean.getReceiveUserName());
                tvTime.setText(dataBean.getCreatedTime());
                dataList.addAll(dataBean.getOrderItems());
                workWarehouseOrderDetailAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    protected void initEvent() {
    }

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

}
