<template>
  <Dialog v-model="dialogVisible" title="新建任务" width="600" class="task-dialog">
    <el-form :model="formData" label-width="auto" ref="formRef" :rules="formRules">
      <el-form-item label="取货类型" prop="name1" required>
        <el-select v-model="formData.name1" placeholder="请选择取货类型" required>
          <el-option label="库位" :value="1" />
          <el-option label="线库" :value="2" />
          <el-option label="区域" :value="3" />
        </el-select>
      </el-form-item>
      <el-form-item label="取货位置" prop="name2" required>
        <el-input v-model="formData.name2" placeholder="请输入取货位置" />
      </el-form-item>
      <el-form-item label="放货类型" prop="name3" required>
        <el-select v-model="formData.name3" placeholder="请选择放货类型">
          <el-option label="库位" :value="1" />
          <el-option label="线库" :value="2" />
          <el-option label="区域" :value="3" />
        </el-select>
      </el-form-item>
      <el-form-item label="放货位置" prop="name4" required>
        <el-input v-model="formData.name4" placeholder="请输入放货位置" />
      </el-form-item>
      <el-form-item label="物料信息" prop="name5">
        <el-input v-model="formData.name5" placeholder="请输入物料信息" />
      </el-form-item>
      <el-form-item label="指定车辆" prop="name6">
        <el-select v-model="formData.name6" placeholder="请选择指定车辆">
          <el-option label="AGV01" :value="1" />
          <el-option label="AGV02" :value="2" />
          <el-option label="AGV03" :value="3" />
        </el-select>
      </el-form-item>
      <el-form-item label="优先级" prop="name7">
        <el-input v-model="formData.name7" placeholder="请输入优先级" />
      </el-form-item>
      <el-form-item label="其他信息" prop="name8">
        <el-input v-model="formData.name8" rows="3" type="textarea" placeholder="请输入其他信息" />
      </el-form-item>
      <div class="task-tips">
        <el-text class="mx-1">若需要发起拼接任务，请点击前往</el-text>
        <el-link type="primary" @click="taskManagement()">任务管理</el-link>
      </div>
    </el-form>

    <template #footer>
      <el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script lang="ts" setup>
const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

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

const formData = ref({
  name1: '',
  name2: '',
  name3: '',
  name4: '',
  name5: '',
  name6: '',
  name7: '',
  name8: ''
})
const formRules = reactive({
  name1: [{ required: true, message: '取货类型不能为空', trigger: 'blur' }],
  name2: [{ required: true, message: '取货位置不能为空', trigger: 'blur' }],
  name3: [{ required: true, message: '放货类型不能为空', trigger: 'blur' }],
  name4: [{ required: true, message: '放货位置不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async () => {
  dialogVisible.value = true
  resetForm()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

//前往任务管理页面
const { push } = useRouter()
const taskManagement = () => {
  push({ name: 'taskManagementCreateTask' })
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    name1: '',
    name2: '',
    name3: '',
    name4: '',
    name5: '',
    name6: '',
    name7: '',
    name8: ''
  }
  formRef.value?.resetFields()
}
</script>

<style lang="scss">
.task-dialog {
  .el-dialog__header {
    border-bottom: none;
  }

  .el-dialog__footer {
    border-top: none !important;
  }
}

.task-tips {
  display: flex;
  align-items: center;
  justify-content: flex-end;
}
</style>
