18034727885
2026-04-27 3a4c1bbb2cf4fb1cdda8a5061b0674f30e7456b4
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
package com.example.smart.ui.item
 
import android.content.Intent
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.basecore.BaseBindingActivity
import com.example.basecore.util.showCenterToast
import com.example.basecore.util.toast
import com.example.smart.R
import com.example.smart.bean.CommonOptionsBean
import com.example.smart.bean.EntityRef
import com.example.smart.bean.ImageRef
import com.example.smart.databinding.ActivityCreateItemBinding
import com.example.smart.enumlist.ProjectType
import com.example.smart.enumlist.ProjectUsageType
import com.example.smart.image.ImageProvider
import com.example.smart.ui.item.adapter.ImageShowAdapter
import com.example.smart.ui.item.bean.ImageBean
import com.example.smart.ui.item.bean.ProjectInfoParam
import com.example.smart.ui.item.contacts.bean.MemberLiveData
import com.example.smart.ui.item.popup.data.DynamicBean
import com.example.smart.widget.common.ICommonDataEvent
import com.example.smart.widget.dialog.LoadingDialog
 
/**
 * 计划详情页 ---> 新建项目
 */
class CreateItemActivity : BaseBindingActivity() {
 
    private val binding by lazy { ActivityCreateItemBinding.inflate(LayoutInflater.from(mContext)) }
 
    private val viewModel: CreateItemViewModel by viewModels()
 
    private val dynamicBean by lazy {
        DynamicBean(
            0,
            "",
            dangerousOptions = ArrayList(),
            superDangerousOptions = ArrayList()
        )
    }
 
    private var province: String = ""
    private var city: String = ""
    private var district: String = ""
 
    private var from = ""
    private var projectId = -1
    private var projectName = ""
    private var clientId = ""
    private var planId = ""
 
    private val loadingDialog: LoadingDialog by lazy { LoadingDialog(this) }
 
    private val mImageProvider by lazy {
        ImageProvider(this, ImageCallBack())
    }
 
    private inner class ImageCallBack : ImageProvider.IImageCallback {
 
        override fun onUploadSuccess(entityRef: List<ImageRef>) {
            entityRef.forEach {
                val bean = ImageBean(it.id, it.name)
                adapter.addImage(bean)
            }
        }
 
        override fun onUploadFailed() {
            mContext.showCenterToast("图片上传失败,请稍后再试...")
        }
    }
 
    private lateinit var adapter: ImageShowAdapter
 
    private val mImageList = ArrayList<ImageBean>()
 
    override fun getRootView() = binding.root
 
    private fun initParams() {
        from = intent.data?.getQueryParameter("from") ?: ""
        projectId = intent.data?.getQueryParameter("projectId")?.toInt() ?: 0
        projectName = intent.data?.getQueryParameter("projectName") ?: ""
        clientId = intent.data?.getQueryParameter("client_id") ?: ""
        planId = intent.data?.getQueryParameter("plan_id") ?: ""
    }
 
    private fun initData() {
        transStatusBar()
        addListener()
        buildOption()
        initParams()
    }
 
    override fun initView() {
        initData()
        binding.apply {
            recyclerView.layoutManager =
                LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false)
            adapter = ImageShowAdapter(mContext, mImageList, false)
            recyclerView.adapter = adapter
 
            recyclerView.isNestedScrollingEnabled = false
            recyclerView.setHasFixedSize(false)
        }
 
        binding.apply {
            takePhotoLayout.setOnClickListener {
                mImageProvider.showImageSelect()
            }
 
            typeWidget.setDataEvent(object : ICommonDataEvent {
                override fun onClickView(containerId: String) {
                    val usageTypes = ProjectType.findUsageTypeByCode(containerId)
                    usageTypes?.let {
                        bindUsageTypes(usageTypes)
                    }
                }
            })
 
            titleLayout.setMoreImg(R.mipmap.ic_creat_item) {
                val param = createProjectParam() ?: return@setMoreImg
                if (projectId > 0) {
                    param.id = projectId
                    titleLayout.setTitle("修改项目")
                    viewModel.editItem(param) { showCenterToast(it) }
                } else {
                    titleLayout.setTitle("新建项目")
                    viewModel.createItem(param) { showCenterToast(it) }
 
                }
            }
        }
 
