/**
 * 商城发票信息维护页面
 * @Author qmx
 */
var el = {
    searchForm: '#searchForm',
    invoiceTitle: '#invoiceTitle',
    addOrEditDialog: null,
    tb: '#tb',
    bodyHeight: document.documentElement.clientHeight
};


/**
 * 文档就行
 */
$(function () {
    loadGrid();
});


/**
 * 表格加载
 */
function loadGrid() {
    var searchHeight = $(el.searchForm).outerHeight();
    var pager = initGridPagerNew(el.bodyHeight - searchHeight, 37, 40);

    $(el.tb).datagrid({
        loadMsg: '数据远程载入中, 请等待...',
        height: el.bodyHeight - searchHeight,
        url: '/pc/mall/invoice/rest/list',
        singleSelect: true,
        rownumbers: true,
        pagination: true,
        pageSize: pager.pageSize,
        pageList: pager.pageList,
        queryParams: {},
        columns: [
            [
                {
                    field: 'ck',
                    checkbox: true
                },
                {
                    field: 'companyCode',
                    title: '公司代码',
                    width: 90
                },
                {
                    field: 'invoiceTitle',
                    title: '抬头',
                    width: 200
                },
                {
                    field: 'invoiceNo',
                    title: '税号',
                    width: 200
                },
                {
                    field: 'invoiceTel',
                    title: '电话号码',
                    width: 100
                },
                {
                    field: 'invoiceAddress',
                    title: '单位地址',
                    width: 200
                },
                {
                    field: 'openingBank',
                    title: '开户银行',
                    width: 200
                },
                {
                    field: 'bankAccount',
                    title: '银行账户',
                    width: 200
                },
                {
                    field: 'bankTel',
                    title: '银行电话',
                    width: 200
                },
                {
                    field: 'bankAddress',
                    title: '银行地址',
                    width: 200
                }
            ]
        ],
        toolbar: [
            {
                text: '新增',
                iconCls: 'icon-add',
                id: 'btn_add',
                handler: function () {
                    addInvoice(0);
                }
            },
            '-',
            {
                text: '编辑',
                iconCls: 'icon-edit',
                id: 'btn_edit',
                handler: function () {
                    addInvoice(1);
                    setOperationLog("编辑按钮", "商城发票信息");
                }
            },
            '-',
            {
                text: '删除',
                id: 'btn_remove',
                iconCls: 'icon-remove',
                handler: function () {
                    delInvoice();
                    setOperationLog("删除按钮", "商城发票信息");
                }
            }
        ], onLoadSuccess: function () {
            displayButtonByUserRole('catalog.invoice.index');
            $(el.tb).datagrid('clearSelections');
        }
    });
}


/**
 * 新增、编辑
 *
 * @param type 0.新增 1.编辑
 */
function addInvoice(type) {
    var title = '新增购物发票', id = 0;
    if (Number(type) === 1) {
        title = '编辑购物发票';

        var row = $(el.tb).datagrid('getSelected');
        if (!row) {
            topWin.layer.alert('请选择一条记录进行操作', {icon: 0});
            return false;
        }

        id = row.id;
    }

    var url = '/pc/mall/invoice/page/detail?id=' + id;
    el.addOrEditDialog = openDialog('addOrEdit', url, {
        title: title,
        width: '800px',
        height: '400px',
        cache: false,
        modal: true,
        border: 'thin',
        onClose: function () {
            el.addOrEditDialog.dialog('destroy');
        },
        buttons: [
            {
                text: '保存',
                iconCls: 'icon-save',
                handler: function () {
                    topWin.frames['addOrEdit'].dataSave(function () {
                        topWin.layer.msg('数据保存成功', {icon: 1});
                        $(el.tb).datagrid('reload');
                        el.addOrEditDialog.dialog('close');
                    });
                }
            },
            {
                text: '关闭',
                iconCls: 'icon-clear',
                handler: function () {
                    $(el.tb).datagrid('clearSelections');
                    el.addOrEditDialog.dialog('close');
                }
            }
        ]
    });
}


/**
 * 删除购物发票
 */
function delInvoice() {
    var row = $(el.tb).datagrid('getSelected');
    if (!row) {
        topWin.layer.alert('请选择一条记录进行操作', {icon: 0});
        return;
    }

    topWin.layer.confirm('您确定要删除选中的记录吗?', {icon: 3}, function (index) {
        topWin.layer.close(index);
        $.ajax({
            type: 'POST',
            url: '/pc/mall/invoice/rest/del',
            data: {invoiceId: row.id},
            success: function () {
                topWin.layer.msg('删除成功', {icon: 1});
                $(el.tb).datagrid('load');
            },
            error: function (data) {
                topWin.layer.alert(data.responseJSON, {icon: 2});
            }
        });
    });
}


/**
 * 搜索
 */
function dataSearch() {
    var queryParams = {};
    queryParams.invoiceTitle = $.trim($(el.invoiceTitle).val());
    $(el.tb).datagrid('options').queryParams = queryParams;
    $(el.tb).datagrid('load');
}


/**
 * 重置查询
 */
function resetSearch() {
    $(el.invoiceTitle).val('');
    dataSearch();
}