import dayjs from 'dayjs'

const commonStore = defineStore("common", {
    state: () => ({
        sevenDaysDate: dayjs().subtract(7, "day").startOf("day").format("YYYY-MM-DD HH:mm:ss"),


        helpCenterList: {
            menuId: undefined,
            contentList: {}
        },//帮助中心公共值存储

        meetingList: {
            roomName: "",//会议室名称
        }
    }),
    persist: {
        storage: window.localStorage, // 指定换成地址
    },
    actions: {
        /**
         * @公共时间 获取当前日期
         */
        getTimes() {
            return {
                startTime: dayjs().startOf("day").format("YYYY-MM-DD HH:mm:ss"),
                endTime: dayjs().format("YYYY-MM-DD HH:mm:ss"),
            };
        },
        // 格式化日期对象
        getNowDate() {
            var date = new Date();
            var sign2 = ":";
            var year = date.getFullYear() // 年
            var month = date.getMonth() + 1; // 月
            var day = date.getDate(); // 日
            var hour = date.getHours(); // 时
            var minutes = date.getMinutes(); // 分
            var seconds = date.getSeconds() //秒
            var weekArr = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期天'];
            var week = weekArr[date.getDay()];
            // 给一位数的数据前面加 “0”
            if (month >= 1 && month <= 9) {
                month = "0" + month;
            }
            if (day >= 0 && day <= 9) {
                day = "0" + day;
            }
            if (hour >= 0 && hour <= 9) {
                hour = "0" + hour;
            }
            if (minutes >= 0 && minutes <= 9) {
                minutes = "0" + minutes;
            }
            if (seconds >= 0 && seconds <= 9) {
                seconds = "0" + seconds;
            }
            return year + "-" + month + "-" + day + " " + hour + sign2 + minutes + sign2 + seconds;
        },

        /**
         * @处理公共日期格式
         */
        formatterDate(date, fmt) {
            let nowDate = {
                yyyy: date.getFullYear(), // 年
                MM: date.getMonth() + 1, // 月份
                dd: date.getDate(), //日
                hh: date.getHours(),
                mm: date.getMinutes(),
                ss: date.getSeconds(),
            };
            if (/(y+)/.test(fmt)) {
                fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
            }
            for (var k in nowDate) {
                if (new RegExp("(" + k + ")").test(fmt)) {
                    fmt = fmt.replace(RegExp.$1, RegExp.$1.length == 1 ? nowDate[k] : ("00" + nowDate[k]).substr(("" + nowDate[k]).length));
                }
            }
            return fmt;
        },

        /**
         * @禁止选择某个时间段
         * @type 1 可选90天之前的时间
         * @type 2 可选当前时间之前所有的时间
         * @开始时间
         */
        startDateDisabled(date, endDate, type) {
            if (type == 1) {
                let data = !endDate ? Date.now() : new Date(endDate);

                let data1 = !endDate ? Date.now() - 3600 * 1000 * 24 * 90 : new Date(endDate) - 3600 * 1000 * 24 * 90;
                return date > data || date < data1;
            } else if (type == 2) {
                let data = !endDate ? Date.now() : new Date(endDate);
                return date > data;
            }
        },
        /**
         * @禁止选择某个时间段
         * @type 1 可选90天之前的时间
         * @type 2 可选当前时间之前所有的时间
         * @结束时间
         */
        endDateDisabled(date, startDate, type) {
            if (type == 1) {
                let data = !startDate ? Date.now() - 3600 * 1000 * 24 * 90 : new Date(startDate) - 3600 * 1000 * 24 * 90;
                return date < data;
            } else if (type == 2) {
                let data = !startDate ? Date.now() - 3600 * 1000 * 24 : new Date(startDate) - 3600 * 1000 * 24;
                return date < data;
            }
        },

        /**
         * @文件下载
         * @param {*} url
         */
        fetchDownloadFile(url) {
            let time = new Date();
            time = this.formatterDate(time, "yyyyMMddhhmmss");

            let fileType = url.split("/")[url.split("/").length - 1].split(".")[1];
            let type = "";
            if (fileType === "xls") {
                type = "application/vnd.ms-excel";
            } else if (fileType === "xlsx") {
                type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            } else if (fileType === "doc") {
                type = "application/msword";
            } else if (fileType === "docx") {
                type = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
            } else if (fileType === "pdf") {
                type = "application/pdf";
            } else if (fileType === "ppt") {
                type = "application/vnd.ms-powerpoint";
            } else if (fileType === "pptx") {
                type = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
            } else if (fileType === "png") {
                type = "image/png";
            } else if (fileType === "jpeg" || fileType === "jpg") {
                type = "image/jpeg";
            } else if (fileType === "gif") {
                type = "image/gif";
            } else if (fileType === "bmp") {
                type = "image/bmp";
            } else if (fileType === "html") {
                type = "text/html";
            } else if (fileType === "txt") {
                type = "text/plain";
            } else if (fileType === "htm") {
                type = "text/html";
            } else if (fileType === "rar") {
                type = "application/x-rar-compressed";
            } else if (fileType === "zip") {
                type = "application/zip";
            } else if (fileType === "bz2") {
                type = "application/x-bzip2";
            }

            fetch(url, {
                method: "get",
                mode: "cors",
            })
                .then((response) => response.blob())
                .then((res) => {
                    const downloadUrl = window.URL.createObjectURL(
                        //new Blob() 对后端返回文件流类型处理
                        new Blob([res], { type: type })
                    );
                    const link = document.createElement("a");
                    link.href = downloadUrl;
                    link.setAttribute("download", time);
                    document.body.appendChild(link);
                    link.click();
                    link.remove();
                })
                .catch((error) => {
                    window.open(url);
                });
        },
    },
});

export default commonStore;
