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.BaseListViewAdapter.CommonAdapter;
import com.example.administrator.zhapp.BaseListViewAdapter.ViewHolder;
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.activity.MipcaActivityCapture;
import com.example.administrator.zhapp.contans.Contans;
import com.example.administrator.zhapp.entity.LyxcItem;
import org.litepal.crud.DataSupport;
import java.util.ArrayList;
import java.util.List;

public class XlyxcActivity extends BaseActivity {

    ListView lv;
    TextView tvNodata;
    Button btn_sys;

    private CommonAdapter<LyxcItem> adapter;
    private ArrayList<LyxcItem> infos;
    private View headView;

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

    @Override
    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 (infos.size() != 0) {
                    Intent intent = new Intent(context, MipcaActivityCapture.class);
                    startActivityForResult(intent, 1);
                } else {
                    showToast("你还没有计划");
                }
            }
        });

        setBack();
        setTitle("楼宇巡查");
        initNFC();
    }

    @Override
    public void initData() {
        infos = new ArrayList<>();
        getDataInSql();
        setList();
    }

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

        List<XfDjjh> xfdjjhs = DataSupport.where("xctypes = ? and download = ?", "3", "1").find(XfDjjh.class);
        for (XfDjjh djjh : xfdjjhs) {
            Cursor cusor = DataSupport.findBySQL("select * from lyxcrwqy where jhid = \"" + djjh.getJhid() + "\" group by qyname");
            while (cusor.moveToNext()) {
                int qyname = cusor.getColumnIndex("qyname");
                int nexttime = cusor.getColumnIndex("nexttime");
                int jhid = cusor.getColumnIndex("jhid");
                int xmid = cusor.getColumnIndex("xmid");
                LyxcItem info = new LyxcItem();
                info.setQy(cusor.getString(qyname));
                info.setJhid(cusor.getString(jhid));
                info.setDqsj(cusor.getString(nexttime));
                info.setXmid(cusor.getString(xmid));
                infos.add(info);
            }
            cusor.close();
        }
    }

    private void setList() {

        if (adapter == null) {
            headView = View.inflate(context, R.layout.gz_item, null);
            headView.findViewById(R.id.head).setVisibility(View.VISIBLE);
            TextView qy = (TextView) headView.findViewById(R.id.tv_qy);
            qy.setText("区域");
            TextView yjzj = (TextView) headView.findViewById(R.id.tv_yjzj);
            yjzj.setText("到期时间");
            lv.addHeaderView(headView, null, false);
            adapter = new CommonAdapter<LyxcItem>(context, R.layout.gz_item, infos) {
                @Override
                protected void convert(ViewHolder viewHolder, LyxcItem item, int position) {
                    viewHolder.setText(R.id.tv_xh, (position + 1) + "");
                    viewHolder.setText(R.id.tv_qy, item.getQy());
                    viewHolder.setText(R.id.tv_yjzj, item.getDqsj());
                }
            };
            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(XlyxcActivity.this, Xlyxc2Activity.class);
//                    intent.putExtra("jhid", infos.get(position - 1).getJhid());
//                    intent.putExtra("qyname", infos.get(position - 1).getQy());
//                    startActivity(intent);
//                }
//            });
        } else {
            adapter.notifyDataSetChanged();
        }
    }

    @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(XlyxcActivity.this, Xlyxc2Activity.class);
                intent.putExtra(Contans.KEY_BQBM, txm);
                intent.putExtra(Contans.NFCOREWM, true);
                startActivity(intent);
            }
        }
    }

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