package com.wd.workoffice.ui.activity.mes;

import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.wd.workoffice.R;
import com.wd.workoffice.app.BaseMesBean;
import com.wd.workoffice.app.WorkToolBarActivity;
import com.wd.workoffice.bean.mesBean.DispatchBean;
import com.wd.workoffice.bean.mesBean.MudBean;
import com.wd.workoffice.bean.mesBean.QualityTraceabilityBean;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.adapter.MudAdapter;
import com.wd.workoffice.ui.adapter.QualityDispatchAdapter;
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.disposables.Disposable;

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

    @BindView(R.id.tv_no)
    TextView tvNo;
    @BindView(R.id.tv_status)
    TextView tvStatus;
    @BindView(R.id.tv_single_code)
    TextView tvSingleCode;
    @BindView(R.id.tv_pro_name)
    TextView tvProName;
    @BindView(R.id.tv_pro_code)
    TextView tvProCode;
    @BindView(R.id.tv_img_code)
    TextView tvImgCode;
    @BindView(R.id.tv_spec)
    TextView tvSpec;
    @BindView(R.id.tv_box_num)
    TextView tvBoxNum;
    @BindView(R.id.tv_factory_name)
    TextView tvFactoryName;
    @BindView(R.id.tv_factory_code)
    TextView tvFactoryCode;
    @BindView(R.id.tv_code)
    TextView tvCode;
    @BindView(R.id.tv_version)
    TextView tvVersion;
    @BindView(R.id.tv_pro)
    TextView tvPro;
    @BindView(R.id.rv_mud)
    RecyclerView rvMud;
    @BindView(R.id.rv_dispatch)
    RecyclerView rvDispatch;
    private Map<String, Object> param;
    private List<DispatchBean> dispatchList;
    private QualityDispatchAdapter dispatchAdapter;
    private QualityTraceabilityBean.ListBean info;
    private List<MudBean> mudList;
    private MudAdapter mudAdapter;

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


    @Override
    protected void initData() {
        info = JSON.parseObject(getIntent().getStringExtra("info"), QualityTraceabilityBean.ListBean.class);
        tvNo.setText(String.format("单支码:%s", info.getSerialNumber ()));
        tvSingleCode.setText(info.getOrderNumber());
        tvProName.setText(info.getProductName());
        tvProCode.setText(info.getProductCode());
        tvImgCode.setText(info.getDrawingNumber());
        tvSpec.setText(info.getSpecification());
        tvBoxNum.setText(info.getBoxNumber());
        tvFactoryName.setText(info.getSiteName());
        tvFactoryCode.setText(info.getSiteCode());
        tvCode.setText(info.getManufactureOrderNumber());
        tvVersion.setText(info.getVerson());
        tvPro.setText(info.getSeriesName());

        mudList = new ArrayList<>();
        mudAdapter = new MudAdapter(R.layout.item_quality_traceability_mud, mudList);
        mudAdapter.bindToRecyclerView(rvMud);

        dispatchList = new ArrayList<>();
        dispatchAdapter = new QualityDispatchAdapter(R.layout.item_quality_traceability_dispatch, dispatchList);
        dispatchAdapter.bindToRecyclerView(rvDispatch);
        param = WorkUtils.simpleParam();
        param.put("orderId", info.getOrderId());
        param.put("productId", info.getProductId());
        getData();
    }


    private void getData() {
        RtfUtils.getMesRtf().dispatchDetail("1", WorkUtils.convertMapToBody(param)).compose(Transformer.schedule()).subscribe(new WorkObserver<BaseMesBean>() {
            @Override
            public void doOnSubscribe(Disposable d) {
            }

            @Override
            public void onFail(String errorMsg) {
                hideLoading();
                toast(errorMsg);
            }

            @Override
            public void onSuccess(BaseMesBean data) {
                hideLoading();
                if (!data.isSuccess()) {
                    toast(data.getMessage());
                    return;
                }
                List<DispatchBean> getList = JSON.parseArray(data.getData().toString(), DispatchBean.class);
                dispatchList.clear();
                dispatchList.addAll(getList);
                dispatchAdapter.notifyDataSetChanged();
            }
        });

        RtfUtils.getMesRtf().productMudDetail("1", WorkUtils.convertMapToBody(param)).compose(Transformer.schedule()).subscribe(new WorkObserver<BaseMesBean>() {
            @Override
            public void doOnSubscribe(Disposable d) {
            }

            @Override
            public void onFail(String errorMsg) {
                hideLoading();
                toast(errorMsg);
            }

            @Override
            public void onSuccess(BaseMesBean data) {
                hideLoading();
                if (!data.isSuccess()) {
                    toast(data.getMessage());
                    return;
                }
                List<MudBean> getList = JSON.parseArray(data.getData().toString(), MudBean.class);
                mudList.clear();
                mudList.addAll(getList);
                mudAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    protected void initEvent() {
    }


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


}
