<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-row>
      <el-col :span="12">
      <el-form-item label="维修时间" prop="repairTime">
        <el-date-picker
          v-model="formData.repairTime"
          type="datetime"
          value-format="x"
          placeholder="选择维修时间"
        />
      </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维修项" prop="repairItem">
            <el-input v-model="formData.repairItem" />
          </el-form-item>
        </el-col>
      <el-col :span="12">
      <el-form-item label="图片" prop="pic">
        <UploadImg v-model="formData.pic" height="50px" />
      </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="t('maintain.phRepair')" prop="repairPerson">
            <el-select multiple v-model="formData.repairPerson" :placeholder="t('maintain.phRepairHolder')" filterable clearable>
              <el-option
                v-for="ap in repairPeople"
                :key="ap.id"
                :label="ap.nickname"
                :value="ap.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
      <el-form-item label="维修内容" prop="repairContent">
        <el-input type="textarea" v-model="formData.repairContent" />
      </el-form-item>
        </el-col>
        <el-col :span="24">
      <el-form-item label="解决办法" prop="solution">
        <el-input type="textarea" v-model="formData.solution" placeholder="请输入解决办法" />
      </el-form-item>
        </el-col>
        <el-col :span="24">
      <el-form-item label="备注" prop="remark">
        <el-input type="textarea" v-model="formData.remark" placeholder="请输入备注" />
      </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { IotJdRecordApi, IotJdRecordVO } from '@/api/pms/maintain/record'
import {ModelVO} from "@/api/ai/model/model";

/** 基地维修记录 表单 */
defineOptions({ name: 'IotJdRecordForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const repairPeople = ref([])
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  repairPerson:undefined,
  repairPersonName:undefined,
  repairItem: undefined,
  id: undefined,
  deviceId: undefined,
  repairTime: undefined,
  repairContent: undefined,
  pic: undefined,
  solution: undefined,
  remark: undefined,
  deviceName: undefined,
  repairId: undefined,
})
const formRules = reactive({
  repairPerson: [{ required: true, message: '设备id不能为空', trigger: 'blur' }],
  deviceId: [{ required: true, message: '设备id不能为空', trigger: 'blur' }],
  repairTime: [{ required: true, message: '维修日期时间不能为空', trigger: 'blur' }],
  repairContent: [{ required: true, message: '维修内容不能为空', trigger: 'blur' }],
  solution: [{ required: true, message: '解决办法不能为空', trigger: 'blur' }],
  repairItem: [{ required: true, message: '维修项不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number, recordId:any) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  formData.value.repairId = id
  // 修改时，设置数据
  if (recordId) {
    formLoading.value = true
    try {
      const data = await IotJdRecordApi.getIotJdRecord(recordId)
      formData.value = data
    } finally {
      formLoading.value = false
    }
  }
  const assist = await IotJdRecordApi.getIotJdRecordAssist(id)
  repairPeople.value = assist
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    let personName = []
    console.log(JSON.stringify(formData.value.repairPerson))
    formData.value.repairPerson.map((repairPersonKey)=>{
      const person = repairPeople.value.filter((item) => item.id ===repairPersonKey)
      if (person.length>0){
        personName.push(person[0].nickname)
      }
    })
    debugger
    formData.value.repairPerson = formData.value.repairPerson.join(',')
    formData.value.repairPersonName = personName.join(',')
    const data = formData.value as unknown as IotJdRecordVO
    if (formType.value === 'create') {
      await IotJdRecordApi.createIotJdRecord(data)
      message.success(t('common.createSuccess'))
    } else {
      await IotJdRecordApi.updateIotJdRecord(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    deviceId: undefined,
    repairTime: undefined,
    repairContent: undefined,
    pic: undefined,
    solution: undefined,
    remark: undefined,
    deviceName: undefined,
  }
  formRef.value?.resetFields()
}
</script>
