/*
* by wzdxx1314@163.com
*
* */
//深层次获取value by arr
export const getDeepPrototy = (arr,prop)=>{
    let finArr = [];
    const getFn = (obj)=>{
        finArr.push(obj[prop])
        if(obj.children && obj.children.length!==0){
            obj.children.forEach(target=>{
                getFn(target)
            })
        }
    }
    arr.forEach(target=>{
        getFn(target)
    })
    return  finArr
}


//通过属性获取对应的值
export const getDeepObj = (arr,prop,value)=>{
    let object;
    const getObj = (obj)=>{
        if(obj[prop] == value){
            object = obj
            return;
        }else{
            if(obj.children && obj.children.length!==0){
                obj.children.forEach(target=>{
                    getObj(target)
                })
            }
        }
    }
    arr.forEach(target=>{
        getObj(target)
    })
    return object
}
//进入全屏
export const  launchFullscreen = (element) =>{
    if(element.requestFullscreen) {
        element.requestFullscreen();
    } else if(element.mozRequestFullScreen) {
        element.mozRequestFullScreen();
    } else if(element.webkitRequestFullscreen) {
        element.webkitRequestFullscreen();
    } else if(element.msRequestFullscreen) {
        element.msRequestFullscreen();
    }
}
// 判断浏览器种类退出全屏
export const exitFullscreen = ()=> {
    if(document.exitFullscreen) {
        document.exitFullscreen();
    } else if(document.mozCancelFullScreen) {
        document.mozCancelFullScreen();
    } else if(document.webkitExitFullscreen) {
        document.webkitExitFullscreen();
    }
}

export const getUuid = ()=>{
    var s = [];
    var hexDigits = "0123456789abcdef";
    for (var i = 0; i < 32; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    }
    s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
    s[8] = s[13] = s[18] = s[23];
    var uuid = s.join("");
    return uuid;
}

export const  getTimestamp = (date)=>{
    date = date || new Date()
    var tmp = Date.parse( date ).toString();
    tmp = tmp.substr(0,10);
    return tmp;
}

export const deepResolve = (obj)=>{
    obj = Object
        .keys(obj).filter(t=>(params[t]!==false && params[t]!==null && params[t]!==''))
        .sort()
    Object.keys(obj).forEach(target=>{

        if(Object.prototype.toString.call(obj[target]) == "[object Array]" ){
            obj[target].forEach(tar=>{
                if(Object.prototype.toString.call(tar) =="[object Object]" ){
                    deepResolve(tar)
                }
            })
        }
        if( Object.prototype.toString.call(obj[target]) == "[object Object]"){
            deepResolve(obj[target])
        }
    })
    return obj
}

export const addIndex = (arr)=>{
    arr.forEach((x,i)=>{
        x.index =i
    })
    return arr
}

export const arrayHas = (arr,val) =>{
    let bool = false
    arr.some(x=>{
        if(x == val){
            bool = true
            return true
        }
    })
    return bool
}

export const combination = (arr) =>{
    if(arr.length==0) {return []}
    function com(arr1,arr2){
        let result = []
        arr1.forEach(target=>{
            arr2.forEach(tar=>{
                result.push([...target,tar])
            })
        })
        return result
    }
    let x = arr[0].map(target=>[target])
    for(var i=1;i<arr.length;i++){
        x = com(x,arr[i])
    }
    return x
}

/*时间戳转换成-年月日时分秒*/
function formatDateTime(inputTime) {
    var date = new Date(inputTime*1000);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes();
    var second = date.getSeconds();
    minute = minute < 10 ? ('0' + minute) : minute;
    second = second < 10 ? ('0' + second) : second;
    return y + '-' + m + '-' + d + ' '+ h + ':' + minute + ':' + second;
}
//获取key-value map给下拉选择器用,并把键值统一成key-value
async function getOptions(list,options={}){
    options={...options};

    for(let item of list){
        const res=await this.$api[item.api]();
        //兼容一下后端有的人爱往res.data.data放有的人爱往res.data放的情况
        const list=res['data']['data']||res['data'];
        options[item.param]=list.map(e=>{
            return {
                id:e[item.idName],
                label:e[item.valueName]||e[Object.keys(e)[1]]//兼容labelKey没写对的情况
            };
        });
    }
    return options;
}

export default {
    getDeepPrototy,
    exitFullscreen,
    launchFullscreen,
    getDeepObj,
    getUuid,
    getTimestamp,
    addIndex,
    arrayHas,
    combination,
    formatDateTime,
    getOptions
}
