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

import androidx.annotation.IdRes;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

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.AuditPermissionBean;
import com.example.administrator.zhapp.entity.XsDqgzBgListBean;
import com.example.administrator.zhapp.entity.XsDqgzShSaveBean;
import com.example.administrator.zhapp.entity.XsRequestInfo;
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.AutoRadioGroup;
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 XsDqgzBgDetailActivity extends BaseActivity {

    InputLayout ilBgms;
    InputLayout ilBglxmc;
    InputLayout ilBgxgr;
    InputLayout ilSqr;
    InputLayout ilSqsj;
    InputLayout ilBgyq;
    InputLayout ilBgbc;
    LinearLayout llAudit;
    InputLayout ilSpr;
    InputLayout ilSpsj;
    InputLayout ilStatus;
    InputLayout ilSpyj;
    LinearLayout llAuditDetail;
    RadioButton rb1;
    RadioButton rb2;
    AutoRadioGroup rg;
    InputLayout ilSpyjInput;
    Button btnAudit;

    private XsDqgzBgListBean.DataBean bean;

    private void findView() {
        ilBgms = findViewById(R.id.il_bgms);
        ilBglxmc = findViewById(R.id.il_bglxmc);
        ilBgxgr = findViewById(R.id.il_bgxgr);
        ilSqr = findViewById(R.id.il_sqr);
        ilSqsj = findViewById(R.id.il_sqsj);
        ilBgyq = findViewById(R.id.il_bgyq);
        ilBgbc = findViewById(R.id.il_bgbc);
        llAudit = findViewById(R.id.ll_audit);
        ilSpr = findViewById(R.id.il_spr);
        ilSpsj = findViewById(R.id.il_spsj);
        ilStatus = findViewById(R.id.il_status);
        ilSpyj = findViewById(R.id.il_spyj);
        llAuditDetail = findViewById(R.id.ll_audit_detail);
        rb1 = findViewById(R.id.rb1);
        rb2 = findViewById(R.id.rb2);
        rg = findViewById(R.id.rg);
        ilSpyjInput = findViewById(R.id.il_spyj_input);
        btnAudit = findViewById(R.id.btn_audit);
        btnAudit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(ilSpyjInput.getContent())) {
                    showToast("意见不能为空");
                    return;
                }
                requestDatas();
            }
        });
    }

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

    @Override
    protected void initView() {

        setTitle("定期工作变更详情");
        setBack();
        bean = (XsDqgzBgListBean.DataBean) getIntent().getExtras().get(Contans.KEY_BG);

        if (bean != null) {

            if (bean.getShqx().equals("1")) {
                llAudit.setVisibility(View.VISIBLE);
            }
            showView();
        }

        ilSpyjInput.setContent("同意");

        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                if (checkedId == R.id.rb1) {
                    ilSpyjInput.setContent("同意");
                } else if (checkedId == R.id.rb2) {
                    ilSpyjInput.setContent("不同意");
                }
            }
        });
    }

    @Override
    protected void initData() {

    }

    private void showView() {

        ilBgms.setContent(bean.getBgms());
        ilBglxmc.setContent(bean.getBglxmc());
        ilBgxgr.setContent(bean.getBgxgr());
        ilSqr.setContent(bean.getSqr());
        ilSqsj.setContent(bean.getSqsj());
        ilBgyq.setContent(bean.getBgyq());
        ilBgbc.setContent(bean.getBgbc());
        ilSpr.setContent(bean.getSpr());
        ilSpsj.setContent(bean.getSpsj());
        ilStatus.setContent(bean.getStatus());
        ilSpyj.setContent(bean.getSpyj());

        if (TextUtils.isEmpty(bean.getSpr())) {
            llAuditDetail.setVisibility(View.GONE);
        }
    }

    private void requestDatas() {

        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 xsResultBaseBean = GsonUtils.GsonToBean(response.get(), XsResultBaseBean.class);
                    if (xsResultBaseBean != null) {
                        showToast(xsResultBaseBean.getMsg());
                        if (xsResultBaseBean.getState() == 1) {
                            finish();
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

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

            }
        });
    }

    private void getAuditPermission() {

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

        requset.setDefineRequestBodyForJson(createAuditJson());

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

                try {
                    AuditPermissionBean bean = GsonUtils.GsonToBean(response.get(), AuditPermissionBean.class);
                    if (bean != null) {
                        if (bean.getState() == 1) {
                            if (bean.getData().size() != 0) {
                                llAudit.setVisibility(View.VISIBLE);
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

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

            }
        });
    }

    private String createAuditJson() {
        XsRequestInfo info = new XsRequestInfo();
        info.setAction("GGJK_SHQX_GET");
        info.setUserid((String) SPUtils.get(context, Contans.USERNAME, ""));
        info.setShmk("10");
        String json = GsonUtils.GsonString(info);
        return json;
    }

    private String createJson() {
        XsDqgzShSaveBean info = new XsDqgzShSaveBean();
        info.setAction("DQGZ_BGSP_SET");
        info.setZc((String) SPUtils.get(context, Contans.BZBH, ""));
        info.setBgid(bean.getBgid());
        info.setSpr((String) SPUtils.get(context, Contans.USERNAME, ""));
//        info.setSpr("ZH_Admin");
        info.setSpzt(rb1.isChecked() ? "1" : "0");
        info.setSpyj(ilSpyjInput.getContent());
        String json = GsonUtils.GsonString(info);
        return json;
    }
}
