package com.unis.sxzcl.ui.template;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;

import com.google.android.material.tabs.TabLayout;
import com.unis.sxzcl.base.BaseFragment;
import com.unis.sxzcl.databinding.FragmentTemplateTabBinding;
import com.unis.sxzcl.widget.BottomMultipleListDialog;
import com.unis.sxzcl.widget.MultipleBean;
import com.unis.sxzcl.widget.TabFragmentPagerAdapter;

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

/**
 * 包名: com.unis.sxzcl.ui.template
 * 类名: DetailTemplateFragment
 * 创建人: mQ
 * 创建时间: 2020/11/18 14:59
 * 更新人:
 * 更新时间: 2020/11/18 14:59
 * 更新说明:
 * 版本号: 1.0
 * 作用描述: 新增页模版
 * 公共模版
 */
public class TemplateTabFragment extends BaseFragment<FragmentTemplateTabBinding> {

    private List<String> mTabs = Arrays.asList("仓房设施", "消防设施", "计量设施", "药品库", "配电室", "设施维修改造");
    private String mTitle = "仓房设施管理";

    private BottomMultipleListDialog mBottomMultipleDialog;

    private List<Fragment> mFragments;

    @Override
    protected FragmentTemplateTabBinding getViewBinding(LayoutInflater inflater, ViewGroup container) {
        return FragmentTemplateTabBinding.inflate(inflater,container,false);
    }

    @Override
    protected void init() {
        mBinding.bar.setLeftClickListener(v -> finish());
        //初始化标题
        mBinding.bar.setTitle(mTitle);
        //初始化标签
        for (String text : mTabs) {
            TabLayout.Tab tab = mBinding.tabLayout.newTab().setText(text);
            mBinding.tabLayout.addTab(tab);
        }
        //初始化仓房选项
        mBinding.tvHouse.setOnClickListener(v -> showHouseDialog());

        mBinding.btnAdd.setOnClickListener(v -> {
            DetailTemplateFragment.startPage(getContext(), "添加仓房设施", null);
        });
        initFragment();
        initViewPage();
    }

    /**
     * 初始化Fragment
     */
    private void initFragment() {
        mFragments = new ArrayList<>();
        for (String tab : mTabs) {
            mFragments.add(TemplateListFragment.newFragment(tab));
        }
    }

    /**
     * 初始化viewpager
     */
    private void initViewPage() {
        TabFragmentPagerAdapter adapter = new TabFragmentPagerAdapter(
                getChildFragmentManager(),
                mTabs,
                mFragments);
        mBinding.viewPager.setAdapter(adapter);
        mBinding.viewPager.setOffscreenPageLimit(mFragments.size());
        mBinding.tabLayout.setupWithViewPager(mBinding.viewPager);
    }

    @Override
    protected void onFirstShow() {

    }

    /**
     * 显示仓房选择
     */
    private void showHouseDialog() {
        if (mBottomMultipleDialog == null) {
            mBottomMultipleDialog = new BottomMultipleListDialog(getContext());
            mBottomMultipleDialog
                    .setTitle("选择仓房")
                    .hideTwoList()
                    .hideThreeList()
                    .hideRightEnter()
                    .setListOne(Arrays.asList(
                            new MultipleBean("全部"),
                            new MultipleBean("1号仓"),
                            new MultipleBean("2号仓"),
                            new MultipleBean("3号仓"),
                            new MultipleBean("4号仓")
                    ))
                    .setOneCallback((v, b1) -> {
                        //第一列点击项
                        mBinding.tvHouse.setText(b1.getName());
                        return true;
                    });
        }
        mBottomMultipleDialog.show();
    }

}
