<template>
  <el-form
    ref="formRef"
    :model="formData"
    :rules="formRules"
    v-loading="formLoading"
    label-width="0px"
    :inline-message="true"
    :default-sort="{ prop: 'sort_date', order: 'descending' }"
  >
    <el-table :data="formData" class="-mt-10px" :stripe="true"  :show-overflow-tooltip="true">
      <el-table-column label="序号" type="index" width="60" />
      <el-table-column disabled label="计划编码" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.code`" :rules="formRules.code" class="mb-0px!">
            <el-input disabled v-model="row.code" placeholder="请输入计划编码" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column disabled="true" label="产品" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item v-show="false" :prop="`${$index}.productId`" :rules="formRules.productId" class="mb-0px!">
            <el-input disabled v-model="row.productId" placeholder="请输入产品ID" />
          </el-form-item>
          <el-form-item :prop="`${$index}.productName`" :rules="formRules.productName" class="mb-0px!">
            <el-input disabled v-model="row.productName" placeholder="请输入产品" />
          </el-form-item>
        </template>
      </el-table-column>
       <el-table-column label="成型数量" min-width="120">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.planNumber`" :rules="formRules.planNumber" class="mb-0px!">
            <el-input v-model="row.planNumber" placeholder="请输入数量" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="热压数量" min-width="120">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.reyaNumber`" :rules="formRules.reyaNumber" class="mb-0px!">
            <el-input v-model="row.planNumber" placeholder="请输入数量" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column prop="sort_date" sortable  label="计划开始时间" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.planStartTime`" :rules="formRules.planStartTime" class="mb-0px!">
            <el-date-picker
              v-model="row.planStartTime"
              type="date"
              value-format="x"
              placeholder="选择计划开始时间"
            />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="计划结束时间" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.planEndTime`" :rules="formRules.planEndTime" class="mb-0px!">
            <el-date-picker
              v-model="row.planEndTime"
              type="date"
              value-format="x"
              placeholder="选择计划结束时间"
            />
          </el-form-item>
        </template>
      </el-table-column>

      <el-table-column label="备注" min-width="250">
        <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="100">
        <template #default="{row, $index }">
          <el-button type="success" @click="handleCopy(row)" link>
            <Icon icon="ep:document-copy" class="mr-5px"/>
          </el-button>
          <el-button type="danger" @click="handleDelete($index)" link>
            <Icon icon="ep:delete" class="mr-5px"/>
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-form>

</template>
<script setup lang="ts">
import { getIntDictOptions, getBoolDictOptions, DICT_TYPE } from '@/utils/dict'
import { TaskApi } from '@/api/mes/task'

const props = defineProps<{
  taskId: undefined // 任务单ID（主表的关联字段）
}>()
const formLoading = ref(false) // 表单的加载中
const formData = ref([])
const formRules = reactive({
  productId: [{ required: true, message: '产品不能为空', trigger: 'blur' }],
  taskId: [{ required: true, message: '任务单不能为空', trigger: 'blur' }],
  planNumber: [{ required: true, message: '成型数量不能为空', trigger: 'blur' }],
  planStartTime: [{ required: true, message: '计划开始时间不能为空', trigger: 'blur' }],
  planEndTime: [{ required: true, message: '计划结束时间不能为空', trigger: 'blur' }],

})
const formRef = ref() // 表单 Ref

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


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

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

/** 表单值 */
const getData = () => {
  return formData.value
}
/** 表单值 */
const setData = (data) => {
  formData.value = data
}
defineExpose({ validate, getData , setData})
/** 复制按钮操作 */
const handleCopy = (row) => {
  const newRow = {
    id: undefined,
    code: undefined,
    productId: undefined,
    productName: undefined,
    planNumber: undefined,
    reyaNumber: undefined,
    planStartTime: undefined,
    planEndTime: undefined,
    remark: undefined,
    priorityNum: undefined,
    isEnable: undefined,
  }
  newRow.code = row.code + (Math.floor(Math.random() * 10) + 1)
  newRow.productId = row.productId
  newRow.remark = row.remark
  newRow.isEnable = row.isEnable
  newRow.planStartTime = row.planStartTime
  newRow.planEndTime = row.planEndTime
  newRow.planNumber= row.planNumber
  newRow.reyaNumber= row.reyaNumber
  newRow.productName = row.productName
  newRow.priorityNum = 1
  formData.value.push(newRow)
}
</script>
