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

import android.graphics.Color;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.administrator.zhapp.R;
import com.example.administrator.zhapp.adapter.MyFragmentAdapter;
import com.example.administrator.zhapp.fragment.BaseFragment;
import com.example.administrator.zhapp.weight.AutoRadioGroup;

import java.util.ArrayList;
import java.util.List;


/**
 * 安健环巡查管理-安健环
 */

public class AjhFragment extends BaseFragment {

    RadioButton rb1;
    RadioButton rb2;
    AutoRadioGroup rg;
    ViewPager vp;

    private Sajhlv2Fragment sajhlv2;
    private YhpcFragment yhpc;
    private MyFragmentAdapter mAdapter;
    private List<Fragment> lists;

    public AjhFragment() {
    }

    public static AjhFragment newInstance() {
        AjhFragment fragment = new AjhFragment();
        return fragment;
    }


    @Override
    public int getContentViewId() {
        return R.layout.fragment_sajh;
    }

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

    @Override
    public void initData() {
        lists = new ArrayList<>();
        sajhlv2 = Sajhlv2Fragment.newInstance("ajh", "参数B");
        yhpc = YhpcFragment.newInstance("参数A", "参数B");
        lists.add(sajhlv2);
        lists.add(yhpc);
        mAdapter = new MyFragmentAdapter(getChildFragmentManager(), lists);
        vp.setAdapter(mAdapter);
        vp.setOffscreenPageLimit(2);

        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {

                switch (position) {
                    case 0:
                        rb1.setChecked(true);
                        rb1.setTextColor(getResources().getColor(R.color.btn));
                        rb2.setTextColor(Color.GRAY);
                        break;
                    case 1:
                        rb2.setChecked(true);
                        rb2.setTextColor(getResources().getColor(R.color.btn));
                        rb1.setTextColor(Color.GRAY);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                if (checkedId == R.id.rb1) {
                    rb1.setTextColor(getResources().getColor(R.color.btn));
                    rb2.setTextColor(Color.GRAY);
                    vp.setCurrentItem(0, false);
                }
                if (checkedId == R.id.rb2) {
                    rb2.setTextColor(getResources().getColor(R.color.btn));
                    rb1.setTextColor(Color.GRAY);
                    vp.setCurrentItem(1, false);
                }
            }
        });
    }

    public void dataFromNFC(String nfcbh) {
        sajhlv2.dataFromNFC(nfcbh);
    }

    public void setqyCode(String qycode) {
        yhpc.setQycode(qycode);
    }
}
