<template>
  <el-dialog title="复启" v-model="visible" width="800px" append-to-body @close="handleClose">
    <el-alert type="info" :closable="false" style="border-color: #0ea5e9; background-color: #ecf7ff; color: #0369a1; height: 35px;">
      <template #default>
        <i class="fa fa-refresh mr-2" style="color: #0369a1;"> 请选择复位方式，完成工单复启。</i>
      </template>
    </el-alert>
    <h4 class="text-sm font-medium text-gray-800 mb-3"></h4>
    <el-form ref="resetFormRef" :model="formData" :rules="resetRules" label-width="120px" label-position="top">
      <el-row>
        <el-col :span="12">
          <el-form-item label="工单编码">
            <el-input v-model="formData.workOrderProjectNo" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="风机编号">
            <el-input v-model="formData.pcsDeviceName" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维保中心">
            <el-input v-model="formData.gxtCenter" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="场站">
            <el-input v-model="formData.pcsStationName" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="品牌">
            <el-input v-model="formData.brand" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="发生时间">
            <el-input v-model="formData.occurTime" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="故障代码"><el-input v-model="formData.faultCode" disabled /></el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="故障条文"><el-input v-model="formData.faultBarcode" disabled /></el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="故障描述">
            <el-input
                v-model="formData.faultDesc"
                type="textarea"
                :rows="3"
                disabled
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="复位方式" prop="resetMethod">
            <el-select
              v-model="formData.resetMethod"
              placeholder="请选择复位方式"
              style="width: 100%"
            >
              <el-option
                v-for="dict in resetMethodOptions"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleCancel">取 消</el-button>
        <el-button type="primary" @click="handleSubmit">复位启机</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, defineProps, defineEmits, getCurrentInstance } from 'vue'
import {resetStartRepairOrder} from "@/api/gxt/repairOrder.js";

// 获取当前实例
const { proxy } = getCurrentInstance()

// 定义属性
const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false
  },
  data: {
    type: Object,
    default: () => ({})
  },
  resetMethodOptions: {
    type: Array,
    default: () => ([])
  },
  onSubmit: {
    type: Function,
    default: null
  }
})

// 定义事件
const emit = defineEmits(['update:modelValue', 'success'])

// 响应式数据
const visible = ref(false)
const formData = ref({})
const resetFormRef = ref()

// 表单验证规则
const resetRules = ref({
  resetMethod: [
    { required: true, message: "请选择复位方式", trigger: "change" }
  ]
})

// 监听modelValue变化
watch(() => props.modelValue, (val) => {
  visible.value = val
  if (val) {
    // 初始化表单数据
    formData.value = { ...props.data, selectedMembers: [] }
  }
})

// 监听visible变化
watch(visible, (val) => {
  emit('update:modelValue', val)
  if (val) {
    // 打开对话框后重置表单验证错误
    proxy.$nextTick(() => {
      if (resetFormRef.value) {
        resetFormRef.value.clearValidate()
      }
    })
  }
})

// 关闭对话框
const handleClose = () => {
  visible.value = false
}

// 取消操作
const handleCancel = () => {
  visible.value = false
}

// 提交操作
const handleSubmit = async () => {
  if (!resetFormRef.value) return

  await resetFormRef.value.validate(async (valid) => {
    if (valid) {
      try {
        // 设置维修方式为复位启机(2)
        const submitData = { ...formData.value, repairMethod: '2' }
        await resetStartRepairOrder(submitData)
        proxy.$modal.msgSuccess("复启成功")
        visible.value = false
        emit('success')
      } catch (error) {
        proxy.$modal.msgError("操作失败: " + (error.message || "未知错误"))
      }
    }
  })
}
</script>

<style scoped>
/* 表单中的列间距调整 */
:deep(.el-col) {
  padding-left: 5px;
  padding-right: 5px;
}
</style>
