var g = {};

/**
 * 打开新窗口
 * @param  {[type]} url    [description]
 * @param  {[type]} title  [description]
 * @param  {[type]} width  [description]
 * @param  {[type]} height [description]
 * @return {[type]}        [description]
 */
function openWin(url, title, width, height) {
    if (width == undefined) {
        width = $(window).width() * 0.6 + 'px';
    }
    if (height == undefined) {
        height = $(window).height() * 0.75 + 'px';
    }
    if (title == undefined) {
        title = '系统管理';
    }
    g.currentWin = layer.open({
        type: 2,
        title: title,
        shadeClose: true,
        shade: [0.1, '#fff'],
        maxmin: true, //开启最大化最小化按钮
        area: [width, height],
        content: url
    });
}

function openHtml(obj, title, width, height) {
    if (width == undefined) {
        width = $(window).width() * 0.6 + 'px';
    }
    if (height == undefined) {
        height = $(window).height() * 0.75 + 'px';
    }
    if (title == undefined) {
        title = '系统管理';
    }
    g.currentHtmlWin = layer.open({
        type: 1,
        shadeClose: true,
        shade: [0.1, '#fff'],
        title: title,
        maxmin: true, //开启最大化最小化按钮
        // skin: 'layui-layer-rim', //加上边框
        area: [width, height],
        content: $(obj).html()
    });
}
/**
 * 添加到标答
 * @param {*}  
 * @param {*}  
 */
function joinTab($title, $url) {
    parent.addTab(null, $title, $url);
}

function closeWin() {
    layer.close(g.currentWin);
}

//无刷新重载表格
function reloadDataTable() {
    try {
        g.currentDataTableReload['reload'].call();
    } catch (ex) {}
}

/**
 * 删除数据
 * @param  {[type]} url [description]
 * @return {[type]}     [description]
 */
function delWin(url, ids) {
    ids = (ids == undefined || ids == '') ? g.currentDataTableIds : ids;
    console.log(g.currentDataTableIds);
    if (ids == '' || ids == undefined) {
        layer.msg('请先选择数据');
        return;
    }
    layer.confirm('是否确认删除？', {
        btn: ['确认', '取消'] //按钮
    }, function() {
        $.getJSON(url + ids, function(data, textStatus) {
            if (data.status == '1') {
                reloadDataTable();
                layer.msg('删除成功');
            } else {
                layer.msg(data.msg);
            }
        });
    }, function() {

    });

}

$(document).ready(function() {
    $('*[eventType=eventForm]').submit(function() {

        $(this).attr('method', 'get');
        return true;

    });

    $('*[eventType=eventAjaxForm]').submit(function() {
        var uid = $(this).serialize() + $(this).attr('action');
        if (g.parentUid == uid) {
            return false;
        }
        g.parentUid = uid;
        var msgIndex = parent.layer.msg('数据提交中...', {
            icon: 16,
            shade: 0.71
        });

        $.post($(this).attr('action'), $(this).serialize(), function(data, status) {
            parent.layer.close(msgIndex);
            if (data.status == '1') {
                try {
                    cbFormSuccess(data);
                } catch (ex) {
                    window.history.back();
                    // parent.window.location.reload();
                }

            } else {
                try {
                    cbFormFail(data);
                } catch (ex) {
                    parent.layer.msg(data.msg);
                }

            }
        });
        return false;
    });
    $(document).on('click', '[eventType=event-view]', function() {
        var url = $(this).attr('uri');
        var pid = $(this).attr('pid');
        var width = $(this).attr('win-width');
        var height = $(this).attr('win-height');
        var id = $(this).parent().find('span').attr('data-' + pid);
        openWin(url + id, '查看详细信息', width, height);
    });
    $(document).on('click', '[eventType=event-edit]', function() {
        var url = $(this).attr('uri');
        var pid = $(this).attr('pid');
        var width = $(this).attr('win-width');
        var height = $(this).attr('win-height');
        var id = $(this).parent().find('span').attr('data-' + pid);
        openWin(url + id, '修改信息', width, height);
    });
    $('form').keyup(function(event) {
        if (event.keyCode == 13) {
            try {
                $(".lay-btn-diy").trigger("click");
            } catch (ex) {}
            try {
                $('*[eventType=event-query-submit]').click();
            } catch (ex) {}
        }
    });


});

try {
    $('*[eventType=event-query-submit]').click(function() {
        $('*[eventType=eventForm]').submit();
    });
} catch (ex) {}

// try {
//   $(".lay-btn-diy").click(function () {
//     $('*[eventType=eventForm]').submit();
//   });
// } catch (ex) { }


function selfTab(name, value) {
    $('*[name=' + name + "]").val(value);
    // $('*[eventType=event-query-submit]').click();
}



layui.use(['table', 'form', 'tree', 'laydate'], function() {
    laydate = layui.laydate //日期;
    lay('*[eventType=event-datetime]').each(function() {
        laydate.render({
            elem: this,
            type: 'datetime',
            trigger: 'click'
        });
    });

    $('select[eventType=event_query]', function(data) {
        // try {
        //   $(".lay-btn-diy").trigger("click");
        // } catch (ex) { }
        // try {
        //   $('*[eventType=event-query-submit]').click();
        // } catch (ex) { }


    });

});

/**
 * 删除数据
 * @param  {[type]} url [description]
 * @return {[type]}     [description]
 */
function editLine(url, title) {
    layer.confirm('是否确认修改？', {
        btn: ['确认', '取消'] //按钮
    }, function() {
        $.getJSON(url, function(data, textStatus) {
            if (data.status == '1') {
                layer.msg('修改成功');
                reloadDataTable();
            } else {
                layer.msg(data.msg);
            }
        });
    }, function() {});
}