package com.example.administrator.zhapp.activity.xfbwxcgl;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import com.example.administrator.zhapp.BaseListViewAdapter.CommonAdapter;
import com.example.administrator.zhapp.BaseListViewAdapter.ViewHolder;
import com.example.administrator.zhapp.DBModel.XfBaxcRwqy;
import com.example.administrator.zhapp.DBModel.XfDjjh;
import com.example.administrator.zhapp.R;
import com.example.administrator.zhapp.activity.BaseActivity;
import com.example.administrator.zhapp.entity.GzInfo;
import org.litepal.crud.DataSupport;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 消防保卫巡查管理-保安巡更
 */
public class XbaxgActivity extends BaseActivity {

    ListView lv;
    TextView tvNodata;

    private CommonAdapter<GzInfo> adapter;
    private List<GzInfo> infos;
    private List<XfBaxcRwqy> rwqys;
    private List<XfDjjh> djjhs;
    private Map<String, List<XfBaxcRwqy>> map;

    private View headView;

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

    @Override
    protected void initView() {
        lv = findViewById(R.id.lv);
        tvNodata = findViewById(R.id.tv_nodata);
    }

    public void initData() {

        setTitle("保安巡更");
        setBack();
        infos = new ArrayList<>();
        rwqys = new ArrayList<>();
        djjhs = new ArrayList<>();
        map = new HashMap<>();
        getDataInSql();
    }

    //读取本地数据库数据
    private void getDataInSql() {

        djjhs.clear();
        rwqys.clear();
        infos.clear();

        djjhs = DataSupport.where("xctypes = ? and download = ?", "2", "1").find(XfDjjh.class);

        for (int i = 0; i < djjhs.size(); i++) {
            rwqys = DataSupport.where("jhid = ?", djjhs.get(i).getJhid()).find(XfBaxcRwqy.class);
            int yj = 0;
            int zong = rwqys.size();
            for (XfBaxcRwqy rwqy : rwqys) {
                if (rwqy.isChecked()) {
                    ++yj;
                }
            }
            if (zong != 0) {
                GzInfo gz = new GzInfo();
                gz.setXh(i + 1);
                gz.setYj(yj);
                gz.setZj(zong);
                gz.setQy(djjhs.get(i).getJhmc());
                gz.setJhid(djjhs.get(i).getJhid());
                infos.add(gz);

            }
        }

        if (adapter == null) {
            headView = View.inflate(context, R.layout.gz_item, null);
            headView.findViewById(R.id.head).setVisibility(View.VISIBLE);
            TextView d = (TextView) headView.findViewById(R.id.tv_qy);
            d.setText("计划名称");

            lv.addHeaderView(headView, null, false);

            adapter = new CommonAdapter<GzInfo>(context, R.layout.gz_item, infos) {
                @Override
                protected void convert(ViewHolder viewHolder, GzInfo item, int position) {
                    viewHolder.setText(R.id.tv_xh, item.getXh() + "");
                    viewHolder.setText(R.id.tv_qy, item.getQy());
                    viewHolder.setText(R.id.tv_yjzj, item.getYj() + "/" + item.getZj());
                }
            };
            lv.setEmptyView(tvNodata);
            lv.setAdapter(adapter);
            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                    Intent intent = new Intent(XbaxgActivity.this, Xbaxg2Activity.class);
                    intent.putExtra("jhid", infos.get(position - 1).getJhid());
                    startActivity(intent);
                }
            });
        } else {
            adapter.notifyDataSetChanged();
        }

    }

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