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.XsDqgzWcSaveBean;
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 XsDqgzWcActivity extends BaseActivity {

    InputLayout ilJhmc;
    InputLayout ilZxqk;
    InputLayout ilCzwt;
    InputLayout ilBz;
    Button btn_save;

    private XsDqGzZxListBean.DataBean dqgz;

    private void findView() {
        ilJhmc = findViewById(R.id.il_jhmc);
        ilZxqk = findViewById(R.id.il_zxqk);
        ilCzwt = findViewById(R.id.il_czwt);
        ilBz = findViewById(R.id.il_bz);
        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_wc;
    }

    @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() {

        XsDqgzWcSaveBean bean = new XsDqgzWcSaveBean();
        bean.setAction("DQGZ_ZXJL_SET");
        bean.setZxid(dqgz.getZxid());
        bean.setZxqk(ilZxqk.getContent());
        bean.setCzwt(ilCzwt.getContent());
        bean.setZxbz(ilBz.getContent());
        bean.setZxr((String) SPUtils.get(context, Contans.USERNAME, ""));

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