layer.config({
    extend: 'extend/layer.ext.js'
});
var page = {
    initTableDataGrid: function () {
        var toolbars;
        if (type == 1) {
            toolbars = [
                {
                    text: tenderBtns.add,
                    iconCls: 'icon-add',
                    hidden: true,
                    handler: function () {
                        type = 1;
                        page.openDetailDialog(0, 0);
                    }
                }, {
                    text: tenderBtns.check,
                    iconCls: 'icon-book',
                    hidden: true,
                    handler: function () {
                        var rows = page.dataGrid.datagrid("getChecked");
                        if (rows.length != 1) {
                            layer.msg("请选择需要查看的");
                            return;
                        }
                        type = 0;
                        page.openDetailDialog(rows[0].id, 1);
                    }
                }];
        } else {
            toolbars = [
                {
                    text: tenderBtns.add,
                    iconCls: 'icon-add',
                    hidden: true,
                    handler: function () {
                        page.openDetailDialog(0, 0);
                    }
                }, {
                    text: '编辑',
                    iconCls: 'icon-edit',
                    hidden: true,
                    handler: function () {
                        var rows = page.dataGrid.datagrid("getChecked");
                        if (rows.length != 1) {
                            layer.msg(tenderTxt.txt11);
                            return;
                        }
                        page.openDetailDialog(rows[0].id, 0);
                    }
                }, {
                    text: tenderBtns.delete,
                    iconCls: 'icon-remove',
                    hidden: true,
                    handler: function () {
                        var rows = page.dataGrid.datagrid("getChecked");
                        if (rows.length != 1) {
                            layer.msg(tenderTxt.txt12);
                            return;
                        }
                        layer.confirm(tenderTitles.isDelete, function (index) {
                            $.ajax({
                                url: g.ctx + 'pc/tender/question/rest/remove',
                                type: "post",
                                data: {id: rows[0].id},
                                success: function () {
                                    layer.close(index);
                                    page.dataGrid.datagrid("reload");
                                }
                            })
                        });
                    }
                }, {
                    text: tenderBtns.check,
                    iconCls: 'icon-book',
                    hidden: true,
                    handler: function () {
                        var rows = page.dataGrid.datagrid("getChecked");
                        if (rows.length != 1) {
                            layer.msg("请选择需要查看的");
                            return;
                        }
                        type = 0;
                        page.openDetailDialog(rows[0].id, 1);
                    }
                }
            ];
        }

        var columns = [[
            {
                field: 'id',
                title: 'id',
                hidden: true
            }, {
                field: 'ck',
                checkbox: true
            },
            {
                field: 'supplier',
                title: '提问人',
                width: '12%',
                align: 'left',
                hidden: true
            },
            {
                field: 'question',
                title: tenderTitles.question,
                width: '24%',
                align: 'left',
                formatter: function (value) {
                    return '<div style="width=250px;word-break:break-all;word-wrap:break-word;white-space:pre-wrap;">' + value + '</div>';
                }
            },
            {
                field: 'answer',
                title: tenderBtns.answer,
                width: '27%',
                align: 'left',
                formatter: function (value) {
                    return '<div style="width=250px;word-break:break-all;word-wrap:break-word;white-space:pre-wrap;">' + value + '</div>';
                }
            },
            {
                field: 'questionTime',
                title: tenderTitles.questionTime,
                width: '140',
                align: 'left'
            },
            {
                field: 'answerTime',
                title: tenderTitles.answerTime,
                width: '140',
                align: 'left'
            },
            {
                field: 'questionFileId',
                title: '附件',
                width: '17%',
                align: 'left',
                formatter: function (val) {
                    if (val) {
                        return '附件[' + val.split(",").length + "]";
                    } else {
                        return '附件[' + val.length + "]";
                    }

                }
            },
        ]];

        page.dataGrid = $('#tb').datagrid({
            iconCls: 'icon-list',
            width: '100%',
            height: '100%',
            url: g.ctx + 'pc/tender/question/rest/list',
            singleSelect: true,
            pagination: true,
            method: "GET",
            loadMsg: tenderTxt.dataLoad,
            rownumbers: true,
            nowrap: false,
            remoteSort: true,
            striped: true,
            page: 1,
            pageSize: 20,
            queryParams: {
                page: 1,
                rows: 10,
                tenderId: tenderId
            },
            columns: columns,
            toolbar: toolbars
        });
        if(isSupplier==='0'){
            $('#tb').datagrid('showColumn','supplier');
        }
    },
    openDetailDialog: function (id, readonly) {
        var url = g.ctx + "pc/tender/question/page/detail?id=" + id
            + "&tenderId=" + tenderId + "&type=" + type + "&readonly=" + readonly;
        page.infoDialogElement = openDialog("questionDetailDialog", url, {
            title: "答疑",
            width: 600,
            height: 800,
            cache: false,
            modal: true,
            border: false,
            draggable: false,
            shadow: false,
            onClose: function () {
                page.infoDialogElement.dialog('destroy');
            },
            buttons: [{
                text: '保存',
                handler: function () {
                    topWin.frames['questionDetailDialog'].page.save(() => {
                        page.infoDialogElement.dialog('destroy');
                        page.dataGrid.datagrid("reload");
                    });
                }
            }, {
                text: '关闭',
                handler: function () {
                    page.infoDialogElement.dialog('close');
                }
            }]
        });
    }
};

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