var el = {
    isSupplier: '#isSupplier',
    id: '#id',
    searchForm: '#searchForm',
    supCodeOrName: '#supCodeOrName',
    companyCode: '#companyCode',
    currencyCode: '#currencyCode',
    paymentType: '#paymentType',
    tb: '#tb',
    defaultColumns: [],
    bodyHeight: document.documentElement.clientHeight
};

$(function () {
    initDict('currency')
    initNecessaryDatas('pc/sys/company/rest/listAll')
    initComBoBoxData();
    el.defaultColumns = [
        {
            field: 'paymentPlanNo',
            title: '付款计划单号',
            width: 140,
            sortable: true
        },
        {
            field: 'supplierCode',
            title: '供应商编码',
            width: 140,
            sortable: true
        },
        {
            field: 'supplierName',
            title: '供应商全称',
            width: 140,
            sortable: true
        },
        {
            field: 'companyCode',
            title: '公司',
            width: 240,
            sortable: true,
            exportEnum:getNecessaryDataList('pc/sys/company/rest/listAll'),
            formatter: function (value) {
                return getNecessaryDataName('pc/sys/company/rest/listAll',value);
            }
        },
        {
            field: 'currencyCode',
            title: '币种',
            width: 80,
            sortable: true,
            formatter: function (value) {
                return getDicName('currency',value)
            }
        },
        {
            field: 'paymentPlanYear',
            title: '付款计划年份',
            width: 100,
            sortable: true,
            editor: {
                type: 'textbox',
                // editable: false
                options: {
                    editable: false,
                }
            }
        },
        {
            field: 'paymentPlanMonth',
            title: '付款计划月份',
            width: 100,
            sortable: true,
            editor: {
                type: 'textbox',
                options: {
                    editable: false,
                }
            }
        } ,
        {
            field: 'paymentType',
            title: '类型',
            width: 100,
            sortable: true,
            formatter: function (value) {
                if (value && value == 'D') {
                    return "对账款";
                } else if (value && value == 'S') {
                    return "预付款";
                } else {
                    return '';
                }
            }
        } ,
        {
            field: 'actualPayAmount',
            title: '应付金额',
            width: 90,
            sortable: true,
            formatter: function (value) {
                return formatterCustomPrice(value,2,true,false)
            }
        },
        {
            field: 'appliedUnpaidAmount',
            title: '已请款未付金额(历史)',
            width: 160,
            sortable: true,
            formatter: function (value) {
                return formatterCustomPrice(value,2,true,false)
            }
        },
        {
            field: 'appliedPaymentAmount',
            title: '本月已请款金额',
            width: 100,
            sortable: true,
            formatter: function (value) {
                return formatterCustomPrice(value,2,true,false)
            }
        },
        {
            field: 'differenceAmount',
            title: '差异金额',
            width: 90,
            sortable: true,
            formatter: function (value) {
                return formatterCustomPrice(value,2,true,false)
            }
        },
        {
            field: 'differencePercent',
            title: '差异比例',
            width: 90,
            sortable: false,
            formatter: function (value) {
                if (value && value === 1) {
                    return 100 + "%";
                } else  if (value && value != 0) {
                    return formatterCustomPrice(value * 100,2,true,false) + "%";
                } else {
                    return formatterCustomPrice(value,2,true,false)
                }
            }
        },
        {
            field: 'paidAmountMonth',
            title: '本月已付款金额',
            width: 100,
            sortable: true,
            formatter: function (value) {
                return formatterCustomPrice(value,2,true,false)
            }
        },
        {
            field: 'remark',
            title: '备注',
            width: 150,
            sortable: false,
            editor: {
                type: 'textbox'
            }
        }
    ]
    loadGrid(el.defaultColumns);
})

function loadGrid(column) {
    var searchHeight = $(el.searchForm).outerHeight(), isSupplier = $(el.isSupplier).val();
    var pager = initGridPagerNew(el.bodyHeight - searchHeight, 37, 40);
    
    $(el.tb).datagrid({
        loadMsg: '数据加载中......',
        emptyMsg: '暂无数据',
        url: g.ctx + 'pc/finance/payment/plan/summary/rest/list',
        height: el.bodyHeight - searchHeight,
        singleSelect: true,
        rownumbers: true,
        // 定义从服务器对数据进行排序
        remoteSort: true,
        // 定义是否允许多列排序
        // multiSort: true,
        pagination: true,
        pageSize: pager.pageSize,
        pageList: pager.pageList,
        queryParams: {
            ppId: $(el.id).val(),
            supCodeOrName: $.trim($(el.supCodeOrName).val()),
            companyCode: $(el.companyCode).combobox('getValue'),
            currencyCode: $(el.currencyCode).combobox('getValue'),
            paymentType: $("#paymentType").val(),
        },
        frozenColumns: [
            [

            ]
        ],
        columns: [column],
        toolbar: [
        ],
        onLoadSuccess: function () {
            $(el.tb).datagrid('clearSelections');
        }
    });
}

function searchAsData() {
    $(el.tb).datagrid('options').queryParams = getQuery();
    $(el.tb).datagrid('load');
}

function getQuery() {
    var queryParams = $(el.tb).datagrid('options').queryParams;
    queryParams.ppId = $(el.id).val();
    queryParams.supCodeOrName = $.trim($(el.supCodeOrName).val());
    queryParams.companyCode = $(el.companyCode).combobox('getValue');
    queryParams.currencyCode = $(el.currencyCode).combobox('getValue');
    queryParams.paymentType = $("#paymentType").val();
    return queryParams;
}

/**
 * 重置查询
 */
function resetAsSearch() {
    $(el.supCodeOrName).val('');
    $(el.companyCode).combobox('setValue', '');
    $(el.currencyCode).combobox('setValue', '');
    $("#paymentType").val("-1");
    searchAsData();
}

function initComBoBoxData() {
    var companyDataUrl = g.ctx + 'pc/sys/company/rest/listAll';

    // 初始化公司代码下拉框
    $(el.companyCode).combobox({
        required: false,
        url: companyDataUrl,
        panelHeight: 'auto',
        valueField: 'companyCode',
        textField: 'codeWithDesc',
        prompt: '--- 请选择 ---',
        editable: true,
    });

    $(el.currencyCode).combobox({
        url: g.ctx + 'pc/sys/dict/getDicByType?type=currency',
        panelHeight: 'auto',
        limitToList: 'true',
        prompt: '-- 请选择 --',
        method: 'GET',
        valueField: 'code',
        textField: 'codeWithDesc',
        editable: true
    });

}

/**
 * 数据导出
 */
function gysgl_derive() {
    var params = getQuery();
    var tableData = getExcelKeyTitle("tb");
    xhrPostExport('pc/finance/payment/plan/summary/rest/summaryExport',  params,tableData)
}