package com.rehome.sgbaxj.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 com.rehome.sgbaxj.DBModel.XSJJHDataBean;
import com.rehome.sgbaxj.DBModel.XSJJHXZDataBean;
import com.rehome.sgbaxj.DBModel.Xjjh;
import com.rehome.sgbaxj.R;
import com.rehome.sgbaxj.adapter.CheckScxsAdapter;
import com.rehome.sgbaxj.base.BaseActivity3;
import com.rehome.sgbaxj.contans.Contans;
import com.rehome.sgbaxj.utils.SPUtils;

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;

public class NoCheckedListActivity extends BaseActivity3 implements View.OnClickListener{

    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) {

            }
        });
        username = (String) SPUtils.get(context, Contans.USERID, "");
        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();//显示数据
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_left:
                finish();
                break;
            case R.id.tv_right:
                break;
        }
    }
    /**
     * 获取数据库的数据
     */
    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);
        });
    }
}