/*
 * The Hefei Ceshi RDC(Research and Development Centre) Group.
 * __________________
 *
 *    Copyright 2015-2020
 *    All Rights Reserved.
 *
 *    NOTICE:  All information contained herein is, and remains
 *    the property of Ceshi Company and its suppliers,
 *    if any.
 */

var page = {
    edit: function (id) {
        var url = g.ctx + "pc/tender/securityDeposit/page/detail?id=" + id;
        page.bondAccountDetailDialog = openDialog("bondAccountDetailDialog",
            url, {
                title: (id === 0 ? '新增' : '编辑') + '保证金账号',
                width: 400,
                height: 600,
                cache: false,
                modal: true,
                border: false,
                draggable: false,
                shadow: false,
                onClose: function () {
                    page.bondAccountDetailDialog.dialog('destroy');
                },
                buttons: [{
                    text: '确定',
                    handler: function () {
                        topWin.frames['bondAccountDetailDialog'].page.save(() => {
                            page.bondAccountDetailDialog.dialog('close');
                            page.loadData();
                        });

                    }
                }, {
                    text: '关闭',
                    handler: function () {
                        page.bondAccountDetailDialog.dialog('close');
                    }
                }]
            });
    },
    initTable: function () {
        var toolbar = [{
            text: '新增',
            iconCls: 'icon-add',
            hidden: true,
            handler: function () {
                page.edit(0);
            }
        }, "-",
            {
                text: '编辑',
                iconCls: 'icon-edit',
                id: "tender_edit_btn",
                handler: function () {
                    var rows = page.datagrid.datagrid('getChecked');
                    if (rows.length != 1) {
                        topWin.layer.msg("请选择一条需要修改的银行账号!");
                        return;
                    }
                    var id = rows[0].id;
                    page.edit(id);
                }
            }, "-",
            {
                text: '删除',
                iconCls: 'icon-remove',
                handler: function () {
                    var rows = page.datagrid.datagrid('getChecked');
                    if (rows.length <= 0) {
                        topWin.layer.msg("请选择需要删除的银行账号!");
                        return;
                    }
                    var ids = [];
                    $.each(rows, function (index, item) {
                        ids.push(item.id);
                    });
                    topWin.confirm('是否删除？', function (index) {
                        $.ajax({
                            url: g.ctx + "pc/tender/securityDeposit/rest/remove",
                            type: "post",
                            data: {ids: ids.join(',')},
                            success: function (data) {
                                page.loadData()
                                parent.topWin.layer.closeAll('dialog');
                                topWin.layer.msg("删除成功")
                            },
                            error: function (data) {
                                topWin.layer.alert(data.responseJSON.message, {icon: 2});
                            }
                        });

                    });

                }
            }]
        var columns = [[
            {
                field: 'ck',
                checkbox: true
            },
            {
                field: 'id',
                hidden: true
            },
            {
                field: 'name',
                title: '账号名称',
                width: 120,
                align: 'left'
            }, {
                field: 'bankNo',
                title: '银行账号',
                width: 120,
                align: 'left'
            },
            {
                field: 'openBank',
                title: '开户行',
                width: 120,
                align: 'left'
            },
            {
                field: 'companyName',
                title: '公司内容',
                width: 120,
                align: 'left'
            },
            {
                field: 'contacts',
                title: '联系人',
                width: 120,
                align: 'left'
            },
            {
                field: 'payType',
                title: '付款方式',
                width: 120,
                align: 'left',
                formatter: function (value, row, index) {
                    return getDicName('payment_way', value)
                }

            },
            {
                field: 'currency',
                title: '币种',
                width: 120,
                align: 'left',
                formatter: function (value, row, index) {
                    return getDicName('currency', value)
                }
            }
        ]];
        page.datagrid = $('#tb').datagrid({
            iconCls: 'icon-list',
            width: '100%',
            height: $(window).height() - 20,
            singleSelect: false,
            pagination: false,
            loadMsg: "数据加载中......",
            rownumbers: true,
            fitColumns: true,
            nowrap: true,
            remoteSort: true,
            striped: true,
            columns: columns,
            toolbar: toolbar
        });
        page.loadData();
    },
    loadData: function () {
        $.ajax({
            url: g.ctx + 'pc/tender/securityDeposit/rest/listAll',
            type: 'get',
            success: function (d) {
                page.datagrid.datagrid("loadData", d);
            }
        });
    },
    getChecked: function () {
        var rows = page.datagrid.datagrid("getChecked");
        if (rows.length != 1) {
            topWin.layer.alert("请选择账号信息", {icon: 2});
            return [];
        }
        return rows;
    }
}
$(function () {
    initDict('payment_way,currency')
    page.initTable();
})