import Vue from 'vue'
import Router from 'vue-router'
import store from '../store'

Vue.use(Router)

const originalPush = Router.prototype.push
Router.prototype.push = function push (location) {
  return originalPush.call(this, location).catch((err) => err)
}
const router = new Router({
  routes: [
    { // 404
      path: '*',
      name: '404',
      component: () => import('@/content/404')
    },
    { // 新增專案
      path: '/addNewProject',
      name: 'AddNewProject',
      component: () => import('@/content/AddNewProject')
    },
    { // 新增標案
      path: '/addNewTender',
      name: 'AddNewTender',
      component: () => import('@/content/AddNewTender')
    },
    { // 新增專案2.0
      path: '/addNewMultiProject',
      name: 'AddNewMultiProject',
      component: () => import('@/content/AddNewMultiProject')
    },
    { // 多專案選擇畫面
      path: '/multiProjects',
      name: 'MultiProjects',
      component: () => import('@/content/MultiProjects')
    },
    { // 專案管理
      path: '/projectManagement',
      name: 'ProjectManagement',
      component: () => import('@/content/ProjectManagement')
    },
    { // 使用者管理
      path: '/userManagement',
      name: 'UserManagement',
      component: () => import('@/content/UserManagement'),
      children: [
        { // 使用者列表
          path: 'userList',
          name: 'UserList',
          component: () => import('@/content/UserManagement/List')
        },
        { // 使用者新增、編輯
          path: 'edit',
          name: 'EditUser',
          component: () => import('@/content/UserManagement/Edit')
        }
      ],
      redirect: { name: 'UserList' }
    },
    { // 首頁
      path: '/',
      name: 'Homepage',
      component: () => import('@/content/Homepage'),
      children: [
        { // 主頁
          path: 'mainpage',
          name: 'Mainpage',
          component: () => import('@/content/Homepage/Mainpage'),
          meta: {
            title: '首頁',
            isOnNavbar: true
          },
          redirect: { name: 'Dashboard' },
          children: [
            {
              path: 'dashboard',
              name: 'Dashboard',
              component: () => import('@/content/Homepage/Mainpage/Dashboard'),
              children: [],
              meta: {
                title: '首頁',
                isOnSidebar: false
              }
            }
          ]
        },
        { // 基本資料
          path: 'basicSetting',
          name: 'BasicSetting',
          component: () => import('@/content/Homepage/BasicSetting'),
          children: [
            { // 專案資料
              path: 'siteSetting',
              name: 'SiteSetting',
              component: () => import('@/content/Homepage/BasicSetting/SiteSetting'),
              children:
                [
                  {
                    path: 'edit',
                    name: 'SiteSettingEdit',
                    component: () => import('@/content/Homepage/BasicSetting/SiteSetting/Edit')
                  }
                ],
              meta: {
                title: '專案資料',
                isOnSidebar: true
              },
              redirect: { name: 'SiteSettingEdit' }
            },
            { // 人員資料
              path: 'staffInformation',
              name: 'StaffInformation',
              component: () => import('@/content/Homepage/BasicSetting/StaffInformation'),
              children:
                [
                  {// 人員資料列表
                    path: 'list',
                    name: 'StaffInformationList',
                    component: () => import('@/content/Homepage/BasicSetting/StaffInformation/List')
                  },
                  {// 人員設定
                    path: 'staffInformationSetting',
                    name: 'StaffInformationSetting',
                    component: () => import('@/content/Homepage/BasicSetting/StaffInformation/StaffInformationSetting'),
                    meta: {
                      loginRequest: true
                    }
                  }
                ],

              meta: {
                title: '人員資料',
                isOnSidebar: true
              },
              redirect: { name: 'StaffInformationList' }
            },
            { // 通訊錄
              path: 'directoryInformation',
              name: 'DirectoryInformation',
              component: () => import('@/content/Homepage/BasicSetting/DirectoryInformation'),
              children:
                [
                  {
                    path: 'list',
                    name: 'DirectoryInformationList',
                    component: () => import('@/content/Homepage/BasicSetting/DirectoryInformation/List')
                  }
                ],
              meta: {
                title: '通訊錄',
                isOnSidebar: true
              },
              redirect: { name: 'DirectoryInformationList' }
            },
            { // 工程現況
              path: 'projectStatus',
              name: 'ProjectStatus',
              component: () => import('@/content/Homepage/BasicSetting/ProjectStatus'),
              children:
                [
                  {
                    path: 'list',
                    name: 'ProjectStatusList',
                    component: () => import('@/content/Homepage/BasicSetting/ProjectStatus/List')
                  }
                ],
              meta: {
                title: '工程現況',
                isOnSidebar: true
              },
              redirect: { name: 'ProjectStatusList' }
            }
          ],
          meta: {
            title: '基本資料',
            isOnNavbar: true
          },
          redirect: { name: 'SiteSettingEdit' }
        },
        { // 公文管理
          path: 'documentManagement',
          name: 'DocumentManagement',
          component: () => import('@/content/Homepage/DocumentManagement'),
          meta: {
            title: '公文管理',
            isOnNavbar: true
          },
          redirect: { name: 'PaperPost' }, // 重定向到總代辦公文列表
          children: [
        { // 發文管理
              path: 'documentManagementPost',
              name: 'DocumentManagementPost',
              component: () => import('@/content/Homepage/DocumentManagement/Post'),
          children: [
            { // 發文作業
              path: 'post',
              name: 'PaperPost',
              component: () => import('@/content/Homepage/DocumentManagement/Post/Paper'),
              children: [
                { // 發文列表
                  path: 'List',
                  name: 'PaperPostList',
                  component: () => import('@/content/Homepage/DocumentManagement/Post/Paper/List')
                },
                { // 發文登記頁面
                  path: 'Create',
                  name: 'PaperPostCreate',
                  component: () => import('@/content/Homepage/DocumentManagement/Post/Paper/Create'),
                  meta: {
                    loginRequest: true
                  }
                },
                { // 檔管發文檢視頁面
                  path: 'archivesPostView',
                  name: 'ArchivesPostView',
                  component: () => import('@/content/Homepage/DocumentManagement/Archives/PostView'),
                  meta: {
                    loginRequest: true
                  }
                }
              ],
              meta: {
                title: '發文作業',
                isOnSidebar: true
              },
              redirect: { name: 'PaperPostList' }
            },
            { // 發文資料管理
              path: 'managePost',
              name: 'ManagePost',
              component: () => import('@/content/Homepage/DocumentManagement/Post/Manage'),
              children: [
                { // 發文資料管理列表
                  path: 'List',
                  name: 'ManagePostList',
                  component: () => import('@/content/Homepage/DocumentManagement/Post/Manage/List')
                }
              ],
              meta: {
                title: '發文資料管理',
                isOnSidebar: true
              },
              redirect: { name: 'ManagePostList' }
            }
          ],
          meta: {
            title: '發文管理',
            isOnSidebar: true
          },
          redirect: { name: 'PaperPostList' }
        },
        { // 收文管理
          path: 'documentManagementReceive',
          name: 'DocumentManagementReceive',
          component: () => import('@/content/Homepage/DocumentManagement/Receive'),
          children: [
            { // 收文作業
              path: 'receive',
              name: 'PaperReceive',
              component: () => import('@/content/Homepage/DocumentManagement/Receive/Paper'),
              children: [
                { // 收文列表
                  path: 'ReceiveList',
                  name: 'PaperReceiveList',
                  component: () => import('@/content/Homepage/DocumentManagement/Receive/Paper/List')
                },
                { // 新增收文登記頁面
                  path: 'create',
                  name: 'PaperReceiveCreate',
                  component: () => import('@/content/Homepage/DocumentManagement/Receive/Paper/Create'),
                  meta: {
                    loginRequest: true
                  }
                },
                { // 檔管收文檢視頁面
                  path: 'archivesReceiveView',
                  name: 'ArchivesReceiveView',
                  component: () => import('@/content/Homepage/DocumentManagement/Archives/ReceiveView'),
                  meta: {
                    loginRequest: true
                  }
                }
              ],
              meta: {
                title: '收文作業',
                isOnSidebar: true
              },
              redirect: { name: 'PaperReceiveList' }
            },
            {// 收文資料管理
              path: 'manageReceive',
              name: 'ManageReceive',
              component: () => import('@/content/Homepage/DocumentManagement/Receive/Manage'),
              children: [
                { // 收文資料管理列表
                  path: 'List',
                  name: 'ManageReceiveList',
                  component: () => import('@/content/Homepage/DocumentManagement/Receive/Manage/List')
                }
              ],
              meta: {
                title: '收文資料管理',
                isOnSidebar: true
              },
              redirect: { name: 'ManageReceiveList' }
            }
          ],
          meta: {
            title: '收文管理',
            isOnSidebar: true
          },
          redirect: { name: 'PaperReceiveList' }
        }
      ]
    },
        { // 工作協同
          path: 'workCollaboration',
          name: 'WorkCollaboration',
          component: () => import('@/content/Homepage/WorkCollaboration'),
          meta: {
            title: '工作協同',
            isOnNavbar: true
          },
          redirect: { name: 'ConferenceManagement' },
          children: [
            { // 會議管理
              path: 'conferenceManagement',
              name: 'ConferenceManagement',
              component: () => import('@/content/Homepage/WorkCollaboration/ConferenceManagement'),
              children: [
                {
                  path: 'conferenceOverview',
                  name: 'ConferenceOverview',
                  component: () => import('@/content/Homepage/WorkCollaboration/ConferenceManagement/ConferenceOverview'),
                  children: [
                    { // 會議項目總覽
                      path: 'Overview',
                      name: 'Overview',
                      component: () => import('@/content/Homepage/WorkCollaboration/ConferenceManagement/ConferenceOverview/Overview')
                    },
                    { // 會議項目詳情
                      path: 'Detail',
                      name: 'ConferenceDetail',
                      component: () => import('@/content/Homepage/WorkCollaboration/ConferenceManagement/ConferenceOverview/Detail')
                    }
                  ],
                  meta: {
                    title: '會議項目總覽',
                    isOnSidebar: true
                  },
                  redirect: { name: 'Overview' }
                },
                {
                  path: 'agendaOverview',
                  name: 'AgendaOverview',
                  component: () => import('@/content/Homepage/WorkCollaboration/ConferenceManagement/AgendaOverview'),
                  children: [
                    { // 議題總覽
                      path: 'Agenda',
                      name: 'Agenda',
                      component: () => import('@/content/Homepage/WorkCollaboration/ConferenceManagement/AgendaOverview/Agenda')
                    },
                    { // 議題檢視
                      path: 'View',
                      name: 'AgendaView',
                      component: () => import('@/content/Homepage/WorkCollaboration/ConferenceManagement/AgendaOverview/View')
                    },
                    { // 查閱議題
                      path: 'Consult',
                      name: 'ConsultAgenda',
                      component: () => import('@/content/Homepage/WorkCollaboration/ConferenceManagement/AgendaOverview/Consult')
                    }
                  ],
                  meta: {
                    title: '議題總覽',
                    isOnSidebar: true
                  },
                  redirect: { name: 'Agenda' }
                }
              ],
              meta: {
                title: '會議管理',
                isOnSidebar: true
              },
              redirect: { name: 'ConferenceOverview' }
            },
            { // 文件提審
              path: 'fileManagement',
              name: 'FileManagement',
              component: () => import('@/content/Homepage/WorkCollaboration/FileManagement'),
              children: [
                { // 審查文件
                  path: 'Review',
                  name: 'FileReview',
                  component: () => import('@/content/Homepage/WorkCollaboration/FileManagement/Review'),
                  children: [
                    { // 審查文件頁面
                      path: 'List',
                      name: 'FileReviewList',
                      component: () => import('@/content/Homepage/WorkCollaboration/FileManagement/Review/List')
                    },
                    { // 新增審查頁面
                      path: 'Create',
                      name: 'FileReviewCreate',
                      component: () => import('@/content/Homepage/WorkCollaboration/FileManagement/Review/Create')
                    },
                    { // 審查頁面
                      path: 'Edit',
                      name: 'FileReviewEdit',
                      component: () => import('@/content/Homepage/WorkCollaboration/FileManagement/Review/Edit')
                    },
                    { // 附件上傳
                      path: 'Upload',
                      name: 'FileReviewUpload',
                      component: () => import('@/content/Homepage/WorkCollaboration/FileManagement/Review/Upload')
                    }
                  ],
                  meta: {
                    title: '審查文件',
                    isOnSidebar: true
                  },
                  redirect: { name: 'FileReviewList' }
                },
                { // 提送文件
                  path: 'Submit',
                  name: 'FileSubmit',
                  component: () => import('@/content/Homepage/WorkCollaboration/FileManagement/Submit'),
                  children: [
                    { // 提送文件頁面
                      path: 'List',
                      name: 'FileSubmitList',
                      component: () => import('@/content/Homepage/WorkCollaboration/FileManagement/Submit/List')
                    },
                    { // 新增提送頁面
                      path: 'Create',
                      name: 'FileSubmitCreate',
                      component: () => import('@/content/Homepage/WorkCollaboration/FileManagement/Submit/Create')
                    },
                    { // 提送頁面
                      path: 'Edit',
                      name: 'FileSubmitEdit',
                      component: () => import('@/content/Homepage/WorkCollaboration/FileManagement/Submit/Edit')
                    },
                    { // 附件上傳
                      path: 'Upload',
                      name: 'FileSubmitUpload',
                      component: () => import('@/content/Homepage/WorkCollaboration/FileManagement/Submit/Upload')
                    }
                  ],
                  meta: {
                    title: '提送文件',
                    isOnSidebar: true
                  },
                  redirect: { name: 'FileSubmitList' }
                },
                { // 文件管控
                  path: 'Control',
                  name: 'FileControl',
                  component: () => import('@/content/Homepage/WorkCollaboration/FileManagement/Control'),
                  children: [
                    { // 新增提審
                      path: 'Deliver',
                      name: 'Deliver',
                      component: () => import('@/content/Homepage/WorkCollaboration/FileManagement/Control/Deliver')
                    },
                    { // 查閱提審
                      path: 'View',
                      name: 'DeliverView',
                      component: () => import('@/content/Homepage/WorkCollaboration/FileManagement/Control/View')
                    }
                  ],
                  meta: {
                    title: '文件管控',
                    isOnSidebar: false
                  },
                  redirect: { name: 'FileControl' }
                }
              ],
              meta: {
                title: '文件提審',
                isOnSidebar: true
              },
              redirect: { name: 'FileReviewList' }
            }
          ]
        },
        { // 文件管理
          path: '/documentCollaboration',
          name: 'DocumentCollaboration',
          component: () => import('@/content/Homepage/DocumentCollaboration'),
          meta: {
            title: '文件管理',
            isOnNavbar: true
          },
          redirect: { name: 'ContractManagement' },
          children: [
            { // 契約管理
              path: 'contractManagement',
              name: 'ContractManagement',
              component: () => import('@/content/Homepage/DocumentCollaboration/ContractManagement'),
              children: [
                { // 契約搜尋
                  path: 'contractSearch',
                  name: 'ContractSearch',
                  component: () => import('@/content/Homepage/DocumentCollaboration/ContractManagement/ContractSearch'),
                  children: [
                    { // 契約搜尋頁面
                      path: 'List',
                      name: 'ContractSearchList',
                      component: () => import('@/content/Homepage/DocumentCollaboration/ContractManagement/ContractSearch/List')
                    }
                  ],
                  meta: {
                    title: '契約搜尋',
                    isOnSidebar: true
                  },
                  redirect: { name: 'ContractSearchList' }
                },
                { // 標案基本資料
                  path: 'contractInformation',
                  name: 'ContractInformation',
                  component: () => import('@/content/Homepage/DocumentCollaboration/ContractManagement/ContractInformation'),
                  children: [
                    { // 標案基本資料頁面
                      path: 'List',
                      name: 'ContractInformationList',
                      component: () => import('@/content/Homepage/DocumentCollaboration/ContractManagement/ContractInformation/List')
                    }
                  ],
                  meta: {
                    title: '標案基本資料',
                    isOnSidebar: true
                  },
                  redirect: { name: 'ContractInformationList' }
                },
                { // 變更契約
                  path: 'contractChanging',
                  name: 'ContractChanging',
                  component: () => import('@/content/Homepage/DocumentCollaboration/ContractManagement/ContractChanging'),
                  children: [
                    { // 變更契約頁面
                      path: 'List',
                      name: 'ContractChangingList',
                      component: () => import('@/content/Homepage/DocumentCollaboration/ContractManagement/ContractChanging/List')
                    },
                    { // 編輯契約頁面
                      path: 'Edit',
                      name: 'ContractChangingEdit',
                      component: () => import('@/content/Homepage/DocumentCollaboration/ContractManagement/ContractChanging/Edit')
                    }
                  ],
                  meta: {
                    title: '變更契約',
                    isOnSidebar: true
                  },
                  redirect: { name: 'ContractChangingList' }
                },
                { // 工程里程碑
                  path: 'milestoneInformation',
                  name: 'MilestoneInformation',
                  component: () => import('@/content/Homepage/DocumentCollaboration/ContractManagement/MilestoneInformation'),
                  children: [
                    { // 工程里程碑頁面
                      path: 'List',
                      name: 'MilestoneInformationList',
                      component: () => import('@/content/Homepage/DocumentCollaboration/ContractManagement/MilestoneInformation/List')
                    }
                  ],
                  meta: {
                    title: '工程里程碑',
                    isOnSidebar: true
                  },
                  redirect: { name: 'MilestoneInformationList' }
                },
                { // 專案查核點
                  path: 'checkpointInformation',
                  name: 'CheckpointInformation',
                  component: () => import('@/content/Homepage/DocumentCollaboration/ContractManagement/CheckpointInformation'),
                  children: [
                    { // 專案查核點頁面
                      path: 'List',
                      name: 'CheckpointInformationList',
                      component: () => import('@/content/Homepage/DocumentCollaboration/ContractManagement/CheckpointInformation/List')
                    }
                  ],
                  meta: {
                    title: '專案查核點',
                    isOnSidebar: true
                  },
                  redirect: { name: 'CheckpointInformationList' }
                },
                { // 展延紀錄
                  path: 'extendedRecord',
                  name: 'ExtendedRecord',
                  component: () => import('@/content/Homepage/DocumentCollaboration/ContractManagement/ExtendedRecord'),
                  children: [
                    { // 展延紀錄頁面
                      path: 'List',
                      name: 'ExtendedRecordList',
                      component: () => import('@/content/Homepage/DocumentCollaboration/ContractManagement/ExtendedRecord/List')
                    },
                    { // 編輯展延紀錄頁面
                      path: 'Record',
                      name: 'ExtendedRecordRecord',
                      component: () => import('@/content/Homepage/DocumentCollaboration/ContractManagement/ExtendedRecord/Record')
                    }
                  ],
                  meta: {
                    title: '展延紀錄',
                    isOnSidebar: true
                  },
                  redirect: { name: 'ExtendedRecordList' }
                }
              ],
              meta: {
                title: '契約管理',
                isOnSidebar: true
              },
              redirect: { name: 'ContractSearchList' }
            },
            { // 品保文件
              path: 'qualityAssurance',
              name: 'QualityAssurance',
              component: () => import('@/content/Homepage/DocumentCollaboration/QualityAssurance'),
              children: [
                {
                  path: 'List',
                  name: 'QualityAssuranceList',
                  component: () => import('@/content/Homepage/DocumentCollaboration/QualityAssurance/List')
                }
              ],
              meta: {
                title: '品保文件',
                isOnSidebar: true
              },
              redirect: { name: 'QualityAssuranceList' }
            }
          ]
        },
        { // 行事曆
          path: '/calendar',
          name: 'Calendar',
          component: () => import('@/content/Homepage/Calendar'),
          meta: {
            title: '行事曆',
            isOnNavbar: true
          },
          redirect: { name: 'Memorabilia' },
          children: [
            {
              path: 'memorabilia',
              name: 'Memorabilia',
              component: () => import('@/content/Homepage/Calendar/Memorabilia'),
              children: [],
              meta: {
                title: '行事曆',
                isOnSidebar: false
              }
            }
          ]
        },
        { // 專案管理
          path: '/PCM',
          name: 'ProjectConstructionManagement',
          component: () => import('@/content/Homepage/ProjectConstructionManagement'),
          meta: {
            title: '專案管理'
          },
          redirect: { name: 'DailyList' },
          children: [
            {
              // 日報
              path: 'daily',
              name: 'Daily',
              component: () => import('@/content/Homepage/ProjectConstructionManagement/Daily'),
              children: [
                { // 日報列表
                  path: 'List',
                  name: 'DailyList',
                  component: () => import('@/content/Homepage/ProjectConstructionManagement/Daily/List')
                },
                { // 新增監造日報
                  path: 'Create',
                  name: 'DailyCreate',
                  component: () => import('@/content/Homepage/ProjectConstructionManagement/Daily/Create')
                }
              ],
              meta: {
                title: '日報',
                isOnSidebar: true
              },
              redirect: { name: 'DailyList' }
            },
            {
              // 品質查證
              path: 'qualityConfirmation',
              name: 'QualityConfirmation',
              component: () => import('@/content/Homepage/ProjectConstructionManagement/QualityConfirmation'),
              children: [],
              meta: {
                title: '品質查證',
                isOnSidebar: true
              }
            },
            {
              // 估驗計價
              path: 'costCertification',
              name: 'CostCertification',
              component: () => import('@/content/Homepage/ProjectConstructionManagement/CostCertification'),
              children: [
                { // 估驗計價列表
                  path: 'List',
                  name: 'CostCertificationList',
                  component: () => import('@/content/Homepage/ProjectConstructionManagement/CostCertification/List')
                }
              ],
              meta: {
                title: '估驗計價',
                isOnSidebar: true
              },
              redirect: { name: 'CostCertificationList' }
            },
            {
              // 進度管理
              path: 'scheduleManagement',
              name: 'ScheduleManagement',
              component: () => import('@/content/Homepage/ProjectConstructionManagement/ScheduleManagement'),
              children: [],
              meta: {
                title: '進度管理',
                isOnSidebar: true
              }
            }
            // {
            //   // 使用者登入
            //   path: 'pcm',
            //   name: 'pcm',
            //   component: () => import('@/content/Homepage/ProjectConstructionManagement/PCM'),
            //   children: [],
            //   meta: {
            //     title: '專案管理',
            //     isOnSidebar: true
            //   }

            // }
          ]
        },
        { // 監造施工管理
          path: '/constructionSupervision',
          name: 'ConstructionSupervision',
          component: () => import('@/content/Homepage/ConstructionSupervision'),
          meta: {
            title: '監造施工管理'
          },
          redirect: { name: 'SupervisionList' },
          children: [
            // {
            //   // 監造日報
            //   path: 'supervision',
            //   name: 'Supervision',
            //   component: () => import('@/content/Homepage/ConstructionSupervision/Supervision'),
            //   children: [
            //     { // 監造日報列表
            //       path: 'List',
            //       name: 'SupervisionList',
            //       component: () => import('@/content/Homepage/ConstructionSupervision/Supervision/List')
            //     },
            //     { // 新增監造日報
            //       path: 'Create',
            //       name: 'SupervisionCreate',
            //       component: () => import('@/content/Homepage/ConstructionSupervision/Supervision/Create')
            //     }
            //   ],
            //   meta: {
            //     title: '監造日報',
            //     isOnSidebar: true
            //   },
            //   redirect: { name: 'SupervisionList' }
            // },
            // {
            //   // 工地查驗
            //   path: 'inspection',
            //   name: 'Inspection',
            //   component: () => import('@/content/Homepage/ConstructionSupervision/Inspection'),
            //   children: [],
            //   meta: {
            //     title: '品質查證',
            //     isOnSidebar: true
            //   }

            // },
            // {
            //   // 估驗計價
            //   path: 'estimatedValuation',
            //   name: 'EstimatedValuation',
            //   component: () => import('@/content/Homepage/ConstructionSupervision/EstimatedValuation'),
            //   children: [
            //     { // 估驗計價列表
            //       path: 'List',
            //       name: 'EstimatedValuationList',
            //       component: () => import('@/content/Homepage/ConstructionSupervision/EstimatedValuation/List')
            //     },
            //     { // 新增監造日報
            //       path: 'Create',
            //       name: 'EstimatedValuationCreate',
            //       component: () => import('@/content/Homepage/ConstructionSupervision/EstimatedValuation/Create')
            //     }
            //   ],
            //   meta: {
            //     title: '估驗計價',
            //     isOnSidebar: true
            //   },
            //   redirect: { name: 'EstimatedValuationList' }
            // },
            // {
            //   // 進度管理
            //   path: 'scheduleManagement',
            //   name: 'ScheduleManagement',
            //   component: () => import('@/content/Homepage/ConstructionSupervision/ScheduleManagement'),
            //   children: [],
            //   meta: {
            //     title: '進度管理',
            //     isOnSidebar: true
            //   }

            // }
          ]
        },
        { // BIM協作平台
          path: '/bimCooperation',
          name: 'BimCooperation',
          component: () => import('@/content/Homepage/BimCooperation'),
          meta: {
            title: 'BIM協作平台',
            isOnNavbar: true
          },
          redirect: { name: 'BimCooperation' },
          children: [
            { // Bim
              path: 'Bim',
              name: 'Bim',
              component: () => import('@/content/Homepage/BimCooperation/Bim'),
              children: [

              ],
              meta: {
                title: 'BIM',
                isOnSidebar: true
              },
              redirect: { name: 'Bim' }
            }
          ]
        }
      ],
      redirect: { name: 'Dashboard' }
    },
    { // 登入畫面
      path: '/signin',
      name: 'SignIn',
      component: () => import('@/content/SignIn'),
      children: [
        { // 使用者登入
          path: 'userLogin',
          name: 'UserLogIn',
          component: () => import('@/content/SignIn/UserLogIn')
        }
      ]
    }
  ]
})

