<template>
  <el-form
    ref="formRef"
    :model="formData"
    :rules="formRules"
    v-loading="formLoading"
    label-width="0px"
    :inline-message="true"
  >
    <el-table :data="formData" class="-mt-10px">
      <el-table-column label="序号" type="index" width="100" />
       <el-table-column label="巡检项目" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.itemName`" :rules="formRules.itemName" class="mb-0px!">
            <el-input v-model="row.itemName" placeholder="请输入巡检项目" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="填报类型" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.reportType`" :rules="formRules.reportType" class="mb-0px!">
          <el-select v-model="row.reportType" placeholder="请选择填报类型">
            <el-option
                v-for="dict in getStrDictOptions(DICT_TYPE.DEVICE_INSPECTION_REPORT_TYPE)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            />
          </el-select>
          </el-form-item>

        </template>
      </el-table-column>
      <el-table-column label="上限" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.maxValue`" :rules="formRules.maxValue" class="mb-0px!">
            <el-input v-model="row.maxValue" placeholder="请输入上限" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="下限" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.minValue`" :rules="formRules.minValue" class="mb-0px!">
            <el-input v-model="row.minValue" placeholder="请输入下限" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="排序" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.sort`" :rules="formRules.sort" class="mb-0px!">
            <el-input v-model="row.sort" placeholder="请输入排序" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="说明" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.remark`" :rules="formRules.remark" class="mb-0px!">
            <el-input v-model="row.remark" placeholder="请输入说明" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column align="center" fixed="right" label="操作" width="60">
        <template #default="{ $index }">
          <el-button @click="handleDelete($index)" link type="danger">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-form>
  <el-row justify="center" class="mt-3">
    <el-button @click="handleAdd" round>+ 添加设备巡检标准内容</el-button>
  </el-row>
</template>
<script setup lang="ts">
import { InspectionStandardApi } from '@/api/device/inspectionstandard'
import {DICT_TYPE, getStrDictOptions} from "@/utils/dict";

const props = defineProps<{
  standId: number // 标准id（主表的关联字段）
}>()
const formLoading = ref(false) // 表单的加载中
const formData = ref<any[]>([])
const formRules = reactive({
})
const formRef = ref() // 表单 Ref

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.standId,
  async (val) => {
    // 1. 重置表单
    formData.value = []
    // 2. val 非空，则加载数据
    if (!val) {
      return;
    }
    try {
      formLoading.value = true
      formData.value = await InspectionStandardApi.getInspectionStandardItemListByStandId(val)
    } finally {
      formLoading.value = false
    }
  },
  { immediate: true }
)

/** 新增按钮操作 */
const handleAdd = () => {
  const row = {
    id: undefined,
    standId: undefined,
    itemName: undefined,
    reportType: undefined,
    minValue: undefined,
    maxValue: undefined,
    sort: undefined,
    remark: undefined,
  }
  row.standId = props.standId as any
  formData.value.push(row)
}

/** 删除按钮操作 */
const handleDelete = (index) => {
  formData.value.splice(index, 1)
}

/** 表单校验 */
const validate = () => {
  return formRef.value.validate()
}

/** 表单值 */
const getData = () => {
  return formData.value
}

defineExpose({ validate, getData })
</script>
