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

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
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.FeedbackDetailBean;
import com.wd.workoffice.bean.mesBean.ManagerUserBean;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.util.DataKeeper;
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.disposables.Disposable;

/**
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class MesQualityClientActivity extends WorkToolBarActivity {


    @BindView(R.id.tv_client_status)
    EditText tvClientStatus;
    @BindView(R.id.tv_client_reply)
    EditText tvClientReply;
    @BindView(R.id.tv_client_person)
    TextView tvClientPerson;
    @BindView(R.id.rl_client_person)
    RelativeLayout rlClientPerson;
    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("rapidResponse", tvClientStatus.getText().toString());
        param.put("customerResponse", tvClientReply.getText().toString());
//        param.put("relatedRecord",tvClientRecord.getText().toString());
        String mesMenuId = WorkUtils.getMesMenuId("市场服务", "质量反馈");
        if (TextUtils.isEmpty(mesMenuId)){
            toast("您没有权限哦");
            return;
        }
        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_client;
    }


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

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 10001:
                if (resultCode == 10001) {
                    ManagerUserBean.ListBean manager = JSON.parseObject(data.getStringExtra("data"), ManagerUserBean.ListBean.class);
                    param.put("closeUserId", manager.getId());
                    param.put("closeUserDate", manager.getName());
                    tvClientPerson.setText(manager.getName());
                }
                break;
        }
    }

}
