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

import android.app.Activity;
import android.content.Intent;
import androidx.annotation.IdRes;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
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.adapter.HmLbAdapter;
import com.example.administrator.zhapp.contans.Contans;
import com.example.administrator.zhapp.entity.HmLbBean;
import com.example.administrator.zhapp.entity.requestbean;
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.InputLayout;
import com.example.administrator.zhapp.weight.TimeSelect;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ruihong on 2019/8/19.
 */

public class HmShLbActivity extends BaseActivity {

    RadioButton rbDw1;
    RadioButton rbDw2;
    RadioButton rbDw3;
    RadioGroup rgDw;
    Button reachButton;
    ListView lv;
    InputLayout hmkssj;
    InputLayout hmjssj;

    private String DwResult = " ", SHZT;
    HmLbAdapter adapter;
    private List<HmLbBean.DataBean> datas;
    public final int REQUESTCODE_FROM_MAIN_TO_OTHER = 1;

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

    @Override
    public void initData() {
        datas = new ArrayList<>();
        hmkssj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TimeSelect dialog = new TimeSelect(context, new TimeSelect.CommitClickListener() {
                    @Override
                    public void confirm(String outPutDate, String outPutDate1, String outPutDate2) {
                        hmkssj.setContent(outPutDate1);
                    }
                });
                dialog.show();
            }
        });
        hmjssj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TimeSelect dialog = new TimeSelect(context, new TimeSelect.CommitClickListener() {
                    @Override
                    public void confirm(String outPutDate, String outPutDate1, String outPutDate2) {
                        hmjssj.setContent(outPutDate1);
                    }
                });
                dialog.show();
            }
        });
        rgDw.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                RadioButton radioButton = (RadioButton) group.findViewById(checkedId);
                DwResult = radioButton.getText().toString();
            }
        });

        reachButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (DwResult.equals(" ")) {
                    showToast("请选择状态");
                } else {
                    if (DwResult.equals("待审批")) {
                        SHZT = "0";
                    } else if (DwResult.equals("审批通过")) {
                        SHZT = "2";
                    } else if (DwResult.equals("审批拒绝")) {
                        SHZT = "3";
                    }
                    datas.clear();
                    requestDatas(SHZT);
                }
            }
        });
    }

    private void requestDatas(String SHZT) {


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

        requset.setDefineRequestBodyForJson(createJson(SHZT));

        NohttpUtils.getInstance().add(this, 0, requset, new HttpListener<String>() {
            @Override
            public void onSucceed(int what, Response<String> response) {
                HmLbBean bean = GsonUtils.GsonToBean(response.get(), HmLbBean.class);
                if (bean.getState() == 1) {
                    showToast(bean.getMsg());
                    datas.addAll(bean.getData());
                    setAdapter();
                }
            }

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

            }
        });
    }

    private void setAdapter() {

        if (adapter == null) {
            adapter = new HmLbAdapter(context, datas);
            lv.setAdapter(adapter);
            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent1 = new Intent(HmShLbActivity.this, HmShLbinfoActivity.class);
                    intent1.putExtra("list", datas.get(position));
                    startActivityForResult(intent1, REQUESTCODE_FROM_MAIN_TO_OTHER);
                }
            });
        } else {
            adapter.setNewData(datas);
        }
    }

    private String createJson(String SHZT) {
        String USERNAME = (String) SPUtils.get(context, Contans.USERNAME, "");
        requestbean info = new requestbean();
        info.setAction("GC_HMJL_SPLB");
        info.setYHID(USERNAME);
        info.setSHZT(SHZT);
        info.setST(hmkssj.getContent());
        info.setET(hmjssj.getContent());
        String json = GsonUtils.GsonString(info);
        return json;
    }

    @Override
    protected void initView() {
        rbDw1 = findViewById(R.id.rb_dw1);
        rbDw2 = findViewById(R.id.rb_dw2);
        rbDw3 = findViewById(R.id.rb_dw3);
        rgDw = findViewById(R.id.rg_dw);
        reachButton = findViewById(R.id.reachButton);
        lv = findViewById(R.id.lv);
        hmkssj = findViewById(R.id.hmkssj);
        hmjssj = findViewById(R.id.hmjssj);

        setBack();
        setTitle("豁免列表");
    }


    @Override
    protected void onResume() {
        super.onResume();
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
            case REQUESTCODE_FROM_MAIN_TO_OTHER:
                if (resultCode == Activity.RESULT_OK) {
                    datas.clear();
                    requestDatas(data.getStringExtra("STATE"));
                    if (data.getStringExtra("STATE").equals("2")) {
                        rbDw2.setChecked(true);
                    } else if (data.getStringExtra("STATE").equals("3")) {
                        rbDw3.setChecked(true);
                    }
                }
                break;

            default:
                break;

        }

    }
}
