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

import android.content.ContentValues;
import android.content.Intent;
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.LyxcXm;
import com.example.administrator.zhapp.DBModel.Lyxcrwqy;
import com.example.administrator.zhapp.R;
import com.example.administrator.zhapp.activity.BaseActivity;
import com.example.administrator.zhapp.contans.Contans;
import com.example.administrator.zhapp.entity.LyxcInfo;
import org.litepal.crud.DataSupport;
import java.util.ArrayList;
import java.util.List;


public class Xlyxc2Activity extends BaseActivity {

    ListView lv;

    private List<LyxcInfo> infos;
    private CommonAdapter<Lyxcrwqy> adapter;
    private List<Lyxcrwqy> qys;
    private List<LyxcXm> xms;

    private View headView;

    private String code = "";
    private boolean nfcorewm;


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

    @Override
    protected void initView() {
        lv = findViewById(R.id.lv);
        setBack();
        setTitle("楼宇巡查");
    }

    @Override
    public void initData() {

        code = getIntent().getExtras().getString(Contans.KEY_BQBM);
        nfcorewm = getIntent().getExtras().getBoolean(Contans.NFCOREWM);

        qys = new ArrayList<>();
        xms = new ArrayList<>();
        infos = new ArrayList<>();

        getDataInSql();
        setListData();
    }

    private void setListData() {
        if (adapter == null) {
            headView = View.inflate(context, R.layout.lyxc_item, null);
            headView.findViewById(R.id.head).setVisibility(View.VISIBLE);
            lv.addHeaderView(headView, null, false);

            adapter = new CommonAdapter<Lyxcrwqy>(context, R.layout.lyxc_item, qys) {
                @Override
                protected void convert(ViewHolder viewHolder, Lyxcrwqy item, int position) {
                    viewHolder.setText(R.id.tv_xh, (position + 1) + "");
                    viewHolder.setText(R.id.tv_xcnr, item.getXMNAME());
                    viewHolder.setText(R.id.tv_zt, item.isChecked() ? "已检" : "未检");
                }
            };
            lv.setAdapter(adapter);
            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent = new Intent(Xlyxc2Activity.this, XlyxcDetaliActivity.class);
                    intent.putExtra("jcnr", qys.get(position - 1).getXMNAME());
                    intent.putExtra("xmid", qys.get(position - 1).getXMID());
                    intent.putExtra("qyid", qys.get(position - 1).getQYID());
                    intent.putExtra("jhid", qys.get(position - 1).getJHID());
                    startActivity(intent);
                }
            });
        } else {
            adapter.notifyDataSetChanged();
        }
    }

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

        qys.clear();

        if (nfcorewm) {
            qys.addAll(DataSupport.where("txmbh = ?", code).find(Lyxcrwqy.class));
            ContentValues values = new ContentValues();
            values.put("smfx", "扫描条码");
            DataSupport.updateAll(Lyxcrwqy.class, values, "txmbh = ?", code);
        } else {
            qys.addAll(DataSupport.where("qynfc = ?", code).find(Lyxcrwqy.class));
            ContentValues values = new ContentValues();
            values.put("smfx", "NFC标签");
            //values.put("checked", 1);//测试
            //values.put("iszc","1");//测试
            DataSupport.updateAll(Lyxcrwqy.class, values, "qynfc = ?", code);
            //DataSupport.updateAll(Lyxcrwqy.class, values);//测试
        }
    }

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