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

import android.content.DialogInterface;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.wd.workoffice.R;
import com.wd.workoffice.app.BaseMesBean;
import com.wd.workoffice.app.WorkToolBarActivity;
import com.wd.workoffice.bean.event.AddTaskEvent;
import com.wd.workoffice.bean.mesBean.TaskMaterials;
import com.wd.workoffice.bean.mesBean.WorkCodeBean;
import com.wd.workoffice.bean.mesBean.WorkTaskBean;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.util.DialogUtils;
import com.wd.workoffice.util.MathUtils;
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.Observable;
import io.reactivex.disposables.Disposable;

/**
 * 实际情况下的物料
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class WorkTrueMaterialsActivity extends WorkToolBarActivity {


    @BindView(R.id.tv_cancel)
    TextView tvCancel;
    @BindView(R.id.tv_save)
    TextView tvSave;
    @BindView(R.id.ll_bottom)
    LinearLayout llBottom;
    @BindView(R.id.tv_code)
    TextView tvCode;
    @BindView(R.id.ll_code)
    LinearLayout llCode;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_num)
    EditText tvNum;
    @BindView(R.id.tv_level)
    TextView tvLevel;
    @BindView(R.id.tv_send)
    TextView tvSend;
    private TaskMaterials.ListBean data;
    private WorkCodeBean.ListBean choosePeople;
    private WorkTaskBean.ListBean info;

    private String[] statusList = new String[]{"是", "否"};
    private Integer[] statusValue = new Integer[]{1, 0};
    private QMUIDialog.MenuDialogBuilder statusDialog;
    private int chooseStatus = -1;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        statusDialog = DialogUtils.listDialog(this);
        statusDialog.addItems(statusList, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                tvSend.setText(statusList[which]);
                chooseStatus = statusValue[which];

            }
        });
    }


    @Override
    protected void initData() {
        info = JSON.parseObject(getIntent().getStringExtra("info"),
                WorkTaskBean.ListBean.class);
        String dataStr = getIntent().getStringExtra("data");
        if (!TextUtils.isEmpty(dataStr)) {
            data = JSON.parseObject(dataStr, TaskMaterials.ListBean.class);
            choosePeople = new WorkCodeBean.ListBean();
            choosePeople.setMaterialCode(data.getItemCode());
            choosePeople.setMaterialName(data.getItemName());

            choosePeople.setStorageCode(data.getLocationName());
            if (data.getSueType()!=null){
                if (data.getSueType()) {
                    chooseStatus = 1;
                    tvSend.setText(statusList[0]);
                } else {
                    chooseStatus = 0;
                    tvSend.setText(statusList[1]);
                }
            }
            tvNum.setText(MathUtils.converData(data.getItemQty(), 3));
            tvName.setText(choosePeople.getMaterialName());
            tvLevel.setText(choosePeople.getStorageDesc());
            tvCode.setText(choosePeople.getMaterialCode());
        }

    }


    @Override
    protected void initEvent() {
    }


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


    @OnClick({R.id.ll_send,R.id.tv_cancel, R.id.tv_save, R.id.ll_code})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_send:
                statusDialog.show();
                break;
            case R.id.tv_cancel:
                finish();
                break;
            case R.id.tv_save:
                if (null == choosePeople) {
                    toast("请选择物料编码");
                    return;
                }
                if (TextUtils.isEmpty(tvNum.getText().toString())) {
                    toast("请填写数量");
                    return;
                }
                Map<String, Object> param = WorkUtils.simpleParam();
                param.put("actId", info.getId());
                param.put("actDesc", info.getDescription());
                param.put("actNum", info.getActNum());
                param.put("woId", info.getWoId());


                param.put("itemCode", choosePeople.getMaterialCode());
                param.put("itemName", choosePeople.getMaterialName());
                param.put("itemQty", tvNum.getText().toString());
                if (chooseStatus!=-1){
                    param.put("sueType", chooseStatus == 1);
                }

                Observable<BaseMesBean> baseMesBeanObservable;
                if (data == null) {
                    baseMesBeanObservable = RtfUtils.getMesRtf().saveMaterial("aa8faf47c93311e98334fa163e1166bd",
                            WorkUtils.convertMapToBody(param));
                } else {
                    param.put("id", data.getId());
                    baseMesBeanObservable = RtfUtils.getMesRtf().updateMaterial("1",
                            WorkUtils.convertMapToBody(param));
                }
                baseMesBeanObservable.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 AddTaskEvent());
                        finish();
                        toast("提交成功");
                    }
                });
                break;
            case R.id.ll_code:
                startActivityForResult(WorkTaskChooseCodeActivity.class, 10001
                        , "id", info.getWoId());
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 10001 && resultCode == 10001) {
            choosePeople = JSON.parseObject(data.getStringExtra("data"), WorkCodeBean.ListBean.class);
            tvName.setText(choosePeople.getMaterialName());
            tvLevel.setText(choosePeople.getStorageDesc());
            tvCode.setText(choosePeople.getMaterialCode());
        }
    }
}
