package com.pan_american.android.ui.document

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.View.OnClickListener
import android.view.ViewGroup
import com.pan_american.android.OASystem
import com.pan_american.android.base.BaseFragment
import com.pan_american.android.databinding.FragmentDocumentBinding

class DocumentFragment : BaseFragment<FragmentDocumentBinding>(), OnClickListener {

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

    override fun onStart() {
        super.onStart()

        initViews()
    }

    override fun initViews() {

        for (item in OASystem.authData) {
            when (item.pageId) {

                OASystem.HOTEL_PREDETERMINE -> {
                    if (OASystem.authorization(OASystem.HOTEL_PREDETERMINE, OASystem.DOWNLOAD)) {
                        if (binding.groupHotel.visibility == View.GONE) {
                            binding.groupHotel.visibility = View.VISIBLE
                        }

                        binding.voucher.setOnClickListener(this)
                        binding.confirmList.setOnClickListener(this)
                        if (OASystem.userInfo.depName != "市场部") {
                            binding.predetermineCost.setOnClickListener(this)
                        }
                    }
                }

                OASystem.AIRPLANE_PAYMENT_INSERT -> {
                    if (OASystem.authorization(OASystem.AIRPLANE_PAYMENT_INSERT, OASystem.DOWNLOAD)) {
                        if (binding.groupAirplaneTicket.visibility == View.GONE) {
                            binding.groupAirplaneTicket.visibility = View.VISIBLE
                        }

                        if (OASystem.userInfo.depName != "市场部") {
                            binding.airplaneReportForm.setOnClickListener(this)
                        }

                        binding.chTravelItinerary.setOnClickListener(this)
                        binding.enTravelItinerary.setOnClickListener(this)
                    }
                }

                OASystem.OP_TRAVEL_ITINERARY -> {
                    if (OASystem.authorization(OASystem.OP_TRAVEL_ITINERARY, OASystem.DOWNLOAD)) {
                        if (binding.groupOp.visibility == View.GONE) {
                            binding.groupOp.visibility = View.VISIBLE
                        }

                        binding.opTravelItineraryWord.setOnClickListener(this)
                        binding.opTravelItineraryPdf.setOnClickListener(this)
                    }
                }

                OASystem.GROUP_CLIENT_LIST -> {
                    if (OASystem.authorization(OASystem.GROUP_CLIENT_LIST, OASystem.DOWNLOAD)) {
                        if (binding.groupManager.visibility == View.GONE) {
                            binding.groupManager.visibility = View.VISIBLE
                        }

                        binding.groupCustomerListCh.visibility = View.VISIBLE
                        binding.groupCustomerListCh.setOnClickListener(this)

                        binding.groupCustomerListEn.visibility = View.VISIBLE
                        binding.groupCustomerListEn.setOnClickListener(this)
                    }
                }

                OASystem.REVERSE_TABLE -> {
                    if (OASystem.authorization(OASystem.REVERSE_TABLE, OASystem.DOWNLOAD)) {
                        if (binding.groupManager.visibility == View.GONE) {
                            binding.groupManager.visibility = View.VISIBLE
                        }

                        binding.reverseTable.visibility = View.VISIBLE
                        binding.reverseTable.setOnClickListener(this)
                    }
                }

                OASystem.ENTER_AND_LEAVE_PAYMENT_DETAIL -> {
                    if (OASystem.authorization(OASystem.ENTER_AND_LEAVE_PAYMENT_DETAIL, OASystem.DOWNLOAD)) {
                        if (binding.groupManager.visibility == View.GONE) {
                            binding.groupManager.visibility = View.VISIBLE
                        }

                        binding.foundsExamineForm.visibility = View.VISIBLE
                        binding.foundsExamineForm.setOnClickListener(this)

                        binding.foundsCalculateForm.visibility = View.VISIBLE
                        binding.foundsCalculateForm.setOnClickListener(this)

                        binding.provinceExamineForm.visibility = View.VISIBLE
                        binding.provinceExamineForm.setOnClickListener(this)

                        binding.cityExamineForm.visibility = View.VISIBLE
                        binding.cityExamineForm.setOnClickListener(this)

                        binding.leaveExamineForm.visibility = View.VISIBLE
                        binding.leaveExamineForm.setOnClickListener(this)

                        binding.reimbursementForm.visibility = View.VISIBLE
                        binding.reimbursementForm.setOnClickListener(this)
                    }
                }
            }
        }
    }

    override fun onClick(view: View) {

        val intent = Intent(OASystem.context, DocumentGroupListActivity::class.java)

        var tag = ""

        when(view.id) {
            binding.voucher.id -> {
                tag = "voucher"
            }

            binding.confirmList.id -> {
                tag = "confirmList"
            }

            binding.predetermineCost.id -> {
                tag = "predetermineCost"
            }

            binding.airplaneReportForm.id -> {
                tag = "airplaneReportForm"
            }

            binding.chTravelItinerary.id -> {
                tag = "chTravelItinerary"
            }

            binding.enTravelItinerary.id -> {
                tag = "enTravelItinerary"
            }

            binding.opTravelItineraryWord.id -> {
                tag = "opTravelItineraryWord"
            }

            binding.opTravelItineraryPdf.id -> {
                tag = "opTravelItineraryPdf"
            }

            binding.groupCustomerListCh.id -> {
                tag = "groupCustomerListCh"
            }

            binding.groupCustomerListEn.id -> {
                tag = "groupCustomerListEn"
            }

            binding.reverseTable.id -> {
                tag = "reverseTable"
            }

            binding.foundsExamineForm.id -> {
                tag = "foundsExamineForm"
            }

            binding.foundsCalculateForm.id -> {
                tag = "foundsCalculateForm"
            }

            binding.provinceExamineForm.id -> {
                tag = "provinceExamineForm"
            }

            binding.cityExamineForm.id -> {
                tag = "cityExamineForm"
            }

            binding.leaveExamineForm.id -> {
                tag = "leaveExamineForm"
            }

            binding.reimbursementForm.id -> {
                tag = "reimbursementForm"
            }
        }

        intent.putExtra("tag", tag)

        startActivity(intent)
    }
}