import type { RouteRecordRaw } from 'vue-router'
import Layout from '@/layout/index.vue'
import { ModuleKey } from '@/enums/ModuleEnum'

/**
 * 销售模块路由（迁自 sp-sales-management）
 *
 * 路径策略：保留原 sp-sales-management 的所有路径（/clue/add、/customer/...、/index、/email 等）
 * 不加 /sales 前缀，避免改写 245 个 sales view 文件内部硬编码的 router.push / <router-link>。
 * 模块归属完全由 meta.module=sales 区分（顶部 ModuleTabs + Sidebar 的过滤都基于 module）。
 *
 * 与 OTR 的隔离：OTR 用户中心走 /otr/system/*，销售个人设置走 /system/updatePwd /user/profile（路径不冲突）。
 *
 * 路由 name 全部加 Sales_ 前缀，避免与 OTR 模块的 name 冲突。
 */
const salesMeta = { module: ModuleKey.SALES }

const salesRoutes: RouteRecordRaw[] = [
    // ============ 业务详情/编辑页（Layout 包装，hidden）============
    {
        path: '',
        component: Layout,
        // 不能用空 path 当 redirect，避免与新项目 / 重定向冲突；这里仅作为 Layout 包装
        meta: { ...salesMeta },
        children: [
            {
                path: '/index',
                component: () => import('@/modules/sales/views/index.vue'),
                name: 'Sales_Index',
                meta: { ...salesMeta, title: '销售首页', icon: 'dashboard', affix: true },
            },
            {
                path: '/clue/add',
                component: () => import('@/modules/sales/views/clue/add.vue'),
                name: 'Sales_AddClue',
                hidden: true,
                meta: { ...salesMeta, title: '新增线索', icon: '' },
            },
            {
                path: '/customer/addCustomer',
                component: () => import('@/modules/sales/views/customer/addCustomer/index.vue'),
                name: 'Sales_AddCustomer',
                hidden: true,
                meta: { ...salesMeta, title: '创建客户', noCache: false },
            },
            {
                path: '/customer/liaison/add',
                component: () => import('@/modules/sales/views/customer/liaison/add.vue'),
                name: 'Sales_AddLiaison',
                hidden: true,
                meta: { ...salesMeta, title: '添加联系人', noCache: false },
            },
            {
                path: '/order/add',
                component: () => import('@/modules/sales/views/orderForm/add.vue'),
                name: 'Sales_AddOrderForm',
                hidden: true,
                meta: { ...salesMeta, title: '添加订单', noCache: false },
            },
            {
                path: '/layout/notice',
                component: () => import('@/layout/components/notice/index.vue'),
                name: 'Sales_LayoutNotice',
                hidden: true,
                meta: { ...salesMeta, title: '内部消息' },
            },
            {
                path: '/target/targetScheme/add',
                component: () => import('@/modules/sales/views/target/targetScheme/add.vue'),
                name: 'Sales_AddTargetScheme',
                hidden: true,
                meta: { ...salesMeta, title: '目标方案创建', noCache: false },
            },
            {
                path: '/refundRecord/detail',
                component: () => import('@/modules/sales/views/finance/refundRecord/detailRefund.vue'),
                name: 'Sales_RefundDetail',
                hidden: true,
                meta: { ...salesMeta, title: '退款详情' },
            },
            {
                path: '/customer/followPanel',
                component: () => import('@/modules/sales/views/customer/followPanel/index.vue'),
                name: 'Sales_FollowPanel',
                hidden: true,
                meta: { ...salesMeta, title: '跟进面板' },
            },
        ],
    },

    // ============ 个人设置（Layout 包装，hidden）============
    {
        path: '/user',
        component: Layout,
        hidden: true,
        redirect: 'noredirect',
        meta: { ...salesMeta },
        children: [
            {
                path: '/system/updatePwd',
                component: () => import('@/modules/sales/views/index.vue'),
                name: 'Sales_UpdatePwd',
                meta: { ...salesMeta, title: '更新密码', icon: 'dashboard' },
            },
            {
                path: '/user/profile',
                component: () => import('@/modules/sales/views/system/user/profile/index.vue'),
                name: 'Sales_Profile',
                meta: { ...salesMeta, title: '个人中心', icon: 'user' },
            },
        ],
    },

    // ============ 邮件（独立全屏，无 Layout 包装）============
    {
        path: '/email',
        component: () => import('@/modules/sales/views/email/index.vue'),
        name: 'Sales_Email',
        hidden: true,
        meta: { ...salesMeta, title: '邮件' },
    },
    {
        path: '/email/send',
        component: () => import('@/modules/sales/views/email/send.vue'),
        name: 'Sales_EmailSend',
        hidden: true,
        meta: { ...salesMeta, title: '写邮件' },
    },
    {
        path: '/email/settings',
        component: () => import('@/modules/sales/views/email/settings.vue'),
        name: 'Sales_EmailSettings',
        hidden: true,
        meta: { ...salesMeta, title: '邮件设置' },
    },
    {
        path: '/email/preview',
        component: () => import('@/modules/sales/views/email/preview.vue'),
        name: 'Sales_EmailPreview',
        hidden: true,
        meta: { ...salesMeta, title: '邮件预览' },
    },
    {
        path: '/customer/followPanel/detail',
        component: () => import('@/modules/sales/views/customer/followPanel/detail.vue'),
        name: 'Sales_FollowPanelDetail',
        hidden: true,
        meta: { ...salesMeta, title: '跟进详情' },
    },
]

export default salesRoutes
