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

import android.text.TextUtils;
import android.widget.EditText;
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.OrderDetail;
import com.wd.workoffice.bean.event.CheckAddStockEvent;
import com.wd.workoffice.bean.workEnum.OrderType;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.util.MathUtils;
import com.wd.workoffice.util.WorkUtils;

import org.greenrobot.eventbus.EventBus;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
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 AddStockApplyAddActivity extends WorkToolBarActivity {
    @BindView(R.id.tv_bottom)
    TextView tvBottom;
    @BindView(R.id.et_img_code)
    TextView etImgCode;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.et_num)
    EditText etNum;
    @BindView(R.id.et_remark)
    EditText etRemark;
    @BindView(R.id.tv_remark_num)
    TextView tvRemarkNum;
    @BindView(R.id.et_box_num)
    EditText etBoxNum;
    private Map<String, Object> param;
    private String orderType;
    private OrderDetail dataBean;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        WorkUtils.addDecimalsListener(etNum);
        WorkUtils.addNumListener(etRemark, tvRemarkNum);
    }

    @Override
    protected void initData() {
        orderType = getIntent().getStringExtra("orderType");
//        tvNum.setText(String.format("入库数量(%s)", getIntent().getStringExtra("num")));
        param = new HashMap<>();
        param.put("productId", getIntent().getStringExtra("productId"));
        param.put("simpleCode", getIntent().getStringExtra("simpleCode"));
        etImgCode.setText(getIntent().getStringExtra("simpleCode"));
        getNum();
    }

    /**
     * 获取数量
     */
    private void getNum() {
        Observable<BaseBean> observable;
        if (TextUtils.equals(orderType, OrderType.SALE.getCode())) {
            observable = RtfUtils.getRtf().saleOrderDetail(getIntent().getStringExtra("id"),"1");
        } else if (TextUtils.equals(orderType, OrderType.OUT_BUY.getCode())) {
            observable = RtfUtils.getRtf().purchaseOrderDetail(getIntent().getStringExtra("id"),"1");
        } else if (TextUtils.equals(orderType, OrderType.SUPPLIER.getCode())) {
            observable = RtfUtils.getRtf().supplierOrderDetail(getIntent().getStringExtra("id"),"1");
        } else if (TextUtils.equals(orderType, OrderType.CRUCIBLE.getCode())) {
            observable = RtfUtils.getRtf().crucibleOrderDetail(getIntent().getStringExtra("id"),"1");
        } else if (TextUtils.equals(orderType, OrderType.IN.getCode())) {
            observable = RtfUtils.getRtf().insideOrderDetail(getIntent().getStringExtra("id"),"1");
        } else {
            observable = RtfUtils.getRtf().insideOrderDetail(getIntent().getStringExtra("id"),"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;
                }
                dataBean = JSON.parseObject(data.getData().toString(), OrderDetail.class);
                List<OrderDetail.OrderItemsBean> orderItems = dataBean.getOrderItems();
                for (OrderDetail.OrderItemsBean orderItem : orderItems) {
                 if (TextUtils.equals(orderItem.getId(),getIntent().getStringExtra("orderItemId"))) {
                     tvNum.setText(String.format("入库数量(最大入库数量%s)", MathUtils.converData(orderItem.getCanInStoreProductSum(),3)));
                 }
                }
            }
        });
    }

    @Override
    protected void initEvent() {
    }


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


    @OnClick(R.id.tv_bottom)
    public void onViewClicked() {
        submit();
    }


    private void submit() {
        String num = etNum.getText().toString();
        String remark = etRemark.getText().toString();
        String boxCode = etBoxNum.getText().toString();
        if (TextUtils.isEmpty(num)) {
            toast("请输入库数量");
            return;
        }
        if (dataBean!=null) {
            List<OrderDetail.OrderItemsBean> orderItems = dataBean.getOrderItems();
            for (OrderDetail.OrderItemsBean orderItem : orderItems) {
                if (TextUtils.equals(orderItem.getId(), getIntent().getStringExtra("orderItemId"))) {
                    if (new BigDecimal( Double.valueOf(num)).compareTo(new BigDecimal(orderItem.getCanInStoreProductSum())) ==1){
                        toast(String.format("可申请入库数量最大为%s",MathUtils.converData(orderItem.getCanInStoreProductSum(), 3)));
                        return;
                    }
                }

            }
        }
        param.put("productAmount", num);
        param.put("boxCode", boxCode);
        param.put("orderItemId", getIntent().getStringExtra("orderItemId"));
        param.put("applyRemark", remark);
        Observable<BaseBean> observable;
        if (TextUtils.equals(orderType, OrderType.SALE.getCode())) {
            observable = RtfUtils.getRtf().addStockApplyAdd(getIntent().getStringExtra("id"), WorkUtils.convertMapToBody(param));
        } else if (TextUtils.equals(orderType, OrderType.OUT_BUY.getCode())) {
            observable = RtfUtils.getRtf().addPoStockApplyAdd(getIntent().getStringExtra("id"), WorkUtils.convertMapToBody(param));
        } else if (TextUtils.equals(orderType, OrderType.SUPPLIER.getCode())) {
            observable = RtfUtils.getRtf().addFoStockApplyAdd(getIntent().getStringExtra("id"), WorkUtils.convertMapToBody(param));
        } else if (TextUtils.equals(orderType, OrderType.CRUCIBLE.getCode())) {
            observable = RtfUtils.getRtf().addStockCoApplyAdd(getIntent().getStringExtra("id"), WorkUtils.convertMapToBody(param));
        } else if (TextUtils.equals(orderType, OrderType.IN.getCode())) {
            observable = RtfUtils.getRtf().addStockIoApplyAdd(getIntent().getStringExtra("id"), WorkUtils.convertMapToBody(param));
        } else {
            observable = RtfUtils.getRtf().addStockApplyAdd(getIntent().getStringExtra("id"), WorkUtils.convertMapToBody(param));
        }
        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;
                }
                toast("提交申请成功");
                EventBus.getDefault().post(new CheckAddStockEvent());
                finish();
            }

        });
    }

}
