package com.rehome.sgbaxj.activity.qfgd;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.rehome.sgbaxj.R;
import com.rehome.sgbaxj.adapter.QfBmlbinfoAdapter;
import com.rehome.sgbaxj.base.BaseActivity3;
import com.rehome.sgbaxj.bean.QfbmlistBean;
import com.rehome.sgbaxj.bean.QfsblistRequestBean;
import com.rehome.sgbaxj.contans.Contans;
import com.rehome.sgbaxj.utils.GsonUtils;
import com.rehome.sgbaxj.utils.HttpListener;
import com.rehome.sgbaxj.utils.NohttpUtils;
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 2018/4/18.
 */

public class ZRbzBmListActivity extends BaseActivity3 {

    RecyclerView rlv;
    List<QfbmlistBean.DataBean> datas;
    QfBmlbinfoAdapter qfBmlbinfoAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_qfbmlist;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {
        initToolbar("部门列表", "确定", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (QfbmlistBean.DataBean dataBean : qfBmlbinfoAdapter.getDatas()) {

                    if (dataBean.isSelected()) {

                        Intent resultIntent = new Intent();

                        Bundle bundle = new Bundle();

                        bundle.putString("BMID", dataBean.getBMID());

                        bundle.putString("BMMC", dataBean.getBMMC());

                        resultIntent.putExtras(bundle);

                        setResult(RESULT_OK, resultIntent);

                        finish();

                    }
                }
            }
        });

        datas = new ArrayList<>();
        requestDatas();
    }

    private void requestDatas() {

        final Request<String> requset = NoHttp.createStringRequest(Contans.IP + Contans.QFGD, RequestMethod.POST);
        requset.setDefineRequestBodyForJson(createJson());
        NohttpUtils.getInstance().add(this, 0, requset, new HttpListener<String>() {
            @Override
            public void onSucceed(int what, Response<String> response) {
                try {
                    QfbmlistBean qfbmlistBean = GsonUtils.GsonToBean(response.get(), QfbmlistBean.class);

                    datas.clear();
                    datas.addAll(qfbmlistBean.getData());
                    setAdapter();

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

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

            }
        });
    }


    private void setAdapter() {

        qfBmlbinfoAdapter = new QfBmlbinfoAdapter(context, datas);

        rlv.setLayoutManager(new LinearLayoutManager(context));

        rlv.setAdapter(qfBmlbinfoAdapter);

        qfBmlbinfoAdapter.setOnChildItemClickListener(new QfBmlbinfoAdapter.OnChildItemClickListener() {
            @Override
            public void onClick(QfbmlistBean.DataBean dataBean) {
//                showToast(dataBean.getBMID());
            }
        });

    }


    private String createJson() {
        QfsblistRequestBean info = new QfsblistRequestBean();
        info.setAction("Q4GD_ZRBZ_GET");
//        info.setYHID((String) SPUtils.get(context, Contans.USERNAME, ""));
//        info.setSJBM("");
        String json = GsonUtils.GsonString(info);
        return json;
    }
}
