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

import android.content.Intent;
import android.database.Cursor;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import com.example.administrator.zhapp.BaseListViewAdapter.CommonAdapter;
import com.example.administrator.zhapp.BaseListViewAdapter.ViewHolder;
import com.example.administrator.zhapp.DBModel.XwaqgcSc;
import com.example.administrator.zhapp.R;
import com.example.administrator.zhapp.activity.BaseActivity;
import org.litepal.crud.DataSupport;
import java.util.ArrayList;
import java.util.List;

public class XwaqgcUploadActivity extends BaseActivity {

    ListView lv;

    private List<XwaqgcSc> scs;
    private View headView;

    private CommonAdapter<XwaqgcSc> adapter;

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

    @Override
    protected void initView() {
        lv = findViewById(R.id.lv);
        setBack();
        setTitle("行为安全观察记录列表");
        scs = new ArrayList<>();
        headView = View.inflate(context, R.layout.xwaqgcjl_item, null);
        headView.findViewById(R.id.head).setVisibility(View.VISIBLE);
    }

    @Override
    public void initData() {

        getData();
        if (scs.size() != 0) {
            setListView();
        }
    }

    private void getData() {
        scs.clear();
        Cursor cursor = DataSupport.findBySQL("select * from xwaqgcsc group by gcid");
        while (cursor.moveToNext()) {
            XwaqgcSc sc = new XwaqgcSc();
            int gzdd = cursor.getColumnIndex("gzdd");
            int cbs = cursor.getColumnIndex("cbs");
            int bgcr = cursor.getColumnIndex("bgcr");
            int gzp = cursor.getColumnIndex("gzp");
            int gznr = cursor.getColumnIndex("gznr");
            int gcid = cursor.getColumnIndex("gcid");
            sc.setGZDD(cursor.getString(gzdd));
            sc.setCBS(cursor.getString(cbs));
            sc.setBGCR(cursor.getString(bgcr));
            sc.setGZP(cursor.getString(gzp));
            sc.setGZNR(cursor.getString(gznr));
            sc.setGCID(cursor.getString(gcid));
            scs.add(sc);
        }
        cursor.close();
    }

    private void setListView() {

        adapter = new CommonAdapter<XwaqgcSc>(context, R.layout.xwaqgcjl_item, scs) {
            @Override
            protected void convert(ViewHolder viewHolder, XwaqgcSc item, int position) {
                viewHolder.setText(R.id.tv_gzdd, item.getGZDD());
                viewHolder.setText(R.id.tv_cbs, item.getCBS());
                viewHolder.setText(R.id.tv_gcr, item.getBGCR());
                viewHolder.setText(R.id.tv_gzp, item.getGZP());
                viewHolder.setText(R.id.tv_nr, item.getGZNR());
            }
        };
        lv.addHeaderView(headView, null, false);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(XwaqgcUploadActivity.this, XwaqgcxgActivity.class);
                intent.putExtra("xwaqgcsc", scs.get(position - 1));
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        getData();
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }
}
