lmw
2024-06-16 03172fc2d9a7717f4a9d8de1c5eca3158a550b30
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
package com.dollearn.student.ui.home
 
import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.provider.Settings
import android.view.View
import cn.sinata.xldutils.activity.SelectPhotoDialog
import cn.sinata.xldutils.gone
import cn.sinata.xldutils.utils.isValidIdCard
import cn.sinata.xldutils.utils.isValidPhone
import cn.sinata.xldutils.utils.myToast
import com.dollearn.student.R
import com.dollearn.student.dialog.ChooseBirthDialog
import com.dollearn.student.dialog.SexDialog
import com.dollearn.student.network.HttpManager
import com.dollearn.student.network.entity.Student
import com.dollearn.student.network.request
import com.dollearn.student.ui.TransparentStatusBarActivity
import com.dollearn.student.utils.Const
import com.dollearn.student.utils.extention.clickDelay
import com.dollearn.student.utils.extention.uploadWithCompress
import com.dollearn.student.utils.interfaces.StringCallback
import kotlinx.android.synthetic.main.activity_add_student.*
import org.greenrobot.eventbus.EventBus
import org.jetbrains.anko.startActivityForResult
import java.lang.Exception
 
class AddStudentActivity:TransparentStatusBarActivity() {
    override fun setContentView() = R.layout.activity_add_student
 
    private val type by lazy { intent.getIntExtra("type", TYPE_COURSE) }
    private val user by lazy { intent.getParcelableExtra<Student>("user") }
    private var isRefused = false
 
    private var avatar = ""
 
