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 onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        initViews()
        initEvents()
    }

    override fun initEvents() {
        binding.voucher.setOnClickListener(this)
        binding.confirmList.setOnClickListener(this)
        binding.predetermineCost.setOnClickListener(this)

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

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

        binding.groupCustomerListCh.setOnClickListener(this)
        binding.groupCustomerListEn.setOnClickListener(this)

        binding.reverseTable.setOnClickListener(this)

        binding.foundsExamineForm.setOnClickListener(this)
        binding.foundsCalculateForm.setOnClickListener(this)
        binding.provinceExamineForm.setOnClickListener(this)
        binding.cityExamineForm.setOnClickListener(this)
        binding.leaveExamineForm.setOnClickListener(this)
        binding.reimbursementForm.setOnClickListener(this)
    }

    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)
                        binding.predetermineCost.setOnClickListener(this)

                        if (isShownHotelCost()) {
                            binding.predetermineCost.visibility = View.VISIBLE
                        }
                    }
                }

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

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

                        if (isShownAirplaneCost()) {
                            binding.airplaneReportForm.visibility = View.VISIBLE
                        }
                    }
                }

                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.ENTRY_AND_EXIT_FEE_DETAIL -> {
                    if (OASystem.authorization(OASystem.ENTRY_AND_EXIT_FEE_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 = ""
        var cTable = 0

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

            binding.confirmList.id -> {
                tag = "confirmList"
                cTable = OASystem.C_TABLE_HOTEL_PREDETERMINE
            }

            binding.predetermineCost.id -> {
                tag = "predetermineCost"
                cTable = OASystem.C_TABLE_HOTEL_PREDETERMINE
            }

            binding.airplaneReportForm.id -> {
                tag = "airplaneReportForm"
                cTable = OASystem.C_TABLE_AIRPLANE_TICKET
            }

            binding.chTravelItinerary.id -> {
                tag = "chTravelItinerary"
                cTable = OASystem.C_TABLE_AIRPLANE_TICKET

            }

            binding.enTravelItinerary.id -> {
                tag = "enTravelItinerary"
                cTable = OASystem.C_TABLE_AIRPLANE_TICKET
            }

            binding.opTravelItineraryWord.id -> {
                tag = "opTravelItineraryWord"
                cTable = OASystem.C_TABLE_GROUND_CONVEY
            }

            binding.opTravelItineraryPdf.id -> {
                tag = "opTravelItineraryPdf"
                cTable = OASystem.C_TABLE_GROUND_CONVEY

            }

            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)
        intent.putExtra("cTable", cTable)

        startActivity(intent)
    }

    private fun isShownHotelCost(): Boolean {
        return if (OASystem.userInfo.depName == "国交部") {
            OASystem.userInfo.jobName == "酒店" || OASystem.userInfo.jobName == "经理" || OASystem.userInfo.jobName == "主管"
        } else {
            OASystem.userInfo.userId == 21
        }
    }

    private fun isShownAirplaneCost(): Boolean {
        return if (OASystem.userInfo.depName == "国交部") {
            OASystem.userInfo.jobName == "机票" || OASystem.userInfo.jobName == "经理" || OASystem.userInfo.jobName == "主管"
        } else {
            OASystem.userInfo.userId == 21
        }
    }
}