<script setup lang="ts">
import { computed, reactive, watch } from 'vue';
import BusinessFormDialog from '@/components/custom/business-form-dialog.vue';

defineOptions({ name: 'ProjectDeleteDialog' });

interface Props {
  projectId: string;
  projectName: string;
}

interface Emits {
  (e: 'submit', payload: Api.Project.DeleteProjectParams): void;
}

const props = defineProps<Props>();
const emit = defineEmits<Emits>();

const visible = defineModel<boolean>('visible', {
  default: false
});

const model = reactive({
  confirmName: '',
  confirmText: '',
  reason: ''
});

const confirmDisabled = computed(() => {
  return (
    !model.reason.trim() || model.confirmName.trim() !== props.projectName || model.confirmText.trim() !== 'DELETE'
  );
});

function handleConfirm() {
  emit('submit', {
    id: props.projectId,
    projectName: model.confirmName.trim(),
    confirmText: model.confirmText.trim(),
    reason: model.reason.trim()
  });
}

watch(
  () => visible.value,
  value => {
    if (!value) {
      return;
    }

    model.confirmName = '';
    model.confirmText = '';
    model.reason = '';
  }
);
</script>

<template>
  <BusinessFormDialog
    v-model="visible"
    title="删除项目"
    preset="sm"
    :confirm-disabled="confirmDisabled"
    confirm-text="确认删除"
    @confirm="handleConfirm"
  >
    <ElAlert
      :title="`请输入当前项目名称 ${projectName || '--'} 和确认口令 DELETE，删除后将退出当前对象上下文。`"
      type="error"
      :closable="false"
      class="mb-16px"
    />
    <ElForm label-position="top">
      <ElFormItem label="删除确认名称">
        <ElInput v-model="model.confirmName" placeholder="请输入当前项目名称" />
      </ElFormItem>
      <ElFormItem label="确认口令">
        <ElInput v-model="model.confirmText" placeholder="请输入 DELETE" />
      </ElFormItem>
      <ElFormItem label="删除原因">
        <ElInput
          v-model="model.reason"
          type="textarea"
          :rows="4"
          maxlength="500"
          show-word-limit
          placeholder="请输入删除原因"
        />
      </ElFormItem>
    </ElForm>
  </BusinessFormDialog>
</template>
