<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible"  width="1300px" v-loading="formLoading">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="120px"
    >
      <el-row :gutter="20" v-show="false">
        <el-col :span="6">
          <el-form-item :label="t('ProductionPlan.TaskSummary.planDialogTaskIdLabel')" prop="taskId">
            <el-input readonly v-model="formData.taskId" :placeholder="t('ProductionPlan.TaskSummary.searchCodePlaceholder')"/>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item :label="t('ProductionPlan.TaskSummary.planDialogTaskCodeLabel')" prop="taskCode">
            <el-input readonly v-model="formData.taskCode" :placeholder="t('ProductionPlan.TaskSummary.searchCodePlaceholder')"/>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="6">
          <el-form-item :label="t('ProductionPlan.TaskSummary.planDialogProductsOfPlanLabel')" prop="productsOfPlan">
            <el-input-number v-model="formData.productsOfPlan" :min="0" class="!w-1/1"/>
          </el-form-item>
        </el-col>
<!--        <el-col :span="6">-->
<!--          <el-form-item label="计划个数" prop="sizeOfPlan">-->
<!--            <el-input-number v-model="formData.sizeOfPlan" :min="0"  class="!w-1/1"/>-->
<!--          </el-form-item>-->
<!--        </el-col>-->
        <el-col :span="8">
          <el-form-item :label="t('ProductionPlan.TaskSummary.planDialogPlanDateLabel')" prop="planDate">
            <el-date-picker
              v-model="formData.planDate"
              value-format="YYYY-MM-DD HH:mm:ss"
              type="dates"
              :placeholder="t('ProductionPlan.TaskSummary.planDialogPlanDatePlaceholder')"
              class="!w-240px"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <el-form-item>
            <el-button type="success" @click="handleQuery">
              <Icon icon="ep:calendar" class="mr-5px"/>
              {{ t('ProductionPlan.TaskSummary.planDialogAutoButtonText') }}
            </el-button>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item>
            <el-button type="danger" @click="resetQuery">
              <Icon icon="ep:delete" class="mr-5px"/>
              {{ t('ProductionPlan.TaskSummary.planDialogResetButtonText') }}
            </el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <!-- 子表的表单 -->
    <el-tabs v-model="subTabsName">
      <el-tab-pane :label="t('ProductionPlan.TaskSummary.planDialogTabPlanLabel')" name="plan">
        <PlanForm ref="planFormRef" :task-id="formData.id"/>
      </el-tab-pane>
      <el-tab-pane :label="t('ProductionPlan.TaskSummary.planDialogTabFinishPlanLabel')" name="finishPlan">
        <el-text>hello</el-text>
      </el-tab-pane>
    </el-tabs>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">{{ t('ProductionPlan.TaskSummary.planDialogSubmitButtonText') }}</el-button>
      <el-button @click="dialogVisible = false">{{ t('ProductionPlan.TaskSummary.planDialogCancelButtonText') }}</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import {TaskApi, TaskVO} from '@/api/mes/task'
import PlanForm from './PlanForm.vue'
import {PlanApi, PlanVO} from "@/api/mes/plan";
import {BomVO} from "@/api/mes/bom";
/** 生产任务单 表单 */
defineOptions({name: 'TaskPlanForm'})

const {t} = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用

const formData = ref({
  taskId: undefined,
  taskCode: undefined,
  sizeOfPlan: 0,
  productsOfPlan: 20000,
  planDate: []

})
const formRules = reactive({
  taskId: [{required: true, message: t('ProductionPlan.TaskSummary.validatorPlanTaskIdRequired'), trigger: 'blur'}],
  productsOfPlan: [{required: true, message: t('ProductionPlan.TaskSummary.validatorPlanProductsOfPlanRequired'), trigger: 'blur'}],

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

/** 子表的表单 */
const subTabsName = ref('plan')
const planFormRef = ref()

/** 打开弹窗 */
const open = async (taskCode: string, taskId: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('ProductionPlan.TaskSummary.planDialogTitlePrefix') + taskCode

  formData.value.taskId = taskId
  formData.value.taskCode = taskCode

}
defineExpose({open}) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 校验子表单
  try {
    await planFormRef.value.validate()
  } catch (e) {
    subTabsName.value = 'plan'
    return
  }
  // 提交请求
  formLoading.value = true

  try {

    // 提交子表的数据
    let planList = planFormRef.value.getData()
    if(planList!=undefined && planList.length > 0) {
      await PlanApi.saveBatchPlan(planList)
      dialogVisible.value = false
      // 发送操作成功的事件
      emit('success')
    }
    else message.alert(t('ProductionPlan.TaskSummary.planDialogEmptyPlanMessage'))
  } finally {
    formLoading.value = false
  }
}

/** 排产 */
const handleQuery = async () => {
  // 提交请求
  formLoading.value = true
  try {
    //const data = formData.value as unknown as TaskVO

    const planData = await TaskApi.generatePlanByTask(formData.value)
    planFormRef.value.setData(planData)

  } finally {
    formLoading.value = false
  }
}

/** 清空列表 */
const resetQuery = () => {
  planFormRef.value.setData(null)
}
</script>
