package com.rdiot.yx485.ui.family

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import androidx.navigation.fragment.findNavController
import com.drake.net.utils.TipUtils
import com.drake.net.utils.scopeDialog
import com.rdiot.yx485.R
import com.rdiot.yx485.base.BaseFragment
import com.rdiot.yx485.base.FamilyDataModelFactory
import com.rdiot.yx485.databinding.FraAddMemberBinding
import com.rdiot.yx485.net.inviteMember
import com.rdiot.yx485.ui.family.model.FamilyDataViewModel
import com.rdiot.yx485.util.setClickLimitListener
import kotlinx.coroutines.delay

/**
 * 邀请家庭成员
 * @author mR2hao
 * @date 2023/1/10
 */
class InviteMemberFragment : BaseFragment<FraAddMemberBinding>() {
    override fun getLayoutId() = R.layout.fra_add_member
    private val familyDataViewModel: FamilyDataViewModel by activityViewModels { FamilyDataModelFactory() }

    override fun initView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ) {
        binding.nb.setLeftClickListener { findNavController().popBackStack() }
        binding.btnInvite.setClickLimitListener {
            if (binding.etPhone.text?.length == 11) {
                doInviteMember()
            } else {
                TipUtils.toast(getString(R.string.please_enter_info_correctly))
            }
        }
    }

    /** 邀请用户 */
    private fun doInviteMember() {
        scopeDialog {
            inviteMember(familyDataViewModel.selFamilyData, binding.etPhone.text.toString()).await()
            delay(500)
            findNavController().popBackStack()
            TipUtils.toast("邀请成功")
        }
    }


}