<template>
  <ContentWrap v-loading="formLoading">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      v-loading="formLoading"
      style="margin-right: 4em; margin-left: 0.5em; margin-top: 1em"
      label-width="130px"
    >
      <div class="base-expandable-content">
        <el-row>
          <el-col :span="12">
            <el-form-item :label="t('main.planCode')" prop="planCode">
              <el-input v-model="formData.planCode" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="t('bomList.name')" prop="inspectOrderTitle">
              <el-input v-model="formData.inspectOrderTitle" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="t('bomList.code')" prop="inspectOrderCode">
              <el-input v-model="formData.inspectOrderCode" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="t('iotMaintain.PersonInCharge')" prop="chargeName">
              <el-select v-model="formData.chargeName" style="width: 100%" disabled />
              <el-option
                v-for="item in deptUsers"
                :key="item.id"
                :label="item.nickname"
                :value="item.id"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="t('iotMaintain.remark')" prop="remark">
              <el-input type="textarea" v-model="formData.remark" disabled />
            </el-form-item>
          </el-col>
        </el-row>
      </div>
    </el-form>
  </ContentWrap>
  <ContentWrap>
    <!-- 列表 -->
      <ContentWrap>
        <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
          <el-table-column :label="t('iotMaintain.deviceCode')" align="center" prop="deviceCode" />
          <el-table-column :label="t('iotMaintain.deviceName')" align="center" prop="deviceName" />
<!--          <el-table-column label="路线名称" align="center" prop="routeName" />-->
          <el-table-column :label="t('iotMaintain.operation')" align="center" min-width="120px">
            <template #default="scope">
              <div style="display: flex; justify-content: center; align-items: center; width: 100%">
                <div style="margin-right: 10px">
                  <Icon style="vertical-align: middle; color: #c36e5b" icon="ep:view" />
                  <el-button
                    style="vertical-align: middle"
                    link
                    type="warning"
                    @click="viewRoute(scope.row.orderDetails)"
                  >
                    {{ t('route.InspectionRoute') }}
                  </el-button>
                </div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </ContentWrap>
  </ContentWrap>
  <ContentWrap>
    <el-form>
      <el-form-item style="float: right">
        <el-button @click="close">{{t('common.cancel')}}</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
  <RouteInspectItemDrawer
    ref="showDrawer"
    :model-value="drawerVisible"
    @update:model-value="(val) => (drawerVisible = val)"
  />
</template>
<script setup lang="ts">
import * as UserApi from '@/api/system/user'
import { ref } from 'vue'
import { useTagsViewStore } from '@/store/modules/tagsView'
import { useCache } from '@/hooks/web/useCache'
import RouteInspectItemDrawer from '@/views/pms/inspect/plan/RouteInspectItemDrawer.vue'
import { IotInspectOrderApi, IotInspectOrderVO } from '@/api/pms/inspect/order'

/** 维修工单 表单 */
defineOptions({ name: 'IotMaintainAe' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
const { delView } = useTagsViewStore() // 视图操作
const { currentRoute, push } = useRouter()
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const drawerVisible = ref<boolean>(false)
const showDrawer = ref()
const list = ref<IotInspectOrderVO[]>([]) // 列表的数据
const { params, name } = useRoute() // 查询参数
const id = params.id
const deptUsers = ref<UserApi.UserVO[]>([]) // 用户列表
const formData = ref({
  planCode: undefined,
  id: undefined,
  inspectOrderTitle: undefined,
  inspectOrderCode: undefined,
  status: undefined,
  remark: undefined,
  deptId: undefined,
  deviceIds: undefined
})
const formRules = reactive({
  planTitle: [{ required: true, message: '巡检计划标题不能为空', trigger: 'blur' }],
  planCode: [{ required: true, message: '巡检计划编号不能为空', trigger: 'blur' }],
  planUnit: [{ required: true, message: '单位不能为空', trigger: 'blur' }],
  charges: [{ required: true, message: '负责人不能为空', trigger: 'blur' }]
})
// 拖动状态管理
const items = ref([])
const deviceChoose = (rows) => {
  list.value = rows
}

const viewRoute = (itemJson) => {
  drawerVisible.value = true
  showDrawer.value.openDrawer(itemJson)
}
const formRef = ref() // 表单 Ref
const inspectItemChoose = (rows) => {
  items.value = []
  items.value = rows
}
const deviceFormRef = ref()
const openForm = () => {
  deviceFormRef.value.open()
}

const close = () => {
  delView(unref(currentRoute))
  push({ name: 'IotInspectOrder', params: {} })
}
const { wsCache } = useCache()
/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调

/** 重置表单 */
onMounted(async () => {
  if (id) {
    formType.value = 'update'
    const iotInspectOrder = await IotInspectOrderApi.getIotInspectOrder(id)
    formData.value = iotInspectOrder
    list.value = iotInspectOrder.details
  } else {
    formType.value = 'create'
  }
})
const handleDelete = async (id: number) => {
  try {
    const index = list.value.findIndex((item) => item.code === id)
    if (index !== -1) {
      // 通过 splice 删除元素
      list.value.splice(index, 1)
    }
  } catch {}
}
</script>
<style scoped>
.base-expandable-content {
  overflow: hidden; /* 隐藏溢出的内容 */
  transition: max-height 0.3s ease; /* 平滑过渡效果 */
}
/* 横向布局容器 */
</style>
