<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}.subject`" :rules="formRules.subject" class="mb-0px!">
            <el-select
              v-model="row.subject"
              clearable
              filterable
              placeholder="请选择项目"
            >
              <el-option
                v-for="item in subjectList"
                :key="item.subject"
                :label="item.subject"
                :value="item.subject"
              />
            </el-select>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="预期" min-width="400">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.expect`" :rules="formRules.expect">
<!--            <Editor v-model="row.expect" height="150px" />-->
            <el-input type="textarea" v-model="row.expect" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="结果" min-width="400">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.result`" :rules="formRules.result">
            <el-input type="textarea" v-model="row.result" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="明细状态" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.detailStatus`" :rules="formRules.detailStatus" class="mb-0px!">
            <el-radio-group v-model="row.detailStatus">
                <el-radio
                  v-for="dict in getStrDictOptions(DICT_TYPE.MES_PASS_OR_NOPASS)"
                  :key="dict.value"
                  :label="dict.value"
                >
                  {{ dict.label }}
                </el-radio>
            </el-radio-group>
          </el-form-item>
        </template>
      </el-table-column>
       <el-table-column align="center" fixed="right" label="操作" width="60">
        <template #default="{ $index }">
          <el-button type="danger" @click="handleDelete($index)" link>
            <Icon icon="ep:delete" />
          </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 { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { MoldRecordApi } from '@/api/mes/moldrecord'
import {MoldBrandApi, MoldBrandVO} from "@/api/erp/mold";
import {MoldRecordSubjectApi, MoldRecordSubjectVO} from "@/api/mes/moldrecordsubject";

const props = defineProps<{
  recordId: undefined // 维保记录id（主表的关联字段）
}>()
const formLoading = ref(false) // 表单的加载中
const formData = ref([])
const formRules = reactive({
})
const formRef = ref() // 表单 Ref
const subjectList = ref<MoldRecordSubjectVO[]>([]) // 模具型号列表
/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.recordId,
  async (val) => {
    // 1. 重置表单
    formData.value = []
    // 2. val 非空，则加载数据
    if (!val) {
      return;
    }
    try {
      formLoading.value = true
      formData.value = await MoldRecordApi.getMoldRecordSubjectDetailListByRecordId(val)
    } finally {
      formLoading.value = false
    }
  },
  { immediate: true }
)

/** 新增按钮操作 */
const handleAdd = () => {
  const row = {
    id: undefined,
    subject: undefined,
    expect: undefined,
    result: undefined,
    detailStatus: undefined,
    recordId: undefined
  }
  row.recordId = props.recordId
  formData.value.push(row)
}

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

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

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

/** 初始化 */
onMounted(async () => {
  // 加载列表
  subjectList.value = await MoldRecordSubjectApi.getList()
})
defineExpose({ validate, getData })
</script>
