/**
 * 供应商名称转换明细表
 */
'use strict';
var el = {
    searchForm: '#searchForm',
    code: '#code',
    simplified: '#simplified',
    traditional: '#traditional',
    tb: '#tb',
    addOrEditDialog: null,
    lookDialog: null,
    bodyHeight: document.documentElement.clientHeight
};


$(function () {

    loadGrid();
});


function loadGrid() {
    var searchHeight = $(el.searchForm).outerHeight();
    var pager = initGridPagerNew(el.bodyHeight - searchHeight, 40, 30);

    $(el.tb).datagrid({
        loadMsg: '数据加载中......',
        url: g.ctx + 'pc/sys/supplierNameConvert/rest/list',
        singleSelect: true,
        height: el.bodyHeight - searchHeight,
        pagination: true,
        pageSize: pager.pageSize,
        pageList: pager.pageList,
        rownumbers: true,
        queryParams: {},
        columns: [
            [
                {
                    field: 'ck',
                    checkbox: true
                },
                {
                    field: 'code',
                    title: '供应商编号',
                    width: 250
                },
                {
                    field: 'simplified',
                    title: '简称',
                    width: 250
                },
                {
                    field: 'traditional',
                    title: '繁体',
                    width: 100
                },
                {
                    field: 'createTime',
                    title: '创建日期',
                    width: 150
                }
            ]
        ],
        toolbar: [
            {
                text: '新增',
                iconCls: 'icon-add',
                handler: function () {
                    addOrEdit(0);
                }
            },
            "-",
            {
                text: '编辑',
                iconCls: 'icon-edit',
                handler: function () {
                    addOrEdit(1);
                }
            },
            "-",
            {
                text: '删除',
                iconCls: 'icon-remove',
                handler: function () {
                    delFactory();
                }
            }
        ]
    });
}


/**
 * 新增或编辑采购组
 *
 * @param type 0:新增 1:编辑
 */
function addOrEdit(type) {
    var id= '';
    if(type===1){
        var cbox = $('#tb').datagrid('getSelected');
        if (cbox === null || cbox === undefined) {
            layer.msg("请选中一条记录");
            return;
        }
        id = cbox.id;
    }

    var NameConvertDialog = openDialog('NameConvertDialog', g.ctx + 'pc/sys/supplierNameConvert/detail?id=' + id, {
        title: type===0?'新增':'编辑',
        width: '700px',
        height: '520px',
        cache: false,
        modal: true,
        border: false,
        draggable: false,
        shadow: false,
        onClose: function () {
            NameConvertDialog.dialog('destroy');
        },
        buttons: [
            {
                text: itablei8n.save,
                iconCls: 'icon-save',
                handler: function () {
                    topWin.frames['NameConvertDialog'].saveOrUpdate(function () {
                        topWin.layer.msg(tipsi18n.czcg, {icon: 1});
                        $(el.tb).datagrid('reload');
                        NameConvertDialog.dialog('close');
                    });
                }
            },
            {
                text: itablei8n.close,
                iconCls: 'icon-cancel',
                handler: function () {
                    NameConvertDialog.dialog('close');
                }
            }
        ]
    });
}

/**
 * 删除采购组
 */
function delFactory() {
    var cbox = $('#tb').datagrid('getSelected');
    if (cbox === null || cbox === undefined) {
        layer.msg("请选中一条记录");
        return;
    }

    var id = cbox.id;

    parent.layer.confirm('是否删除？', function (index) {
        var loading = top.layer.msg('处理中', {icon: 16, shade: [0.1, '#000'], time: false});
        var url = g.ctx + 'pc/sys/supplierNameConvert/rest/remove';
        $.ajax({
            url: url,
            method: "post",
            data: {id: id},
            success: function (data) {
                parent.layer.close(index);
                $(el.tb).datagrid('reload');
            },
            complete: function(data) {
                top.layer.close(loading);
            },
            error: function (data) {
                layer.close(index);
                layer.alert(JSON.parse(data.responseText).message);
            }
        });
    });
}


