$(function () {
    loadDataGride();

});

function loadDataGride() {
    var toolbars = [{
        text: '新增', id: 'btn_add', iconCls: 'icon-add', handler: function () {
            openAddOrEditDialog()
        }
    }, "-", {
        text: '编辑', id: 'btn_edit', iconCls: 'icon-edit', handler: function () {
            var row = $('#tb').datagrid('getSelected');
            if (!row) {
                topWin.layer.msg("请选中一条记录");
                return;
            }
            if (row.state !== 1 && row.state !== 4) {
                topWin.layer.msg("只有新建和未通过状态可以编辑");
                return;
            }
            openAddOrEditDialog(row.id)
        }
    }, "-", {
        text: '启用|禁用', iconCls: 'icon-remove', handler: function () {
            var row = $('#tb').datagrid('getSelected');
            if (!row) {
                topWin.layer.msg("请选中一条记录");
                return;
            }
            var state = row.state;
            var msg = "是否修改状态?";
            var new_state = 1;
            if (state === 1) {
                msg = "是否禁用?";
                new_state = 0;
            }
            if (state === 0) {
                msg = "是否启用?";
            }
            topWin.layer.confirm(msg, {icon: 3}, function (index) {
                $.ajax({
                    url: g.ctx + 'pc/supplier/qualification/rest/updateState',
                    data: {id: row.id, state: new_state},
                    type: "POST",
                    success: function (data) {
                        topWin.layer.alert("保存成功", {icon: 1}, function (idx) {
                            topWin.layer.close(idx);
                            $('#tb').datagrid('reload');
                        });
                    },
                    error: function (data) {
                        topWin.layer.alert(data.responseJSON.message, {icon: 2});
                    }
                })
            })
        }
    }]
    if (selected === 1) {
        toolbars = []
    }

    $('#tb').datagrid({
        iconCls: 'icon-list',
        title: '',
        method: "GET",
        singleSelect: single === 1,
        pagination: true,
        nowrap: true,
        idField: 'id',
        pageSize: 50,
        loadMsg: "数据加载中......",
        rownumbers: true,
        striped: true,
        url: g.ctx + 'pc/supplier/qualification/rest/list?codes=' + codes,
        columns: [[{field: 'ck', checkbox: true}, {
            field: 'state', title: '状态', width: '60', align: 'left', formatter: function (value) {
                if (value === 1) {
                    return "<font color='green'>正常</font>"
                }
                if (value === 0) {
                    return "<font color='red'>禁用</font>"
                }
            }
        }, {
            field: 'version', title: '版本', width: '150', align: 'left'
        }, {
            field: 'scene', title: '类型', width: '150', align: 'left',
            formatter: function (value, row) {
                switch (value) {
                    case 1:
                        return '资质'
                    case 2:
                        return '协议'
                    case 3:
                        return '认证报告'
                }
            }
        }, {
            field: 'code', title: '资质编码', width: '150', align: 'left'
        }, {
            field: 'name', title: '资质名称', width: '150', align: 'left'
        }, {
            field: 'remark', title: '备注', width: '160', align: 'left'
        }, {
            field: 'fileId', title: '样例文件', width: '250', align: 'left', formatter: function (value, row) {
                if (value) {
                    var fileHtml = "<a href='" + g.ctx + "pc/sys/file/rest/download/" + value + "'>" + row.fileName + "</a>";
                    return fileHtml;
                }
            }
        }, {
            field: 'createName', title: '创建人', width: '100', align: 'left'
        }, {
            field: 'createTime', title: '创建时间', width: '150', align: 'left'
        }]],
        height: $(window).height() - $('#searchForm').outerHeight() - 10,
        width: '100%',
        toolbar: toolbars
    });
}

function openAddOrEditDialog(id) {

    var qualificationEditDialog = openDialog('qualificationEditDialog', g.ctx + 'pc/supplier/qualification/detail?scene=' + scene + '&id=' + (id == undefined ? '' : id), {
        title: id == undefined ? "新增" : "编辑",
        width: '370px',
        height: '460px',
        cache: false,
        modal: true,
        border: false,
        draggable: true,
        shadow: false,
        onClose: function () {
            qualificationEditDialog.dialog('destroy');
        },
        buttons: [{
            text: itablei8n.save, handler: function () {
                topWin.frames['qualificationEditDialog'].save(function () {
                    qualificationEditDialog.dialog('close');
                    $('#tb').datagrid('reload');
                });
            }
        }, {
            text: itablei8n.close, handler: function () {
                qualificationEditDialog.dialog('close');
            }
        }]
    });
}

function getChecked(call) {
    var rows = $('#tb').datagrid('getChecked');
    if (rows.length <= 0) {
        topWin.layer.msg("请选中至少一条记录");
        return;
    }
    call(rows)
}