<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle">
    <el-form
      ref="formRef"
      v-loading="formLoading"
      :model="formData"
      :rules="formRules"
      label-width="auto"
      class="system-menu-form"
    >
      <el-form-item :label="t('SystemManagement.Menu.parent')">
        <el-tree-select
          v-model="formData.parentId"
          :data="menuTree"
          :default-expanded-keys="[0]"
          :props="defaultProps"
          check-strictly
          node-key="id"
        />
      </el-form-item>
      <el-form-item :label="t('SystemManagement.Menu.name')" prop="name">
        <el-input v-model="formData.name" clearable :placeholder="t('SystemManagement.Menu.namePlaceholder')" />
      </el-form-item>
      <el-form-item :label="t('SystemManagement.Menu.enName')">
        <el-input v-model="formData.enName" clearable :placeholder="t('SystemManagement.Menu.enNamePlaceholder')" />
      </el-form-item>
      <el-form-item :label="t('SystemManagement.Menu.type')" prop="type">
        <el-radio-group v-model="formData.type">
          <el-radio-button
            v-for="dict in getIntDictOptions(DICT_TYPE.SYSTEM_MENU_TYPE)"
            :key="dict.label"
            :value="dict.value"
          >
            {{ dict.label }}
          </el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="formData.type !== 3" :label="t('SystemManagement.Menu.icon')">
        <IconSelect v-if="clientType === 1" v-model="formData.icon" clearable />
        <AppIconSelect v-else v-model="formData.icon" clearable />
      </el-form-item>
      <el-form-item v-if="formData.type !== 3" :label="t('SystemManagement.Menu.path')" prop="path">
        <template #label>
          <Tooltip
            :message="t('SystemManagement.Menu.pathTooltipMessage')"
            :title="t('SystemManagement.Menu.pathTooltipTitle')"
          />
        </template>
        <el-input v-model="formData.path" clearable :placeholder="t('SystemManagement.Menu.pathPlaceholder')" />
      </el-form-item>
      <el-form-item v-if="formData.type === 2" :label="t('SystemManagement.Menu.component')" prop="component">
        <el-input v-model="formData.component" clearable :placeholder="t('SystemManagement.Menu.componentPlaceholder')" />
      </el-form-item>
      <el-form-item v-if="formData.type === 2" :label="t('SystemManagement.Menu.componentName')" prop="componentName">
        <el-input v-model="formData.componentName" clearable :placeholder="t('SystemManagement.Menu.componentNamePlaceholder')" />
      </el-form-item>
      <el-form-item v-if="formData.type !== 1" :label="t('SystemManagement.Menu.permission')" prop="permission">
        <template #label>
          <Tooltip
            :message="t('SystemManagement.Menu.permissionTooltipMessage')"
            :title="t('SystemManagement.Menu.permissionTooltipTitle')"
          />
        </template>
        <el-input v-model="formData.permission" clearable :placeholder="t('SystemManagement.Menu.permissionPlaceholder')" />
      </el-form-item>
      <el-form-item :label="t('SystemManagement.Menu.sort')" prop="sort">
        <el-input-number v-model="formData.sort" :min="0" clearable controls-position="right" />
      </el-form-item>
      <el-form-item :label="t('SystemManagement.Menu.status')" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio
            v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)"
            :key="dict.label"
            :value="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="formData.type !== 3" :label="t('SystemManagement.Menu.visible')" prop="visible">
        <template #label>
          <Tooltip :message="t('SystemManagement.Menu.visibleTooltipMessage')" :title="t('SystemManagement.Menu.visibleTooltipTitle')" />
        </template>
        <el-radio-group v-model="formData.visible">
          <el-radio key="true" :value="true" border>{{ t('SystemManagement.Menu.visibleShow') }}</el-radio>
          <el-radio key="false" :value="false" border>{{ t('SystemManagement.Menu.visibleHide') }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="formData.type !== 3" :label="t('SystemManagement.Menu.alwaysShow')" prop="alwaysShow">
        <template #label>
          <Tooltip
            :message="t('SystemManagement.Menu.alwaysShowTooltipMessage')"
            :title="t('SystemManagement.Menu.alwaysShowTooltipTitle')"
          />
        </template>
        <el-radio-group v-model="formData.alwaysShow">
          <el-radio key="true" :value="true" border>{{ t('SystemManagement.Menu.always') }}</el-radio>
          <el-radio key="false" :value="false" border>{{ t('SystemManagement.Menu.notAlways') }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="formData.type === 2" :label="t('SystemManagement.Menu.keepAlive')" prop="keepAlive">
        <template #label>
          <Tooltip
            :message="t('SystemManagement.Menu.keepAliveTooltipMessage')"
            :title="t('SystemManagement.Menu.keepAliveTooltipTitle')"
          />
        </template>
        <el-radio-group v-model="formData.keepAlive">
          <el-radio key="true" :value="true" border>{{ t('SystemManagement.Menu.keep') }}</el-radio>
          <el-radio key="false" :value="false" border>{{ t('SystemManagement.Menu.notKeep') }}</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button :disabled="formLoading" type="primary" @click="submitForm">{{ t('common.ok') }}</el-button>
      <el-button @click="dialogVisible = false">{{ t('common.cancel') }}</el-button>
    </template>
  </Dialog>
</template>
<script lang="ts" setup>
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import * as MenuApi from '@/api/system/menu'
import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
import { CommonStatusEnum, SystemMenuTypeEnum } from '@/utils/constants'
import { defaultProps, handleTree } from '@/utils/tree'
import AppIconSelect from '@/components/Icon/src/AppIconSelect.vue'

defineOptions({ name: 'SystemMenuForm' })

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

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const clientType = ref(1) // 客户端类型：1=Web，2=APP
const formData = ref({
  id: undefined,
  name: '',
  enName: '',
  permission: '',
  type: SystemMenuTypeEnum.DIR,
  sort: Number(undefined),
  parentId: 0,
  path: '',
  icon: '',
  component: '',
  componentName: '',
  status: CommonStatusEnum.ENABLE,
  visible: true,
  keepAlive: true,
  alwaysShow: true,
  clientType: 1
})
const formRules = computed(() => ({
  name: [{ required: true, message: t('SystemManagement.Menu.nameRequired'), trigger: 'blur' }],
  sort: [{ required: true, message: t('SystemManagement.Menu.sortRequired'), trigger: 'blur' }],
  path: [{ required: clientType.value === 1, message: t('SystemManagement.Menu.pathRequired'), trigger: 'blur' }],
  status: [{ required: true, message: t('SystemManagement.Menu.statusRequired'), trigger: 'blur' }]
}))
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number, parentId?: number, currentClientType?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  clientType.value = currentClientType ?? 1
  resetForm()
  if (parentId) {
    formData.value.parentId = parentId
  }
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await MenuApi.getMenu(id)
    } finally {
      formLoading.value = false
    }
  }
  // 获得菜单列表
  await getTree()
}
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 {
    if (
      formData.value.type === SystemMenuTypeEnum.DIR ||
      formData.value.type === SystemMenuTypeEnum.MENU
    ) {
      if (!isExternal(formData.value.path)) {
        if (formData.value.parentId === 0 && formData.value.path.charAt(0) !== '/') {
          message.error(t('SystemManagement.Menu.pathMustStartWithSlash'))
          return
        } else if (formData.value.parentId !== 0 && formData.value.path.charAt(0) === '/') {
          message.error(t('SystemManagement.Menu.pathMustNotStartWithSlash'))
          return
        }
      }
    }
    const { terminalType: _terminalType, ...data } = formData.value as typeof formData.value & { terminalType?: number }
    data.clientType = clientType.value
    if (formType.value === 'create') {
      await MenuApi.createMenu(data as unknown as MenuApi.MenuVO)
      message.success(t('common.createSuccess'))
    } else {
      await MenuApi.updateMenu(data as unknown as MenuApi.MenuVO)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
    // 清空，从而触发刷新
    wsCache.delete(CACHE_KEY.ROLE_ROUTERS)
  }
}

/** 获取下拉框[上级菜单]的数据  */
const menuTree = ref<Tree[]>([]) // 树形结构
const getTree = async () => {
  menuTree.value = []
  const res = await MenuApi.getSimpleMenusList(clientType.value)
  let menu: Tree = { id: 0, name: t('SystemManagement.Menu.parentRoot'), children: [] }
  menu.children = handleTree(res)
  menuTree.value.push(menu)
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    name: '',
    enName: '',
    permission: '',
    type: SystemMenuTypeEnum.DIR,
    sort: Number(undefined),
    parentId: 0,
    path: '',
    icon: '',
    component: '',
    componentName: '',
    status: CommonStatusEnum.ENABLE,
    visible: true,
    keepAlive: true,
    alwaysShow: true,
    clientType: 1
  }
  formRef.value?.resetFields()
}

/** 判断 path 是不是外部的 HTTP 等链接 */
const isExternal = (path: string) => {
  return /^(https?:|mailto:|tel:)/.test(path)
}
</script>
