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

import android.content.Intent;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.RelativeLayout;
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.event.AddMachineAddChildEvent;
import com.wd.workoffice.bean.event.RefreshMachineEvent;
import com.wd.workoffice.bean.mesBean.MesProBean;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.activity.mes.ticket.MesTicketProActivity;
import com.wd.workoffice.util.WorkUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.Map;

import androidx.annotation.Nullable;
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 MesStockAddActivity extends WorkToolBarActivity {
    @BindView(R.id.tv_pro)
    TextView tvPro;
    @BindView(R.id.rl_pro)
    RelativeLayout rlPro;
    @BindView(R.id.et_img_code)
    TextView etImgCode;
    @BindView(R.id.tv_stock)
    TextView tvStock;
    @BindView(R.id.et_content)
    EditText etContent;
    private Map<String, Object> param;
    private MesProBean.ListBean choosePro;
    private String from;

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

    @Override
    protected void initData() {
        from = getIntent().getStringExtra("from");
        param = WorkUtils.simpleParam();
        param.put("continuousCasterCode", getIntent().getStringExtra("continuousCasterCode"));
    }


    @Override
    protected void initEvent() {
    }

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


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_finish, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.finish:
                submit();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void submit() {
        if (choosePro == null) {
            toast("请选择产品");
            return;
        }
        String content = etContent.getText().toString();
        param.put("otherInformationExplain", content);
        param.put("productsName", choosePro.getProductName());
        param.put("figureNumber", choosePro.getDrawingNumber());
//        param.put("stockNumber", choosePro.gets);
        if (TextUtils.equals(from, "1")) {
            EventBus.getDefault().post(new AddMachineAddChildEvent(5, JSON.toJSONString(param)));
            finish();
        } else {
            RtfUtils.getMesRtf().stockAdd("1", "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;
                    }
                    finish();
                    toast("添加成功");
                    EventBus.getDefault().post(new RefreshMachineEvent());
                }
            });
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 10001 && resultCode == 10001) {
            choosePro = JSON.parseObject(data.getStringExtra("data"), MesProBean.ListBean.class);
            tvPro.setText(choosePro.getProductName());
            etImgCode.setText(choosePro.getDrawingNumber());
//            tvStock.setText(choosePro.get);
        }
    }

    @OnClick(R.id.rl_pro)
    public void onViewClicked() {
        startActivityForResult(MesUserProActivity.class, 10001, "id", getIntent().getStringExtra("id"));
    }
}
