﻿var client = new Edo.rpc.Client('/AjaxRpc/Server.aspx');

//获取查询字符串指定Key的值
var getQueryValue = function (search, key) {
    var str = search.substring(1, search.length);
    var arry = str.split("&");
    var r;
    for (r in arry) {
        if (arry[r].split("=")[0] == key) {
            return arry[r].split("=")[1];
        }
    }

};


//用户数据
var getDataTable = function (sql) {
    var action = client.Proxy.ExecuteTable(sql);
    if (action.mess == "") {
        var jsonData = Edo.util.JSON.decode(action.result);
        var data = new Edo.data.DataTable(jsonData);
        data.set('idField', "GUID");
        return data;
    }
    else {
        alert(action.mess);
    }
};

var  getScalar=function(commandText){
    var action = client.Proxy.ExecuteScalar(commandText);
    if (action.mess == "") {
        return action.result;
    }
    else {
        alert(action.mess);
    }
}

var saveDataTable = function (tableName, adds, updates, dels, delCols, delPicPaths) {
    Edo.MessageBox.saveing('保存', '数据保存中...');
    var mess = "";
    client.Proxy.SaveTable(tableName, adds, updates, dels, delCols, delPicPaths, function (action) {
        if (action.mess == '') {
            table.data.reload();
            Edo.MessageBox.hide();
        }
        else {
            Edo.MessageBox.hide();
            mess = action.mess
            alert(action.mess);
        }
    });
    return mess;
};

function NewGuid() {
    var guid = "";
    for (var i = 1; i <= 32; i++) {
        var n = Math.floor(Math.random() * 16.0).toString(16);
        guid += n;
        if ((i == 8) || (i == 12) || (i == 16) || (i == 20))
            guid += "-";
    }
    return guid;
};

/*
图片文件上传对话框
t=对话框title
i=文件保存参数,格式：保存路径@width$保存路径@width
n=图片名称
callback=回调函数
*/
var fileUploadBox = function (t, n, i, callback) {
    var title = t;
    if (!title) {
        title = 'File upload';
    }
    var imgInfo = i;
    if (!imgInfo) {
        imgInfo = '';
    };
    var imgName = n;
    if (!imgName) {
        imgName = 'temp';
    };
    var win = new Edo.containers.Window();

    var file = {
        id: "file",
        type: 'fileupload',
        width: 300,
        swfUploadConfig: {              //swfUploadConfig配置对象请参考swfupload组件说明
            upload_url: '/APL/FileUpload.aspx', //上传地址
            flash_url: '/Scripts/SwfUpload/swfupload.swf',
            button_image_url: '/Images/UploadText.png',      //按钮图片地址
            file_types: '*.gif;*.png;*.jpg',                        //上传文件后缀名限制
            file_post_name: 'Fdata',                                //上传的文件引用名
            file_size_limit: '3000',                               //文件上传
            post_params: {                                  //上传文件的时候, 可以附加一些信息参数
                imgInfo: imgInfo,
                imgName: imgName
            }
        },
        onfilequeueerror: function (e) {
            alert("文件选择错误:" + e.message);
        },
        onfilequeued: function (e) {
            //alert("文件选择成功");
            //this.upload();    //也可以在选择文件后, 默认直接上传
        },
        onfilestart: function (e) {
            //alert("开始上传");
            win.mask();
        },
        onfileerror: function (e) {
            alert("上传失败:" + e.message);

            win.unmask();

        },
        onfilesuccess: function (e) {
            //调用回调函数
            if (callback) {
                callback(true);
            }

            //Edo.util.Dom.append(document.body, '<img src="/Upload/' + e.serverData + '"/>');
            win.unmask();
            win.destroy();
        }
    };
    win.set(
        {
            title: title,
            horizontalAlign: 'center',
            children: [
                {
                    type: 'space',
                    height: 5
                },
                file,
                {
                    type: 'space',
                    height: 5
                },
//                {
//                    type: 'formitem',
//                    label: '图片名称',
//                    children: [
//                        { id: 'txtResolveName', type: 'text', text: '' }
//                    ]
//                },
                {
                    type: 'space',
                    height: 5
                },
                {
                    type: 'ct',
                    layout: 'horizontal',
                    horizontalAlign: 'center',
                    horizontalGap: 10,
                    children: [
                        {
                            type: 'button',
                            width: 80,
                            text: 'Save',
                            onclick: function (e) {
                                //upload对象就是swfupload的原生对象
                                Edo.get('file').upload.startUpload();
                            }
                        },
                        {
                            type: 'button',
                            width: 80,
                            text: 'Close',
                            onclick: function (e) { close(); }
                        }
                    ]

                }
            ]
        }
    );
    var close = function () {
        win.destroy();
    };

    win.showModal = function () {
        win.show(null, null, true);
    };
    return win;
};

