package com.bjzc.bhzhgd.activity.sbxj;


import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import org.litepal.crud.DataSupport;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.litepal.crud.DataSupport.where;

import com.bjzc.bhzhgd.DBModel.XSJJHDataBean;
import com.bjzc.bhzhgd.DBModel.XSJJHXZDataBean;
import com.bjzc.bhzhgd.DBModel.Xjjh;
import com.bjzc.bhzhgd.R;
import com.bjzc.bhzhgd.adapter.CheckScxsAdapter;
import com.bjzc.bhzhgd.base.BaseActivity3;
import com.bjzc.bhzhgd.contans.Contans;
import com.bjzc.bhzhgd.utils.RSAUtils;
import com.bjzc.bhzhgd.utils.SPUtils;

public class NoCheckedListActivity extends BaseActivity3 {

    ListView lv;
    TextView tvNodata;
    private View headView;
    private TextView noChecked;
    private CheckScxsAdapter adapter;
    private List<Xjjh> dbxjjh = new ArrayList<>();
    private List<String> deleteZxids = new ArrayList<>();
    private Map<String, List<XSJJHDataBean>> xsJjhDataBeanMap = new HashMap<>();
    private Map<String, List<XSJJHXZDataBean>> xsJjhBaDataBeanMap = new HashMap<>();
    private String type = null;
    private String username;

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

    @Override
    public void initView() {
        initToolbar("未检", "", new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        String UserIDTemp = (String) SPUtils.get(context, Contans.USERID, "");
        username = RSAUtils.decryptBASE64StrLocal(UserIDTemp);

        lv = findViewById(R.id.lv);
        tvNodata = findViewById(R.id.tv_nodata);
        Intent intent = getIntent();
        type = intent.getStringExtra("type");
        lv.setEmptyView(tvNodata);
        headView = View.inflate(context, R.layout.no_checkbox_scxscb_item, null);
        noChecked = headView.findViewById(R.id.tv_yjzj);
        noChecked.setText("未检/总计");
        lv.addHeaderView(headView, null, false);
    }

    @Override
    public void initData() {
        if (type != null && type.equals("BA")) {
            getDataInSQLBA();//获取保安数据
        } else {
            getDataInSQL();//获取运行，燃料数据
        }
        setListData();//显示数据
    }

    /**
     * 获取数据库的数据
     */
    public void getDataInSQLBA() {
        dbxjjh.clear();
        dbxjjh.addAll(where("download = ? and mk = ?  and username = ?", "1", type, username).find(Xjjh.class));
        xsJjhBaDataBeanMap.clear();
        for (int i = 0; i < dbxjjh.size(); i++) {
            Xjjh dataBean = dbxjjh.get(i);
            if (!dataBean.getZxid().equals("")) {
                List<XSJJHXZDataBean> qydDataBeen = DataSupport.where("zxid = ?  and username = ?", dataBean.getZxid(), username).find(XSJJHXZDataBean.class);
                int count = 0;
                for (int j = 0; j < qydDataBeen.size(); j++) {
                    if (!qydDataBeen.get(j).isBAChecked()) {
                        count++;
                    }
                }
                dataBean.setCountPercent(String.valueOf(count));
                xsJjhBaDataBeanMap.put(dataBean.getZxid(), qydDataBeen);
            }
        }
    }
    /**
     * 获取数据库的数据
     */
    public void getDataInSQL() {
        dbxjjh.clear();
        dbxjjh.addAll(where("download = ? and mk = ?  and username = ?", "1", type, username).find(Xjjh.class));
        xsJjhDataBeanMap.clear();
        for (int i = 0; i < dbxjjh.size(); i++) {
            Xjjh dataBean = dbxjjh.get(i);
            List<XSJJHDataBean> qydDataBeen = DataSupport.where("zxid = ?  and username = ?", dataBean.getZxid(), username).find(XSJJHDataBean.class);
            Log.i("app", String.valueOf(qydDataBeen.size()));
            int count = 0;
            for (int j = 0; j < qydDataBeen.size(); j++) {
                if (!qydDataBeen.get(j).isChecked()) {
                    count++;
                }
            }
            dataBean.setCountPercent(String.valueOf(count));
            xsJjhDataBeanMap.put(dataBean.getZxid(), qydDataBeen);
        }
    }
    /**
     *   列表适配数据
     */
    private void setListData() {
        adapter = new CheckScxsAdapter(context, dbxjjh);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener((adapterView, view, i, l) -> {
            Xjjh item = dbxjjh.get(i-1);
            Bundle bundle = new Bundle();
            Intent intent = new Intent(this, CheckedStatusListActivity.class);
            intent.putExtra("type", type);
            intent.putExtra("checkType", "noChecked");
            intent.putExtra("zxid", item.getZxid());
            intent.putExtras(bundle);
            startActivity(intent);
        });
    }
}