18034727885
2026-08-07 bbc64160643c4ed818eb574831dc303b0a979089
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
package com.driver.igotrip.ui.main.small_cargo
 
import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.util.TypedValue
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import cn.sinata.rxnetty.LogHelper
import cn.sinata.xldutils.utils.*
import com.driver.igotrip.R
import com.driver.igotrip.base.BaseEvent
import com.driver.igotrip.base.Const
import com.driver.igotrip.base.MyApplication
import com.driver.igotrip.base.MyBaseActivity
import com.driver.igotrip.base.gaode.AMapKit
import com.driver.igotrip.base.gaode.AMapKit.addMarker
import com.driver.igotrip.base.gaode.AMapKit.getIcon
import com.driver.igotrip.base.gaode.AMapKit.getTimeType
import com.driver.igotrip.base.gaode.AMapKit.initMap
import com.driver.igotrip.base.gaode.AMapKit.moveCamera
import com.driver.igotrip.bean.*
import com.driver.igotrip.extension.extClearInvalidZero
import com.driver.igotrip.netUtls.*
import com.driver.igotrip.ui.DialogUtil
import com.driver.igotrip.ui.UiUtil
import com.driver.igotrip.ui.main.MediaOperationActivity
import com.driver.igotrip.ui.main.ReassignActivity
import com.driver.igotrip.utils.DateUtil
import com.driver.igotrip.utils.MyUtils
import com.driver.igotrip.utils.view.SlidingButton
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.SupportMapFragment
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.Marker
import com.google.android.gms.maps.model.Polyline
import com.google.gson.Gson
import com.trello.rxlifecycle3.android.ActivityEvent
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import org.greenrobot.eventbus.EventBus
import org.jetbrains.anko.find
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast
import java.util.concurrent.TimeUnit
 
 
class SmallCargoActivity : MyBaseActivity() {
    /***
     *     1=待接单,2=待出发,3=待到达预约地点,4=待取货,5=送货中,6=已送达,7=待支付,8=需补差价,9=已取货,10=已取消,11=改派中,12=已支付差价
 
    integer($int32)
    订单状态(1=待接单,2=待出发,3=待到达预约地点,4=待乘客上车,5=服务中,6=完成服务,7=待支付,8=待评价,9=已完成,10=已取消,11=改派中,12=取消待支付)
     */
 
    /***
     * //    流程操作状态(3=开始出发预约点,4=到达预约点,5=开始服务,6=服务结束)
     * 页面一共4个marker 1:预约地点 2:起点 3终点 4:司机点
     */
    val orderType by lazy {
        intent.getStringExtra("orderType") ?: ""
    }
 
    val orderId by lazy {
        intent.getStringExtra("orderId") ?: ""
    }
    var isTimeStart = false //限制计时器
 
    var makeMarker: Marker? = null
    var startMarker: Marker? = null
    var endMarker: Marker? = null
    var carMarker: Marker? = null
    var markerLine: Polyline? = null
    var mapInitStatus = false // 地图初始化状态
    lateinit var aMap: GoogleMap
    var orderBean: OrderBean? = null
    var graylist = arrayListOf<LatLng>() // 灰色轨迹
    var grayline: Polyline? = null
 
    //业务多媒体类型
    var businessMediaType: Int = 0
 
    private val tv_name by lazy { find<TextView>(R.id.tv_name) }
    private val tv_car_num by lazy { find<TextView>(R.id.tv_car_num) }
    private val tv_people_cargo by lazy { find<TextView>(R.id.tv_people_cargo) }
    private val tv_remark by lazy { find<TextView>(R.id.tv_remark) }
    private val tv_money_cargo by lazy { find<TextView>(R.id.tv_money_cargo) }
    private val tv_time by lazy { find<TextView>(R.id.tv_time) }
    private val tv_start_address by lazy { find<TextView>(R.id.tv_start_address) }
    private val tv_end_address by lazy { find<TextView>(R.id.tv_end_address) }
    private val tv_gap_money by lazy { find<TextView>(R.id.tv_gap_money) }
    private val btn_gap_money by lazy { find<TextView>(R.id.btn_gap_money) }
    private val iv_move by lazy { find<View>(R.id.iv_move) }
    private val iv_phone by lazy { find<View>(R.id.iv_phone) }
    private val iv_phone1 by lazy { find<View>(R.id.iv_phone1) }
    private val iv_police by lazy { find<View>(R.id.iv_police) }
    private val iv_to_gd by lazy { find<View>(R.id.iv_to_gd) }
    private val slide_btn by lazy { find<SlidingButton>(R.id.slide_btn) }
 
