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

import android.content.ContentValues;
import android.graphics.Color;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.widget.RadioButton;
import android.widget.RadioGroup;
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.MyFragmentAdapter;
import com.example.administrator.zhapp.contans.Contans;
import com.example.administrator.zhapp.fragment.ajhxcgl.AjhFragment;
import com.example.administrator.zhapp.fragment.ajhxcgl.SbdjFragment;
import com.example.administrator.zhapp.weight.AutoRadioGroup;
import com.example.administrator.zhapp.weight.NoscrollViewPager;
import org.litepal.crud.DataSupport;
import java.util.ArrayList;
import java.util.List;

/**
 * 安健环巡查管理-安健环&设备点检
 */

public class AjhAndSbjdActivity extends BaseActivity {

    NoscrollViewPager vp_content;
    RadioButton rb1;
    RadioButton rb2;
    AutoRadioGroup rg;
    private SbdjFragment sf;

    private AjhFragment af;
    private List<Fragment> list;
    private MyFragmentAdapter adapter;
    private ArrayList<DjjhRwQy> djjhs;

    private String ewm;
    private boolean nfcorewm;

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

    @Override
    protected void initView() {
        vp_content = findViewById(R.id.vp_centent);
        rb1 = findViewById(R.id.rb1);
        rb2 = findViewById(R.id.rb2);
        rg = findViewById(R.id.rg);
    }

    @Override
    public void initData() {

        Bundle bundle = this.getIntent().getExtras();
        ewm = bundle.getString(Contans.KEY_BQBM);
        nfcorewm = bundle.getBoolean(Contans.NFCOREWM);

//        initNFC();

        setTitle("安健环巡检");
        setBack();
        djjhs = new ArrayList<>();
        sf = SbdjFragment.newInstance(true, djjhs);
        af = AjhFragment.newInstance();
        list = new ArrayList<>();
        list.add(af);
        list.add(sf);
        adapter = new MyFragmentAdapter(getSupportFragmentManager(), list);
        vp_content.setAdapter(adapter);
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (i == R.id.rb1) {
                    vp_content.setCurrentItem(0, false);
                    title.setText("安健环巡检");
                    rb1.setTextColor(Color.WHITE);
                    rb2.setTextColor(Color.GRAY);
                } else if (i == R.id.rb2) {
                    vp_content.setCurrentItem(1, false);
                    title.setText("设备点检");
                    rb2.setTextColor(Color.WHITE);
                    rb1.setTextColor(Color.GRAY);
                }
            }
        });

        getDataInSqlite();

    }

    //获取数据库数据
    private void getDataInSqlite() {
        djjhs.clear();
        if (nfcorewm) {
            ContentValues values = new ContentValues();
            values.put("SMFX", 0);
            DataSupport.updateAll(DjjhRwQy.class, values, "NFCBH = ?", ewm);
//            djjhs.addAll(DataSupport.where("bqbm = ? and sbzt = ?", result, 1 + "").order("assetnum").order("pointnum").find(DjjhRwQy.class));
            djjhs.addAll(DataSupport.where("bqbm = ? and sbzt = ?", ewm, 1 + "").find(DjjhRwQy.class));
            if (djjhs.size() != 0) {
                sf.setLists(djjhs);
            }
        } else {
            ContentValues values = new ContentValues();
            values.put("SMFX", 0);
            DataSupport.updateAll(DjjhRwQy.class, values, "NFCBH = ?", ewm);
//            djjhs.addAll(DataSupport.where("nfcbh = ? and sbzt = ?", result, 1 + "").order("assetnum").order("pointnum").find(DjjhRwQy.class));
            djjhs.addAll(DataSupport.where("nfcbh = ? and sbzt = ?", ewm, 1 + "").find(DjjhRwQy.class));
            if (djjhs.size() !=0) {
                sf.setLists(djjhs);//测试
            }
        }
        //djjhs.addAll(DataSupport.findAll(DjjhRwQy.class));
    }

//    @Override
//    public void handleNfc(String result) {
//        super.handleNfc(result);
//        af.dataFromNFC(result);
//
//        List<Ajhxzrwqy> qys = DataSupport.where("nfcbh = ?", result).find(Ajhxzrwqy.class);
//        if (qys.size() != 0) {
//            af.setqyCode(qys.get(0).getAREACODE());
//        }
//
//        ContentValues values = new ContentValues();
//        values.put("SMFX", 0);
//        DataSupport.updateAll(DjjhRwQy.class, values, "NFCBH = ?", result);
////        getDataInSqlite(result, false);
////        ArrayList<DjjhRwQy> rwqys = new ArrayList<>();
////
////        rwqys.addAll(DataSupport.where("nfcbh = ?", result).find(DjjhRwQy.class));
////        sf.setLists(rwqys);
//        sf.setLists(djjhs);//测试
//    }

    public TextView getToolbar() {
        return title;
    }

    public void getJhPoint() {
        sf.setXcjs();
    }

//    public void setList(String result) {
//        ContentValues values = new ContentValues();
//        values.put("SMFX", 1);
//        DataSupport.updateAll(DjjhRwQy.class, values, "BQBM = ?", result);
////        getDataInSqlite(result, true);
//    }

    public void getSjcjNext() {
        sf.setSjcjNext();
    }

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