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

import android.content.ContentValues;
import android.graphics.Color;
import androidx.fragment.app.Fragment;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import com.example.administrator.zhapp.DBModel.XfDjjhRwqy;
import com.example.administrator.zhapp.DBModel.XfDjjhRwqyList;
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.xfbwxcgl.FhmAqckFragment;
import com.example.administrator.zhapp.fragment.xfbwxcgl.MhqcFragment;
import com.example.administrator.zhapp.fragment.xfbwxcgl.XfsFragment;
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;
import java.util.Map;

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

    NoscrollViewPager vp;
    RadioButton rb1;
    RadioButton rb2;
    RadioButton rb3;
    AutoRadioGroup rg;

    private List<Fragment> list;
    private MyFragmentAdapter adapter;
    private MhqcFragment mhqc;
    private XfsFragment xfs;
    private FhmAqckFragment fhmaqck;

    private List<XfDjjhRwqyList> rwqylist;
    private List<XfDjjhRwqy> rwqys;
    private Map<String, List<XfDjjhRwqy>> map;
    private String code = "";
    private boolean nfcorewm;

    private ArrayList<XfDjjhRwqy> mhqcs;
    private ArrayList<XfDjjhRwqy> xfss;
    private ArrayList<XfDjjhRwqy> fhmaqcks;


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

    protected void initView() {
        vp = findViewById(R.id.vp);
        rb1 = findViewById(R.id.rb1);
        rb2 = findViewById(R.id.rb2);
        rg = findViewById(R.id.rg);
        rb3 = findViewById(R.id.rb3);
    }

    public void initData() {

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

        setTitle("灭火器材");
        setBack();
        list = new ArrayList<>();
        rwqylist = new ArrayList<>();
        rwqys = new ArrayList<>();
        mhqcs = new ArrayList<>();
        xfss = new ArrayList<>();
        fhmaqcks = new ArrayList<>();

        getDataInSqlite();

        mhqc = MhqcFragment.newInstance(mhqcs);
        xfs = XfsFragment.newInstance(xfss);
        fhmaqck = FhmAqckFragment.newInstance(fhmaqcks);

        list.add(mhqc);
        list.add(xfs);
        list.add(fhmaqck);

        adapter = new MyFragmentAdapter(getSupportFragmentManager(), list);
        vp.setAdapter(adapter);
        vp.setOffscreenPageLimit(3);
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {

                if (i == R.id.rb1) {
                    rb1.setTextColor(Color.WHITE);
                    rb2.setTextColor(Color.GRAY);
                    rb3.setTextColor(Color.GRAY);
                    vp.setCurrentItem(0, false);
                    title.setText("灭火器材");
                } else if (i == R.id.rb2) {
                    rb1.setTextColor(Color.GRAY);
                    rb2.setTextColor(Color.WHITE);
                    rb3.setTextColor(Color.GRAY);
                    vp.setCurrentItem(1, false);
                    title.setText("消防栓");
                } else if (i == R.id.rb3) {
                    rb1.setTextColor(Color.GRAY);
                    rb2.setTextColor(Color.GRAY);
                    rb3.setTextColor(Color.WHITE);
                    vp.setCurrentItem(2, false);
                    title.setText("防火门及安全出口");
                }
            }
        });
    }

    public void getDataInSqlite() {

        mhqcs.clear();
        xfss.clear();
        fhmaqcks.clear();
        if (nfcorewm) {
            ContentValues values = new ContentValues();
            values.put("smfx", "扫描条码");
            DataSupport.updateAll(XfDjjhRwqy.class, values, "txmbh = ?", code);

            mhqcs.addAll(DataSupport.where("txmbh = ? and xfname = ?", code, "灭火器材").find(XfDjjhRwqy.class));
            xfss.addAll(DataSupport.where("txmbh = ? and xfname = ?", code, "消防栓").find(XfDjjhRwqy.class));
            fhmaqcks.addAll(DataSupport.where("txmbh = ? and xfname = ?", code, "防火门安全出口灯").find(XfDjjhRwqy.class));
        } else {

            ContentValues values = new ContentValues();
            values.put("smfx", "NFC标签");
            //values.put("checked", 1);//测试
            DataSupport.updateAll(XfDjjhRwqy.class, values, "qynfc = ?", code);
            //DataSupport.updateAll(XfDjjhRwqy.class, values);//测试
            mhqcs.addAll(DataSupport.where("qynfc = ? and xfname = ?", code, "灭火器材").find(XfDjjhRwqy.class));
            xfss.addAll(DataSupport.where("qynfc = ? and xfname = ?", code, "消防栓").find(XfDjjhRwqy.class));
            fhmaqcks.addAll(DataSupport.where("qynfc = ? and xfname = ?", code, "防火门安全出口灯").find(XfDjjhRwqy.class));
        }

    }

    @Override
    protected void onRestart() {
        super.onRestart();
        getDataInSqlite();
        mhqc.setListadapter();
        xfs.setListadapter();
        fhmaqck.setListadapter();
    }

}
