import formUserGlobal from "../../assets/js/global/formUserGlobal";
import formEvent from "./formEvent";
import apiForm from "../../assets/js/api/apiForm";

export default {

    buttonRecordRe: [],
    timer: null,

    /**
     * 按键记录赋值
     * buttonName 按键名称
     * rowDataId 数据行ID
     * dataId 数据ID 唯一标识
     * userId 用户ID
     * UserName 用户名
     * dataForm 数据来源
     */
    buttonInnerPush(clickPosition, clickContent, userId, userName, listViewId, formId, rowDataId) {
        this.scheduledTasks()
        //判断是否要开启定时任务
        // 判断是行内按键还是顶部按键
        let buttonRe = {
            clickPosition: clickPosition,
            rowDataId: rowDataId,
            clickContent: clickContent,
            formId: formId,
            clickTime : this.viewDateFormat(new Date(), 'yyyy-MM-dd HH:mm:ss'),
            dataFrom : 'internal',
            listViewId : listViewId,
            clickUserId : userId,
            clickUserName: userName,
        };
        this.buttonRecordRe.push(buttonRe);
    },

    /**
     * 按键记录赋值
     * buttonName 按键名称
     * rowDataId 数据行ID
     * dataId 数据ID 唯一标识
     * userId 用户ID
     * UserName 用户名
     * dataForm 数据来源
     */
    buttonPush(clickPosition, clickContent, userId, userName) {
        this.scheduledTasks()
        // 判断是行内按键还是顶部按键
        let clickUserId;
        let clickUserName;
        if (userId) {
            clickUserId = userId;
            clickUserName = userName;
        } else {
            clickUserId = formUserGlobal.getLoginUser().id;
            clickUserName = formUserGlobal.getLoginUser().userName;
        }
        // 判断是行内按键还是顶部按键
        let buttonRe = {
            clickPosition: clickPosition,
            clickContent: clickContent,
            clickTime : this.viewDateFormat(new Date(), 'yyyy-MM-dd HH:mm:ss'),
            dataFrom : 'external',
            clickUserId : clickUserId,
            clickUserName: clickUserName,
        };
        this.buttonRecordRe.push(buttonRe);
    },


     scheduledTasks() {
        if (this.timer) {
            return;
        }
        //定时任务3分钟请求一次记录按键[]
        this.timer = setInterval(() => {
            if (this.buttonRecordRe.length > 0) {
                this.querzRequest();
            }
        }, 180000);
    },

    /**
     * 清除定时器
     */
    clearTask(){
        clearInterval(this.timer);
    },
    /**
     * 请求后台记录数据
     */
    querzRequest() {
        if (this.buttonRecordRe.length > 0) {
            let dataJson = JSON.stringify(this.buttonRecordRe);
            formEvent.axios.post(apiForm.apis.plFormButtonRecord.batchOperate, {"dataJson": dataJson}).then(res => {
                if (res && res.data && !res.data.operateSuccess) {
                    //
                } else {
                    this.buttonRecordRe = [];
                }
            })
        }
    },

    /**
     * 时间格式化方法（查看表单视图时用到）
     * @param d
     * @param fmt
     * @return {string|void}
     */
    viewDateFormat(d, fmt) {
        if (d) {
            let date = new Date(d);
            let o = {
                "M+": date.getMonth() + 1,                 //月份
                "d+": date.getDate(),                    //日
                "H+": date.getHours(),                   //小时
                "m+": date.getMinutes(),                 //分
                "s+": date.getSeconds(),                 //秒
                "q+": Math.floor((date.getMonth() + 3) / 3), //季度
                "S": date.getMilliseconds()             //毫秒
            };
            if (/(y+)/.test(fmt)) {
                fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
            }
            for (let k in o) {
                if (new RegExp("(" + k + ")").test(fmt)) {
                    fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
                }
            }
        } else {
            fmt = '';
        }
        return fmt;
    },
}