<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle">
    <el-form ref="formRef" v-loading="formLoading" :model="formData" :rules="formRules" label-width="100px">
      <el-form-item :label="t('system.parentMenu')">
        <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('system.menuName')" prop="name">
        <el-input v-model="formData.name" clearable :placeholder="t('system.pleaseEnterMenuName')" />
      </el-form-item>
      <el-form-item :label="t('system.menuNameEn')" prop="nameEn">
        <el-input v-model="formData.nameEn" clearable :placeholder="t('system.pleaseEnterMenuName')" />
      </el-form-item>
      <el-form-item :label="t('system.menuNameJp')" prop="nameJp">
        <el-input v-model="formData.nameJp" clearable :placeholder="t('system.pleaseEnterMenuName')" />
      </el-form-item>
      <el-form-item :label="t('system.menuType')" 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" :label="dict.value">
            {{ dict.label }}
          </el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="formData.type !== 3" :label="t('system.menuIcon')">
        <IconSelect v-model="formData.icon" clearable />
      </el-form-item>
      <el-form-item v-if="formData.type !== 3" :label="t('system.routeAddress')" prop="path">
        <template #label>
          <Tooltip message="t('system.cache5')" title="t('system.routeAddress')" />
        </template>
        <el-input v-model="formData.path" clearable :placeholder="t('system.pleaseEnterRouteAddress')" />
      </el-form-item>
      <el-form-item v-if="formData.type === 2" :label="t('system.componentAddress')" prop="component">
        <el-input v-model="formData.component" clearable :placeholder="t('system.cache6')" />
      </el-form-item>
      <el-form-item v-if="formData.type === 2" :label="t('system.componentName')" prop="componentName">
        <el-input v-model="formData.componentName" clearable :placeholder="t('system.cache6')" />
      </el-form-item>
      <el-form-item v-if="formData.type !== 1" :label="t('system.permissionIdentity')" prop="permission">
        <template #label>
          <Tooltip message="t('system.cache4')" :title="t('system.permissionIdentity')" />
        </template>
        <el-input v-model="formData.permission" clearable :placeholder="t('system.pleaseEnterPermissionIdentity')" />
      </el-form-item>
      <el-form-item :label="t('system.displaySort')" prop="sort">
        <el-input-number v-model="formData.sort" :min="0" clearable controls-position="right" />
      </el-form-item>
      <el-form-item :label="t('system.menuStatus')" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)" :key="dict.label" :label="dict.value">
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="formData.type !== 3" :label="t('system.displayStatus')" prop="visible">
        <template #label>
          <Tooltip :message="t('system.cache3')" />
        </template>
        <el-radio-group v-model="formData.visible">
          <el-radio :value="'0'">{{ t('mall.display') }}</el-radio>
          <el-radio :value="'1'">{{ t('mall.hide') }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="formData.type !== 3" :label="t('system.alwaysDisplay')" prop="alwaysShow">
        <template #label>
          <Tooltip :message="t('system.cache2')" :title="t('system.alwaysDisplay')" />
        </template>
        <el-radio-group v-model="formData.alwaysShow">
          <el-radio :value="'0'">{{ t('system.always') }}</el-radio>
          <el-radio :value="'1'">{{ t('system.no1') }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="formData.type === 2" :label="t('system.cacheStatus')" prop="keepAlive">
        <template #label>
          <Tooltip :message="t('system.cache1')" :title="t('system.cacheStatus')" />
        </template>
        <el-radio-group v-model="formData.keepAlive">
          <el-radio :value="'0'">{{ t('system.cache') }}</el-radio>
          <el-radio :value="'1'">{{ t('system.noCache') }}</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button :disabled="formLoading" type="primary" @click="submitForm">{{ t('common.confirm') }}</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'

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 formData = ref({
  id: undefined,
  name: '',
  nameEn: '',
  nameJp: '',
  permission: '',
  type: SystemMenuTypeEnum.DIR,
  sort: Number(undefined),
  parentId: 0,
  path: '',
  icon: '',
  component: '',
  componentName: '',
  status: CommonStatusEnum.ENABLE,
  visible: true,
  keepAlive: true,
  alwaysShow: true
})
const formRules = reactive({
  name: [{ required: true, message: t('system.menuNameCannotBeEmpty'), trigger: 'blur' }],
  sort: [{ required: true, message: t('system.menuOrderCannotBeEmpty'), trigger: 'blur' }],
  path: [{ required: true, message: t('system.routeAddressCannotBeEmpty'), trigger: 'blur' }],
  status: [{ required: true, message: t('system.statusCannotBeEmpty'), trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number, parentId?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  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('system.thePathMustStartWith'))
          return
        } else if (formData.value.parentId !== 0 && formData.value.path.charAt(0) === '/') {
          message.error(t('system.thePathCannotStartWith'))
          return
        }
      }
    }
    const data = formData.value as unknown as MenuApi.MenuVO
    if (formType.value === 'create') {
      await MenuApi.createMenu(data)
      message.success(t('common.createSuccess'))
    } else {
      await MenuApi.updateMenu(data)
      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()
  let menu: Tree = { id: 0, name: t('system.mainCategory'), children: [] }
  menu.children = handleTree(res)
  menuTree.value.push(menu)
}

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

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