package com.dayu.general.activity
|
|
import android.content.Context
|
import android.content.Intent
|
import android.os.Bundle
|
import android.util.TypedValue
|
import android.view.View
|
import android.widget.RadioButton
|
import android.widget.Toast
|
import androidx.appcompat.app.AppCompatActivity
|
import com.dayu.baselibrary.net.subscribers.SubscriberListener
|
import com.dayu.baselibrary.utils.ToastUtil
|
import com.dayu.baselibrary.view.TitleBar
|
import com.dayu.general.R
|
import com.dayu.general.bean.net.CardInfoResult
|
import com.dayu.general.bean.net.PaymentMethod
|
import com.dayu.general.bean.net.PaymentMethodResponse
|
import com.dayu.general.databinding.ActivityRechargeDetailBinding
|
import com.dayu.general.net.ApiManager
|
import com.dayu.general.net.BaseResponse
|
|
class RechargeDetailActivity : AppCompatActivity() {
|
|
private lateinit var binding: ActivityRechargeDetailBinding
|
private var cardInfo: CardInfoResult? = null
|
private var cardAddress: String? = null
|
|
// 支付方式相关属性
|
private var paymentMethod: String = "现金"
|
private var paymentId: Long = 0
|
private var paymentMethodList: List<PaymentMethod> = listOf()
|
|
companion object {
|
private const val EXTRA_CARD_INFO = "extra_card_info"
|
private const val EXTRA_CARD_ADDRESS = "extra_card_address"
|
|
fun start(context: Context, cardInfo: CardInfoResult?, cardAddress: String?) {
|
val intent = Intent(context, RechargeDetailActivity::class.java)
|
intent.putExtra(EXTRA_CARD_INFO, cardInfo)
|
intent.putExtra(EXTRA_CARD_ADDRESS, cardAddress)
|
context.startActivity(intent)
|
}
|
}
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
super.onCreate(savedInstanceState)
|
binding = ActivityRechargeDetailBinding.inflate(layoutInflater)
|
setContentView(binding.root)
|
|
// 隐藏默认ActionBar
|
supportActionBar?.hide()
|
|
// 获取传递的数据
|
cardInfo = intent.getSerializableExtra(EXTRA_CARD_INFO) as? CardInfoResult
|
cardAddress = intent.getStringExtra(EXTRA_CARD_ADDRESS)
|
|
initView()
|
displayCardInfo()
|
// 获取支付方式
|
getPaymentMethods()
|
}
|
|
private fun initView() {
|
// 设置TitleBar的返回按钮点击事件
|
binding.titleBar.setOnItemclickListner(TitleBar.ClickType_LEFT_IMAGE) {
|
finish()
|
}
|
|
// 设置按钮点击事件
|
binding.rechargeRegistBtn.setOnClickListener {
|
// 处理写卡逻辑
|
handleWriteCard()
|
}
|
}
|
|
/**
|
* 获取支付方式列表
|
*/
|
private fun getPaymentMethods() {
|
ApiManager.getInstance().requestGetLoading(
|
this,
|
"sell/paymentmethod/get",
|
PaymentMethodResponse::class.java,
|
null,
|
object : SubscriberListener<BaseResponse<PaymentMethodResponse>>() {
|
override fun onNext(response: BaseResponse<PaymentMethodResponse>) {
|
if (response.success) {
|
// 获取支付方式列表
|
val paymentMethods = response.content?.obj ?: listOf()
|
if (paymentMethods.isNotEmpty()) {
|
paymentMethodList = paymentMethods
|
// 更新支付方式显示
|
updatePaymentMethodRadioGroup()
|
}
|
} else {
|
Toast.makeText(
|
this@RechargeDetailActivity,
|
"获取支付方式失败: ${response.msg}",
|
Toast.LENGTH_SHORT
|
).show()
|
}
|
}
|
|
override fun onError(e: Throwable?) {
|
super.onError(e)
|
Toast.makeText(
|
this@RechargeDetailActivity,
|
"获取支付方式失败: ${e?.message ?: "网络异常"}",
|
Toast.LENGTH_SHORT
|
).show()
|
}
|
}
|
)
|
}
|
|
/**
|
* 更新支付方式RadioGroup
|
*/
|
private fun updatePaymentMethodRadioGroup() {
|
// 清空原有RadioButton
|
binding.newCardPaymentMethod.removeAllViews()
|
|
// 动态添加RadioButton
|
paymentMethodList.forEachIndexed { index, method ->
|
val radioButton = RadioButton(this)
|
radioButton.id = View.generateViewId() // 生成唯一ID
|
radioButton.layoutParams = android.widget.LinearLayout.LayoutParams(
|
0,
|
resources.getDimensionPixelSize(R.dimen.dimen_40),
|
1.0f
|
)
|
|
// 如果不是最后一个按钮,添加右边距
|
if (index < paymentMethodList.size - 1) {
|
(radioButton.layoutParams as android.widget.LinearLayout.LayoutParams).rightMargin =
|
resources.getDimensionPixelSize(R.dimen.dimen_15)
|
}
|
|
radioButton.text = method.name
|
radioButton.background = resources.getDrawable(R.drawable.radio_selector)
|
radioButton.buttonDrawable = null
|
radioButton.gravity = android.view.Gravity.CENTER
|
radioButton.setTextColor(resources.getColorStateList(R.color.radio_button_text_color))
|
radioButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14f)
|
|
// 添加到RadioGroup
|
binding.newCardPaymentMethod.addView(radioButton)
|
|
// 默认选中第一个
|
if (index == 0) {
|
radioButton.isChecked = true
|
paymentMethod = method.name
|
paymentId = method.id
|
}
|
}
|
|
// 设置支付方式选择监听
|
binding.newCardPaymentMethod.setOnCheckedChangeListener { group, checkedId ->
|
// 根据选中的ID获取支付方式
|
for (i in 0 until group.childCount) {
|
val radioButton = group.getChildAt(i) as RadioButton
|
if (radioButton.id == checkedId) {
|
paymentMethod = radioButton.text.toString()
|
paymentId = paymentMethodList[i].id
|
break
|
}
|
}
|
}
|
}
|
|
private fun displayCardInfo() {
|
if (cardInfo == null) {
|
ToastUtil.show("卡信息为空")
|
finish()
|
return
|
}
|
|
cardInfo?.let { info ->
|
// 设置卡片信息
|
binding.redInitCode.text = cardAddress ?: ""
|
binding.userName.text = info.userName ?: ""
|
binding.redCardNum.text = info.cardNum ?: ""
|
binding.redRemainderBlance.text = "${info.balance ?: 0} 元"
|
|
// 设置卡状态和对应颜色
|
val cardStatus = when (info.status) {
|
1 -> "正常"
|
2 -> "挂失"
|
3 -> "锁定"
|
4 -> "无效卡片"
|
else -> "未知"
|
}
|
binding.redStatu.text = cardStatus
|
|
// 根据卡状态设置不同颜色
|
val statusColor = when (info.status) {
|
1 -> android.graphics.Color.parseColor("#4CAF50") // 绿色-正常
|
2 -> android.graphics.Color.parseColor("#FF9800") // 橙色-挂失
|
3 -> android.graphics.Color.parseColor("#F44336") // 红色-锁定
|
else -> android.graphics.Color.parseColor("#757575") // 灰色-未知
|
}
|
binding.redStatu.setTextColor(statusColor)
|
}
|
}
|
|
private fun handleWriteCard() {
|
// 验证充值金额
|
val rechargeAmountStr = binding.rechargeMorny.text.toString().trim()
|
if (rechargeAmountStr.isEmpty()) {
|
ToastUtil.show("请输入充值金额")
|
return
|
}
|
|
val rechargeAmount = try {
|
rechargeAmountStr.toDouble()
|
} catch (e: NumberFormatException) {
|
ToastUtil.show("请输入有效的充值金额")
|
return
|
}
|
|
if (rechargeAmount <= 0) {
|
ToastUtil.show("充值金额必须大于0")
|
return
|
}
|
|
// 获取赠送金额(可选)
|
val bonusAmountStr = binding.rechargeWater.text.toString().trim()
|
val bonusAmount = if (bonusAmountStr.isNotEmpty()) {
|
try {
|
bonusAmountStr.toDouble()
|
} catch (e: NumberFormatException) {
|
0.0
|
}
|
} else {
|
0.0
|
}
|
|
// 显示确认信息
|
val totalAmount = rechargeAmount + bonusAmount
|
val confirmMessage = """
|
确认充值信息:
|
卡号:${cardAddress}
|
充值金额:${rechargeAmount}元
|
赠送金额:${bonusAmount}元
|
总金额:${totalAmount}元
|
支付方式:${paymentMethod}
|
""".trimIndent()
|
|
ToastUtil.show(confirmMessage)
|
|
// TODO: 调用写卡API
|
// 这里可以添加实际的写卡逻辑
|
}
|
}
|