$(function () {
    page.initTableDataGrid();
    page.bindEvent();
});


var page = {
    dataGrid: {},
    dialogElement: {},
    bindEvent: function () {
        $("#search_btn").bind('click', page.tableReload);
        $("#reset_btn").bind('click', function () {
            $("#searchForm").form('reset');
        });
    },

    tableReload: function () {
        var params = {};
        var agreeName = $("#agreeName").val();
        page.dataGrid.datagrid("load", {"agreeName": agreeName});
    },

    initTableDataGrid: function () {
        var toolbars = [
            {
                text: '新增',
                iconCls: 'icon-add',
                id:'outside_icon_add',
                hidden: true,
                handler: function () {
                    page.editTender(0,0);
                }
            }, "-",
            {
                text: '编辑',
                iconCls: 'icon-edit',
                id:'outside_icon_edit',
                hidden: true,
                handler: function () {
                    var row = $('#tb').datagrid('getSelected');
                    if (!row) {
                        parent.layer.msg("请选中一条记录！");
                        return;
                    }
                    var id = row.id;
                    page.editTender(id,1);
                }
            }, "-",
            {
                text: '复制',
                iconCls: 'icon-cut',
                id:'outside_icon_cut',
                hidden: true,
                handler: function () {
                    var row = $('#tb').datagrid('getSelected');
                    if (!row) {
                        parent.layer.msg("请选中一条记录！");
                        return;
                    }
                    var id = row.id;
                    page.editTender(id,2);
                }
            }, "-",
            {
                text: '删除',
                iconCls: 'icon-remove',
                id:'outside_icon_remove',
                hidden: true,
                handler: function () {
                    var row = $('#tb').datagrid('getSelected');
                    if (!row) {
                        parent.layer.msg("请选中一条记录！");
                        return;
                    }
                    var id = row.id;
                    page.delTender(id);
                }
            }
        ];

        var columns = [[
            {
                field: 'ck',
                checkbox: true
            }, {
                field: 'id',
                hidden: true
            },
            {
                field: 'name',
                title: '协议名称',
                width: 90,
                align: 'left',

            },
            {
                field: 'agreementVersion',
                title: '协议版本',
                width: 90,
                align: 'left'
            },
            {
                field: 'createUserName',
                title: '创建人',
                width: 120,
                align: 'center',
            },
            {
                field: 'createTime',
                title: '创建时间',
                width: 150,
                align: 'left',
                formatter: function (v, r) {
                    if (v != null && v != undefined && v != '') {
                        return DateTimeFormatMs(v);
                    }
                    return "";
                }
            }
        ]];

        page.dataGrid = $('#tb').datagrid({
            iconCls: 'icon-list',
            width: '100%',
            height: $(window).height() - $('#searchForm').outerHeight() - 10,
            url: g.ctx + 'pc/supplier/supplierAgreement/rest/list',
            singleSelect: true,
            pagination: true,
            loadMsg: "数据加载中......",
            rownumbers: true,
            fitColumns: true,
            nowrap: true,
            remoteSort: true,
            striped: true,
            page: 1,
            pageSize: 20,
            queryParams: {
                page: 1,
                rows: 10
            },
            columns: columns,
            toolbar: toolbars,

        });
        page.tableReload();
    },
    delTender:function (id) {
        parent.layer.confirm('是否确认删除', {icon: 3, title:'提示'}, function(index){
            $.ajax({
                url:g.ctx+"pc/supplier/supplierAgreement/rest/remove",
                method:"post",
                async:false,
                data:{"id":id},
                success: function () {
                    parent.layer.msg('操作成功', {icon: 1});
                    page.tableReload();
                },
                error: function (data) {
                    parent.layer.alert(data.responseJOSN.message, {icon: 2});
                }
            });
            layer.close(index);
        });

    },
    editTender: function (id,type) {
        page.dialogElement = openDialog("editDialog", g.ctx + "pc/supplier/supplierAgreement/detail?id="+id+"&type="+type, {
            title: (type == 0 ? "新增" :type==1?"编辑":"复制"),
            width: '95%',
            height: '90%',
            cache: false,
            modal: true,
            border: false,
            draggable: true,
            shadow: false,
            onClose: function () {
                page.dialogElement.dialog('destroy');
            },
            buttons: [{
                text: '提交',
                handler: function () {
                    topWin.frames['editDialog'].saveOrUpdate(page.closeDialog);
                }
            }, {
                text: '关闭',
                handler: function () {
                    page.dialogElement.dialog('close');
                }
            }]
        });
    },

    closeDialog: function () {
        page.dialogElement.dialog('close');
        page.tableReload();
    }

};

