package com.example.administrator.zhapp.activity.xscbgl;

import android.view.View;
import android.widget.Button;

import com.example.administrator.zhapp.R;
import com.example.administrator.zhapp.activity.BaseActivity;
import com.example.administrator.zhapp.contans.Contans;
import com.example.administrator.zhapp.entity.XsDqGzZxListBean;
import com.example.administrator.zhapp.entity.XsDqgzYqQxSaveBean;
import com.example.administrator.zhapp.entity.XsResultBaseBean;
import com.example.administrator.zhapp.utils.GsonUtils;
import com.example.administrator.zhapp.utils.HttpListener;
import com.example.administrator.zhapp.utils.NohttpUtils;
import com.example.administrator.zhapp.utils.SPUtils;
import com.example.administrator.zhapp.weight.CommitDialog;
import com.example.administrator.zhapp.weight.InputLayout;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;


/**
 * 定期工作执行取消
 */
public class XsDqgzQxActivity extends BaseActivity {

    InputLayout ilJhmc;
    InputLayout ilYy;
    Button btn_save;


    private XsDqGzZxListBean.DataBean dqgz;

    private void findView() {
        ilJhmc = findViewById(R.id.il_jhmc);
        ilYy = findViewById(R.id.il_yy);
        btn_save = findViewById(R.id.btn_save);
        btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CommitDialog dialog = new CommitDialog(context, new CommitDialog.CommitDialogListener() {
                    @Override
                    public void confirm() {
                        saveData();
                    }

                    @Override
                    public void cancel() {

                    }
                });
                dialog.setTvMsg("是否完成该工作?");
                dialog.show();
            }
        });
    }

    @Override
    public int getContentViewID() {
        return R.layout.activity_xs_dqgz_qx;
    }

    @Override
    protected void initView() {

        findView();
        setTitle("工作取消");
        setBack();

        dqgz = (XsDqGzZxListBean.DataBean) getIntent().getExtras().get(Contans.KEY_DQGZ);
        if (dqgz != null) {
            ilJhmc.setContent(dqgz.getJhmc());
        }
    }

    @Override
    protected void initData() {

    }

    private void saveData() {

        final Request<String> requset = NoHttp.createStringRequest(Contans.IP + Contans.XSDJ, RequestMethod.POST);
        requset.setDefineRequestBodyForJson(createJson());

        NohttpUtils.getInstance().add(this, 0, requset, new HttpListener<String>() {
            @Override
            public void onSucceed(int what, Response<String> response) {

                try {
                    XsResultBaseBean bean = GsonUtils.GsonToBean(response.get(), XsResultBaseBean.class);
                    if (bean != null) {
                        showToast(bean.getMsg());
                        if (bean.getState() == 1) {
                            finish();
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailed(int what, Response<String> response) {

            }
        });

    }

    private String createJson() {

        XsDqgzYqQxSaveBean bean = new XsDqgzYqQxSaveBean();
        bean.setAction("DQGZ_ZXBG_SET");
        bean.setZc((String) SPUtils.get(context, Contans.BZBH, ""));
        bean.setZxid(dqgz.getZxid());
        bean.setBglx("2");
        bean.setBgms(ilYy.getContent());
        bean.setBgxgr("");
        bean.setBgyq("");
        bean.setBgbc("");
        bean.setSqr((String) SPUtils.get(context, Contans.USERNAME, ""));

        String json = GsonUtils.GsonString(bean);
        return json;
    }
}
