package com.rehome.sgbaxj.activity.regular;


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

import java.util.ArrayList;
import java.util.List;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import org.litepal.crud.DataSupport;

/**
 * Created by huang on 2021/12/09
 * 定期工作-任务管理
 */
public class DqgzUpAndDownActivity extends BaseActivity3 implements View.OnClickListener {


    private DrawerLayout mDrawerLayout;
    private ViewPager mViewPager;
    private TabLayout mTabLayout;
    private List<Fragment> fragments = new ArrayList<>();
    private List<Fragment> list;
    private MyFragmentAdapter adapter;

    private DqgzDownFragment xzxsjhfragment;
    private DqgzUpFragment scxsjhfragmet;
    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() {
//        listPro.add("运行专业");
//        listPro.add("燃料专业");

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_left:
                finish();
                break;
            case R.id.tv_right:
                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());
                                //专业选择改变发送广播,刷新任务下载页面
                                sendBroadcast(intent);
                            }
                        }
                    });
                    listDialog.setTvTitle("请选择专业");
                    listDialog.show();;
                }
                break;
        }
    }


    public void initData() {
        Intent intent = getIntent();
        type = intent.getStringExtra("type");
        if(type!=null&&type.equals("YX")){
            initToolbar("定期任务数据管理", "选择专业", this);
            professionList = DataSupport.findAll(Profession.class);
            if(professionList!=null&&professionList.size()>0){
                for(Profession profession : professionList){
                    listPro.add(profession.getZymc());
                }
            }

        }else{
            initToolbar("定期任务数据管理", "", this);
        }

        mViewPager = findViewById(R.id.viewpager);
        xzxsjhfragment = DqgzDownFragment.newInstance(type);
        scxsjhfragmet = DqgzUpFragment.newInstance(type);
        list = new ArrayList<>();
        list.add(xzxsjhfragment);
        list.add(scxsjhfragmet);
        adapter = new MyFragmentAdapter(getSupportFragmentManager(), list);
        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 DqgzDownFragment(type));
        fragments.add(new DqgzUpFragment(type));
        FragmentAdapter mFragmentAdapteradapter = new FragmentAdapter(getSupportFragmentManager(), fragments, titles);
        mViewPager.setAdapter(mFragmentAdapteradapter);
        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.setTabsFromPagerAdapter(mFragmentAdapteradapter);
    }
}
