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

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.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.WorkTaskBean;
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 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 WorkTaskActivity 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_no)
    TextView tvNo;
    @BindView(R.id.tv_desc)
    EditText tvDesc;
    @BindView(R.id.tv_time)
    EditText tvTime;
    @BindView(R.id.tv_status)
    TextView tvStatus;
    private WorkTaskBean.ListBean data;

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


    @Override
    protected void initData() {
        String info = getIntent().getStringExtra("data");
        if (!TextUtils.isEmpty(info)) {
            data = JSON.parseObject(info, WorkTaskBean.ListBean.class);
            tvTime.setText(data.getTime() + "");
            tvDesc.setText(data.getDescription());
        }

    }


    @Override
    protected void initEvent() {
    }


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


    @OnClick({R.id.tv_cancel, R.id.tv_save})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_cancel:
                finish();
                break;
            case R.id.tv_save:
                String desc = tvDesc.getText().toString();
                String time = tvTime.getText().toString();
                if (TextUtils.isEmpty(desc)) {
                    toast("描述不能为空");
                    return;
                }
                if (TextUtils.isEmpty(time)) {
                    toast("估计持续时间(h)不能为空");
                    return;
                }
                Map<String, Object> param = WorkUtils.simpleParam();
                param.put("status", "1");
                param.put("description", desc);
                param.put("time", time);
                param.put("woId", getIntent().getStringExtra("id"));
                Observable<BaseMesBean> baseMesBeanObservable;
                if (data == null) {
                    baseMesBeanObservable = RtfUtils.getMesRtf().saveActivity("1",
                            WorkUtils.convertMapToBody(param));
                } else {
                    param.put("id", data.getId());
                    baseMesBeanObservable = RtfUtils.getMesRtf().updateActivity("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;
        }
    }
}
