<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="t('ProductionPlan.TaskSummary.planFormIndexColumn')" type="index" width="60" />
      <el-table-column disabled :label="t('ProductionPlan.TaskSummary.planFormCodeColumn')" min-width="170">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.code`" :rules="formRules.code" class="mb-0px!">
            <el-input disabled v-model="row.code" :placeholder="t('ProductionPlan.TaskSummary.planFormCodePlaceholder')" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column disabled="true" :label="t('ProductionPlan.TaskSummary.planFormProductColumn')" 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="t('ProductionPlan.TaskSummary.planFormProductIdPlaceholder')" />
          </el-form-item>
          <el-form-item :prop="`${$index}.productName`" :rules="formRules.productName" class="mb-0px!">
            <el-input disabled v-model="row.productName" :placeholder="t('ProductionPlan.TaskSummary.planFormProductNamePlaceholder')" />
          </el-form-item>
        </template>
      </el-table-column>
       <el-table-column :label="t('ProductionPlan.TaskSummary.planFormPlanNumberColumn')" 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="t('ProductionPlan.TaskSummary.planFormPlanNumberPlaceholder')" />
          </el-form-item>
        </template>
      </el-table-column>

      <el-table-column prop="sort_date" sortable  :label="t('ProductionPlan.TaskSummary.planFormPlanStartTimeColumn')" 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="t('ProductionPlan.TaskSummary.planFormPlanStartTimePlaceholder')"
            />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column :label="t('ProductionPlan.TaskSummary.planFormPlanEndTimeColumn')" 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="t('ProductionPlan.TaskSummary.planFormPlanEndTimePlaceholder')"
            />
          </el-form-item>
        </template>
      </el-table-column>

      <el-table-column :label="t('ProductionPlan.TaskSummary.planFormRemarkColumn')" 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="t('ProductionPlan.TaskSummary.planFormOperateColumn')" 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 { t } = useI18n() // 国际化

const formRules = reactive({
  productId: [{ required: true, message: t('ProductionPlan.TaskSummary.validatorPlanFormProductIdRequired'), trigger: 'blur' }],
  taskId: [{ required: true, message: t('ProductionPlan.TaskSummary.validatorPlanFormTaskIdRequired'), trigger: 'blur' }],
  planNumber: [{ required: true, message: t('ProductionPlan.TaskSummary.validatorPlanFormPlanNumberRequired'), trigger: 'blur' }],
  planStartTime: [{ required: true, message: t('ProductionPlan.TaskSummary.validatorPlanFormPlanStartTimeRequired'), trigger: 'blur' }],
  planEndTime: [{ required: true, message: t('ProductionPlan.TaskSummary.validatorPlanFormPlanEndTimeRequired'), 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,
    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.productName = row.productName
  newRow.priorityNum = 1
  formData.value.push(newRow)
}
</script>
