lmw
2023-05-12 f67802a41f9e01444d1115f34ecc6e1beb05fc3b
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
package com.fuban.user.ui.trip
 
import android.app.Activity
import android.graphics.Typeface
import android.view.View
import android.widget.CheckBox
import androidx.core.os.bundleOf
import cn.sinata.xldutils.*
import cn.sinata.xldutils.utils.*
import com.fuban.user.R
import com.fuban.user.interfaces.StringCallback
import com.fuban.user.network.Apis
import com.fuban.user.network.HttpManager
import com.fuban.user.network.entity.Order
import com.fuban.user.network.request
import com.fuban.user.ui.TransparentStatusBarActivity
import com.fuban.user.ui.crosscity.ShuttleBusActivity
import com.fuban.user.dialog.PayDialog
import com.fuban.user.dialog.ShareDialog
import com.fuban.user.dialog.SpecialSuggestDialog
import com.fuban.user.dialog.TipDialog
import com.fuban.user.utils.Const
import com.fuban.user.utils.pay.PayListener
import com.fuban.user.utils.pay.PayUtil
import com.fuban.user.wxapi.WXPayEntryActivity
import com.share.utils.ShareUtils
import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram
import com.tencent.mm.opensdk.openapi.WXAPIFactory
import com.umeng.socialize.UMShareListener
import com.umeng.socialize.bean.SHARE_MEDIA
import kotlinx.android.synthetic.main.activity_order_detail.*
import org.jetbrains.anko.sdk27.coroutines.onClick
import org.jetbrains.anko.startActivity
 
class OrderDetailActivity : TransparentStatusBarActivity(), PayListener, UMShareListener {
    override fun setContentView() = R.layout.activity_order_detail
 
    private val id by lazy {
        intent.getIntExtra("id", 0)
    }
    private val type by lazy {
        intent.getIntExtra("type", 0)
    }
    private var order: Order? = null
    private var payMoney = 0.0//支付费用
    private var isGotoWx = false //是否跳转到微信小程序支付 true:回来时需要查询是否支付成功
 
    private val mPayDialog by lazy {
        val payDialog = PayDialog()
        payDialog.setCallback(object : PayDialog.Callback {
            override fun onOk(way: Int, couponId: Int) {
                pay(way, couponId)
                payDialog.dismiss()
            }
 
            override fun onClose() {
            }
        })
        payDialog
    }
    private var score = 0
 
    override fun initClick() {
        iv_call.setOnClickListener {
            callPhone(order?.driverPhone)
        }
        tv_action.setOnClickListener {
            if (order?.state == 7 || order?.state == 12) {
                mPayDialog.show(supportFragmentManager, "pay")
            } else if (order?.state == 8) {
                HttpManager.orderEvaluate(id, score, type, checkList.joinToString(",") { it })
                    .request(this) { _, _ ->
                        setResult(Activity.RESULT_OK)
                        myToast("评价成功")
                        tv_action.gone()
                        tv_title1.text = "评价详情"
                        getData()
                        getRedMoney()
                    }
            }
        }
        rating.setOnRatingChangeListener {
            if (!rating.clickable)
                return@setOnRatingChangeListener
            tv_action.visible()
            if (it == 5f && score != 5) {
                inputLabel(true)
            }
            if (it != 5f && (score == 0 || score == 5)) {
                inputLabel(false)
            }
            score = it.toInt()
        }
        iv_header_home.setOnClickListener {
            if (order != null)
                startActivity<DriverDetailActivity>("id" to order!!.driverId)
        }
        tv_detail.onClick {
            startActivity<PriceDetailActivity>("order" to order)
        }
        tv_pay_offline.onClick {
//            val tipDialog = TipDialog()
//            tipDialog.arguments = bundleOf("msg" to "是否确认已线下支付费用?","ok" to "确认","cancle" to "取消")
//            tipDialog.setCallback(object :TipDialog.OnClickCallback{
//                override fun onOk() {
//                    HttpManager.completeOrder(id,type).request(this@OrderDetailActivity){_,_->
//                        onPaySuccess()
//                    }
//                }
//
//                override fun onCancel() {
//                }
//            })
//            tipDialog.show(supportFragmentManager,"offline")
        }
    }
 
