package com.pan_american.android.ui.group_management.group_info

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.pan_american.android.OASystem
import com.pan_american.android.R
import com.pan_american.android.base.BaseFragment
import com.pan_american.android.databinding.FragmentGroupInfoExaminationBinding
import org.greenrobot.eventbus.EventBus

class GroupInfoExaminationFragment : BaseFragment<FragmentGroupInfoExaminationBinding>() {

    override fun getViewBinding(
        inflater: LayoutInflater, container: ViewGroup?, bundle: Bundle?
    ) = FragmentGroupInfoExaminationBinding.inflate(layoutInflater, container, false)

    override fun onStart() {
        super.onStart()

        initViews()
        initEvents()
    }

    override fun onStop() {
        super.onStop()

        OASystem.groupOperationRequest.apply {
            cgrwspbmmc = binding.departmentName.getText()
            cgrwspwh = binding.examinationCode.text.toString()
            zzscbmmc = binding.politicalCheckName.getText()
            zzscspwh = binding.politicalCheckCode.text.toString()
        }
    }

    override fun initViews() {

        binding.commit.apply {
            when (OASystem.groupOperationRequest.status) {
                1 -> {
                    text = resources.getString(R.string.add)
                }

                2 -> {
                    text = resources.getString(R.string.update)
                }
            }
        }

        binding.apply {
            departmentName.setText(OASystem.groupOperationRequest.cgrwspbmmc)
            examinationCode.setText(OASystem.groupOperationRequest.cgrwspwh)
            politicalCheckName.setText(OASystem.groupOperationRequest.zzscbmmc)
            politicalCheckCode.setText(OASystem.groupOperationRequest.zzscspwh)
        }

        if (!OASystem.authorization(OASystem.GROUP_INFORMATION, OASystem.EDIT)) {
            binding.apply {
                departmentName.isEnabled = false
                examinationCode.isEnabled = false
                politicalCheckName.isEnabled = false
                politicalCheckCode.isEnabled = false

                commit.visibility = View.GONE
            }
        }
    }

    override fun initEvents() {
        binding.commit.setOnClickListener {

            OASystem.groupOperationRequest.apply {
                cgrwspbmmc = binding.departmentName.getText()
                cgrwspwh = binding.examinationCode.text.toString()
                zzscbmmc = binding.politicalCheckName.getText()
                zzscspwh = binding.politicalCheckCode.text.toString()
            }

            EventBus.getDefault().post(OASystem.groupOperationRequest)
        }
    }
}