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
package com.fuban.user.ui.crosscity
 
import android.app.Activity
import android.content.Intent
import android.view.View
import androidx.core.os.bundleOf
import cn.sinata.xldutils.utils.*
import com.fuban.user.FBApplication
import com.fuban.user.R
import com.fuban.user.interfaces.StringCallback
import com.fuban.user.network.HttpManager
import com.fuban.user.network.entity.DriverInfo
import com.fuban.user.network.entity.Line
import com.fuban.user.network.entity.Shift
import com.fuban.user.network.request
import com.fuban.user.ui.TransparentStatusBarActivity
import com.fuban.user.dialog.H5Dialog
import com.fuban.user.dialog.PassengerDialog
import com.fuban.user.dialog.SeatDialog
import com.fuban.user.utils.Const
import kotlinx.android.synthetic.main.activity_target_cross_order.*
import org.jetbrains.anko.sdk27.coroutines.onClick
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.startActivityForResult
 
 
class TargetCrossOrderActivity : TransparentStatusBarActivity() {
    override fun setContentView() = R.layout.activity_target_cross_order
 
    private val driver by lazy {
        intent.getParcelableExtra<DriverInfo>("driver")
    }
    private var startId:Int? = null //出发站点
    private var endId:Int? = null //到达站点
    private var startCity = ""
    private var endCity = ""
    private var line :Line? = null
    private var shift :Shift? = null
 
    private var passengerCount = 0//乘客数
    private var passengers = ""  //乘车人ids
    private var startLat = 0.0
    private var startLon = 0.0
    private var endLat = 0.0
    private var endLon = 0.0
    private var distance = 0.0 //预估距离,通过价格接口获取
 
    private val remarks by lazy {
        arrayOf(remark_1,remark_2,remark_3,remark_4)
    }
 
    override fun initClick() {
        tv_start_city.onClick {
            startActivityForResult<ChooseCityActivity>(Const.RequestCode.START_SITE)
        }
        tv_end_city.onClick {
            if (startId != null)
                startActivityForResult<ChooseCityActivity>(Const.RequestCode.END_SITE,"startId" to startId)
            else
                myToast("请先选择起点")
        }
        tv_shift.onClick {
            if (line != null)
                startActivityForResult<ShiftActivity>(Const.RequestCode.CROSS_SHIFT,"data" to line, "driverId" to driver.id)
            else
                myToast("请先选择线路")
        }
        rg_whole.setOnCheckedChangeListener { group, checkedId ->
            gp_seat.visibility = if (checkedId == R.id.rb_whole) View.GONE else View.VISIBLE
            getPrice()
        }
        tv_start.onClick {
            getAnchors(true)
        }
 
        tv_end.onClick {
            getAnchors(false)
        }
        tv_passenger_num.onClick {
            if (shift!=null){
                val passengerCountDialog = PassengerDialog()
                passengerCountDialog.arguments = bundleOf("max" to shift?.laveSeat)
                passengerCountDialog.setCallback(object : PassengerDialog.Callback{
                    override fun onOk(num: Int,ids:String) {
                        passengers = ids
                        if (num!=passengerCount){ //人数发生变换,重新选座,算价
                            tv_seat.text = ""
                            passengerCount = num
                            tv_passenger_num.text = "${num}人"
                            getPrice()
                        }
                    }
                })
                passengerCountDialog.show(supportFragmentManager,"num")
            }else
                myToast("请先选择班次")
        }
        tv_seat.onClick {
            getSeatInfo()
        }
        tv_action.onClick{
            if (!checkLogin())
                return@onClick
            if (line == null){
                myToast("请选择线路")
                return@onClick
            }
            if (shift == null){
                myToast("请选择班次")
                return@onClick
            }
            if (startLat == 0.0){
                myToast("请选择上车点")
                return@onClick
            }
            if (endLat == 0.0){
                myToast("请选择下车点")
                return@onClick
            }
            if (rb_pin.isChecked&&tv_seat.text.isEmpty()){
                myToast("请选择座位")
                return@onClick
            }
            tv_action.isEnabled = false
            HttpManager.getH5(11).request(this@TargetCrossOrderActivity,success = {_,data->
                tv_action.isEnabled = true
                data?.apply {
                    val h5Dialog = H5Dialog()
                    h5Dialog.arguments = bundleOf("title" to "《直通车乘车须知》","content" to optString("content"))
                    h5Dialog.setCallback(object :StringCallback{
                        override fun onRlt(rlt: String) {
                            makeOrder()
                        }
                    })
                    h5Dialog.show(supportFragmentManager,"h5")
                }
            }){_,_->
                tv_action.isEnabled = true
            }
        }
    }
 
    override fun initView() {
        title = "确认班次"
        driver.run {
            iv_avatar.setImageURI(avatar)
            tv_name.text = "$name $licensePlate"
            tv_car_info.text = "$brand·$carColor"
            tv_score.text = String.format("%.1f", fraction)
        }
    }
 
    /**
     * 查询线路
     */
    private fun queryLine(){
        showDialog()
        HttpManager.queryLines(startId!!,endId!!).request(this,success = { _, data->
            data?.apply {
                when(size){
                    0-> {
                        myToast("没有可用线路,请重新选择")
                        endId = null
                        tv_end_city.text = ""
                    }
                    1-> line = this[0]
                    else->{
                        startActivityForResult<ChooseLineActivity>(Const.RequestCode.CROSS_LINE,"data" to this,"isScan" to true)
                    }
                }
            }
        }){_,_->
            endId = null
            tv_end_city.text = ""
        }
    }
 
