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

import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
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.ChooseDeptBean;
import com.wd.workoffice.bean.mesBean.FeedbackDetailBean;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.util.DataKeeper;
import com.wd.workoffice.util.UserKeeper;
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 flexible.xd.android_base.utils.TimeUtils;
import io.reactivex.disposables.Disposable;

/**
 * 二、质量部信息详情
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class MesQualityDeptActivity extends WorkToolBarActivity {


    @BindView(R.id.tv_desc)
    EditText tvDesc;
    @BindView(R.id.tv_dept)
    TextView tvDept;
    @BindView(R.id.rl_dept)
    RelativeLayout rlDept;
    private Map<String, Object> param;

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

    @Override
    protected void initData() {
        param = WorkUtils.simpleParam();
        FeedbackDetailBean data = JSON.parseObject(getIntent().getStringExtra("data"), FeedbackDetailBean.class);
        param.put("id", data.getId());
        param.put("procId", data.getProcId());
    }

    private void submit() {
        param.put("factoryAnalysis", tvDesc.getText().toString());
        param.put("responsibleQualityId", UserKeeper.getInstance().getMesUid());
        param.put("responsibleQualityDate", TimeUtils.getNowString());
        RtfUtils.getMesRtf().startFeedback(WorkUtils.getMesMenuId("市场服务","质量反馈"), 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();
            }
        });
    }

    @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);
    }

    @Override
    protected void initEvent() {
    }

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

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 10001:
                if (resultCode == 10001) {
                    ChooseDeptBean.ListBean manager = JSON.parseObject(data.getStringExtra("data"), ChooseDeptBean.ListBean.class);
                    param.put("siteId",manager.getSiteId());
                    param.put("siteCode", manager.getSiteCode());
                    param.put("siteName", manager.getSiteName());
                    tvDept.setText(manager.getSiteName());
                }
                break;
        }
    }



    @OnClick(R.id.rl_dept)
    public void onViewClicked() {
        startActivityForResult(MesChooseQuestionDeptActivity.class,10001);
    }
}
