package com.bjzc.bhzhgd.activity.qfgd;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bjzc.bhzhgd.R;
import com.bjzc.bhzhgd.adapter.QfSblbinfoAdapter;
import com.bjzc.bhzhgd.base.BaseActivity3;
import com.bjzc.bhzhgd.bean.QXRequestBean;
import com.bjzc.bhzhgd.bean.QfsblistBean;
import com.bjzc.bhzhgd.bean.QfsblistRequestBean;
import com.bjzc.bhzhgd.bean.QxdBean;
import com.bjzc.bhzhgd.contans.Contans;
import com.bjzc.bhzhgd.utils.GsonUtils;
import com.bjzc.bhzhgd.utils.HttpListener;
import com.bjzc.bhzhgd.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 org.greenrobot.eventbus.EventBus;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by ruihong on 2018/4/17.
 */

public class ComfirmSblistActivity extends BaseActivity3 {

    List<QfsblistBean.DataBean> datas;
    RecyclerView rlv;
    private QfSblbinfoAdapter qfSblbinfoAdapter;
    private QfsblistBean.DataBean dataBean;


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

    @Override
    public void initView() {
        initToolbar("设备列表", "确定", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (QfsblistBean.DataBean dataBean : qfSblbinfoAdapter.getDatas()) {
                    if (dataBean.isSelected()) {
                        String SBID = dataBean.getSBID();
                        requestDatas2(SBID);
                    }

                }
            }
        });
        rlv = findViewById(R.id.rlv);
    }

    @Override
    public void initData() {
        datas = new ArrayList<>();
        requestDatas();
    }


    private void requestDatas() {

        Log.i("app", Contans.IP + Contans.QFGD);
        final Request<String> requset = NoHttp.createStringRequest(Contans.IP + Contans.QFDDVICELIST, RequestMethod.POST);
        requset.setDefineRequestBodyForJson(createJson());
        NohttpUtils.getInstance().add(this, 0, requset, new HttpListener<String>() {
            @Override
            public void onSucceed(int what, Response<String> response) {
                try {
                    String resutlString = response.get();
                    Log.i("app",resutlString);
//                    QfsblistBean qfsblistBean = GsonUtils.GsonToBean(resutlString, QfsblistBean.class);
//                    datas.clear();
//                    datas.addAll(qfsblistBean.getData());
//                    setAdapter();

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

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

            }
        });
    }

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

    private void requestDatas2(String SBID) {

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

        requset.setDefineRequestBodyForJson(createJson2(SBID));

        NohttpUtils.getInstance().add(this, 0, requset, new HttpListener<String>() {
            @Override
            public void onSucceed(int what, Response<String> response) {
                try {
                    QxdBean qxdBean = GsonUtils.GsonToBean(response.get(), QxdBean.class);

                    if (qxdBean.getData().size() == 0) {

                        for (QfsblistBean.DataBean dataBean : qfSblbinfoAdapter.getDatas()) {

                            if (dataBean.isSelected()) {

                                Intent resultIntent = new Intent();

                                Bundle bundle = new Bundle();

                                bundle.putString("SBID", dataBean.getSBID());

                                bundle.putString("SBMC", dataBean.getSBMC());

                                resultIntent.putExtras(bundle);

                                setResult(RESULT_OK, resultIntent);

                                finish();

                            }

                        }

                    } else {

                        if (qxdBean.getState() == 1) {

                            AlertDialog.Builder builder = new AlertDialog.Builder(context);
                            builder.setTitle("提示");
                            builder.setTitle("此设备有提单,是否去查看");
                            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {

                                    for (QfsblistBean.DataBean dataBean : qfSblbinfoAdapter.getDatas()) {

                                        if (dataBean.isSelected()) {

                                            Intent resultIntent = new Intent();

                                            Bundle bundle = new Bundle();

                                            bundle.putString("SBID", dataBean.getSBID());

                                            bundle.putString("SBMC", dataBean.getSBMC());

                                            resultIntent.putExtras(bundle);

                                            setResult(RESULT_OK, resultIntent);

                                            finish();

                                        }

                                    }

                                }
                            });

                            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {

                                    for (QfsblistBean.DataBean dataBean : qfSblbinfoAdapter.getDatas()) {

                                        if (dataBean.isSelected()) {

//                                            Intent intent = new Intent(ComfirmSblistActivity.this, QfpmqxgdListActivity.class);
//                                            intent.putExtra("SBBH", dataBean.getSBID());
//                                            startActivity(intent);
                                        }
                                    }





                                }
                            });
                            builder.show();

                        }
                    }


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


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

            }
        });
    }

    private String createJson2(String SBID) {
        QXRequestBean info = new QXRequestBean();
        info.setAction("Q4GD_QXGD_GET");
        info.setYHID("");
        info.setGZDH("");
        info.setZRBM("");
        info.setGZYXJ("");
        info.setSBBH(SBID);
        info.setGZZT("");
        info.setPL_WK_DO("");
        info.setPL_WK_TE("");
        String json = GsonUtils.GsonString(info);
        return json;
    }

    private void setAdapter() {

        qfSblbinfoAdapter = new QfSblbinfoAdapter(context, datas);

        rlv.setLayoutManager(new LinearLayoutManager(context));

        rlv.setAdapter(qfSblbinfoAdapter);

        qfSblbinfoAdapter.setOnChildItemClickListener(new QfSblbinfoAdapter.OnChildItemClickListener() {
            @Override
            public void onClick(QfsblistBean.DataBean dataBean) {

//                Intent intent = new Intent(ComfirmSblistActivity.this, OtherSblistActivity.class);
//                intent.putExtra("FSBID", dataBean.getSBID());
//                intent.putExtra("SBMC", dataBean.getSBMC());
//                startActivity(intent);
//                finish();

            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


}
