/**
 * 公司主数据选择器
 * view/basis/company_select.ftl
 */
'use strict';
var el = {
    searchForm: '#searchForm',
    code: '#code',
    name: '#name',
    tb: '#tb',
    bodyHeight: document.documentElement.clientHeight
};
$(function () {
    loadDateGird();
});


/**
 * 初始化表格
 */
function loadDateGird() {
    var searchHeight = $(el.searchForm).outerHeight();
    var pager = initGridPagerNew(el.bodyHeight - searchHeight, 37, 40);

    $(el.tb).datagrid({
        loadMsg: '数据远程载入中,请等待...',
        url: g.ctx + 'pc/sys/exchangeRate/rest/list',
        method: "GET",
        queryParams: {},
        singleSelect: true,
        pagination: true,
        pageSize: pager.pageSize,
        pageList: pager.pageList,
        rownumbers: true,
        height: el.bodyHeight - searchHeight,
        columns: [
            [
                {
                    field: 'exchangeType',
                    title: '当月初汇率类型',
                    width: 300
                },
                {
                    field: 'originalCurrency',
                    title: '从货币',
                    width: 200
                },
                {
                    field: 'convertExchange',
                    title: '到货币',
                    width: 200
                },
                {
                    field: 'monthDate',
                    title: '日期',
                    width: 200
                },
                {
                    field: 'exchange',
                    title: '汇率',
                    width: 200
                },
                {
                    field: 'createTime',
                    title: '创建时间',
                    width: 150,
                    formatter: function (value) {
                        if (!value) {
                            return '';
                        }
                        return moment(value).format('YYYY-MM-DD');
                    }
                }
            ]
        ],
        // toolbar: [
        //     {
        //         text: '同步',
        //         iconCls: 'icon-reload',
        //         handler: function () {
        //             reload();
        //         }
        //     }
        // ]
    });
}


function reload() {
    topWin.layer.confirm('您确认要从SAP同步汇率信息吗?', {icon: 3}, function (index) {
        var index_wait;
        $.ajax({
            url: g.ctx + 'pc/sys/exchangeRate/rest/sync',
            type: 'GET',
            beforeSend: function () {
                index_wait = top.layer.load(0, {  //发送请求前调用load方法
                    shade: [0.5, '#fff'],  //0.5透明度的白色背景
                });
            },
            complete: function () {
                top.layer.close(index_wait);
            },
            success: function () {
                topWin.layer.close(index);
                topWin.layer.msg('同步成功', {icon: 1});
                $(el.tb).datagrid('reload');
            },
            error: function (data) {
                topWin.layer.alert(data.responseJSON.message, {icon: 2});
            }
        });
    });
}