import store from '@/store'
import {SET_PRODUCT} from "../store/modules/app/type";

const getProductList = async () => {
    let productList = store.getters.productList;
    if (productList.length == 0) {
        const res = await VE_API.product.list();
        if (res.code == 0) {
            store.commit(`app/${SET_PRODUCT}`, res.data)
            productList = res.data;
        }
    }
    return productList;
}

const arrayToString = (array) => {
    let names = "";
    let i = 0;
    for(let layerName of array){
        if(i > 0){
            names += ","
        }
        names += layerName
        i++;
    }
    return names;
}
function dateFormat(fmt, date) {
    let ret;
    const opt = {
        "Y+": date.getFullYear().toString(),        // 年
        "m+": (date.getMonth() + 1).toString(),     // 月
        "d+": date.getDate().toString(),            // 日
        "H+": date.getHours().toString(),           // 时
        "M+": date.getMinutes().toString(),         // 分
        "S+": date.getSeconds().toString()          // 秒
        // 有其他格式化字符需求可以继续添加，必须转化成字符串
    };
    for (let k in opt) {
        ret = new RegExp("(" + k + ")").exec(fmt);
        if (ret) {
            fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
        };
    };
    return fmt;
}
const getWeek = ()=>{
    var str = "";
    var week = new Date().getDay();
    if (week == 0) {
        str = "星期日";
    } else if (week == 1) {
        str = "星期一";
    } else if (week == 2) {
        str = "星期二";
    } else if (week == 3) {
        str = "星期三";
    } else if (week == 4) {
        str = "星期四";
    } else if (week == 5) {
        str = "星期五";
    } else if (week == 6) {
        str = "星期六";
    }
    return str;
}
const getMoment = ()=>{
    let date = new Date()
    let hours = date.getHours()
    if(hours >= 0 && hours < 12){
        return '上午'
    }else{
        return '下午'
    }
}
const isDayOrNight = ()=>{
    let h = parseInt(dateFormat("HH",new Date()))
    return h > 6 && h < 18;
}
export{
    getProductList,
    arrayToString,
    getWeek,
    getMoment,
    dateFormat,
    isDayOrNight,

};
