var el = {
    searchForm: '#searchForm',
    tb: '#tb',
    btn_add: '#btn_add',
    btn_edit: '#btn_edit',
    btn_copy: '#btn_copy',
    btn_publish: '#btn_publish',
    btn_end: '#btn_end',
    btn_remove: '#btn_remove',
    btn_cancel: '#btn_cancel',
    btn_add_supplier: '#btn_add_supplier',
    bodyHeight: document.documentElement.clientHeight
};

$(function () {
    initDictName('message_type');
    // initNecessaryDatas('pc/sys/purchaseOrg/rest/listAll,pc/sys/purchaseGroup/rest/listAll,pc/sys/company/rest/listAll');
    initCombo();
    loadGrid();
});
/**
 * 初始化下拉框
 */
function initCombo() {
}
function loadGrid() {
    var searchHeight = $(el.searchForm).outerHeight();
    var pager = initGridPagerNew(el.bodyHeight - searchHeight, 37, 40);
    $(el.tb).datagrid({
        height: el.bodyHeight - searchHeight,
        url: g.ctx + 'pc/message/launch/rest/list',
        singleSelect: true,
        pagination: true,
        pageSize: pager.pageSize,
        pageList: pager.pageList,
        loadMsg: '数据加载中......',
        rownumbers: true,
        columns:[[
            {
                field: 'ck',
                checkbox: true
            },
            {
                field: 'isDraft',
                title: '状态',
                width: 90,
                formatter: function (value) {
                    switch (Number(value)) {
                        case 0:
                            return '<span style="color:black;">新建</span>';
                        case 1:
                            return '<span style="color:green;">已发布</span>';
                        default:
                            return '';
                    }
                }
            },
            {
                field: 'messageType',
                title: '公告类型',
                width: '10%',
                align: 'left',
                formatter: function (value, row) {
                    return getDicName('message_type',value);
                }
            },
            {
                field: 'title',
                title: '公告标题',
                width: '30%',
                align: 'left'
            },
            {
                field: 'addresseeNum',
                title: '收信人数量',
                width: '10%',
                align: 'center'
            },
             {
                field: 'evlPersons',
                title: '确认进度',
                width: '15%',
                align: 'center',
                formatter: function (val, row) {
                    if (row.isSupplierConfirm === 0) {
                        return "无需确认"
                    } else {
                        var evlPersons = row.confirmedNum +'/'+ row.addresseeNum;
                        return '<span style="color:  #0b69e3;">' + evlPersons + '</span>';
                    }
                }
            },
           {
                field: 'bidCount',
                title: '回复进度',
                width: '15%',
                align: 'center',
                formatter: function (val, row) {
                    if (row.isSupplierReply === 0) {
                        return "无需回复"
                    } else {
                        var bidCount = row.replyNum +'/'+ row.addresseeNum;
                        return '<span style="color:  #0b69e3;">' + bidCount + '</span>';
                    }
                }
            },
            {
                field: 'createTime',
                title: '发件时间',
                width: '10%',
                align: 'left',
                formatter: function (value, row, index) {
                    if (!value) {
                        return '';
                    }
                    return moment(value).format("YYYY-MM-DD HH:mm");
                }
            }
            ]],
        toolbar: [
            {
                text: '新增',
                iconCls: 'icon-add',
                id: 'btn_add',
                handler: function () {
                    detail(otpTypeEnum.ADD);
                }
            }, "-",
            {
                text: '编辑',
                id: 'btn_edit',
                iconCls: 'icon-edit',
                handler: function () {
                    detail(otpTypeEnum.EDIT);
                }
            }, "-",
            {
                text: '发布',
                iconCls: 'icon-ok',
                id: 'btn_publish',
                handler: function () {
                    publish();
                }
            },"-",
            {
                text: '删除',
                iconCls: 'icon-remove',
                id: 'btn_remove',
                handler: function () {
                    remove();
                }
            },"-",
            {
                text: '查看',
                iconCls: 'icon-find',
                id: 'btn_look',
                handler: function () {
                    look();
                }
            },
        ],
        onLoadSuccess: function () {
            displayButtonByUserRole("message_launch");
            $(el.tb).datagrid('clearSelections');
        },
        onDblClickRow: function () {
            look();
        },
        onCheck: function (rowIndex) {
            var rows = $(el.tb).datagrid('getRows');
            var isDraft = rows[rowIndex].isDraft;
            //状态；0-新建；1-发布
            switch (Number(isDraft)) {
                case 0:
                    $(el.btn_add).linkbutton('enable');
                    $(el.btn_edit).linkbutton('enable');
                    $(el.btn_publish).linkbutton('enable');
                    $(el.btn_remove).linkbutton('enable');
                    break;
                case 1:
                    $(el.btn_add).linkbutton('enable');
                    $(el.btn_edit).linkbutton('disable');
                    $(el.btn_publish).linkbutton('disable');
                    $(el.btn_remove).linkbutton('disable');
                    break;
                default:
                    break;
            }
        }
    });
}

