package com.bjzc.bhzhgd.activity.sbxj;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import com.bjzc.bhzhgd.DBModel.Profession;
import com.bjzc.bhzhgd.base.BaseActivity3;
import com.bjzc.bhzhgd.contans.Contans;
import com.bjzc.bhzhgd.weight.ListDialog;
import com.google.android.material.tabs.TabLayout;
import com.bjzc.bhzhgd.R;
import com.bjzc.bhzhgd.activity.sbxdj.FragmentAdapter;

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

import androidx.fragment.app.Fragment;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.viewpager.widget.ViewPager;
import org.litepal.crud.DataSupport;


/**
 * 设备巡点检管理--现场巡检
 */
public class XjMainActivity extends BaseActivity3 {

    private ViewPager mViewPager;
    private TabLayout mTabLayout;
    private List<Fragment> fragments = new ArrayList<>();
    private String type = null;
    private List<String> listPro = new ArrayList<>();
    private List<Profession> professionList = new ArrayList<>();


    @Override
    public int getLayoutId() {
        return R.layout.activity_mainxj;
    }

    @Override
    public void initView() {
        mViewPager = findViewById(R.id.viewpager);
    }

    public void initData() {
        Intent intent = getIntent();
        type = intent.getStringExtra("type");
        if(type!=null&&type.equals("YX")){
            initToolbar("巡检数据管理", "选择专业", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(type.equals("YX")){
                        ListDialog listDialog = new ListDialog(context, listPro, new ListDialog.ListDialogListener() {
                            @Override
                            public void selectText(String text, int position) {
                                if(professionList!=null&&professionList.size()>0){
                                    Profession profession = professionList.get(position);
                                    Log.i("app",profession.getZyid());
                                    //刷新计划下载界面
                                    Intent intent = new Intent(Contans.ACTION_PROFESSIONCHANGE);
                                    intent.putExtra("professionId",profession.getZyid());
                                    //专业选择改变发送广播,刷新任务下载页面
                                    LocalBroadcastManager.getInstance(context).sendBroadcast(intent);;
                                }
                            }
                        });
                        listDialog.setTvTitle("请选择专业");
                        listDialog.show();;
                    }
                }
            });
            professionList = DataSupport.findAll(Profession.class);
            if(professionList!=null&&professionList.size()>0){
                for(Profession profession : professionList){
                    listPro.add(profession.getZymc());
                }
            }

        }else{
            initToolbar("巡检数据管理", "", new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
        }
        initViewPager();
    }

    private void initViewPager() {
        mTabLayout = findViewById(R.id.tabs);
        List<String> titles = new ArrayList<>();
        titles.add("数据下载");
        titles.add("数据上传");
        for (int i = 0; i < titles.size(); i++) {
            mTabLayout.addTab(mTabLayout.newTab().setText(titles.get(i)));
        }
        fragments.add(new XzxsjhFragment(type));
        fragments.add(new ScxsjhFragment(type));
        FragmentAdapter mFragmentAdapter = new FragmentAdapter(getSupportFragmentManager(), fragments, titles);
        mViewPager.setAdapter(mFragmentAdapter);
        mTabLayout.setupWithViewPager(mViewPager);
    }
}