import { nextTick } from 'vue'
import { getArrValue } from 'js-fast-way'
import { getBizDictionary, getDictionary } from '~api/other'

//设置分割滑块
export const setSplit = (ids, data) => {
    //配置参考： https://split.js.org/#/?direction=vertical&snapOffset=0
    nextTick(() => {
        setTimeout(() => {
            window.$split(ids, data)
        }, 300)
    }).then()
}

//刷新页面
export const reloadPage = () => {
    setTimeout(() => {
        window.location.reload()
    }, 1500)
}

/**
 * 效验是否为数字或小数的数字
 * @param text  字符串内容
 * @param lose  是否允许负数，默认允许
 * @returns {boolean}
 */
export const isNumberReg = (text, lose = true) => {
    let pattern = lose ? /^-?\d+(.\d{1,2})?$/ : /^[0-9]+([.]{1}[0-9]+){0,1}$/
    return pattern.test(text)
}

//获取字典数据
export const getDictionaryData = async (code, biz = false) => {
    let res = []
    if (biz) {
        const { data } = await getBizDictionary({ code: code })
        res = getArrValue(data)
    } else {
        const { data } = await getDictionary({ code: code })
        res = getArrValue(data)
    }
    //处理数据
    let newArr = []
    for (let i = 0; i < res.length; i++) {
        newArr.push({
            label: res[i]['dictValue'],
            value: Number(res[i]['dictKey']),
        })
    }
    return newArr
}

//操作确认
export const actionConfirm = (cbk) => {
    window?.$messageBox?.alert('请确认要执行此操作吗？', '操作确认', {
        showCancelButton: true,
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
        callback: (action) => {
            if (action === 'confirm') {
                cbk()
            }
        },
    })
}

//动态加载线上js文件
export const addDocumentsJs = () => {
    return new Promise((resolve) => {
        const script = document.createElement('script')
        script.src = 'http://47.110.251.215:6831/web-apps/apps/api/documents/api.js'
        script.type = 'text/javascript'
        document.head.appendChild(script)
        script.onload = () => {
            resolve()
        }
    })
}

//判断是否为网址
export const isPathUrl = (path) => {
    return /^(https?:|mailto:|tel:)/.test(path)
}


//获取当前域名
export const getTopUrl = () => {
    return window.location.href.split('/#/')[0]
}

//设置系统名称
export const setAppName = (name) => {
    const title = window.document.title
    window.document.title = `${title}${name ? ' - ' + name : ''}`
}