function detail(optType){
    var id = '';
    if (optType===otpTypeEnum.EDIT){
        var row = $(el.tb).datagrid('getSelected');
        if (!row) {
            topWin.layer.alert('请选中一条记录', {icon: 0});
            return false;
        }
        id = row.id;
    }
    var dialogElement = openDialog("editDialog", g.ctx + "pc/message/launch/detail?id="+id+"&optType="+optType, {
        title: (optType === otpTypeEnum.ADD ? "新建公告" :"编辑公告"),
        width: '80%',
        height: '100%',
        cache: false,
        modal: true,
        border: false,
        draggable: false,
        shadow: false,
        onClose: function () {
            dialogElement.dialog('destroy');
        },
        buttons: [{
            text: '提交',
            iconCls: 'icon-save',
            handler: function () {
                topWin.frames['editDialog'].saveOrUpdate(function () {
                    topWin.layer.msg('数据保存成功', {icon: 1});
                    dialogElement.dialog('close');
                    $(el.tb).datagrid('reload');
                });
            }
        }, {
            text: '关闭',
            iconCls: 'icon-cancel',
            handler: function () {
                dialogElement.dialog('close');
            }
        }]
    });

}

function look(){
    var row = $(el.tb).datagrid('getSelected');
    if (!row) {
        topWin.layer.alert('请选中一条记录', {icon: 0});
        return false;
    }
    var lookDialogElement = openDialog("lookDialog", g.ctx + "pc/message/launch/look?id="+row.id, {
        title: "查看",
        width: '80%',
        height: '100%',
        cache: false,
        modal: true,
        border: false,
        draggable: false,
        shadow: false,
        onClose: function () {
            lookDialogElement.dialog('destroy');
        },
        buttons: [{
            text: '关闭',
            iconCls: 'icon-cancel',
            handler: function () {
                lookDialogElement.dialog('close');
            }
        }]
    });
}

/**
 * 发布
 */
function publish(){
    var row = $(el.tb).datagrid('getSelected');
    if (!row) {
        topWin.layer.alert('请选中一条记录', {icon: 0});
        return false;
    }
    var id = row.id;
    parent.layer.confirm('是否确认发布？', function (index) {
        var loading = top.layer.msg('处理中', {icon: 16, shade: [0.1, '#000'], time: false});
        $.ajax({
            url: g.ctx + 'pc/message/launch/rest/publish',
            method: "post",
            data: {id: id},
            success: function () {
                parent.layer.close(index);
                $(el.tb).datagrid('reload');
                $(el.tb).datagrid('clearSelections');
            },
            complete: function(data) {
                top.layer.close(loading);
            },
            error: function (data) {
                parent.layer.alert(JSON.parse(data.responseText).message,{icon: 2, shadeClose: true, title: '温馨提示'});
            }
        });
    });
}

/**
 * 删除
 */
function remove(){
    var row = $(el.tb).datagrid('getSelected');
    if (!row) {
        topWin.layer.alert('请选中一条记录', {icon: 0});
        return false;
    }
    var id = row.id;
    parent.layer.confirm('是否确认删除？', function (index) {
        var loading = top.layer.msg('处理中', {icon: 16, shade: [0.1, '#000'], time: false});
        $.ajax({
            url: g.ctx + 'pc/message/launch/rest/remove',
            method: "post",
            data: {id: id},
            success: function () {
                parent.layer.close(index);
                $(el.tb).datagrid('reload');
                $(el.tb).datagrid('clearSelections');
            },
            complete: function(data) {
                top.layer.close(loading);
            },
            error: function (data) {
                parent.layer.alert(JSON.parse(data.responseText).message,{icon: 2, shadeClose: true, title: '温馨提示'});
            }
        });
    });
}
