import { arrIndex } from 'js-fast-way'

//取数组中的值
export const getRowsValue = (arr, key, key2, value) => {
    if (value) {
        const index = arrIndex(arr, key, value)
        return arr[index][key2]
    } else {
        return ''
    }
}

//删除提醒
export const delMessage = (cbk) => {
    window?.$messageBox?.alert('请谨慎考虑后，确认是否需要删除？', '删除提醒', {
        showCancelButton: true,
        confirmButtonText: '确认删除',
        cancelButtonText: '取消',
        type: 'warning',
        callback: (action) => {
            if (action === 'confirm') {
                cbk()
            }
        },
    })
}

//日期格式化
export const dateFormat = (date, format) => {
    format = format || 'yyyy-MM-dd hh:mm:ss'
    if (date !== 'Invalid Date') {
        let o = {
            'M+': date.getMonth() + 1, //month
            'd+': date.getDate(), //day
            'h+': date.getHours(), //hour
            'm+': date.getMinutes(), //minute
            's+': date.getSeconds(), //second
            'q+': Math.floor((date.getMonth() + 3) / 3), //quarter
            'S': date.getMilliseconds(), //millisecond
        }
        if (/(y+)/.test(format)) {
            format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
        }
        for (let k in o) {
            if (new RegExp('(' + k + ')').test(format)) {
                format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length))
            }
        }
        return format
    }
    return ''
}

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

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

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