罗明文
2024-06-19 481723ce3c05d74fec53b8567b9c79d77bdcc155
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
package com.dollearn.student.ui.worldcup
 
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.core.os.bundleOf
import androidx.recyclerview.widget.LinearLayoutManager
import cn.sinata.xldutils.activity.BaseActivity
import cn.sinata.xldutils.gone
import cn.sinata.xldutils.utils.SpanBuilder
import cn.sinata.xldutils.utils.myToast
import cn.sinata.xldutils.utils.showAllowingStateLoss
import cn.sinata.xldutils.visible
import com.github.zackratos.ultimatebar.UltimateBar
import com.google.gson.Gson
import com.dollearn.student.R
import com.dollearn.student.dialog.*
import com.dollearn.student.network.HttpManager
import com.dollearn.student.network.entity.Banner
import com.dollearn.student.network.entity.Student
import com.dollearn.student.network.entity.WorldCupBean
import com.dollearn.student.network.request
import com.dollearn.student.ui.H5Activity
import com.dollearn.student.ui.home.PayResultActivity
import com.dollearn.student.ui.home.adapter.HomeBannerAdapter
import com.dollearn.student.ui.home.adapter.StudentAdapter
import com.dollearn.student.utils.Const
import com.dollearn.student.utils.event.EmptyEvent
import com.dollearn.student.utils.extention.clickDelay
import com.dollearn.student.utils.interfaces.StringCallback
import com.dollearn.student.utils.pay.PayListener
import com.dollearn.student.utils.pay.PayUtil
import kotlinx.android.synthetic.main.activity_join_world_cup.*
import org.greenrobot.eventbus.EventBus
import org.jetbrains.anko.startActivityForResult
import org.jetbrains.anko.textColorResource
 
class JoinWCActivity : BaseActivity(), PayListener {
 
    private val match by lazy { intent.getParcelableExtra<WorldCupBean>("match") as WorldCupBean }
    private val bannerImg = arrayListOf<Banner>()
    private val bannerAdapter by lazy {
        HomeBannerAdapter(bannerImg, this)
    }
 
