var red = {
    showMsg: function(option) {
        var defOption = {msg: "操作成功", type:"info", placement: "bottom-right"};

        option = option || defOption;
        for (var k in defOption) {
            option[k] = option[k] || defOption[k]
        }

        new $.zui.Messager(option.msg, {
            type: option.type // 定义颜色主题
            ,placement: option.placement
        }).show();
    },
    getData: function(key, defaultValue) {
        var v = localStorage.getItem(key) || defaultValue || "";
        if (typeof(v) == "string" && v.startsWith("{") && v.endsWith("}")) {
            v = JSON.parse(v);
        } else if (typeof(v) == "string" && v.startsWith("[") && v.endsWith("]")) {
            v = JSON.parse(v);
        }
        return v;
    },
    setData: function(key, value) {
        var v = value;
        if (typeof(v) == "object") {
            v = JSON.stringify(value);
        }
        localStorage.setItem(key, v);
    },
    getPlatId: function() {
        var plat = red.getData("sysPlat") || {};
        return plat["_key"];
    },
    getPlatToken: function() {
        var plat = red.getData("sysPlat") || {};
        return plat["token"];
    },
    getJSON: function (url, para, callback) {
        para["platToken"] = red.getPlatToken();
        $.getJSON(url, para, function (json) {
            json = json || {};
            red.loginCheck(json);
            var data = json;
            if (json.code == -1) {
                red.showMsg({type:"error", msg: json.message});
                return;
            }

            if (json.code == 0) {
                data = json.body;
            }

            callback(data);
        });
    },
    post: function(url, para, callback) {
        para["platToken"] = red.getPlatToken();
        $.post(url, para, function (json) {
            red.loginCheck(json);
            if (json.code == -1) {
                red.showMsg({msg: json.message, type: "error"});
                return;
            }

            if (callback) {

                callback(json.code == 0 ? json.body : json);
            } else {
                red.showMsg();
            }
        });
    },

    //TODO: 提取统一查询、请求，失败提示
    //QTASK find list
    qtaskCall: function (para, callback) {

        /*$.p$.post("/db/list", {fBean: JSON.stringify(fBean)}, function (json) {
            vm.list = json.body;
        });*/
        $.getJSON("/qtask/call", para, function (json) {
            callback(json);
        });
    },

    //db find
    dbQuery: function (para, callback) {
        $.getJSON("/db/list", para, function (json) {
            red.loginCheck(json);
            if (json.code == -1) {
                console.log("json.code == -1")
            }

            callback(json);
        });
    },
    dbPlats: function(callBack) {
        red.getJSON("/meta/db_plat_list", {}, function (json) {
            callBack(json);
        });
    },

    putAll: function(t, s) {
        t = t || {};
        s = s || {};

        for (var k in s) {
            t[k] = s[k];
        }
        return t;
    },

    timeFmt: function (date,fmt){
        fmt = fmt || "yyyy-MM-dd HH:mm:ss";
        var 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(var 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)));
        return fmt;
    },
    loginCheck: function (json) {
        if (json && json["code"] == -2) {
            red.showMsg({type:"error", placement: "center", msg: "登陆过期，请前往登陆"});
            setTimeout(function () {
                location.href = "/user/login.html";
            }, 2000);
        }
    }
}