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

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
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.WorkPeopleBean;
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 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 WorkTruePeopleActivity 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_code)
    TextView tvCode;
    @BindView(R.id.ll_code)
    LinearLayout llCode;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.tv_level)
    TextView tvLevel;
    private WorkTaskBean.ListBean data;
    private WorkPeopleBean.ListBean choosePeople;
    private WorkTaskBean.ListBean info;

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


    @Override
    protected void initData() {
        info = JSON.parseObject(getIntent().getStringExtra("info"),
                WorkTaskBean.ListBean.class);
        tvNo.setText(info.getActNum());
        String dataStr = getIntent().getStringExtra("data");
        if (!TextUtils.isEmpty(dataStr)) {
            data = JSON.parseObject(dataStr, WorkTaskBean.ListBean.class);
            choosePeople =new WorkPeopleBean.ListBean();
            choosePeople.setCraftId(data.getCraftId());
            choosePeople.setCraftName(data.getCraftName());
            choosePeople.setSkillId(data.getCraftName());
            choosePeople.setSkillCode(data.getCraftName());
            choosePeople.setUserCode(data.getUserCode());
            choosePeople.setUserId(data.getUserId());
            choosePeople.setName(data.getUserName());
            tvName.setText(choosePeople.getName());
            tvLevel.setText(choosePeople.getLevel());
            tvCode.setText(choosePeople.getUserCode());
            tvType.setText(choosePeople.getCraftName());
        }

    }


    @Override
    protected void initEvent() {
    }


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


    @OnClick({R.id.tv_cancel, R.id.tv_save, R.id.ll_code})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_cancel:
                finish();
                break;
            case R.id.tv_save:
                if (null == choosePeople) {
                    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("craftId", choosePeople.getCraftId());
                param.put("craftName", choosePeople.getCraftName());
                param.put("skillId", choosePeople.getSkillId());
                param.put("skillName", choosePeople.getSkillCode());
                param.put("userCode", choosePeople.getUserCode());
                param.put("userId", choosePeople.getUserId());
                param.put("userName", choosePeople.getName());
                param.put("woId", info.getWoId());
                param.put("delete", false);

                Observable<BaseMesBean> baseMesBeanObservable;
                if (data == null) {
                    baseMesBeanObservable = RtfUtils.getMesRtf().saveLabor("aa8faf47c93311e98334fa163e1166bd",
                            WorkUtils.convertMapToBody(param));
                } else {
                    param.put("id", data.getId());
                    baseMesBeanObservable = RtfUtils.getMesRtf().updateLabor("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(WorkTaskChoosePeopleActivity.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"), WorkPeopleBean.ListBean.class);
            tvName.setText(choosePeople.getName());
            tvLevel.setText(choosePeople.getLevel());
            tvCode.setText(choosePeople.getUserCode());
            tvType.setText(choosePeople.getCraftName());
        }
    }
}
