lmw
2023-06-13 adf8013576cbdd12e5ebea8ff7e32baf5d558b27
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package com.kuanzhai.user.dialog
 
import android.content.DialogInterface
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.WebChromeClient
import android.webkit.WebSettings
import android.webkit.WebViewClient
import androidx.core.os.bundleOf
import androidx.fragment.app.DialogFragment
import cn.sinata.xldutils.activity.BaseActivity
import cn.sinata.xldutils.utils.myToast
import cn.sinata.xldutils.utils.showAllowingStateLoss
import com.kuanzhai.user.R
import com.kuanzhai.user.network.HttpManager
import com.kuanzhai.user.network.entity.Card
import com.kuanzhai.user.network.request
import com.kuanzhai.user.utils.Const
import com.kuanzhai.user.utils.pay.PayListener
import com.kuanzhai.user.utils.pay.PayUtil
import kotlinx.android.synthetic.main.dialog_card.*
import kotlinx.android.synthetic.main.item_card_coupon.view.*
import org.jetbrains.anko.matchParent
import org.jetbrains.anko.sdk27.coroutines.onClick
import org.jetbrains.anko.wrapContent
 
class CardDetailDialog: DialogFragment(), PayListener {
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.dialog_card,container,false)
    }
 
    private val card by lazy { arguments?.getParcelable<Card>("data") }
    private val type by lazy { arguments?.getInt("type") }
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_FRAME,R.style.Dialog)
    }
    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        dialog?.window?.setLayout(matchParent, wrapContent)
        dialog?.window?.setGravity(Gravity.BOTTOM)
        dialog?.setCanceledOnTouchOutside(true)
    }
 
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        tv_type.text = when(type){
            Const.OrderType.TYPE_TAXI->"出租车"
            Const.OrderType.TYPE_SPECIAL->"快车/专车"
            Const.OrderType.TYPE_CROSS_CITY->"跨城出行"
            Const.OrderType.TYPE_TRANS_CROSS->"跨城物流"
            Const.OrderType.TYPE_SAME_CITY->"同城物流"
            else->""
        }
        card?.apply {
            tv_title.text = name
            var info = getCardInfo()
            if (type == Const.OrderType.TYPE_TRANS_CROSS||type == Const.OrderType.TYPE_SAME_CITY)
                info = info.replace("打车","寄件")
            tv_describe.text = "%s|%s可用".format(info,cityName)
            tv_origin.text = "原价:%.2f元".format(originalPrice.toDouble())
            tv_price.text = "现价:%.2f元".format(sellingPrice.toDouble())
            tv_pay.text = "立即支付%.2f元".format(sellingPrice.toDouble())
            tv_detail.text = "购买后%s\n%s可用".format(getCardInfo(),cityName)
        }
 
        val settings = tv_rule.settings
        settings.javaScriptEnabled = true
        settings.javaScriptCanOpenWindowsAutomatically = true
        settings.defaultTextEncodingName = "utf-8"
        settings.domStorageEnabled = true
        tv_rule.webChromeClient = object : WebChromeClient() {}
        tv_rule.webViewClient = object : WebViewClient() {}
        getData()
        PayUtil.addPayListener(this)
        tv_cancel.onClick { dismissAllowingStateLoss() }
        tv_pay.onClick {
            val payCardDialog = PayCardDialog()
            payCardDialog.arguments = bundleOf("data" to card)
            payCardDialog.showAllowingStateLoss(childFragmentManager,"pay")
            payCardDialog.setCallback(object :PayCardDialog.Callback{
                override fun onOk(way: Int) {
                    HttpManager.payTaxiCard(card?.id?:0,way).request(requireActivity() as BaseActivity){_,data->
                        if (way == 3)
                            onPaySuccess()
                        else if (way == 2)
                            PayUtil.aliPay(activity!!,data!!.orderInfo)
                        else
                            PayUtil.weChatPay(data!!)
                    }
                }
            })
        }
    }
 
    private fun getData() {
        HttpManager.getTaxiCardInfo(card!!.id).request(requireActivity() as BaseActivity){_,data->
            val sHead =
                "<html><head><meta name=\"viewport\" content=\"width=device-width, " + "initial-scale=1.0, minimum-scale=0.5, maximum-scale=2.0, user-scalable=yes\" />" + "<style>img{max-width:100% !important;height:auto !important;}</style>" + "<style>body{max-width:100% !important;}</style>" + "</head><body>"
            tv_rule.loadDataWithBaseURL(null,sHead+data!!.note, "text/html; charset=utf-8", "utf-8",null)
            data.couponList.forEach {
                val inflate = layoutInflater.inflate(R.layout.item_card_coupon, ll_coupon, false)
                inflate.tv_title_coupon.text = it.couponName
                inflate.tv_range.text =
                    if (it.type == 2)
                        "%s满%.2f元减%.2f元".format(if (it.userType == 0) "无门槛" else it.getCardRangeStr(),it.fullMoney,it.money)
                    else
                        "%s%.2f元抵扣券".format(if (it.userType == 0) "无门槛" else it.getCardRangeStr(),it.money)
                inflate.tv_money.text = "%.2f".format(it.money)
                ll_coupon.addView(inflate)
            }
            if (data.timeQuantum.isNullOrEmpty()||data.timeQuantum[0] == "00:00:00 - 23:59:59"){
                tv_deadline.text = "全天可用"
            }else{
                val joinToString = data.timeQuantum.joinToString("") {
                    it.replace(" ", "") + (if (data.timeQuantum.indexOf(it) % 2 == 0) " " else "\n")
                }
                tv_deadline.text = joinToString.substring(0,joinToString.length-1) +"可用"
            }
        }
    }
 
 
    interface Callback{
        fun onOk(way: Int,couponId: Int)
    }
 
    private var callback: Callback? = null
 
    fun setCallback(callback: Callback){
        this.callback = callback
    }
 
    override fun onPaySuccess() {
        myToast("购买成功")
        callback?.onOk(1,1)
        dismissAllowingStateLoss()
    }
 
    override fun onPayCancel() {
        myToast("取消支付")
    }
 
    override fun onDismiss(dialog: DialogInterface) {
        PayUtil.removePayListener(this)
        PayUtil.unregisterApp()
        super.onDismiss(dialog)
    }
}