    override fun initView() {
        titleBar.addRightButton("投诉司机", onClickListener = View.OnClickListener {
            order?.driverId?.let {
                startActivity<ComplainActivity>("id" to it)
            }
        })
        showDialog()
        getData()
        PayUtil.addPayListener(this)
    }
 
    private fun getData() {
        HttpManager.queryOrderInfo(id, type).request(this) { _, data ->
            order = data
            data?.run {
                tv_start.text = startAddress
                tv_end.text = endAddress
                tv_time.text = travelTime
                iv_header_home.setImageURI(driverAvatar)
                tv_name.text = "$driverName $licensePlate"
                tv_car_info.text = "$brand·$carColor"
                tv_score.text = String.format("%.1f", score)
                tv_count.text = String.format("%d单", orderNum)
                if (type == Const.OrderType.TYPE_CROSS_CITY) {
                    tv_passenger_num.visible()
                    tv_passenger_num.text =
                        String.format("%d人乘车:%s号座位", peopleNumber, seatNumber).replace(",", "号,")
                    tv_remark.visibility = if (remark.isNotEmpty()) View.VISIBLE else View.GONE
                    tv_remark.text = remark
                }
                setStateUI()
                if (state == 10 || state == 12)
                    tv_tip.text = "注:司机已接单后用户取消订单费用"
                else if (tipMoney == 0.0) {
                    tv_tip.invisible()
                } else {
                    tv_tip.typeface = Typeface.DEFAULT_BOLD
                    val content = String.format("其中含小费:¥%.2f", tipMoney)
                    tv_tip.text = SpanBuilder(content).color(
                        this@OrderDetailActivity,
                        6,
                        content.length,
                        R.color.colorPrimary
                    ).build()
                }
            }
        }
    }
 
    /**
     * 评价成功以后需要查询是否可以获得分享红包
     */
    private fun getRedMoney() {
        HttpManager.queryRedMoney(id, type).request(this) { _, data ->
            data?.let {
                val money = it.optDouble("amount")
                if (money > 0) {
                    val tipDialog = TipDialog()
                    tipDialog.arguments = bundleOf(
                        "msg" to String.format("分享APP给好友可以获得%.2f元打车红包", money),
                        "ok" to "去分享", "cancel" to "不分享"
                    )
                    tipDialog.setCallback(object : TipDialog.OnClickCallback {
                        override fun onOk() {
                            val shareDialog = ShareDialog()
                            shareDialog.setCallback(object : ShareDialog.Callback {
                                override fun onOk(way: Int) { //0:wx 1:qq 2:msg
                                    ShareUtils.share(
                                        this@OrderDetailActivity,
                                        if (way == 0) SHARE_MEDIA.WEIXIN else if (way == 1) SHARE_MEDIA.QQ else
                                            SHARE_MEDIA.MORE,
                                        Const.Share.NORMAL_CONTENT,
                                        Const.Share.TITLE,
                                        String.format(
                                            Apis.SHARE_URL,
                                            SPUtils.instance().getInt(Const.User.USER_ID)
                                        ),
                                        this@OrderDetailActivity,
                                        ""
                                    )
                                    shareDialog.dismiss()
                                    getRed(money)
                                }
                            })
                            shareDialog.show(supportFragmentManager, "share")
                        }
 
                        override fun onCancel() {}
                    })
                    tipDialog.show(supportFragmentManager, "red")
                }
            }
 
        }
    }
 
