package com.example.smart.ui.project import android.content.Intent import android.content.pm.PackageManager import android.text.TextUtils import android.view.Gravity import android.view.LayoutInflater import android.view.View import android.widget.FrameLayout import android.widget.ImageView import androidx.activity.viewModels import androidx.core.app.ActivityCompat import androidx.fragment.app.viewModels import com.example.basecore.BaseBindingActivity import com.example.basecore.amap.ILocationCallBack import com.example.basecore.amap.LocationProvider import com.example.basecore.util.SchemeUtils import com.example.basecore.util.ToastUtils import com.example.basecore.util.dp2px import com.example.smart.R import com.example.smart.databinding.ActivityProjectDetailsBinding import com.example.smart.ui.options.device.RequestDeviceActivity import com.example.smart.ui.project.popup.ProjectDetailsPopupWindow import com.example.smart.ui.project.provider.ProjectPageAdapter import com.example.smart.util.projectEventScheme import com.example.smart.widget.dialog.CommonDialog import com.example.smart.widget.dialog.LoadingDialog import com.google.android.material.tabs.TabLayoutMediator import com.example.basecore.util.showCenterToast import com.example.basecore.util.showOnlyToast import com.example.basecore.util.startAct import com.example.basecore.util.toast import com.example.smart.enumlist.CheckTaskState import com.example.smart.ui.exclude.ExcludeActivity import com.example.smart.ui.project.bean.TaskDetailInfo import com.example.smart.ui.project.check.CheckViewModel import com.example.smart.ui.project.check.EditDialog import com.example.smart.ui.project.check.bean.CheckBean import com.example.smart.ui.project.check.bean.CheckRefresh import com.example.smart.ui.project.frag.ProjectParam import com.example.smart.ui.project.onduty.PostListActivity import com.example.smart.ui.project.onduty.bean.OnDutyBean import com.example.smart.util.createWorkScheme import com.example.smart.util.signProject import org.greenrobot.eventbus.EventBus class ProjectDetailsActivity : BaseBindingActivity() { private val binding by lazy { ActivityProjectDetailsBinding.inflate(LayoutInflater.from(mContext)) } private val viewModel: ProjectViewModel by viewModels() private val loadingDialog: LoadingDialog by lazy { LoadingDialog(this) } private var clientId: String = "" private var taskDetailInfo: TaskDetailInfo? = null private lateinit var taskId: String private lateinit var planId: String private lateinit var projectId: String private val checkVM: CheckViewModel by viewModels() val dialog by lazy { EditDialog(this, checkVM) { EventBus.getDefault().post(CheckRefresh()) } } private val mLocationProvider by lazy { LocationProvider(mContext) } override fun getRootView() = binding.root fun getProjectId(): Int { return taskDetailInfo?.project?.id ?: -1 } fun getTaskDetailInfo(): TaskDetailInfo? { return taskDetailInfo } override fun initView() { transStatusBar() taskId = intent.data?.getQueryParameter("task_id") ?: "" planId = intent.data?.getQueryParameter("plan_id") ?: "" projectId = intent.data?.getQueryParameter("project_id") ?: "" //实测实量 val tabArray = arrayListOf( "问题清单", "设备问题", "设备清单", "在岗情况", "检查信息", "正确做法", "检查小组" ) val param = ProjectParam(planId, taskId, projectId) binding.viewPager.adapter = ProjectPageAdapter(this, tabArray.size, param) val dp28 = dp2px(28) binding.run { TabLayoutMediator( tabLayout, viewPager ) { tab, position -> tab.text = tabArray[position] }.attach() val s4 = ImageView(mContext) val layout4 = FrameLayout.LayoutParams(dp28, dp28) s4.setImageResource(R.mipmap.ic_project_sign3) layout4.gravity = Gravity.END or Gravity.CENTER_VERTICAL layout4.rightMargin = dp2px(30) + dp28 s4.layoutParams = layout4 s4.setOnClickListener { taskDetailInfo?.let { if (it.state == CheckTaskState.Finished) { mContext.showOnlyToast("项目已关闭,无需签到") return@setOnClickListener } } val name = taskDetailInfo?.project?.name ?: "" val clientName = taskDetailInfo?.client?.name ?: "" SchemeUtils.openScheme( mContext, "$signProject?pro_name=${name}&pro_id=${getProjectId()}&client_name=$clientName" ) } titleLayout.getContainerView()?.addView(s4) //轮空项 val imgEdit = ImageView(mContext) val layout = FrameLayout.LayoutParams(dp28, dp28) imgEdit.setImageResource(R.mipmap.ic_project_exclude) layout.gravity = Gravity.END or Gravity.CENTER_VERTICAL layout.rightMargin = dp2px(18) imgEdit.layoutParams = layout imgEdit.setOnClickListener { if (taskDetailInfo == null) { return@setOnClickListener } if (taskDetailInfo?.isClosed() == true) { showOnlyToast("项目已关闭") return@setOnClickListener } startAct(mContext) { putExtra("title", binding.projectNameTv.text) putExtra("taskId", taskId) putExtra("is_close", taskDetailInfo?.state == CheckTaskState.Finished) } } titleLayout.getContainerView()?.addView(imgEdit) } addListener() bindMenu() requestData() } private fun addListener() { binding.apply { commonLayout.setOnClickListener { if (taskDetailInfo == null) { return@setOnClickListener } val pop = ProjectDetailsPopupWindow( mContext, taskDetailInfo!!.id, taskDetailInfo!!.project.id ) pop.showPopupWindow() } locationImg.setOnClickListener { if (!canUpdateTask()) { return@setOnClickListener } val dialog = CommonDialog() .setContent("确认更新项目位置信息吗?") dialog.setCertainListener { locationPermission() } dialog.show(supportFragmentManager, "") } } } private fun bindMenu() { val intent = Intent() binding.apply { //添加设备 menuItem1.setOnClickListener { if (!canUpdateTask()) { return@setOnClickListener } menuLayout.close(false) intent.setClassName(mContext, RequestDeviceActivity::class.java.name) intent.putExtra("id", taskId) intent.putExtra("project_id", getProjectId()) startActivity(intent) } //添加问题 menuItem2.setOnClickListener { if (!canUpdateTask()) { return@setOnClickListener } SchemeUtils.openScheme( mContext, "$projectEventScheme?from=issue&id=$taskId&plan_id=$planId" ) menuLayout.close(false) } //正确做法 menuItem3.setOnClickListener { if (!canUpdateTask()) { return@setOnClickListener } menuLayout.close(false) SchemeUtils.openScheme( mContext, "$projectEventScheme?from=correct&id=$taskId&plan_id=$planId" ) } menuItem7.setOnClickListener { if (taskDetailInfo?.state == CheckTaskState.Finished || taskDetailInfo?.state == CheckTaskState.Reviewed) { showOnlyToast("项目已关闭") return@setOnClickListener } val content = if (canReviewTask()) "确认关闭复核任务?" else "确认关闭检查任务?" val pop = CommonDialog().setContent(content).setCertainListener { if (canReviewTask()) { viewModel.closeReviewProject(taskId) { toast(it) } } else { viewModel.closeProject(taskId) { toast(it) } } } pop.show(supportFragmentManager, "1") menuLayout.close(false) } // 在岗情况 menuItem8.setOnClickListener { if (!canUpdateTask()) { return@setOnClickListener } menuLayout.close(false) intent.setClassName(mContext, PostListActivity::class.java.name) intent.putExtra("id", taskId) startActivity(intent) } // 检查信息 menuItem9.setOnClickListener { dialog.show( CheckBean(checkTaskId = if (taskId.isEmpty()) -1 else taskId.toInt()) ) } } } fun canUpdateTask(): Boolean { if (canReviewTask()) { showOnlyToast("复核状态不可更新") return false } if (taskDetailInfo?.state != CheckTaskState.Published && taskDetailInfo?.state != CheckTaskState.EXECUTING && taskDetailInfo?.state != CheckTaskState.Rejected ) { showOnlyToast("项目未开始或已关闭") return false } return true } private fun requestData() { if (TextUtils.isEmpty(taskId)) { toast("请检查Project_id") return } viewModel.requestTaskInfo(taskId) } private fun bindData() { if (taskDetailInfo == null) { return } val info = taskDetailInfo!! clientId = info.id.toString() binding.apply { projectNameTv.text = info.project.name val address = if (TextUtils.isEmpty(info.location)) "" else info.location addressTv.text = address clientNameTv.text = info.client?.name // val date = if(TextUtils.isEmpty(info.updateTime)) info.createTime else info.updateTime dateTv.text = info.date if (canReviewTask()) { menuItem7.labelText = "关闭复核任务" } } } override fun bindDataObserver() { super.bindDataObserver() viewModel.taskInfo.observe(this) { taskDetailInfo = it binding.apply { if (it.state == CheckTaskState.Finished) { locationImg.visibility = View.GONE } else { locationImg.visibility = View.VISIBLE } } bindData() } viewModel.addressInfo.observe(this) { binding.addressTv.text = taskDetailInfo?.location } viewModel.closeResultBean.observe(this) { showCenterToast("项目已关闭") finish() } } private fun onGenerateLocation() { mLocationProvider.getLocation(object : ILocationCallBack { override fun onLocationSuccess(address: String, lat: Double, lon: Double) { binding.commonLayout.post { if (TextUtils.isEmpty(address)) { toast("位置信息获取失败,请重试...") return@post } if (taskDetailInfo == null) { return@post } taskDetailInfo?.location = address val gps = ArrayList() gps.add(lat) gps.add(lon) taskDetailInfo?.gps = gps viewModel.editTask(taskDetailInfo!!) } } override fun onLocationFailed(reason: String) { binding.commonLayout.post { toast("位置信息获取失败,请重试...") return@post } } }) } private fun locationPermission() { val granted = PackageManager.PERMISSION_GRANTED val fineLocation = android.Manifest.permission.ACCESS_FINE_LOCATION val coarseLocation = android.Manifest.permission.ACCESS_COARSE_LOCATION if (ActivityCompat.checkSelfPermission(this, fineLocation) != granted && ActivityCompat.checkSelfPermission(this, coarseLocation) != granted ) { val permissionList = listOf(fineLocation, coarseLocation) requestPermission(permissionList) } else { onGenerateLocation() } } fun canReviewTask(): Boolean { return taskDetailInfo?.state == CheckTaskState.Reviewing || taskDetailInfo?.state == CheckTaskState.WaitForReview } }