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


import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import com.example.administrator.zhapp.R;
import com.example.administrator.zhapp.activity.BaseActivity;
import com.example.administrator.zhapp.adapter.XsDqgzBgListAdapter;
import com.example.administrator.zhapp.contans.Contans;
import com.example.administrator.zhapp.entity.XsDqgzBgListBean;
import com.example.administrator.zhapp.entity.XsRequestInfo;
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.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;


/**
 * 定期工作变更
 */
public class XsDqgzBgActivity extends BaseActivity {

    ListView lv;

    private List<XsDqgzBgListBean.DataBean> datas;

    private XsDqgzBgListAdapter adapter;


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


    @Override
    protected void initView() {
        lv = findViewById(R.id.lv);
        setTitle("定期工作变更");
        setBack();
        datas = new ArrayList<>();

        requestDatas();
    }

    @Override
    protected void initData() {

    }

    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 {
                    XsDqgzBgListBean xsDqgzBgListBean = GsonUtils.GsonToBean(response.get(), XsDqgzBgListBean.class);
                    if (xsDqgzBgListBean != null) {
                        if (xsDqgzBgListBean.getState() == 1) {
                            showToast(xsDqgzBgListBean.getMsg());
                            datas.clear();
                            datas.addAll(xsDqgzBgListBean.getData());
                            setAdapter();
                        } else {
                            showToast(xsDqgzBgListBean.getMsg());
                            datas.clear();
                            setAdapter();
                        }
                    } else {
                        showToast(R.string.data_error);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

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

            }
        });
    }

    private void setAdapter() {

        if (adapter == null) {
            adapter = new XsDqgzBgListAdapter(context, datas);
            lv.setAdapter(adapter);
            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                    Intent intent = new Intent(XsDqgzBgActivity.this, XsDqgzBgDetailActivity.class);
                    intent.putExtra(Contans.KEY_BG, datas.get(position));
                    startActivity(intent);

                }
            });

        } else {
            adapter.notifyDataSetChanged();
        }
    }

    private String createJson() {
        XsRequestInfo info = new XsRequestInfo();
        info.setAction("DQGZ_BGJL_GET");
//        info.setZc((String) SPUtils.get(context, Contans.BZBH, ""));
        info.setSpr((String) SPUtils.get(context, Contans.USERNAME, ""));
        info.setJz(Contans.YXCB_ZY_ID);
//        info.setSpr("ZH_Admin");
        info.setBglx("");
        info.setSpzt("");
        info.setRownum("");
        String json = GsonUtils.GsonString(info);
        return json;
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        requestDatas();
    }
}