    private fun setStateUI() {
        tv_pay_offline.gone()
        when (order?.state) {
            7 -> { //待支付
                tv_pay_offline.visible() //新增提示文字
                payMoney = order!!.orderMoney
                tv_money.text = String.format("¥%.2f", payMoney)
                title = "支付费用"
                tv_action.visible()
                tv_title.visible()
//                titleBar.getRightButton(0)?.gone() //刘矩:都加上投诉
                tv_action.text = "立即支付"
                queryBalance()
                if (order!!.device == 2) {//车载端,显示线下付款按钮 2.0客户反馈,不要该按钮
//                    tv_pay_offline.visible()
                }
                if (type == 1) //专车有查看明细
                    tv_detail.visible()
            }
            8 -> { //待评价
                title = if (type == Const.OrderType.TYPE_CROSS_CITY) "已完成" else "行程结束"
                tv_money.text = String.format("¥%.2f", order!!.payMoney)
                if (0.0 == order!!.payMoney && order!!.tipMoney == 0.0) { //其他方式收费
                    tv_money.invisible()
                    tv_tip.invisible()
                }
                tv_title.hide()
                tv_action.text = "匿名评价"
                titleBar.getRightButton(0)?.visible()
                cl_evaluate.visible()
                tv_detail.visible()
                if (type == Const.OrderType.TYPE_SPECIAL) //专车有查看明细
                    tv_detail.visible()
                if (type == Const.OrderType.TYPE_CROSS_CITY) {//跨城要提示摆渡车
                    val specialSuggestDialog = SpecialSuggestDialog()
                    specialSuggestDialog.setCallback(object : StringCallback {
                        override fun onRlt(rlt: String) {
                            if (rlt == "ok")
                                startActivity<ShuttleBusActivity>("order" to order,"isStart" to false)
                        }
                    })
                    specialSuggestDialog.show(supportFragmentManager,"suggest")
                }
            }
            9 -> { //已完成
                title = "行程结束"
                tv_money.text = String.format("¥%.2f", order!!.payMoney)
                if (0.0 == order!!.payMoney && order!!.tipMoney == 0.0) { //其他方式收费
                    tv_money.invisible()
                    tv_tip.invisible()
                }
                tv_action.gone()
                titleBar.getRightButton(0)?.visible()
                cl_evaluate.visible()
                rating.isClickable = false
                rating.setStar(order!!.orderScore.toFloat())
                fl_label.removeAllViews()
                val list = arrayListOf<String>()
                list.addAll(if (order!!.orderScore == 5) highList else lowList)
                val split = order!!.evaluate.split(",")
                list.forEach {
                    val view = layoutInflater.inflate(R.layout.item_score_label, null) as CheckBox
                    view.text = it
                    view.isEnabled = false
                    if (it in split)
                        view.isChecked = true
                    fl_label.addView(view)
                }
                tv_detail.visible()
            }
            10 -> {
                title = "已取消"
                tv_money.text = String.format("¥%.2f", order!!.cancelPayMoney)
                titleBar.getRightButton(0)?.gone() //刘矩:已取消没有投诉
                if (type == Const.OrderType.TYPE_CROSS_CITY) { //跨城取消没有扣钱
                    tv_money.gone()
                    tv_tip.gone()
                }
            }
            12 -> {//取消待支付
                title = "支付费用"
                titleBar.getRightButton(0)?.gone()
                payMoney = order!!.cancelPayMoney
                tv_money.text = String.format("¥%.2f", payMoney)
                tv_action.visible()
                tv_title.visible()
                tv_action.text = "立即支付"
//                queryBalance()
                mPayDialog.arguments =
                    bundleOf("isCancel" to true, "money" to payMoney, "id" to id, "type" to type)
            }
        }
    }
 
    private val lowList = arrayListOf(
        "道路不熟", "服务态度恶劣", "车辆肮脏", "车内有异味", "车牌号不符",
        "司机吸烟", "打电话玩手机", "未坐好就开车", "未提醒系安全带", "未提醒开门注意", "过路口不减速", "索要好评"
    )
    private val highList = arrayListOf("车内整洁", "活地图认路准", "驾驶平稳", "态度好服务棒")
    private val checkList = arrayListOf<String>()
    private fun inputLabel(isHigh: Boolean) {
        fl_label.removeAllViews()
        checkList.clear()
        val list = arrayListOf<String>()
        list.addAll(if (isHigh) highList else lowList)
        list.forEach {
            val view = layoutInflater.inflate(R.layout.item_score_label, null) as CheckBox
            view.text = it
            view.setOnCheckedChangeListener { _, isChecked ->
                if (isChecked)
                    checkList.add(view.text.toString())
                else
                    checkList.remove(view.text.toString())
            }
            fl_label.addView(view)
        }
    }
 
