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) { 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() 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): List { val list = mutableListOf() it.forEach { list.add(CommonOptionsBean(it.name, it.desc)) } return list } /** * 将枚举转为CommonOptionsBean */ private fun trans2CommonOptionsBean(it: List): List { val list = mutableListOf() 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() //施工许可证图片 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) } }