var page;
var el = {
    id: '#id',
    title: '#title',
    tb: '#tb',
    createTime: '#createTime',
    createUserName: '#createUserName',
    messageTypeName: '#messageTypeName',
    ccNames: '#ccNames',
    addresseeNames: '#addresseeNames',
    originalRows: [],
    attachmentIdList: '#attachmentIdList',
    attachments: '.attachments',
    messageReplyContent: '#messageReplyContent',
    replayList: '#replayList',
    messageAddresseeId: '#messageAddresseeId',
    messageContent: '#messageContent',
    bodyHeight: document.documentElement.clientHeight
};

$(function () {
    // page.initMessageData();

    loadReceiverGrid();
});

/**
 * 加载收信人数据
 */
function loadReceiverGrid() {
    var url = g.ctx + 'pc/message/addressee/rest/list?messageCenterId='+$(el.id).val();
    var columns = [
        {
            field: 'addresseeCode',
            title: '收件人账号',
            width: '10%',
            align: 'left'
        },
        {
            field: 'addresseeName',
            title: '收件人名称',
            width: '10%',
            align: 'left'

        },
        {
            field: 'isSupplierConfirmed',
            title: '是否确认',
            width: '10%',
            align: 'left',
            exportEnum:[
                {code:0,codeWithDesc:'未确认'},
                {code:1,codeWithDesc:'已确认'}
            ],
            formatter: function (val, row) {
                if (val ===0)
                    return  "<font color='red'>未确认</font>";
                else
                    return "已确认"
            }
        },
        {
            field: 'isSupplierReplied',
            title: '是否回复',
            width: '10%',
            align: 'left',
            exportEnum:[
                {code:0,codeWithDesc:'未回复'},
                {code:1,codeWithDesc:'已回复'}
            ],
            formatter: function (val, row) {
                if (val ===0)
                    return  "<font color='red'>未回复</font>";
                else
                    return "已回复"
            }
        },
        {
            field: 'replyContent',
            title: '回复内容',
            width: '30%',
            align: 'left'
        },
        {
            field: 'replyFile',
            title: '回复附件',
            width: '30%',
            align: 'left',
            exportHidden: true,
            formatter: function (value, row, index) {
                return '<div class="supplier-file-form custom-file-form-style" data-options=\'{"deleteEnable":false, "previewEnable":true, "uploadEnable": false, "moreFileUploadEnable": true, "fileSize": 50, "fileTypes": ".xls,.doc,.txt,.pdf"}\' data-fileIds = "'+value+'"></div>';
            }
        }
    ];
    $(el.tb).datagrid({
        iconCls: 'icon-list',
        loadMsg: '数据加载中......',
        url: url,
        height: el.bodyHeight - 40,
        pageSize: 100,
        pageList: [10, 50, 100, 200, 500, 1000],
        singleSelect: false,
        pagination: true,
        rownumbers: true,
        loadFilter: pagerFilter,
        columns: [
            columns
        ],
        toolbar: [
            {
                text: '导出',
                iconCls: 'icon-print',
                handler: function () {
                    openExportDialog('pc/message/addressee/rest/export?messageCenterId='+$(el.id).val(),'tb', {});
                }
            }
        ],
        onLoadSuccess: function (data) {
            initFileForm('supplier-file-form');
        },
    });
}

function pagerFilter(data) {
    if (typeof data.length == 'number' && typeof data.splice == 'function')     {   // is array
        data = {
            total: data.length,
            rows: data
        }
    }
    var dg = $(this);
    var opts = dg.datagrid('options');
    var pager = dg.datagrid('getPager');
    pager.pagination({
        onSelectPage: function (pageNum, pageSize) {
            opts.pageNumber = pageNum;
            opts.pageSize = pageSize;
            pager.pagination('refresh', {
                pageNumber: pageNum,
                pageSize: pageSize
            });
            dg.datagrid('loadData', data);
        }
    });
    if (!data.originalRows) {
        data.originalRows = (data.rows);
        el.originalRows = (data.rows);
    }
    var start = (opts.pageNumber - 1) * parseInt(opts.pageSize);
    var end = start + parseInt(opts.pageSize);
    data.rows = (data.originalRows.slice(start, end));
    return data;
}








