<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible"  width="1400px" v-loading="formLoading">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="120px"
    >
      <el-row>
        <el-col :span="6">
          <el-form-item :label="t('ProductionPlan.Task.planDialogTaskCodeLabel')" prop="taskCode">
            <el-input disabled v-model="formData.taskCode" :placeholder="t('ProductionPlan.Task.planDialogTaskCodePlaceholder')"/>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item :label="t('ProductionPlan.Task.planDialogNumberLabel')" prop="number">
            <el-input disabled v-model="formData.number" :placeholder="t('ProductionPlan.Task.planDialogNumberPlaceholder')"/>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item :label="t('ProductionPlan.Task.planDialogPlanNumberLabel')" prop="planNumber">
            <el-input v-model="formData.planNumber" :placeholder="t('ProductionPlan.Task.planDialogPlanNumberPlaceholder')"/>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="6">
          <el-form-item :label="t('ProductionPlan.Task.planDialogProductsOfPlanLabel')" prop="productsOfPlan">
            <el-input-number v-model="formData.productsOfPlan" :min="0" class="!w-1/1"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="t('ProductionPlan.Task.planDialogPlanDateLabel')" prop="planDate">
            <el-date-picker
              v-model="formData.planDate"
              value-format="YYYY-MM-DD HH:mm:ss"
              type="dates"
              :placeholder="t('ProductionPlan.Task.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.Task.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.Task.planDialogResetButtonText') }}
            </el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <!-- 子表的表单 -->
    <el-tabs v-model="subTabsName">
      <el-tab-pane :label="t('ProductionPlan.Task.planDialogTabPlanLabel')" name="plan">
        <PlanForm ref="planFormRef" :task-id="formData.id"/>
      </el-tab-pane>
      <el-tab-pane :label="t('ProductionPlan.Task.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.Task.planDialogSubmitButtonText') }}</el-button>
      <el-button @click="dialogVisible = false">{{ t('ProductionPlan.Task.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";
/** 生产任务单 表单 */
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,
  taskDetailId: undefined,
  taskCode: undefined,
  sizeOfPlan: 0,
  productsOfPlan: 20000,
  planDate: [],
  number: undefined,

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

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

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

/** 打开弹窗 */
const open = async (taskId: number, taskDetailId: number, productId: number, productName:string, num:number,finishNumber:number) => {
  dialogVisible.value = true
  dialogTitle.value = t('ProductionPlan.Task.planDialogTitlePrefix') + productName
  formData.value.taskId = taskId
  formData.value.taskDetailId = taskDetailId
  formData.value.taskCode = productName
  formData.value.number = num
  formData.value.planNumber = 0
  if(num>-1 && finishNumber>-1){
    formData.value.planNumber = num-finishNumber
  }
}
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.Task.planDialogEmptyPlanMessage'))
  } finally {
    formLoading.value = false
  }
}

/** 排产 */
const handleQuery = async () => {
  // 提交请求
  formLoading.value = true
  try {
    const planData = await TaskApi.generatePlanByTaskDetail(formData.value)
    planFormRef.value.setData(planData)

  } finally {
    formLoading.value = false
  }
}

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