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

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.wd.workoffice.R;
import com.wd.workoffice.app.WorkToolBarActivity;
import com.wd.workoffice.bean.ProDetail;
import com.wd.workoffice.bean.ProductBean;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.util.WorkUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
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 WorkProDetailActivity extends WorkToolBarActivity {

    @BindView(R.id.tv_card)
    TextView tvCard;
    @BindView(R.id.tv_img_card)
    TextView tvImgCard;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_spec)
    TextView tvSpec;
    @BindView(R.id.tv_clazz)
    TextView tvClazz;
    @BindView(R.id.tv_kind)
    TextView tvKind;
    @BindView(R.id.tv_unit)
    TextView tvUnit;
    @BindView(R.id.tv_weight)
    TextView tvWeight;
    @BindView(R.id.btn_price)
    Button btnPrice;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.tv_status)
    TextView tvStatus;
    @BindView(R.id.tv_process)
    TextView tvProcess;
    @BindView(R.id.tv_dep)
    TextView tvDep;
    @BindView(R.id.tv_dep_name)
    TextView tvDepName;
    @BindView(R.id.tv_dep_type)
    TextView tvDepType;
    @BindView(R.id.tv_dep_address)
    TextView tvDepAddress;
    @BindView(R.id.tv_dep_price)
    TextView tvDepPrice;
    @BindView(R.id.tv_dep_person)
    TextView tvDepPerson;
    @BindView(R.id.tv_dep_person_phone)
    TextView tvDepPersonPhone;
    @BindView(R.id.tv_dep_person_dep)
    TextView tvDepPersonDep;
    @BindView(R.id.ll_on_sale)
    LinearLayout llOnSale;
    @BindView(R.id.ll_dep)
    LinearLayout llDep;
    private ProductBean.RecordsBean proBean;
    private String  id;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
    }

    @Override
    protected void initData() {
        String pro = getIntent().getStringExtra("pro");
        if (!TextUtils.isEmpty(pro)) {
            proBean = JSON.parseObject(pro, ProductBean.RecordsBean.class);
            id=proBean.getId()+"";
            getData(id);
            tvCard.setText(proBean.getSoleCode());
            tvImgCard.setText(proBean.getSimpleCode());
            tvName.setText(proBean.getName());
            tvSpec.setText(proBean.getSpec());
            tvClazz.setText(proBean.getProductTypeName());
            tvKind.setText(proBean.getOfKind());
            tvUnit.setText(proBean.getUnit());
            tvWeight.setText(String.valueOf(proBean.getWeight()));
            tvPrice.setText(String.valueOf(proBean.getInternalPrice()));
            tvStatus.setText(TextUtils.equals(proBean.getStatus(), "1") ? "正常" : "冻结");
            tvProcess.setText(proBean.getProcessType());
        }else {
            id=getIntent().getStringExtra("id");
            getData(id);
        }
    }

    private void getData(String id) {
        RtfUtils.getRtf().getProDetail(id).compose(Transformer.schedule()).subscribe(new WorkObserver<ProDetail>() {
            @Override
            public void doOnSubscribe(Disposable d) {
            }

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

            @Override
            public void onSuccess(ProDetail data) {
                hideLoading();
                if (data.getCode() != 0) {
                    toast(data.getMessage());
                    return;
                }
                ProDetail.DataBean info = data.getData();
                ProDetail.DataBean.ProductInfoBean productInfo = info.getProductInfo();
                tvCard.setText(productInfo.getSoleCode());
                tvImgCard.setText(productInfo.getSimpleCode());
                tvName.setText(productInfo.getName());
                tvSpec.setText(productInfo.getSpec());
                tvClazz.setText(productInfo.getProductTypeName());
                tvKind.setText(productInfo.getOfKind());
                tvUnit.setText(productInfo.getUnit());
                tvWeight.setText(String.valueOf(productInfo.getWeight()));
                tvPrice.setText(String.valueOf(productInfo.getInternalPrice()));
                tvStatus.setText(TextUtils.equals(productInfo.getStatus(), "1") ? "正常" : "冻结");
                tvProcess.setText(productInfo.getProcessType());

                if (info.getOutDeptRelList() != null && info.getOutDeptRelList().size() != 0) {
                    tvDep.setText(info.getOutDeptRelList().get(0).getDeptName());
                    llOnSale.setVisibility(View.VISIBLE);
                }
                if (info.getTransitPartRelList() != null && info.getTransitPartRelList().size() != 0) {
                    ProDetail.DataBean.TransitPartRelListBean depData = info.getTransitPartRelList().get(0);
                    llDep.setVisibility(View.VISIBLE);
                    tvDepName.setText(depData.getName());
                    tvDepType.setText("类别:" + WorkUtils.getClientType(depData.getPartTypeCode()));
                    tvDepAddress.setText("地址:" + depData.getAddress());
                    tvDepPrice.setText("渠道价格:" + (depData.getChannelPrice() == null ? "无" : String.valueOf(depData.getChannelPrice())));
                    tvDepPerson.setText("维护人:" + depData.getAccountName());
                    tvDepPersonPhone.setText("维护人手机号:" + depData.getPhone());
                    tvDepPersonDep.setText("维护部门:" + depData.getDeptName());
                }

//        transitPartRelList 往来单位关联
//     *      partTypeCode： SUPPLIER-供应商 CUSTOMER-客户 TRANSPORTERS-运输商
//     *      name：往来单位名称
//     *      address：往来单位地址
//     *      accountName：负责人
//     *      deptName：负责部门
//     *  outDeptRelList 上架情况
//     *      createdTime：上架时间
//     *      deptName：上架部门
//     *      outByUserName：上架操作人
            }
        });
    }

    @Override
    protected void initEvent() {
    }

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


    @OnClick(R.id.btn_price)
    public void onViewClicked() {
        startActivity(WorkProPriceActivity.class, "id", id);
    }

}
