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

import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;

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.AddMachineModifyChildEvent;
import com.wd.workoffice.bean.event.RefreshMachineEvent;
import com.wd.workoffice.bean.mesBean.MachineDetailBean;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.util.WorkUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.Map;

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 MesMiddleModifyActivity extends WorkToolBarActivity {

    @BindView(R.id.et_middle_status)
    EditText etMiddleStatus;
    @BindView(R.id.et_three_status)
    EditText etThreeStatus;
    @BindView(R.id.et_type)
    EditText etType;
    private Map<String, Object> param;
    private MachineDetailBean.SettleListBean dataBean;
    private String from;

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

    @Override
    protected void initData() {
        dataBean = JSON.parseObject(getIntent().getStringExtra("data"), MachineDetailBean.SettleListBean.class);
        param = WorkUtils.simpleParam();
        param.put("continuousCasterCode", dataBean.getContinuousCasterCode());
        param.put("id", dataBean.getId());
        etMiddleStatus.setText(dataBean.getLadleContractSituation());
        etThreeStatus.setText(dataBean.getContinuousCastingContractSituation());
        etType.setText(dataBean.getSettlementMethod());
        from = getIntent().getStringExtra("from");
    }


    @Override
    protected void initEvent() {
    }

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


    @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() {
        String middleStatus = etMiddleStatus.getText().toString();
        String threeStatus = etThreeStatus.getText().toString();
        String type = etType.getText().toString();
        param.put("ladleContractSituation", middleStatus);
        param.put("continuousCastingContractSituation", threeStatus);
        param.put("settlementMethod", type);
        if (TextUtils.equals(from, "1")) {
            EventBus.getDefault().post(new AddMachineModifyChildEvent(5, JSON.toJSONString(param)
                    , Integer.valueOf(getIntent().getStringExtra("position"))));
            finish();
            return;
        }
        RtfUtils.getMesRtf().middleAdd("1", "2", 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.getMsg());
                    return;
                }
                finish();
                toast("添加成功");
                EventBus.getDefault().post(new RefreshMachineEvent());
            }
        });
    }


}
