/*
 * The Hefei Ceshi RDC(Research and Development Centre) Group.
 * __________________
 *
 *    Copyright 2015-2020
 *    All Rights Reserved.
 *
 *    NOTICE:  All information contained herein is, and remains
 *    the property of Ceshi Company and its suppliers,
 *    if any.
 */

/**
 *
 * @author BOGON
 * @version 1.0
 */
var page = {
    submitFlag: 0,
    initDatagrid: function () {
        var addBtn = {
            text: '新增',
            iconCls: 'icon-add',
            handler: function () {
                page.addOrEdit(0, 0);
            }
        };
        var ediBtn = {
            text: '编辑',
            id: 'edit_btn',
            iconCls: 'icon-edit',
            handler: function () {
                var row = $('#tb').datagrid('getSelected');
                if (row == null) {
                    topWin.layer.msg("请选择需要修改的模板!");
                    return;
                }
                var id = row.id;
                page.addOrEdit(id, 0);
            }
        };
        var delBtn = {
            text: '删除',
            iconCls: 'icon-remove',
            id: 'del_btn',
            handler: function () {
                page.del();
            }
        };

        var copyBtn = {
            text: '复制',
            iconCls: 'icon-plan-copy',
            handler: function () {
                var row = $('#tb').datagrid('getSelected');
                if (row == null) {
                    topWin.layer.msg("请选择需要复制的模板!");
                    return;
                }
                var id = row.id;
                page.addOrEdit(id, 1);
            }
        };
        var toolbar = [addBtn, "-", ediBtn, "-", delBtn, "-", copyBtn];
        $('#tb').datagrid({
            height: $(window).height() - $('#searchForm').outerHeight() - 10,
            width: '100%',
            url: g.ctx + 'pc/tender/template/rest/list',
            singleSelect: true,
            pagination: true,
            autoHeight: true,
            method: "GET",
            loadMsg: "数据加载中......",
            rownumbers: true,
            fitColumns: false,
            nowrap: true,
            remoteSort: true,
            striped: true,
            page: 1,
            pageSize: 10,
            queryParams: {
                page: 1,
                rows: 10
            },
            toolbar: toolbar,
            columns: [[
                {
                    field: 'ck',
                    checkbox: true
                },

                {
                    field: 'tenderTypeId',
                    title: '招标类型',
                    width: '10%',
                    align: 'left',
                    formatter: function (val) {
                        return getNecessaryDataName("pc/tender/type/rest/listAll", val)
                    }
                },
                {
                    field: 'purchaseGroupCode',
                    title: '采购组编码',
                    width: '10%',
                    align: 'left'
                },
                {
                    field: 'tt',
                    title: '采购组名称',
                    width: '10%',
                    align: 'left',
                    formatter: function (value, row, index) {
                        if (value) {
                            return getNecessaryDataName("pc/sys/purchaseGroup/rest/listAll", row.purchaseGroupCode)
                        }
                        if (row.purchaseGroupCode && row.purchaseGroupCode == 0) {
                            return "全体采购组";
                        }
                    }
                },
                {
                    field: 'name',
                    title: '模板名称',
                    width: '25%',
                    align: 'left'
                }, {
                    field: 'createName',
                    title: '创建人',
                    width: '10%',
                    align: 'left'
                }, {
                    field: 'createTime',
                    title: '创建时间',
                    width: '10%',
                    align: 'left'
                }
            ]]
        })
    },
    closeDialog: function () {
        page.dialogElement.dialog('close');
        $('#tb').datagrid("reload");
    },
    addOrEdit: function (id, type) {
        page.dialogElement = openDialog("editDialog", g.ctx + "pc/tender/template/page/detail?type=" + type + "&id=" + id, {
            title: (id == 0 ? "新增" : "编辑") + "模板",
            width: 1200,
            height: 753,
            cache: false,
            modal: true,
            border: false,
            draggable: false,
            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');
                }
            }]
        });
    },
    del: function () {
        if (page.submitFlag == 1) {
            return
        }
        var cbox = $('#tb').datagrid('getSelected');
        if (!cbox) {
            parent.topWin.layer.msg("请选中一条记录");
            return;
        }
        var id = cbox.id;
        page.submitFlag = 1
        parent.topWin.layer.confirm('是否删除？', {icon: 3}, function (index) {
            var url = g.ctx + 'pc/tender/template/rest/remove';
            $.ajax({
                url: url,
                method: "POST",
                data: {id: id},
                complete: function () {
                    page.submitFlag = 1
                },
                success: function (data) {
                    $('#tb').datagrid('load');
                    parent.topWin.layer.closeAll('dialog');
                },
                error: function (data) {
                    topWin.layer.alert(data.responseJSON.message, {icon: 2});
                }
            });
        });
    }
}
$(function () {
    initNecessaryData("pc/tender/type/rest/listAll", 'id', 'name')
    initNecessaryData("pc/sys/purchaseGroup/rest/listAll", 'purchaseGroupCode', 'purchaseGroupName')
    page.initDatagrid();
})