    /**
     * 查询余额和优惠券以供支付
     */
    private fun queryBalance() {
        HttpManager.queryBalance(id, type).request(this) { _, data ->
            data?.let {
                mPayDialog.arguments = bundleOf(
                    "isCancel" to (order?.state == 12), "balance" to it.optDouble("balance"),
                    "coupon" to it.optInt("coupon"), "money" to payMoney, "id" to id, "type" to type
                )
            }
        }
    }
 
    /**
     * @param way 1=微信,2=支付宝,3=余额
     */
    private fun pay(way: Int, couponId: Int) {
//        if (way == 1){ //微信支付,跳转微信小程序
//            val api = WXAPIFactory.createWXAPI(this, Const.WX_APP_ID)
//            val req = WXLaunchMiniProgram.Req()
//            req.userName = "gh_bc8f199f11ae" // 填小程序原始id
//            val content = if (order!!.state == 12)"{\"cancleId\":${order!!.cancelId}}"
//                else if (couponId!=0) "{\"couponId\":${couponId}}"
//                else ""
//            req.path = "/pages/appPay/appPay?orderId=${id}&orderType=${type}&type=${if (order!!.state == 12) 2 else 1}" +
//                    "&userType=1&uid=${SPUtils.instance().getInt(Const.User.USER_ID)}&content=${content}"
//            req.miniprogramType = WXLaunchMiniProgram.Req.MINIPTOGRAM_TYPE_RELEASE// 可选打开 开发版,体验版和正式版
//            isGotoWx = true
//            api.sendReq(req)
//        }else{
        if (order!!.state == 12)
            HttpManager.cancleOrderTaxi(id, way, order!!.cancelId, type).request(this) { _, data ->
                if (way == 3)
                    onPaySuccess()
                else if (way == 2)
                    PayUtil.aliPay(this, data!!.orderString)
                else if (way == 1)
                    startActivity<WXPayEntryActivity>("type" to 1, "data" to data)
 
            } else
            HttpManager.payTaxiOrder(id, way, type, if (couponId == 0) null else couponId)
                .request(this) { _, data ->
                    if (way == 3)
                        onPaySuccess()
                    else if (way == 2)
                        PayUtil.aliPay(this, data!!.orderString)
                    else if (way == 1)
                        startActivity<WXPayEntryActivity>("type" to 1,"data" to data)
                }
//        }
    }
 
    /**
     * 分享后获得红包
     */
    private fun getRed(money: Double) {
        HttpManager.shareRedEnvelope(id, type).request(this) { _, _ ->
            val tipDialog = TipDialog()
            tipDialog.arguments = bundleOf(
                "msg" to String.format("您收到一个%.2f元的打车红包,已经放入钱包,下次打车可抵扣金额", money),
                "ok" to "确定", "isAlert" to true
            )
            tipDialog.show(supportFragmentManager, "red")
        }
    }
 
    override fun onPaySuccess() {
        myToast("支付成功")
        setResult(Activity.RESULT_OK)
        tv_title.hide()
        tv_action.gone()
        if (order?.state == 12) {
            title = "已取消"
            titleBar.getRightButton(0).gone()
        } else {
            showDialog()
            getData()
        }
    }
 
    override fun onPayCancel() {
 
    }
 
    override fun onResume() {
        super.onResume()
        if (isGotoWx) {
            isGotoWx = false
            HttpManager.queryOrderInfo(id, type).request(this) { _, data ->
                if (data?.state == order?.state) {
                    myToast("支付失败")
                } else {
                    onPaySuccess()
                }
            }
        }
    }
 
 
    override fun onDestroy() {
        super.onDestroy()
        PayUtil.removePayListener(this)
    }
 
    override fun onResult(p0: SHARE_MEDIA?) {
    }
 
    override fun onCancel(p0: SHARE_MEDIA?) {
    }
 
    override fun onError(p0: SHARE_MEDIA?, p1: Throwable?) {
    }
 
    override fun onStart(p0: SHARE_MEDIA?) {
 
    }
 
}