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.XsjhListAdapter;
import com.example.administrator.zhapp.contans.Contans;
import com.example.administrator.zhapp.entity.XsJhListBean;
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 XsBackLogActivity extends BaseActivity {

    ListView lv;

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

    private List<XsJhListBean.DataBean> datas;

    private XsjhListAdapter adapter;

    @Override
    protected void initView() {
        lv = findViewById(R.id.lv);
        setTitle("待办任务");
        setBack();
        requestDatas();

        datas = new ArrayList<>();
    }

    @Override
    protected void initData() {

    }

    private void requestDatas() {


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

        requset.setDefineRequestBodyForJson(createJson());

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

                try {
                    XsJhListBean xsJhListBean = GsonUtils.GsonToBean(response.get(), XsJhListBean.class);
                    if (xsJhListBean != null) {
                        if (xsJhListBean.getState() == 1) {
//                            showToast(xsJhListBean.getMsg());
                            datas.clear();
                            datas.addAll(xsJhListBean.getData());
                            setAdapter();
                        } else {
                            showToast(xsJhListBean.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 XsjhListAdapter(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(XsBackLogActivity.this, XscbqyActivity.class);
                    intent.putExtra(Contans.KEY_XSCBJH, datas.get(position));
                    startActivity(intent);
                }
            });

        } else {
            adapter.notifyDataSetChanged();
        }
    }

    private String createJson() {
        XsRequestInfo info = new XsRequestInfo();
        info.setAction("XSCB_ZXJL_GET");
        info.setZymc(Contans.YXCB_ZY_ID);
        info.setZc((String) SPUtils.get(context, Contans.BZBH, ""));
        String json = GsonUtils.GsonString(info);
        return json;
    }

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