    /**
     * 查询有效范围
     */
    private fun getAnchors(isStart:Boolean){
        if (shift!=null){
            showDialog()
            HttpManager.queryLocation(if (isStart) startId!! else endId!!).request(this){_,data->
                startActivityForResult<ChooseMapPositionActivity>(if (isStart) Const.RequestCode.START_ADDRESS else Const.RequestCode.END_ADDRESS,
                    "isStart" to isStart,"city" to if (isStart) startCity else endCity,"siteId" to if (isStart) startId else endId,
                    "data" to data)
            }
        }else{
            myToast("请先选择班次")
        }
    }
 
    private fun getSeatInfo(){
        if (shift != null){
            showDialog()
            HttpManager.querySeat(shift!!.lineShiftDriverId).request(this){_,data->
                data?.apply {
                    val seatDialog = SeatDialog()
                    seatDialog.arguments = bundleOf("max" to totalNumber,"seat" to content,"count" to passengerCount)
                    seatDialog.setCallback(object : StringCallback {
                        override fun onRlt(rlt: String) {
                            tv_seat.text = rlt
                            getPrice()
                        }
                    })
                    seatDialog.show(supportFragmentManager,"seat")
                }
            }
        }else
            myToast("请先选择班次")
    }
 
    /**
     * 计算预估价格
     */
    private fun getPrice(){
        if (line!=null&&shift!=null&&startLat!=0.0&&endLat!=0.0&&(tv_seat.text.isNotEmpty()||rb_whole.isChecked))
            HttpManager.queryOrderMoney(line!!.id,"$startLon,$startLat","$endLon,$endLat",passengerCount,tv_seat.text.toString(),
                if (rb_whole.isChecked) 2 else 1,shift!!.serverCarModelId,shift!!.totalSeat).request(this){_,data->
                data?.apply {
                    tv_money.text = SpanBuilder(String.format("预估金额:¥%.2f",optDouble("price"))).color(this@TargetCrossOrderActivity,0,5,R.color.black)
                        .size(0,5,13).build()
                    distance = optDouble("distance")
                }
            }
        else
            tv_money.text = SpanBuilder("预估金额:¥--").color(this,0,5,R.color.black)
                .size(0,5,13).build()
    }
 
    private fun makeOrder() {
        showDialog()
        HttpManager.orderCrossCity(shift!!.carId,
            line!!.id,
            shift!!.lineShiftDriverId,
            shift!!.driverId,
            distance,
            2,
            startLat,
            startLon,
            endLat,
            endLon,
            tv_shift.text.toString() + ":00",
            FBApplication.lat,
            FBApplication.lon,
            tv_start.text.toString(),
            tv_end.text.toString(),
            FBApplication.address,
            remarks.filter { it.isChecked }.joinToString(",") { it.text },
            passengerCount,
            tv_seat.text.toString(),passengers,
            if (rb_whole.isChecked) 2 else 1,
            shift!!.serverCarModelId,
            shift!!.totalSeat
        ).request(this){ _, data ->
//            startActivity<MainActivity>()
            setResult(Activity.RESULT_OK)
            startActivity<PayCrossActivity>("id" to data?.optInt("id"))
            finish()
        }
    }
 
 
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK&&data!=null){
            when (requestCode) {
                Const.RequestCode.START_SITE -> {
                    startId = data.getIntExtra("id",0)
                    startCity = data.getStringExtra("city")?:""
                    tv_start_city.text = data.getStringExtra("name")
                    endId = null
                    tv_end.text = ""
                    line = null
                    shift = null
                    tv_shift.text = ""
                    tv_start.text = ""
                    tv_end.text = ""
                    tv_seat.text = ""
                    tv_count.text = ""
                    startLat = 0.0
                    endLat = 0.0
                }
                Const.RequestCode.END_SITE -> {
                    endId = data.getIntExtra("id",0)
                    endCity = data.getStringExtra("city")?:""
                    tv_end_city.text = data.getStringExtra("name")
                    line = null
                    shift = null
                    tv_shift.text = ""
                    tv_start.text = ""
                    tv_end.text = ""
                    tv_seat.text = ""
                    tv_count.text = ""
                    startLat = 0.0
                    endLat = 0.0
                    queryLine()
                }
                Const.RequestCode.CROSS_LINE -> {
                    line = data.getParcelableExtra("line")
                    shift = null
                    tv_shift.text = ""
                    tv_start.text = ""
                    tv_end.text = ""
                    tv_seat.text = ""
                    tv_count.text = ""
                    startLat = 0.0
                    endLat = 0.0
                }
                Const.RequestCode.CROSS_SHIFT -> {
                    shift = data.getParcelableExtra("shift")
                    tv_shift.text = data.getLongExtra("time",0L).toTime("yyyy-MM-dd ")+ shift?.startTime
                    tv_count.text = "可乘人数${shift?.number}"
                    tv_seat.text = ""
                }
                Const.RequestCode.START_ADDRESS -> {
                    tv_start.text = data.getStringExtra("name")
                    startLat = data.getDoubleExtra("lat",0.0)
                    startLon = data.getDoubleExtra("lon",0.0)
                }
                Const.RequestCode.END_ADDRESS -> {
                    tv_end.text = data.getStringExtra("name")
                    endLat = data.getDoubleExtra("lat",0.0)
                    endLon = data.getDoubleExtra("lon",0.0)
                }
            }
            getPrice()
        }
    }
}