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

import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
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.CheckFeedbackEvent;
import com.wd.workoffice.bean.mesBean.BoxBean;
import com.wd.workoffice.bean.mesBean.ChooseMachineBean;
import com.wd.workoffice.bean.mesBean.DefectBean;
import com.wd.workoffice.bean.mesBean.MesClientBean;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.activity.mes.ticket.MesTicketClientActivity;
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 MesQualityAddActivity extends WorkToolBarActivity {

    @BindView(R.id.tv_client)
    TextView tvClient;
    @BindView(R.id.rl_client)
    RelativeLayout rlClient;
    @BindView(R.id.tv_manager)
    TextView tvManager;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_box)
    TextView tvBox;
    @BindView(R.id.rl_box)
    RelativeLayout rlBox;
    @BindView(R.id.tv_pro_code)
    TextView tvProCode;
    @BindView(R.id.rl_pro)
    RelativeLayout rlPro;
    @BindView(R.id.tv_pro_name)
    TextView tvProName;
    @BindView(R.id.tv_spec)
    TextView tvSpec;
    @BindView(R.id.tv_img_code)
    TextView tvImgCode;
    @BindView(R.id.et_num)
    TextView etNum;
    @BindView(R.id.tv_mark_code)
    TextView tvMarkCode;
    @BindView(R.id.tv_machine)
    TextView tvMachine;
    @BindView(R.id.tv_steel)
    TextView tvSteel;
    @BindView(R.id.tv_defect)
    TextView tvDefect;
    @BindView(R.id.rl_defect)
    RelativeLayout rlDefect;
    @BindView(R.id.et_desc)
    TextView etDesc;
    private MesClientBean.ListBean chooseClient;
    private ChooseMachineBean chooseMachine;
    private Map<String, Object> param;
    private BoxBean chooseBox;
    private DefectBean.ListBean chooseDefect;

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


    @Override
    protected void initData() {
        param = WorkUtils.simpleParam();

    }


    @Override
    protected void initEvent() {
    }

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


    @OnClick({R.id.rl_machine, R.id.rl_client, R.id.rl_box, R.id.rl_defect})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_client:
                startActivityForResult(MesTicketClientActivity.class,
                        10001);
                break;
            case R.id.rl_machine:
                if (chooseClient == null) {
                    toast("请先选择客户");
                    return;
                }
                startActivityForResult(MesChooseMachineActivity.class,
                        10002, "code", chooseClient.getCustomerCode());
                break;
            case R.id.rl_box:
                if (chooseClient == null) {
                    toast("请先选择客户");
                    return;
                }
                startActivityForResult(MesChooseBoxActivity.class,
                        10003, "code", chooseClient.getCustomerCode());
                break;
            case R.id.rl_defect:
                if (chooseBox == null) {
                    toast("请先选择箱号");
                    return;
                }
                startActivityForResult(MesChooseDefectActivity.class,
                        10004, "code", chooseBox.getBasketId());
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 10001 && resultCode == 10001) {
            chooseClient = JSON.parseObject(data.getStringExtra("data"), MesClientBean.ListBean.class);
            tvClient.setText(chooseClient.getCustomerName());
//            tvPhone.setText(chooseClient.get());
            tvManager.setText(chooseClient.getSalesManagerName());
            param.put("customerId", chooseClient.getCustomerCode());
            param.put("customerName", chooseClient.getCustomerName());
            param.put("saleUserCode", chooseClient.getSalesManagerCode());
            param.put("saleUserName", chooseClient.getSalesManagerName());
        } else if (requestCode == 10002 && resultCode == 10001) {
            chooseMachine = JSON.parseObject(data.getStringExtra("data"), ChooseMachineBean.class);
            tvMachine.setText(chooseMachine.getCasterName());
            tvSteel.setText(chooseMachine.getSteelTypeId());
            etNum.setText(chooseMachine.getDefectCount());
            param.put("steelTypeId", chooseMachine.getSteelTypeId());
            param.put("casterName", chooseMachine.getCasterName());
            param.put("defectCount", chooseMachine.getDefectCount());
        } else if (requestCode == 10003 && resultCode == 10001) {
            chooseBox = JSON.parseObject(data.getStringExtra("data"), BoxBean.class);
            tvProCode.setText(chooseBox.getProductCode());
            tvProName.setText(chooseBox.getProductName());
            tvSpec.setText(chooseBox.getSpecification());
            tvImgCode.setText(chooseBox.getDrawingNumber());
            tvMarkCode.setText(chooseBox.getSerialNumber());
            tvBox.setText(chooseBox.getBoxId());
            param.put("serialNumber", chooseBox.getSerialNumber());
            param.put("drawingNumber", chooseBox.getDrawingNumber());
            param.put("specification", chooseBox.getSpecification());
            param.put("productCode", chooseBox.getProductCode());
            param.put("productName", chooseBox.getProductName());
            param.put("boxCode", chooseBox.getBoxNumber());
            param.put("boxId", chooseBox.getBoxId());
        } else if (requestCode == 10004 && resultCode == 10001) {
            chooseDefect = JSON.parseObject(data.getStringExtra("data"), DefectBean.ListBean.class);
            tvDefect.setText(chooseDefect.getDefectName());
            etDesc.setText(chooseDefect.getRemark());
            param.put("mainCause", chooseDefect.getRemark());
            param.put("defectCode", chooseDefect.getDefectCode());
            param.put("defectName", chooseDefect.getDefectName());
        }
    }

    @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:
                if (chooseClient == null) {
                    toast("请先选择客户");
                    break;
                }
                if (chooseMachine == null) {
                    toast("请先选择连铸机");
                    break;
                }
                if (chooseBox == null) {
                    toast("请先选择箱号");
                    break;
                }
                if (chooseDefect == null) {
                    toast("请先选择缺陷");
                    break;
                }
//                param.put("", "");
                submit();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void submit() {
        param.put("status","1");
        RtfUtils.getMesRtf().feedbackAdd("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;
                }
                EventBus.getDefault().post(new CheckFeedbackEvent());
                toast("新增成功");
                finish();
            }
        });
    }
}