    //跳转另一个页面并接收返回结果启动器
    private lateinit var launcherForResult: ActivityResultLauncher<Intent>
 
    override fun setContentView() {
        setContentView(R.layout.activity_trip_cargo)
    }
 
    fun initView2() {
        setTitleText("")
        tv_Right.text = getString(R.string.申请改派短)
        callOrder() {
            initMineLocation()
        }
        onclick()
        addSockectCancel()
 
        val language = SPUtils.getString(Const.language)
        if (language == Const.Language.ENGLISH || language == Const.Language.FRENCH)
            btn_gap_money.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12f)
        else
            btn_gap_money.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14f)
        LogHelper.logRecord("处理快件订单orderId:${orderId} orderType:${orderType}")
    }
 
    override fun initView() {
        launcherForResult =
            registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
                when (businessMediaType) {
                    0 -> {
                        if (result.resultCode == RESULT_OK && result.data != null) {
                            val result = result.data!!.getStringArrayListExtra("result")
                            showConfirmPriceDialog(result!!.joinToString(separator = ","))
                        } else {
 
                        }
                    }
 
                    1 -> {
                        if (result.resultCode == RESULT_OK && result.data != null) {
                            val result = result.data!!.getStringArrayListExtra("result")
                            showConfirmDeliveryDialog(result!!.joinToString(separator = ","))
                        } else {
 
                        }
                    }
                }
            }
    }
 
    private fun addSockectCancel() {
        checkOrderStatue()
        MyApplication.addOrderView(listener)
    }
 
    private val listener = object : MyApplication.OrderStatueView {
        override fun orderInfo(data: OrderSimpleData?) {
            data?.let {
                if (it.status == 10 || it.status == 11 || it.status == 12) {
                    MyApplication.currentOrderType = ""
                    MyApplication.currentOrderId = ""
                    Handler(Looper.getMainLooper()).post {
                        finish()
                    }
                }
                if ((it.orderType == 4 || it.orderType == 5) && it.orderId.toString() == orderId && (it.status != 10 && it.status != 11 && it.status != 12)) {
                    Handler(Looper.getMainLooper()).post {
                        callOrder() {}
                    }
                }
            }
        }
    }
    var handler: Handler? = null
    fun checkOrderStatue() {
        val map = getMapByAny()
        map["orderId"] = orderId
        map["orderType"] = orderType
        callNet(false, Api.orderStateSocket, map) { it ->
            val orderSimpleData =
                Gson().fromJson(
                    it,
                    OrderSimpleData::class.java
                )
            orderSimpleData?.let {
                if (it.status == 10 || it.status == 12) {
                    MyApplication.currentOrderType = ""
                    MyApplication.currentOrderId = ""
                    Handler(Looper.getMainLooper()).post {
//                        MyApplication.getTTsManager().setVideoText(it.audioUrl)
                        finish()
                    }
                }
            }
 
        }
        handler?.postDelayed({ checkOrderStatue() }, 3000)
    }
 
 
    override fun onBackPressed() {
        EventBus.getDefault().post(BaseEvent(BaseEvent.UPDATA_MAIN_CAR))
        super.onBackPressed()
    }
 
    override fun finish() {
        EventBus.getDefault().post(BaseEvent(BaseEvent.UPDATA_MAIN_CAR))
        super.finish()
    }
 
    private fun onclick() {
        tv_Right.clickDelay {
            val map = getMapByAny()
            map["orderId"] = orderId
            map["orderType"] = orderType
            callNet(Api.queryReassignMoney, map, {
                val data = gson.fromJson<RessignBean>(it, RessignBean::class.java)
                LogHelper.logRecord("查询订单改派金额成功orderId:${orderId} orderType:${orderType} data:${Gson().toJson(data)}")
                startActivity<ReassignActivity>(
                    "orderId" to orderId,
                    "orderType" to orderType
                )
            }) { code, msg ->
                LogHelper.logRecord("查询订单改派金额失败orderId:${orderId} orderType:${orderType} code:${code} msg:${msg}")
            }
        }
 
        btn_gap_money.clickDelay {
            orderBean?.let { orderBean ->
                if (orderBean.data.orderState == 13) {
                    toast(getString(R.string.用户已支付差价))
                    return@clickDelay
                }
                val dialog = DialogUtil.getDialog(this, R.layout.dialog_show_cha_money)
                val viewContent = DialogUtil.getView(dialog)
                val view_hint_close_car_cha = viewContent.find<View>(R.id.view_hint_close_car_cha)
                val tv_car_num_close_cha = viewContent.find<View>(R.id.tv_car_num_close_cha)
                val tv_sure_car_num_cha = viewContent.find<View>(R.id.tv_sure_car_num_cha)
                val et_num_zg_cha = viewContent.find<EditText>(R.id.et_num_zg_cha)
                view_hint_close_car_cha.setOnClickListener {
                    dialog.dismiss()
                }
                tv_car_num_close_cha.setOnClickListener {
                    dialog.dismiss()
                }
                tv_sure_car_num_cha.setOnClickListener {
                    val num = et_num_zg_cha.getContent()
                    if (num.isEmpty()) {
                        toast(getString(R.string.请输入金额))
                        return@setOnClickListener
                    }
                    if (num.toDouble() > 9999) {
                        toast(getString(R.string.金额不能大于9999元))
                        return@setOnClickListener
                    }
                    if (num.toDouble() <= 0) {
                        toast(getString(R.string.金额不能小于0元))
                        return@setOnClickListener
                    }
                    callseatmoney(num, {
                        LogHelper.logRecord("补差价操作成功orderId:${orderId} orderType:${orderType}")
                        dialog.dismiss()
                        btn_gap_money.text =
                            getString(R.string.差价待确认).format(num.extClearInvalidZero())
                        btn_gap_money.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, 0, 0)
//                        toast(getString(R.string.set_success))
                    }) { code, msg ->
                        LogHelper.logRecord("补差价操作失败orderId:${orderId} orderType:${orderType} code:${code} msg:${msg}")
                    }
                }
            }
        }
 
        tv_gap_money.clickDelay {
//            btn_gap_money.callOnClick()
        }
 
        iv_move.setOnClickListener {
            orderBean?.let {
                when (it.data.orderState) {
                    2, 3 -> {
                        moveCamera(aMap, makeMarker!!.position, carMarker!!.position)
                    }
 
                    4 -> {
                        moveCamera(aMap, carMarker!!.position)
                    }
 
                    5, 6 -> {
                        moveCamera(aMap, carMarker!!.position, endMarker!!.position)
                    }
                }
            }
        }
 
        iv_police.clickDelay {
            orderBean?.let {
                MyUtils.getInstans().CallPhone(this, it.data.emergencyCall)
            }
        }
 
 
        iv_to_gd.clickDelay {
            orderBean?.let {
                when (it.data.orderState) {
                    2, 3 -> {
                        startNavi(
                            carMarker!!.position.latitude,
                            carMarker!!.position.longitude,
                            makeMarker!!.position.latitude,
                            makeMarker!!.position.longitude
                        )
                    }
 
                    4 -> {
                        showToast(getString(R.string.没有终点无法导航))
                    }
 
                    5, 6 -> {
                        startNavi(
                            carMarker!!.position.latitude,
                            carMarker!!.position.longitude,
                            endMarker!!.position.latitude,
                            endMarker!!.position.longitude
                        )
                    }
                }
            }
        }
 
        // old 1=待接单,2=待出发,3=待到达预约地点,4=待取货,5=送货中,6=已送达,7=待支付,8=需补差价,9=已取货,10=已取消,11=改派中,12=已支付差价
        // new 1=待接单,2=待出发,3=待到达预约地点,4=待乘客上车,5=服务中,6=完成服务,7=待支付,8=待评价,9=已完成,10=已取消,11=改派中,12=取消待支付,13=补差价
        slide_btn.onSwipeListener = {
            orderBean?.let {
                when (it.data.orderState) {
                    2, 3, 4 -> {
                        if (it.data.orderState == 4) {
                            if (orderType == "4") {
                                businessMediaType = 0
                                launcherForResult.launch(
                                    MediaOperationActivity.getIntent(
                                        this,
                                        getString(R.string.请上传图片),
                                        getString(R.string.确认取货), 3, 1
                                    )
                                )
                            } else {
                                it.data.orderState++
                                callStatue("1", it.data.orderState, {
                                    LogHelper.logRecord("更新订单状态为送件中成功orderId:${orderId} orderType:${orderType}")
                                    iv_to_gd.callOnClick()
                                }) { code, msg ->
                                    it.data.orderState--
                                    LogHelper.logRecord("更新订单状态为送件中失败orderId:${orderId} orderType:${orderType} code:${code} msg:${msg}")
                                }
                                showStatueMapUI(it)
                            }
                        } else {
                            it.data.orderState++
                            callStatue(it.data.orderState, "", {
                                LogHelper.logRecord("更新订单状态成功orderId:${orderId} orderType:${orderType} state:${it.data.orderState}")
                                if (it.data.orderState == 3)
                                    iv_to_gd.callOnClick()
                            }) { code, msg ->
                                it.data.orderState--
                                LogHelper.logRecord("更新订单状态失败orderId:${orderId} orderType:${orderType} code:${code} msg:${msg}")
                            }
                            showStatueMapUI(it)
                        }
                    }
 
                    5, 6 -> {
//                        sendCode() {
                        businessMediaType = 1
                        launcherForResult.launch(
                            MediaOperationActivity.getIntent(
                                this,
                                getString(R.string.请上传图片),
                                getString(R.string.确认送达), 3, 1
                            )
                        )
//                        }
                    }
 
                    12 -> {
                        it.data.orderState = 5
                        callStatue(it.data.orderState, "", {
                            LogHelper.logRecord("取消待支付订单更新状态为送件中成功orderId:${orderId} orderType:${orderType}")
                        }) { code, msg ->
                            LogHelper.logRecord("取消待支付订单更新状态为送件中失败orderId:${orderId} orderType:${orderType} code:${code} msg:${msg}")
                        }
                        showStatueMapUI(it)
                    }
 
                    8 -> {
//                        toast(getString(R.string.等待用户支付差价))
                    }
 
                    else -> {
 
                    }
                }
            }
        }
 
        iv_phone.clickDelay {
            orderBean?.let {
                MyUtils.getInstans().CallPhone(this, it.data.userPhone)
            }
        }
        iv_phone1.clickDelay {
            orderBean?.let {
                MyUtils.getInstans().CallPhone(this, it.data.phone)
            }
        }
 
    }
 
    //显示确认送达弹窗
    private fun showConfirmDeliveryDialog(url: String) {
        orderBean?.let {
            it.data.orderState = 6
            val dialog = DialogUtil.getDialog(this, R.layout.dialog_show_cargo_money)
            val viewContent = DialogUtil.getView(dialog)
            val view_hint_close_car = viewContent.find<View>(R.id.view_hint_close_car)
            val tv_car_num_close = viewContent.find<View>(R.id.tv_car_num_close)
            val tv_money = viewContent.find<TextView>(R.id.tv_money)
            val tv_sure_car_num = viewContent.find<TextView>(R.id.tv_sure_car_num)
            val et_num_zg = viewContent.find<EditText>(R.id.et_num_zg)
            view_hint_close_car.setOnClickListener {
                dialog.dismiss()
            }
            tv_car_num_close.setOnClickListener {
                dialog.dismiss()
            }
            if (it.data.driverPay == "1") {
                tv_money.visible()
                tv_money.text =
                    getString(R.string.确认现金金额).format(if (orderType == "4") it.data.expressPayAllMoney else if (it.data.payMoney == 0.0) it.data.orderMoney else it.data.payMoney)
                        .replace(",", ".").extClearInvalidZero()
            }
            tv_sure_car_num.setOnClickListener {
                val num = et_num_zg.getContent()
                if (num.isEmpty()) {
                    toast(getString(R.string.请输入验证码))
                    return@setOnClickListener
                }
                confirmDelivery(6, num, url, {
                    LogHelper.logRecord("确认快件已送达成功orderId:${orderId} orderType:${orderType}")
                    dialog.dismiss()
                    finish()
                }) { code, msg ->
                    LogHelper.logRecord("确认快件已送达失败orderId:${orderId} orderType:${orderType} code:${code} msg:${msg}")
                }
            }
        }
    }
 
    //显示确认价格弹窗
    private fun showConfirmPriceDialog(url: String) {
        orderBean?.let {
            DialogUtil.getDelAndSureDialog(
                this,
                getString(R.string.确认现金金额).format(it.data.expressPayAllMoney)
                    .replace(",", ".").extClearInvalidZero(),
                {},
                {
                    confirmPickUp(url, 5, {
                        LogHelper.logRecord("确认收到此订单费用成功orderId:${orderId} orderType:${orderType}")
                        it.data.orderState = 5
                        showStatueMapUI(it)
                        iv_to_gd.callOnClick()
                    }) { code, msg ->
                        LogHelper.logRecord("确认收到此订单费用失败orderId:${orderId} orderType:${orderType} code:${code} msg:${msg}")
                    }
                })
        }
    }
 
    fun startNavi(startLat: Double, startLon: Double, endLat: Double, endLon: Double) {
        AMapKit.startNavi(this, startLat, startLon, endLat, endLon)
    }
 
    private fun callseatmoney(num: String, click: () -> Unit, error: (code: Int, msg: String?) -> Unit) {
        val map = getMapByAny()
        map["orderId"] = orderId
        map["difference"] = doubleTwo(num.toDouble())
        callNet(Api.makeUpTheDifference, map, {
            click()
        }, error)
    }
 
    private fun callStatue(i: Int, num: String, click: () -> Unit, error: (code: Int, msg: String?) -> Unit) {
        val map = getMapByAny()
        map["orderId"] = orderId
        map["orderType"] = orderType
        map["state"] = i
        if (num.isNotEmpty())
            map["pickUpCode"] = num
        Log.e(
            "定位==============》",
            "${MyApplication.getLocation()?.latitude},${MyApplication.getLocation()?.longitude}"
        )
        map["lat"] = MyApplication.getLocation()?.latitude ?: 0.0
        map["lon"] = MyApplication.getLocation()?.longitude ?: 0.0
        callNet(Api.process, map, {
            click()
        }, error)
    }
 
    //确认送达
    private fun confirmDelivery(i: Int, num: String, imgTwo: String, click: () -> Unit, error: (code: Int, msg: String?) -> Unit) {
        val map = getMapByAny()
        map["orderId"] = orderId
        map["orderType"] = orderType
        map["state"] = i
        if (num.isNotEmpty())
            map["pickUpCode"] = num
        Log.e(
            "定位==============》",
            "${MyApplication.getLocation()?.latitude},${MyApplication.getLocation()?.longitude}"
        )
        map["imgTwo"] = imgTwo
        map["lat"] = MyApplication.getLocation()?.latitude ?: 0.0
        map["lon"] = MyApplication.getLocation()?.longitude ?: 0.0
        callNet(Api.process, map, {
            click()
        }, error)
    }
 
    private fun callStatue(phone: String, i: Int, click: () -> Unit, error: (code: Int, msg: String?) -> Unit) {
        val map = getMapByAny()
        map["orderId"] = orderId
        map["orderType"] = orderType
        map["state"] = i
//        map["phone"] = phone
        map["lat"] = MyApplication.getLocation()?.latitude ?: 0.0
        map["lon"] = MyApplication.getLocation()?.longitude ?: 0.0
        callNet(Api.process, map, {
            click()
        }, error)
    }
 
    //确认收获
    private fun confirmPickUp(imgOne: String, i: Int, click: () -> Unit, error: (code: Int, msg: String?) -> Unit) {
        val map = getMapByAny()
        map["orderId"] = orderId
        map["orderType"] = orderType
        map["imgOne"] = imgOne
        map["state"] = i
        map["lat"] = MyApplication.getLocation()?.latitude ?: 0.0
        map["lon"] = MyApplication.getLocation()?.longitude ?: 0.0
        callNet(Api.process, map, {
            click()
        }, error)
    }
 
    private var movedByFinger = false
 
    /***
     * 定位时司机与线路的改变
     */
    var lastMoveByGesture = 0L //上次手势操作的时间
    private fun initMineLocation() {
        aMap.setOnMyLocationChangeListener { it ->
            it?.let {
                MyApplication.setLocation(it.latitude, it.longitude)
            }
            orderBean?.let {
                when (it.data.orderState) {
                    2, 3 -> {
                        changeCarThree(false)
                    }
 
                    4 -> {
                        //getTripInfo(4,false)
                        changeCarThree(false)
                    }
 
                    5, 6 -> {
                        changeCarFive(false)
                    }
                }
            }
        }
    }
 
    private fun changeCarThree(isMove: Boolean) {
        if (makeMarker != null && carMarker != null) {
            //getTripInfo(3,isMove)
            AMapKit.initRouteLine(
                this,
                makeMarker!!.position,
                carMarker!!.position,
                object : (MutableList<LatLng>, Float, Long) -> Unit {
                    override fun invoke(p1: MutableList<LatLng>, p2: Float, p3: Long) {
                        if (isFinishing || isDestroyed) {
                            return
                        }
                        val tance = AMapKit.getTance(this@SmallCargoActivity, p2)
                        val content = getString(R.string.距取货点, tance)
                        val sp = SpanBuilder.content(content)
                            .colorSpan(
                                this@SmallCargoActivity,
                                content.indexOf(tance),
                                content.indexOf(tance) + tance.length,
                                R.color.color_5DB03D
                            ).build()
                        val view = creatCarView(sp)
                        carMarker!!.setIcon(getIcon(view))
                        addline(p1)
                        if (isMove)
                            moveCamera(aMap, makeMarker!!.position, carMarker!!.position)
                    }
                })
        }
    }
 
    private fun changeCarFive(isMove: Boolean) {
        if (carMarker != null && endMarker != null) {
            //getTripInfo(5,isMove)
            AMapKit.initRouteLine(
                this,
                carMarker!!.position,
                endMarker!!.position,
                object : (MutableList<LatLng>, Float, Long) -> Unit {
                    override fun invoke(p1: MutableList<LatLng>, p2: Float, p3: Long) {
                        if (isFinishing || isDestroyed) {
                            return
                        }
                        addline(p1)
 
                        val tance = AMapKit.getTance(this@SmallCargoActivity, p2)
                        val time = p3.toString()
                        val content = getString(R.string.预计还需, tance, time)
                        val sp = SpanBuilder.content(content).colorSpan(
                            this@SmallCargoActivity,
                            content.indexOf(tance),
                            content.indexOf(tance) + tance.length,
                            R.color.color_5DB03D
                        )
                            .colorSpan(
                                this@SmallCargoActivity,
                                content.indexOf(time),
                                content.indexOf(time) + time.length,
                                R.color.color_5DB03D
                            ).build()
                        val view = creatCarView(sp)
                        carMarker!!.setIcon(getIcon(view))
                        if (isMove) {
                            moveCamera(aMap, carMarker!!.position, endMarker!!.position)
                        }
                    }
                })
        }
    }
 
    private fun callOrder(reNavi: Boolean = false, function: () -> Unit) {
        MyApplication.currentOrderId = orderId
        MyApplication.currentOrderType = orderType
        val map = getMapByAny()
        map["orderId"] = orderId
        map["orderType"] = orderType
        callNet(Api.queryOrderInfo, map) { it ->
            gson.fromJson(it, OrderBean::class.java)?.also {
                orderBean = it
                showUi(it)
                function()
                if (reNavi) {
                    EventBus.getDefault().post(
                        BaseEvent(
                            BaseEvent.RELOAD_NAVI,
                            it.data?.endLat?.toString(),
                            it.data?.endLon?.toString()
                        )
                    )
                }
            }
        }
    }
 
    private fun showUi(orderBean: OrderBean?) {
        orderBean?.let {
            if (it.data.isFrozen == 2) {
                toast(getString(R.string.已冻结))
                finish()
            }
            tv_name.text = orderBean.data.userName
//            tv_name.text = orderBean.data.nickName
            tv_car_num.text = getString(R.string.次寄物, it.data.historyNum.toString())
            tv_time.text = it.data.travelTime_
            tv_start_address.text = it.data.startAddress
            tv_end_address.text = it.data.endAddress
            showStatueMapUI(it)
//            var cargoStrType = if (orderBean.data.cargoType == "1") "普通小件X" else "贵重物品X"
//            tv_tv_num_cargo.text = cargoStrType + orderBean.data.peopleNumber
            tv_people_cargo.text = orderBean.data.nickName + "-" + orderBean.data.phone
            tv_remark.text = orderBean.data.remark
            tv_money_cargo.gone()
//            if (orderBean.data.tipMoney != null && orderBean.data.tipMoney > 0.0) {
//                tv_money_cargo.text = doubleTwo(orderBean.data.tipMoney) + "元加急费"
//            } else {
//                tv_money_cargo.gone()
//            }
            if (!it.data.differenceMoney.isNullOrEmpty() && it.data.differenceMoney.toDouble() > 0) {
                btn_gap_money.text =
                    getString(R.string.差价点击修改).format(it.data.differenceMoney.extClearInvalidZero())
                btn_gap_money.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, 0, 0)
            }
            val latLngList = mutableListOf<LatLng>()
            for (item in orderBean.data.orderPositionList) {
                latLngList.add(LatLng(item.lat, item.lon))
            }
            graylist.clear()
            graylist.addAll(latLngList)
            grayline = AMapKit.drawLine(this, aMap, graylist, R.color.gray)
        }
    }
 
    private fun getAllMark(it: OrderBean) {
        if (makeMarker == null) {
            val view = createView(R.layout.item_map_market_make, this)
            view.find<ImageView>(R.id.iv_img).setImageResource(UiUtil.getLangResereveBitmap())
            makeMarker = addMarker(aMap, it.data.startLat, it.data.startLon, view, "")
        }
        makeMarker!!.isVisible = false
        if (startMarker == null) {
            val view = createView(R.layout.item_map_market, this)
            view.find<ImageView>(R.id.iv_img).setImageResource(UiUtil.getLangStartBitmap())
            startMarker = addMarker(aMap, it.data.startLat, it.data.startLon, view, "")
        }
        startMarker!!.isVisible = false
        if (endMarker == null) {
            val view = createView(R.layout.item_map_market, this)
            view.find<ImageView>(R.id.iv_img).setImageResource(UiUtil.getLangEndBitmap())
            endMarker = addMarker(aMap, it.data.endLat, it.data.endLon, view, "")
        } else { //可能修改终点
            endMarker?.position = LatLng(it.data.endLat, it.data.endLon)
        }
        endMarker!!.isVisible = false
        if (carMarker == null) {
            val carView = creatCarView("")
            carMarker = addMarker(
                aMap,
                MyApplication.getLocation()?.latitude ?: 0.0,
                MyApplication.getLocation()?.longitude ?: 0.0,
                carView,
                ""
            )
        }
        carMarker!!.isVisible = false
        if (markerLine != null) {
            markerLine!!.isVisible = false
        }
    }
 
    fun creatCarView(content: CharSequence): View {
        val carView = createView(R.layout.item_map_market, this)
        carView.find<ImageView>(R.id.iv_img).setImageResource(R.mipmap.car_trip)
        val tv_hint = carView.find<TextView>(R.id.tv_hint)
        tv_hint.visible()
        tv_hint.text = content
        return carView
    }
 
    fun creatCarViewHint(content: CharSequence): View {
        val carView = createView(R.layout.item_map_market, this)
        carView.find<ImageView>(R.id.iv_img).setImageResource(R.mipmap.car_trip)
        val tv_hint = carView.find<TextView>(R.id.tv_hint)
        tv_hint.visible()
        tv_hint.text = content
        return carView
    }
 
    //    流程操作状态(3=开始出发预约点,4=到达预约点,5=开始服务,6=服务结束)
    private fun showStatueMapUI(it: OrderBean) {
        getAllMark(it)
        tv_gap_money.gone()
        btn_gap_money.gone()
        if (it.data.orderState == 4 || it.data.orderState == 8 || it.data.orderState == 13) {
            tv_gap_money.visible()
            btn_gap_money.visible()
        }
        tv_Right.visible()
        when (it.data.orderState) {
            2, 3 -> {
                if (it.data.reassign == 1) {
                    tv_Right.gone()
                }
                iv_to_gd.visible()
                makeMarker?.isVisible = true
                carMarker?.isVisible = true
                changeCarThree(true)
                closeTimeStart()
                if (it.data.orderState == 2) {
                    slide_btn.changeButtonText(getString(R.string.出发前往取货地点))
                    setTitleText(getString(R.string.去领取货物))
                } else {
                    slide_btn.changeButtonText(getString(R.string.到达取件地点))
                    setTitleText(getString(R.string.去领取货物))
                }
            }
//            4, 8, 12 -> {
            4, 12 -> {
                iv_to_gd.gone()
                if (it.data.reassign == 1) {
                    tv_Right.gone()
                }
                closeLine()
                carMarker?.isVisible = true
                startTimeStart(
                    ((System.currentTimeMillis() - DateUtil.dateToStamp(
                        DateUtil.TYPE0,
                        it.data.waitTime
                    )) / 1000).toInt()
                )
                moveCamera(aMap, carMarker!!.position)
                setTitleText(getString(R.string.等待取货))
                slide_btn.changeButtonText(getString(R.string.确认收货))
            }
 
            5, 6 -> {
                closeTimeStart()
                iv_to_gd.visible()
                tv_Right.gone()
                startMarker?.isVisible = true
                endMarker?.isVisible = true
                carMarker?.isVisible = true
                changeCarFive(true)
                setTitleText(getString(R.string.送货中))
                slide_btn.changeButtonText(getString(R.string.确认送达))
            }
        }
    }
 
    private fun startTimeStart(waitTime: Int) {
        if (!isTimeStart) {
            isTimeStart = true
            refreshTime(waitTime) {
                val timeType = getTimeType(it)
                val content = getString(R.string.您已等待, timeType)
                val build = SpanBuilder.content(content).colorSpan(
                    this,
                    content.indexOf(timeType),
                    content.indexOf(timeType) + timeType.length,
                    R.color.main_login_orange
                ).build()
                val view = creatCarViewHint(build)
                carMarker!!.setIcon(getIcon(view))
            }
        }
    }
 
    private var timeDisposable: Disposable? = null
 
    private fun refreshTime(waitTime: Int, click: (num: Int) -> Unit) {
        if (null != timeDisposable && !timeDisposable!!.isDisposed) {
            timeDisposable!!.dispose()
        }
        timeDisposable = Observable
            .interval(1, TimeUnit.SECONDS)
            .take(100000000)
            .subscribeOn(Schedulers.computation())
            .observeOn(AndroidSchedulers.mainThread())
            .compose(bindUntilEvent(ActivityEvent.DESTROY))
            .subscribe { aLong ->
                val num = waitTime + aLong
                click(num.toInt())
            }
    }
 
    private fun closeTimeStart() {
        if (timeDisposable != null) {
            timeDisposable!!.dispose()
        }
    }
 
    override fun onDestroy() {
        super.onDestroy()
        MyApplication.removeOrderView(listener)
        if (handler != null) {
            handler?.removeCallbacksAndMessages(null)
        }
        closeTimeStart()
    }
 
 
    private fun addline(allLine: MutableList<LatLng>) {
        closeLine()
        markerLine = AMapKit.drawLine(this, aMap, allLine)
    }
 
    fun closeLine() {
        if (markerLine != null) {
            markerLine!!.remove()
        }
    }
 
    override fun setOnclick() {
 
    }
 
    @SuppressLint("MissingPermission")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val fragment = supportFragmentManager.findFragmentById(R.id.map_view) as SupportMapFragment
        initMap(fragment) {
            mapInitStatus = true
            aMap = it
            aMap.isMyLocationEnabled = true
            initView2()
            it.setOnCameraMoveStartedListener {
                movedByFinger = it == GoogleMap.OnCameraMoveStartedListener.REASON_GESTURE
            }
            it.setOnCameraMoveListener {
                if (movedByFinger)
                    lastMoveByGesture = System.currentTimeMillis()
            }
            it.setOnCameraIdleListener {
                movedByFinger = false
            }
        }
    }
 
    override fun onEventMainThread(event: BaseEvent?) {
        super.onEventMainThread(event)
        when (event!!.code) {
            BaseEvent.REAGASSIGN_SUCCESS -> {
                MyApplication.currentOrderId = ""
                MyApplication.currentOrderType = ""
                finish()
            }
 
            BaseEvent.BACK_CAR_OVER -> {
                finish()
            }
 
            BaseEvent.DIFF_PRICE_AGREE -> {
                if (mapInitStatus) initView2()
            }
 
            BaseEvent.DIFF_PRICE_REFUSE -> {
                btn_gap_money.text = getString(R.string.补差价)
                btn_gap_money.setCompoundDrawablesRelativeWithIntrinsicBounds(
                    R.mipmap.ic_add,
                    0,
                    0,
                    0
                )
            }
 
            BaseEvent.MODIFY_ADDRESS_SUC -> {
                callOrder(true) {}
            }
        }
    }
}