import { createApp } from 'vue';
import App from './App.vue';
import router from './router';
import axios from 'axios';
import store from '@/store/index.js';
import OrganizationView from '@/views/systemview/OrganizationView.vue';
import Role from '@/views/systemview/Role.vue';
import Menu from '@/views/systemview/Menu.vue';
import UserList from '@/views/systemview/UserList.vue';
import VncShow from '@/views/VncShow.vue';
import EmailCenter from '@/views/systemview/EmailCenter';
import UploadCae from '@/views/tool/UploadCae.vue';
import TestReport from '@/views/tool/TestReport';
import ReportLsit from '@/views/tool/ReportLsit';
import AduitReportList from '@/views/tool/AduitReportList';
import TestCase from '@/views/autotest/TestCase.vue';
// import RequestsTemplate from '@/views/tool/RequestsTemplate.vue';
import MailTool from '@/views/tool/MailTool.vue';
import MoveUIRun from '@/views/autotest/MoveUIRun.vue';
import CreateOrder from '@/views/tool/CreateOrder.vue';
import CodeCover from './views/tool/CodeCover.vue';
import Echarts from "vue-echarts";
import 'echarts';
import ViewUIPlus from 'view-ui-plus';
import { GcSpreadSheets, GcWorksheet, GcColumn } from '@grapecity-software/spread-sheets-vue'
// import VueSocketio from 'vue-socket.io'
import io from 'socket.io-client'


const app = createApp(App).use(store)
import 'view-ui-plus/dist/styles/viewuiplus.css'

// const socket = new VueSocketio({ connection: SocketIo('ws://127.0.0.1') })


const socket = io({ connection: 'ws://localhost', autoConnect: false })

axios.defaults.withCredentials = true
axios.defaults.baseURL = '/api'
app.config.globalProperties.$http = axios
app.config.globalProperties.$socket = socket
app.component('v-chart', Echarts)
app.component('gc-spread-sheets', GcSpreadSheets);
app.component('gc-worksheet', GcWorksheet);
app.component('gc-column', GcColumn);


const routers = [
    {
        path: 'vcnshow',
        name: 'vncshow',
        component: VncShow
    }, {
        path: 'organization',
        name: 'organization',
        component: OrganizationView
    }, {
        path: 'role',
        name: 'role',
        component: Role
    }, {
        path: 'menu',
        name: 'menu',
        component: Menu
    }, {
        path: 'userlist',
        name: 'userlist',
        component: UserList
    }, {
        path: 'report',
        name: 'report',
        component: TestReport
    }, {
        path: 'emialcenter',
        name: 'emialcenter',
        component: EmailCenter
    }, {
        path: 'uploadcase',
        name: 'uploadcase',
        component: UploadCae
    }, {
        path: 'reportlsit',
        name: 'reportlsit',
        component: ReportLsit
    }, {
        path: 'aduitreportlist',
        name: 'aduitreportlist',
        component: AduitReportList
    }, {
        path: 'testcase',
        name: 'testcase',
        component: TestCase
    }, {
        path: 'moveuirun',
        name: 'moveuirun',
        component: MoveUIRun
    },
    {
        path: 'createorder',
        name: 'createorder',
        component: CreateOrder
    },
    {
        path: 'mailtool',
        name: 'mailtool',
        component: MailTool
    },{
        path: 'codecover',
        name: 'codecover',
        component: CodeCover
    }
]

axios.interceptors.request.use(function (config) {
    if (store.getters.gettoken != "" && config.url != "user/login") config.headers['token'] = store.getters.gettoken
    return config;
});



router.beforeEach(async (to, from, next) => {
    const a = (value) => {
        for (let each of value) {
            if (each.router) {
                const index_ = routers.findIndex(ele => { return ele.name == each.router })
                if (index_ != -1) { router.addRoute('home', { 'name': each.router, 'path': each.router, component: routers[index_].component }) }
            }
            if (each.children) {
                a(each.children)
            }
        }
    }
    const findMenu = (menu, each) => {
        for (let ele of menu) {
            if (ele.router == each && each) return true
            if (ele.children && findMenu(ele.children, each)) return true
        }
    }
    if (to.path == '/login') return next();
    if (localStorage.getItem('token')) {
        let userinfo = localStorage.getItem("userinfo")
        if (!userinfo) {
            var res = await axios.get("user/userdetail", 'get')
            userinfo = res.data.data
        } else {
            userinfo = JSON.parse(userinfo)
        }
        const menu = userinfo.menu
        if (findMenu(menu, to.fullPath.split('/').pop()) && !router.hasRoute(to.name)) {
            a(menu)
            return next(to)
        }
        return next()
    } else {
        return next('/login')
    }
})

function request(method, url, data, apply, successFun = () => { }, failFun = () => { }) {
    axios[method](url, method != 'get' ? data : { params: data }).then(res => {
        const data = JSON.parse(JSON.stringify(res.data))
        if (method != "get" && apply) apply.$Message.success(data.message);
        successFun(res.data)
    }).catch(error => {
        try {
            if (error.response.data.code == 401) {
                apply.$Message.error(error.response.data.message);
                router.push({ name: 'login' })
            } else {
                apply && apply.$Message.error(error.response.data.message);
                failFun(error.response.data)
            }
        }
        catch {
            apply && apply.$Message.error("系统处理错误");
            failFun()
        }
    })
}

function getElements(page_id, apply, fun) {
    request('get', 'user/elements', { "id": page_id }, apply, fun)
}




app.config.globalProperties.$getElements = getElements

app.config.globalProperties.$request = request

app.config.globalProperties.elePro = function (menu_id) {
    return null
}

app.config.globalProperties.$routers = routers

app.provide("$http", axios);
app.provide("$socket", socket);
app.provide("$request", request);
app.provide("$getElements", getElements);
app.use(ViewUIPlus).use(router).mount('#app')