router.beforeEach(async (to, from, next) => {
  // Vue.nextTick(() => {
  //   if (to.meta !== undefined) {
  //     if (to.meta.title !== undefined) { document.title = to.meta.title }
  //   }
  // })
  // console.log(store.state.user.current)
  if (to.name === 'UserLogIn' || to.name === '404') {
    next()
  } else {
    // console.log('This is exclude userlogin and 404')
    let authentication = await store.dispatch('user/authentication')
    // console.log(authentication)
    if (authentication.status) {
      // console.log('不用再驗證')
      store.commit('user/setCurrent', authentication.data)
      await store.dispatch('getEnums')
      await store.dispatch('project/lookupProject')
      next()
    } else {
      // console.log('需要驗證')
      if (to.meta.loginRequest) {
        // console.log('有點東西')
        next({
          name: 'UserLogIn',
          query: {
            search: {
              path: to.path,
              query: to.query
            }
          }
        })
      } else {
        // console.log('沒東西')
        next({name: 'UserLogIn'})
      }
    }
  }
})
Vue.prototype.$recursiveChildrenSearch = function ({ routes, name }) {
  routes = routes === undefined ? this.$router.options.routes : routes
  name = name === undefined ? this.$route.name : name
  for (let route of routes) {
    if (route.name === name) {
      return route.children === undefined ? null : route.children
    } else if (route.children === undefined) continue
    else if (route.children.length > 0) {
      const result = this.$recursiveChildrenSearch({ routes: route.children, name })
      if (result === null) continue
      else return result
    }
  }
  return null
}

export default router