    private val students = arrayListOf<Student>()
    private val studentAdapter = StudentAdapter(students, StudentAdapter.TYPE_MATCH_DETAIL)
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_join_world_cup)
        initView()
        initClick()
    }
 
    fun initClick() {
        tv_add_student.clickDelay {
            ChooseStudentDialog.show(supportFragmentManager,
                ChooseStudentDialog.TYPE_WORLD,students,object : ChooseStudentDialog.ResultCallback{
                override fun checked(list: List<Student>) {
                    students.clear()
                    students.addAll(list)
                    studentAdapter.notifyDataSetChanged()
                }
            },null,true,match.gender,match.age)
        }
        tv_action.setOnClickListener {
            if (cb_rule.isChecked){
                if (students.isEmpty())
                    myToast("请添加参赛人员")
                else if (match.storeInfos?.size == 1){
                    match.storeInfos?.firstOrNull()?.let {
                        ConfirmShopDialog.show(supportFragmentManager,it.storeCoverDrawing?:"",it.name?:"",it.address?:"",
                            if (it.distance == null) "" else "${it.distance}km",object :StringCallback{
                                override fun onResult(rst: String) {
                                    if (tv_price.text == "免费"){
                                        pay(3)
                                    }else
                                        PayDialog.show(supportFragmentManager,false,object :StringCallback{
                                            override fun onResult(rst: String) {
                                                pay(rst.toInt())
                                            }
                                        },students.size*((match.cash?:"0.0").toDouble()),students.size*((match.paiCoin?:"0.0").toDouble().toInt()),students.size*((match.classHour?:"0.0").toDouble().toInt()),null,students.filter { it.isStudent == 0 }.isNullOrEmpty())
                                }
                            },ConfirmShopDialog.TYPE_WORLD)
                    }
                }else{
                    ConfirmShopsDialog.show(supportFragmentManager,match.storeInfos?.joinToString("\n") { it.name }?:"",object :StringCallback{
                        override fun onResult(rst: String) {
                            if (tv_price.text == "免费"){
                                pay(3)
                            }else
                                PayDialog.show(supportFragmentManager,false,object :StringCallback{
                                    override fun onResult(rst: String) {
                                        pay(rst.toInt())
                                    }
                                },students.size*((match.cash?:"0.0").toDouble()),students.size*((match.paiCoin?:"0.0").toDouble().toInt()),students.size*((match.classHour?:"0.0").toDouble().toInt()),null,students.filter { it.isStudent == 0 }.isNullOrEmpty())
                        }
                    })
                }
            }else
                myToast("请阅读并同意《社区世界杯报名协议》")
        }
        tv_rule.setOnClickListener {
            HttpManager.getH5(5).request(this@JoinWCActivity){ _, data->
                data?.let {
                    startActivityForResult<H5Activity>(1,"title" to "社区世界杯报名协议","url" to it)
                }
            }
        }
    }
 
    fun initView() {
        titleBar.titleView.text = title
        titleBar.titleView.textColorResource = R.color.white
        titleBar.leftView.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.back_white,0,0,0)
        UltimateBar.with(this)
            .statusDark(false)
            .create().transparentBar() //沉浸状态栏
        banner.adapter = bannerAdapter
        rv_student.layoutManager = LinearLayoutManager(this)
        rv_student.adapter = studentAdapter
        studentAdapter.delCallback = object : StringCallback {
            override fun onResult(rst: String) {
                students.removeAt(rst.toInt())
                studentAdapter.notifyDataSetChanged()
            }
        }
        match.apply {
            if (registrationClosingTime.isNullOrEmpty())
                registrationClosingTime = endTime
            storeInfos?.let {
                val stores = StringBuffer()
                for (s in it.indices) {
                    if (s == it.size - 1) stores.append(it[s].name)
                    else stores.append(it[s].name + ",")
                }
                tv_shops.text = stores
                if (stores.length > 20) {
                    mtvcheckmore.visible()
                    mtvcheckmore.clickDelay {
                        val checkShopsDialog = CheckShopsDialog()
                        checkShopsDialog.arguments =
                            bundleOf("shopinfo" to stores.toString(),"type" to CheckShopsDialog.TYPE_WORLD_SITE)
                        checkShopsDialog.showAllowingStateLoss(
                            supportFragmentManager,
                            "checkshops"
                        )
                    }
                } else {
                    mtvcheckmore.gone()
                }
            }
 
            val list = infoImg.split(",").map {
                Banner(url = it)
            }
            bannerImg.addAll(list)
            bannerAdapter.notifyDataSetChanged()
            tv_name.text = name
            tv_hot.text = heat.toString()
            tv_deadline.text = registrationClosingTime
            tv_start_time.text = startTime
            tv_end_time.text = endTime
            tv_age.text = "${age}岁"
            tv_sex.text = getJoinSex()
            tv_city.text = "${province}|${city}"
            tv_play_address.text = address
            if (cash!=null&&cash.toDouble()!=0.0){
                val price = "支付:¥%s/人".format(cash)
                tv_price.text = SpanBuilder(price).color(this@JoinWCActivity,0,3,R.color.textColor66).bold(3,price.length).build()
                tv_price.visible()
            }
            if (paiCoin!=null&&paiCoin.toDouble()!=0.0){
                val coin = "玩湃币:%s币/人".format(paiCoin)
                tv_coin.text = SpanBuilder(coin).color(this@JoinWCActivity,0,4,R.color.textColor66).bold(4,coin.length).build()
                tv_coin.visible()
            }
            if (classHour!=null&&classHour.toDouble()!=0.0){
                val time = "课时:%s课时/人".format(classHour)
                tv_course_time.text = SpanBuilder(time).color(this@JoinWCActivity,0,3,R.color.textColor66).bold(3,time.length).build()
                tv_course_time.visible()
            }
            if (tv_price.visibility == View.GONE&&tv_coin.visibility == View.GONE&&tv_course_time.visibility == View.GONE){ //免费课
                tv_price.visible()
                tv_price.text = "免费"
            }
        }
        PayUtil.addPayListener(this)
    }
 
    private fun pay(payType:Int,coursePaymentId:String? = null){
//        if (payType == 4&&coursePaymentId == null){//课时支付  //todo 蒲志兵:课时支付暂时由他判断
//            HttpManager.paymentCompetitionCourseList(match.id).request(this){_,data->
//                val list = arrayListOf<PayCourse>()
//                list.addAll(data?: arrayListOf())
//                if (list.isEmpty())
//                    myToast("暂无可用课时")
//                else
//                    CoursePayDialog.show(supportFragmentManager,object :StringCallback{
//                        override fun onResult(rst: String) {
//                            pay(payType, rst)
//                        }
//                    },students.size*((match.classPrice?:"0").toInt()), list)
//            }
//        }else
 
        HttpManager.paymentWorldCup(match.id, Gson().toJson(students),payType).request(this,false,success = {_,data->
            if (payType == 2){
                PayUtil.aliPay(this,data?.orderInfo?:"")
            }else if (payType == 3||payType == 4){
                EventBus.getDefault().post(EmptyEvent(Const.EventCode.REFRESH_USER)) //玩湃币支付后刷新首页
                onPaySuccess()
            }else{
                PayUtil.initWeChatPay(this, Const.WX_APP_ID)
                PayUtil.weChatPay(data!!)
            }
        }){_,msg->
            startActivityForResult<PayResultActivity>(1,"type" to PayResultActivity.TYPE_WORLD_FAILED,"msg" to msg)
        }
    }
 
    override fun onDestroy() {
        super.onDestroy()
        PayUtil.removePayListener(this)
        PayUtil.unregisterApp()
    }
 
    override fun onPaySuccess() {
        startActivityForResult<PayResultActivity>(1,"type" to PayResultActivity.TYPE_WORLD_SUC)
        finish()
    }
 
    override fun onPayCancel() {
    }
 
    override fun onPayError(msg: String) {
        startActivityForResult<PayResultActivity>(1,"type" to PayResultActivity.TYPE_WORLD_FAILED)
    }
 
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK&&requestCode == 1){
            tv_action.postDelayed({
                if (tv_price.text == "免费"){
                    pay(3)
                }else
                    PayDialog.show(supportFragmentManager,false,object :StringCallback{
                        override fun onResult(rst: String) {
                            pay(rst.toInt())
                        }
                    },students.size*((match.cash?:"0.0").toDouble()),students.size*((match.paiCoin?:"0.0").toDouble().toInt()),students.size*((match.classHour?:"0.0").toDouble().toInt()),null,students.filter { it.isStudent == 0 }.isNullOrEmpty())
            },500)
        }
    }
 
}