package com.cheng.blzb.ui.fragment.mine.order.invoice

import android.graphics.Color
import android.os.Build
import android.text.TextUtils
import android.view.View
import com.cheng.blzb.R
import com.cheng.blzb.bean.OrderEntity
import com.cheng.blzb.databinding.FragmentInvoiceBinding
import com.cheng.blzb.event.OrderEvent
import com.efs.sdk.memleaksdk.monitor.internal.bi
import com.example.base.common.RxBus
import com.example.base.extensions.gone
import com.example.base.extensions.onClick
import com.example.base.extensions.toast
import com.example.base.extensions.visible
import com.example.base.ui.BaseFragment
import com.google.gson.JsonObject
import okhttp3.RequestBody.Companion.toRequestBody
import java.text.DecimalFormat

class InvoiceFragment : BaseFragment<FragmentInvoiceBinding, InvoiceViewModel>() {
    private var userType = 0 // 0 企业 1 个人
    private var invoiceType = 0 // 0 普票 1 专票

    private var orderInfo: OrderEntity? = null

    override fun initView() {
        super.initView()
        mTitleBar?.setBackgroundColor(Color.WHITE)
        setStyle()
    }

    override fun initData() {
        super.initData()
        orderInfo = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            arguments?.getSerializable("item", OrderEntity::class.java)
        } else {
            arguments?.getSerializable("item") as? OrderEntity
        }

        binding.rb2.visibility = if (orderInfo!!.invoice_major) View.VISIBLE else View.GONE
        binding.tv2.visibility = if (orderInfo!!.invoice_major) View.VISIBLE else View.GONE

        binding.tvAmount.text = DecimalFormat("0.00").format(orderInfo!!.total_fee.toFloat())
    }

    override fun initListener() {
        super.initListener()
        binding.rgTitleType.setOnCheckedChangeListener { _, checkedId ->
            when(checkedId) {
                R.id.rb_title1 -> {
                    userType = 0
                    setStyle()
                }
                R.id.rb_title2 -> {
                    userType = 1
                    setStyle()
                }
            }
        }

        binding.rgType.setOnCheckedChangeListener { _, checkedId ->
            when(checkedId) {
                R.id.rb1 -> {
                    invoiceType = 0
                    setStyle()
                }
                R.id.rb2 -> {
                    invoiceType = 1
                    setStyle()
                }
            }
        }

        binding.tv1.onClick {
            binding.rb1.isChecked = true
        }

        binding.tv2.onClick {
            binding.rb2.isChecked = true
        }

        binding.btnNext.onClick {
            commit()
        }
    }

    override fun initObserve() {
        super.initObserve()
        mViewModel.updateLiveData.observe(this) {
            toast("提交成功")
            RxBus.defaultInstance.post(OrderEvent())
            requireActivity().finish()
        }
    }

    private fun setStyle() {
        if (userType == 0) {
            binding.layoutType.visible()
            binding.layoutEnterpriseName.visible()
            binding.layoutEnterpriseCode.visible()
            binding.layoutUsername.gone()
            binding.layoutIdCard.gone()
            if (invoiceType == 0) {
                binding.layoutEnterpriseAddress.gone()
                binding.layoutEnterprisePhone.gone()
                binding.layoutBankName.gone()
                binding.layoutBankNo.gone()
            } else {
                binding.layoutEnterpriseAddress.visible()
                binding.layoutEnterprisePhone.visible()
                binding.layoutBankName.visible()
                binding.layoutBankNo.visible()
            }
        } else {
            binding.layoutUsername.visible()
            binding.layoutIdCard.visible()

            binding.layoutType.gone()
            binding.layoutEnterpriseName.gone()
            binding.layoutEnterpriseCode.gone()
            binding.layoutEnterpriseAddress.gone()
            binding.layoutEnterprisePhone.gone()
            binding.layoutBankName.gone()
            binding.layoutBankNo.gone()
        }
    }

    private fun commit() {
        val enterpriseName = binding.etEnterpriseName.text.toString().trim()
        val enterpriseCode = binding.etEnterpriseCode.toString().trim()
        val enterpriseAddress = binding.etEnterpriseAddress.text.toString().trim()
        val enterprisePhone = binding.etEnterprisePhone.text.toString().trim()
        val bankName = binding.etBankName.text.toString().trim()
        val bankNo = binding.etBankNo.text.toString().trim()

        val username = binding.etUsername.text.toString().trim()
        val idCard = binding.etIdCard.text.toString().trim()

        val email = binding.etEmail.text.toString().trim()

        if (userType == 0) {
            if (TextUtils.isEmpty(enterpriseName)) {
                toast("请输入企业名称")
                return
            }
            if (TextUtils.isEmpty(enterpriseCode)) {
                toast("请输入纳税人识别号")
                return
            }
            if (invoiceType == 1) {
                if (TextUtils.isEmpty(enterpriseAddress)) {
                    toast("请输入详细地址")
                    return
                }
                if (TextUtils.isEmpty(enterprisePhone)) {
                    toast("请输入电话号码")
                    return
                }
                if (TextUtils.isEmpty(bankName)) {
                    toast("请输入开户行")
                    return
                }
                if (TextUtils.isEmpty(bankNo)) {
                    toast("请输入开户行账户")
                    return
                }
            }
        } else {
            if (TextUtils.isEmpty(username)) {
                toast("请输入您的姓名")
                return
            }
            if (TextUtils.isEmpty(idCard)) {
                toast("请输入您的身份证号码")
                return
            }
        }
        if (TextUtils.isEmpty(email)) {
            toast("请输入邮箱地址")
            return
        }
        val jsonObject = JsonObject()
        jsonObject.addProperty("order_id", orderInfo!!.id)
        jsonObject.addProperty("invoice_status", "2")
        val paramsObj = JsonObject()
        paramsObj.addProperty("email", email)
        if (userType == 0) {
            paramsObj.addProperty("name", enterpriseName)
            paramsObj.addProperty("tax_no", enterpriseCode)
            paramsObj.addProperty("invoice_type", if (invoiceType == 0) "普票" else "专票")
            if (invoiceType == 1) {
                paramsObj.addProperty("address", enterpriseAddress)
                paramsObj.addProperty("phone", enterprisePhone)
                paramsObj.addProperty("bank", bankName)
                paramsObj.addProperty("bank_number", bankNo)
            }
        } else {
            paramsObj.addProperty("name", username)
            paramsObj.addProperty("tax_no", idCard)
        }
        jsonObject.add("invoice_params", paramsObj)
        mViewModel.updateOrder(jsonObject.toString().toRequestBody())
    }
}