package com.quansu.heifengwuliu.fragment

import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import com.quansu.heifengwuliu.R
import com.quansu.heifengwuliu.base.MBFragment
import com.quansu.heifengwuliu.databinding.FragmentDelivergoodsBinding
import com.ysnows.base.base.BRepository
import com.ysnows.base.base.BViewModel

/**
 * 发货
 *Created by shihuiyun
 *on 2020/9/11
 */
class DeliverGoodsFragment : MBFragment<BViewModel<BRepository>, FragmentDelivergoodsBinding>() {

    var fragments = emptyArray<Fragment>()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initFragments()
        switchContent(0)
    }


    override fun listeners() {
        super.listeners()

        binding.butSingle.setOnClickListener {
            binding.butSingle.setTextColor(Color.parseColor("#ffffff"))
            binding.butMore.setTextColor(Color.parseColor("#292929"))
            binding.butSingle.setBackgroundResource(R.drawable.bg_chose_on)
            binding.butMore.setBackgroundResource(R.drawable.bg_chose_off)


            switchContent(0)
        }
        binding.butMore.setOnClickListener {
            binding.butSingle.setTextColor(Color.parseColor("#292929"))
            binding.butMore.setTextColor(Color.parseColor("#ffffff"))
            binding.butSingle.setBackgroundResource(R.drawable.bg_chose_off)
            binding.butMore.setBackgroundResource(R.drawable.bg_chose_on)
            switchContent(1)
        }


    }


    private fun initFragments() {
        val oneFragment = OwnerSingleFragment()
        val twoFragment = OwnerMoreFragment()
        fragments = arrayOf(
                oneFragment,
                twoFragment
        )
    }

    private fun switchContent(index: Int) {

        val childFragmentManager: FragmentManager = childFragmentManager
        val transaction = childFragmentManager.beginTransaction()
        var fragment = childFragmentManager.findFragmentByTag("tab_$index")
        if (fragment == null) {
            fragment = fragments[index]
            if (fragment != null) {
                transaction.add(R.id.fl_container, fragment, "tab_$index")
            }
        } else {
            transaction.show(fragment)
        }
        for (i in fragments.indices) {
            fragment = childFragmentManager.findFragmentByTag("tab_$i")
            if (fragment == null || index == i) {
                continue
            }
            transaction.hide(fragment)
        }
        transaction.commitAllowingStateLoss()
    }


    override fun binding(inflater: LayoutInflater, container: ViewGroup?): FragmentDelivergoodsBinding {
        return FragmentDelivergoodsBinding.inflate(layoutInflater)
    }

    override fun title(): String? {
        return null
    }

    override val isRxbus: Boolean
        get() = true
}
