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

import android.content.ContentValues;
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.DBModel.DjjhRwQy;
import com.example.administrator.zhapp.R;
import com.example.administrator.zhapp.activity.BaseActivity;
import com.example.administrator.zhapp.adapter.GzAdapter;
import com.example.administrator.zhapp.contans.Contans;
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 SjhlxActivity extends BaseActivity {

    ListView lv;
    private List<GzInfo> list;
    private GzAdapter adapter;
    private View headView;
    private TextView jhlx;
    private String ewm;
    private boolean lx;//true 二维码  false 条形码
    private String nfc;

    private String[] jxlxs = {"运行前点检", "运行中点检", "检修点检"};
    private Map<String, ArrayList<DjjhRwQy>> map;

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

    protected void initView() {
        lv = findViewById(R.id.lv);
        headView = View.inflate(this, R.layout.gz_item, null);
        headView.findViewById(R.id.head).setVisibility(View.VISIBLE);
        jhlx = (TextView) headView.findViewById(R.id.tv_qy);
        jhlx.setText("计划类型");
        ewm = getIntent().getExtras().getString(Contans.KEY_BQBM);
        lx = getIntent().getExtras().getBoolean("lx");
    }

    public void initData() {

        setTitle("计划类型");
        setBack();

        map = new HashMap<>();
        list = new ArrayList<>();
        getDataInSqlite();

        adapter = new GzAdapter(this, list);
        lv.addHeaderView(headView, null, false);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(SjhlxActivity.this, SdlbActivity.class);
                intent.putExtra(Contans.KEY_FLAG, Contans.DLB);
                intent.putExtra("jhlx", list.get(i - 1).getQy());
                intent.putExtra(Contans.KEY_BQBM, ewm);
                startActivity(intent);
            }
        });
    }

    //查询数据库数据
    private void getDataInSqlite() {


        //保存扫码方式
        if (lx) {
            //二维码
            ContentValues values = new ContentValues();
            values.put("SMFX", 1);
            DataSupport.updateAll(DjjhRwQy.class, values, "BQBM = ?", ewm);
        } else {
            //NFC
            ContentValues values = new ContentValues();
            values.put("SMFX", 0);
            DataSupport.updateAll(DjjhRwQy.class, values, "NFCBH = ?", nfc);
        }

        list.clear();
        map.clear();
        int index = 0;
        for (int i = 0; i < jxlxs.length; i++) {
            ArrayList<DjjhRwQy> rwqys = new ArrayList<>();
            rwqys.addAll(DataSupport.where("jhlx = ? and BQBM = ?", jxlxs[i], ewm).find(DjjhRwQy.class));
            if (rwqys.size() != 0) {
                ++index;
                int zj = rwqys.size();
                int yj = 0;
                for (int j = 0; j < rwqys.size(); j++) {
                    if (rwqys.get(j).isChecked()) {
                        yj++;
                    }
                }
                GzInfo info = new GzInfo();
                info.setXh(index);
                info.setYj(yj);
                info.setZj(zj);
                info.setQy(jxlxs[i]);
                list.add(info);
                map.put(jxlxs[i], rwqys);
            }
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        //刷新listview
        getDataInSqlite();
        adapter.notifyDataSetChanged();
    }
}