        //表示是修改项目
        if (projectId > 0) {
            loadingDialog.show()
            requestProjectInfo(projectId)
            binding.addMissionTv.visibility = View.GONE
            binding.titleLayout.setTitle("修改项目")
        }else{
            binding.addMissionTv.visibility = View.VISIBLE
        }
 
        if (!TextUtils.isEmpty(projectName)) {
            binding.titleLayout.setTitle(projectName)
        }
    }
 
    private fun requestProjectInfo(id: Int) {
        viewModel.requestProjectData(id) {
            toast(it)
        }
    }
 
    /**
     * 生成options
     */
    private fun buildOption() {
        val values = ProjectType.values()
        val commonOptionsBeans = trans2CommonOptionsBean(values)
        binding.typeWidget.bindData(commonOptionsBeans)
 
        bindUsageTypes(values[0])
    }
 
    private fun bindUsageTypes(type: ProjectType) {
        val usageTypes = type.projectUsageType()
        val usageOptionsBean = trans2CommonOptionsBean(usageTypes)
 
        binding.useWidget.bindData(usageOptionsBean)
    }
 
    /**
     * 将枚举转为CommonOptionsBean
     */
    private fun trans2CommonOptionsBean(it: Array<ProjectType>): List<CommonOptionsBean> {
        val list = mutableListOf<CommonOptionsBean>()
        it.forEach {
            list.add(CommonOptionsBean(it.name, it.desc))
        }
        return list
    }
 
    /**
     * 将枚举转为CommonOptionsBean
     */
    private fun trans2CommonOptionsBean(it: List<ProjectUsageType>): List<CommonOptionsBean> {
        val list = mutableListOf<CommonOptionsBean>()
        it.forEach {
            list.add(CommonOptionsBean(it.name, it.desc))
        }
        return list
    }
 
 
    override fun bindDataObserver() {
        super.bindDataObserver()
 
        viewModel.createResult.observe(this@CreateItemActivity) {
            MemberLiveData.clear()
//            if (from == "plan") {//从创建计划过来的
//                PlanCreateEvent.setResult(PlanEventBean(CODE_PROJECT_SUCCESS))
//            } else {
            toast("添加项目【${binding.nameEdt.text}】成功")
//            }
            if (it != null) {
                // 新建项目-设置任务流程:mode=true,选择刚创建的项目
                val pop = CreateMissionPopWindow(mContext, planId.toInt(), it.id, {
                    finish()
                }, mode = true)
                pop.showPopupWindow()
            }
        }
 
        viewModel.editResult.observe(this) {
            toast("项目修改成功")
            finish()
        }
 
        viewModel.projectBean.observe(this) {
            loadingDialog.dismiss()
            bindProject(it)
        }
    }
 
    /**
     * 绑定已经存在项目
     */
    private fun bindProject(bean: ProjectInfoParam) {
        binding.run {
            nameEdt.focusable = View.NOT_FOCUSABLE
            nameEdt.isFocusableInTouchMode = false
            nameEdt.setText(bean.name)
 
            typeWidget.setContentWithId(bean.type.desc, bean.type.name)
            useWidget.setContentWithId(bean.usageType.desc, bean.usageType.name)
 
            clientId = bean.client.id.toString()
 
            val addressInfo = bean.addressInfo
            if(addressInfo != null){
                province = addressInfo.province
                city = addressInfo.city
                district = addressInfo.district
                addressWidget.setContentTxt(addressInfo.province + addressInfo.city + addressInfo.district)
                addressDetailWidget.setEditTxt(addressInfo.detail)
            }
 
            if(bean.constructionCompanyInfo != null){
                buildCompanyWidget.setEditTxt(bean.constructionCompanyInfo.name)
                buildCompanyLeaderWidget.setEditTxt(bean.constructionCompanyInfo.leader)
            }
            if(bean.supervisorCompanyInfo != null){
                supervisorWidget.setEditTxt(bean.supervisorCompanyInfo.name)
                supervisorLeaderWidget.setEditTxt(bean.supervisorCompanyInfo.leader)
            }
            if(bean.developmentCompanyInfo != null){
                developmentCompanyWidget.setEditTxt(bean.developmentCompanyInfo.name)
                developmentCompanyLeaderWidget.setEditTxt(bean.developmentCompanyInfo.leader)
            }
            licenseWidget.setEditTxt(bean.constructionLicense)
        }
 
        if (bean.constructionLicenseFiles != null && bean.constructionLicenseFiles.isNotEmpty()) {
            for (data in bean.constructionLicenseFiles) {
                val imgBean = ImageBean(data.id.toString(), data.name)
                mImageList.add(imgBean)
            }
            adapter.notifyDataSetChanged()
        }
    }
 
 
    /**
     * 创建项目
     */
    private fun createProjectParam(): ProjectInfoParam? {
        val params = ProjectInfoParam()
        binding.apply {
            if (nameEdt.text.isEmpty()) {
                toast("请输入项目名称")
                return null
            }
 
            if (typeWidget.getContent().isEmpty()) {
                toast("请输入项目类型")
                return null
            }
            if (useWidget.getContent().isEmpty()) {
                toast("请输入用途类型")
                return null
            }
            if (buildCompanyWidget.getContent().isEmpty()) {
                toast("请输入建设单位")
                return null
            }
            if (developmentCompanyWidget.getContent().isEmpty()) {
                toast("请输入施工单位")
                return null
            }
 
            params.id = projectId
 
            params.name = nameEdt.text.toString()
            params.type = ProjectType.valueOf(typeWidget.getContainerId())
            params.usageType = ProjectUsageType.valueOf(useWidget.getContainerId())
 
            val addressInfo = ProjectInfoParam.AddressInfo()
            params.addressInfo = addressInfo
 
            addressInfo.city = city
            addressInfo.district = district
            addressInfo.province = province
 
            if (addressDetailWidget.getContent() != "省市区") {
                addressInfo.detail = addressDetailWidget.getContent()
            }
 
            // TODO 委托方只在创建项目时需要,不能修改
            params.client = EntityRef(clientId.toInt())
 
            //建设单位
            val constructionCompanyInfo = ProjectInfoParam.CompanyInfo()
            constructionCompanyInfo.name = buildCompanyWidget.getContent()
            constructionCompanyInfo.leader = buildCompanyLeaderWidget.getContent()
            params.constructionCompanyInfo = constructionCompanyInfo
 
            //监理单位
            val supervisorCompanyInfo = ProjectInfoParam.CompanyInfo()
            supervisorCompanyInfo.name = supervisorWidget.getContent()
            supervisorCompanyInfo.leader = supervisorLeaderWidget.getContent()
            params.supervisorCompanyInfo = supervisorCompanyInfo
 
            //施工单位
            val developmentCompanyInfo = ProjectInfoParam.CompanyInfo()
            developmentCompanyInfo.name = developmentCompanyWidget.getContent()
            developmentCompanyInfo.leader = developmentCompanyLeaderWidget.getContent()
            params.developmentCompanyInfo = developmentCompanyInfo
 
            //施工许可号
            params.constructionLicense = licenseWidget.getContent()
 
            val files = ArrayList<EntityRef>()
            //施工许可证图片
            for (bean in mImageList) {
                files.add(EntityRef(bean.uri.toInt()))
            }
            params.constructionLicenseFiles = files
 
        }
        return params
    }
 
    private fun addListener() {
        binding.apply {
            //新增项目名称
//            addNameTv.setOnClickListener {
//
//            }
 
            //项目地址 - 已禁用城市选择功能
            addressWidget.setOnClickListener {
                // 城市选择功能已禁用,不执行任何操作
                // 或者可以显示一个提示信息
                toast("城市选择功能已禁用")
            }
 
            //添加动态信息
            addDynamicTv.setOnClickListener {
//                dynamicBean.checkTask = IdRef(projectId)
//                val pop = DynamicPopupWindow(mContext, dynamicBean)
//                pop.showPopupWindow(0, ScreenUtil.getScreenHeight(mContext))
            }
 
            //设置任务信息
            addMissionTv.setOnClickListener {
                val param = createProjectParam() ?: return@setOnClickListener
                viewModel.createItem(param) {
                    showCenterToast(it)
                }
            }
        }
    }
 
 
    override fun onDestroy() {
        super.onDestroy()
        MemberLiveData.clear()
    }
 
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        mImageProvider.onActivityResult(requestCode, resultCode, data)
    }
}