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

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import com.example.administrator.zhapp.DBModel.XfDjjhRwqy;
import com.example.administrator.zhapp.R;
import com.example.administrator.zhapp.activity.BaseActivity;
import com.example.administrator.zhapp.activity.MipcaActivityCapture;
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.List;


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

    ListView lv;
    TextView tvNodata;
    Button btn_sys;

    private List<GzInfo> list;
    private View headView;
    private GzAdapter adapter;


    private List<String> qynames;
    //    private List<XfDjjhRwqy> rwqynames;
    private List<XfDjjhRwqy> rwqys;

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

    protected void initView() {
        lv = findViewById(R.id.lv);
        tvNodata = findViewById(R.id.tv_nodata);
        btn_sys = findViewById(R.id.btn_sys);
        btn_sys.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (list.size() != 0) {
                    Intent intent = new Intent(context, MipcaActivityCapture.class);
                    startActivityForResult(intent, 1);
                } else {
                    showToast("你还没有计划");
                }
            }
        });
    }

    public void initData() {

        initNFC();

        setTitle("消防巡查");
        setBack();

        qynames = new ArrayList<>();
        rwqys = new ArrayList<>();
        list = new ArrayList<>();
        getDataInSqlite();
        setListData();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        getDataInSqlite();
        adapter.notifyDataSetChanged();
    }

    private void getDataInSqlite() {

        qynames.clear();
        rwqys.clear();
        list.clear();
        Cursor cursor = DataSupport.findBySQL("select * from xfdjjhrwqy group by qyname");
        while (cursor.moveToNext()) {
            XfDjjhRwqy rwqy = new XfDjjhRwqy();
            int index = cursor.getColumnIndex("qyname");
            //rwqy.setQyname(cursor.getString(index));
            qynames.add(cursor.getString(index));
        }
        cursor.close();
        rwqys = DataSupport.findAll(XfDjjhRwqy.class);
        for (int i = 0; i < qynames.size(); i++) {
            GzInfo info = new GzInfo();
            info.setXh(i + 1);
            info.setQy(qynames.get(i));
            ArrayList<XfDjjhRwqy> rwqy = new ArrayList<>();
            rwqy.addAll(DataSupport.where("qyname = ?", qynames.get(i)).find(XfDjjhRwqy.class));
            // map.put(rwqylist.get(i).getQymc(), rwqy);
            int yj = 0;
            for (int j = 0; j < rwqy.size(); j++) {
                if (rwqy.get(j).isChecked()) {
                    yj++;
                }
            }
            int zj = rwqy.size();
            info.setYj(yj);
            info.setZj(zj);
            list.add(info);
        }
    }

    private void setListData() {
        headView = View.inflate(this, R.layout.gz_item, null);
        headView.findViewById(R.id.head).setVisibility(View.VISIBLE);
        adapter = new GzAdapter(this, list);
        lv.setEmptyView(tvNodata);
        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(XxfxcActivity.this, XxfxcDetaliActivity.class);
//                intent.putExtra("qy", qynames.get(i - 1));
//                startActivity(intent);
//            }
//        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 1) {
            if (resultCode == Activity.RESULT_OK) {
                Bundle bundle = data.getExtras();
                String txm = bundle.getString("result");
                Intent intent = new Intent(XxfxcActivity.this, XxfxcDetaliActivity.class);
                intent.putExtra(Contans.KEY_BQBM, txm);
                intent.putExtra(Contans.NFCOREWM, true);
                startActivity(intent);
            }
        }
    }

    //处理nfc
    @Override
    public void handleNfc(String result) {
        super.handleNfc(result);
        if (list.size() != 0) {
            Intent intent = new Intent(XxfxcActivity.this, XxfxcDetaliActivity.class);
            intent.putExtra(Contans.KEY_BQBM, result);
            intent.putExtra(Contans.NFCOREWM, false);
            startActivity(intent);
        } else {
            showToast("你还没有计划");
        }
    }

}