    override fun initClick() {
        iv_avatar.clickDelay {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                if (Environment.isExternalStorageManager())
                    startActivityForResult<SelectPhotoDialog>(2)
                else if (!isRefused) {
                    try {
                        val intent = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION)
                        intent.data = Uri.parse("package:${packageName}")
                        startActivityForResult(intent, 1024)
                    } catch (e: Exception) {
                        val intent = Intent()
                        intent.action = Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION
                        startActivityForResult(intent, 1024)
                    }
 
                } else {
                    myToast("缺少文件权限")
                }
            } else
                startActivityForResult<SelectPhotoDialog>(1)
        }
        tv_birth.clickDelay {
            val chooseBirthDialog = ChooseBirthDialog()
            chooseBirthDialog.setCallback(object : ChooseBirthDialog.Callback {
                override fun onOk(s: String) {
                    tv_birth.text = s
                }
            })
            chooseBirthDialog.show(supportFragmentManager, "birth")
        }
        tv_sex.clickDelay {
            SexDialog.show(supportFragmentManager,object :StringCallback{
                override fun onResult(rst: String) {
                    tv_sex.text = rst
                }
            })
        }
        tv_action.clickDelay {
            if (iv_avatar.visibility == View.VISIBLE && avatar.isEmpty()){
                myToast("请上传头像")
                return@clickDelay
            }
            val name = et_name.text.toString()
            if (name.isNullOrEmpty()){
                myToast("请输入成员姓名")
                return@clickDelay
            }
            var birth = tv_birth.text.toString()
            if (birth.isNullOrEmpty()){
                myToast("请选择生日")
                return@clickDelay
            }
            val sex = tv_sex.text.toString()
            if (sex.isNullOrEmpty()){
                myToast("请选择性别")
                return@clickDelay
            }
            val height = et_height.text.toString()
            if (type!= TYPE_EDIT_MATCH&&type!= TYPE_EDIT_WORLD&&height.isNullOrEmpty()){
                myToast("请填写身高")
                return@clickDelay
            }
            var heightD = 0.0
            if (type!= TYPE_EDIT_MATCH&&type!= TYPE_EDIT_WORLD){
                try {
                    heightD = height.toDouble()
                }catch (e:Exception){
                    myToast("身高输入有误")
                    return@clickDelay
                }
            }
            val weight = et_weight.text.toString()
            if (type!= TYPE_EDIT_MATCH&&type!= TYPE_EDIT_WORLD&&weight.isNullOrEmpty()){
                myToast("请填写体重")
                return@clickDelay
            }
            var weightD = 0.0
            if (type!= TYPE_EDIT_MATCH&&type!= TYPE_EDIT_WORLD){
                try {
                    weightD = weight.toDouble()
                }catch (e:Exception){
                    myToast("体重输入有误")
                    return@clickDelay
                }
            }
            val idCard = et_id_card.text.toString()
            val phone = et_phone.text.toString()
            when(type){
                TYPE_ADD_MATCH ->{
                    if (phone.isNotEmpty()&&!phone.isValidPhone()){
                        myToast("请输入正确的手机号")
                        return@clickDelay
                    }
                    if (idCard.isValidIdCard())
                        birth = "${idCard.substring(6,10)}-${idCard.substring(10,12)}-${idCard.substring(12,14)}"
                    HttpManager.addParticipant(avatar,name,birth,if (sex == "男") 1 else 2,heightD,weightD,
                        if (idCard.isNullOrEmpty()) null else idCard,if (phone.isNullOrEmpty()) null else phone)
                        .request(this){_,data->
                            myToast("添加成功")
                            setResult(Activity.RESULT_OK)
                            finish()
                        }
                }
                TYPE_ADD_WORLD->{
                    if (!phone.isValidPhone()){
                        myToast("请输入正确的手机号")
                        return@clickDelay
                    }
                    if (!idCard.isValidIdCard()){
                        myToast("请输入正确的身份证号")
                        return@clickDelay
                    }
                    birth = "${idCard.substring(6,10)}-${idCard.substring(10,12)}-${idCard.substring(12,14)}"
                    HttpManager.addParticipant(avatar,name,birth,if (sex == "男") 1 else 2,heightD,weightD,
                        if (idCard.isNullOrEmpty()) null else idCard,if (phone.isNullOrEmpty()) null else phone)
                        .request(this){_,data->
                            myToast("添加成功")
                            setResult(Activity.RESULT_OK)
                            finish()
                        }
                }
                TYPE_EDIT_MATCH, TYPE_EDIT_WORLD->{
                    if (!idCard.isValidIdCard()){
                        myToast("请输入正确的身份证号")
                        return@clickDelay
                    }
                    birth = "${idCard.substring(6,10)}-${idCard.substring(10,12)}-${idCard.substring(12,14)}"
                    HttpManager.editParticipant(user?.avatar?:"",user?.id?:"",null,null,null,name,idCard,if (sex == "男") 1 else 2,birth)
                        .request(this){_,data->
                            myToast("修改成功")
                            setResult(Activity.RESULT_OK)
                            finish()
                        }
                }
                else->{
                    if (!idCard.isValidIdCard()){
                        myToast("请输入正确的身份证号")
                        return@clickDelay
                    }
                    birth = "${idCard.substring(6,10)}-${idCard.substring(10,12)}-${idCard.substring(12,14)}"
                    HttpManager.addStudent(name,birth,if (sex == "男") 1 else 2,avatar,heightD,weightD, if (idCard.isNullOrEmpty()) null else idCard,if (phone.isNullOrEmpty()) null else phone,if (type == TYPE_EDIT_STUDENT) user?.stuId else null).request(this){_,data->
                        myToast(if (type == TYPE_EDIT_STUDENT) "修改成功" else "添加成功")
                        EventBus.getDefault().post(Const.EventCode.CHANGE_COURSDATA)
                        setResult(Activity.RESULT_OK)
                        finish()
                    }
                }
 
            }
 
        }
    }
 
    override fun initView() {
        when (type) {
            TYPE_COURSE ->{
                tv_id_title.setCompoundDrawablesRelativeWithIntrinsicBounds(R.mipmap.ic_must,0,0,0)
            }
            TYPE_ADD_MATCH -> {
                title = "添加人员"
                tv_birth.setCompoundDrawablesRelativeWithIntrinsicBounds(0,0,R.mipmap.calender,0)
    //            tv_2.setCompoundDrawablesRelativeWithIntrinsicBounds(R.mipmap.ic_must,0,0,0)
            }
            TYPE_ADD_WORLD -> {
                title = "添加人员"
                tv_birth.setCompoundDrawablesRelativeWithIntrinsicBounds(0,0,R.mipmap.calender,0)
                tv_2.setCompoundDrawablesRelativeWithIntrinsicBounds(R.mipmap.ic_must,0,0,0)
                tv_id_title.setCompoundDrawablesRelativeWithIntrinsicBounds(R.mipmap.ic_must,0,0,0)
            }
            TYPE_EDIT_MATCH -> {
                title = "选择人员"
                iv_avatar.gone()
                cl_add.gone()
                tv_birth.setCompoundDrawablesRelativeWithIntrinsicBounds(0,0,R.mipmap.calender,0)
                et_name.setText(user?.name)
                tv_birth.text = user?.birthday
                tv_sex.text = if (user?.gender == 1) "男" else "女"
            }
            TYPE_EDIT_WORLD -> {
                title = "人员管理"
                iv_avatar.gone()
                cl_add.gone()
                tv_birth.setCompoundDrawablesRelativeWithIntrinsicBounds(0,0,R.mipmap.calender,0)
                tv_id_title.setCompoundDrawablesRelativeWithIntrinsicBounds(R.mipmap.ic_must,0,0,0)
                et_name.setText(user?.name)
                tv_birth.text = user?.birthday
                tv_sex.text = if (user?.gender == 1) "男" else "女"
                et_name.isEnabled = false
                tv_birth.isEnabled = false
                tv_sex.isEnabled = false
            }
            TYPE_EDIT_STUDENT -> {
                tv_id_title.setCompoundDrawablesRelativeWithIntrinsicBounds(R.mipmap.ic_must,0,0,0)
                title = "修改运动营成员信息"
                avatar = user?.stuHeadImg?:""
                iv_avatar.setImageURI(avatar)
                et_name.setText(user?.stuName)
                tv_birth.text = user?.birthday
                tv_sex.text = if (user?.sex == 1) "男" else "女"
                et_height.setText(user?.stuHeight.toString())
                et_weight.setText(user?.stuWeight.toString())
                et_phone.setText(user?.stuPhone)
                et_id_card.setText(user?.idCard)
            }
        }
    }
 
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK && requestCode == 1024 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.R){
            isRefused = !Environment.isExternalStorageManager()
        }else if (resultCode == Activity.RESULT_OK&&data!=null){
            data.getStringExtra("path")!!.uploadWithCompress(this, object :
                StringCallback {
                override fun onResult(rst: String) {
                    avatar = rst
                    iv_avatar.setImageURI(avatar)
                }
            })
        }
    }
 
    companion object{
        const val TYPE_COURSE = 1//添加运动营成员
        const val TYPE_ADD_MATCH = 2//添加参赛人员  //1.23:人员管理也是赛事人员同流程和判断条件,因此复用type
        const val TYPE_ADD_WORLD = 5//添加世界杯人员
        const val TYPE_EDIT_WORLD = 6//编辑世界杯人员
        const val TYPE_EDIT_MATCH = 3//修改参赛人员
        const val TYPE_EDIT_STUDENT = 4//修改运动营成员信息
    }
}