/**
 * 对账模块购销合同JS
 *
 *
 */
'use strict';
var el = {
    searchForm: '#searchForm',
    userId: '#userId',
    accountStatementNo: '#accountStatementNo',
    createTimeStart: '#createTimeStart',
    createTimeEnd: '#createTimeEnd',
    supplierCode:'#supplierCode',
    supplierName:'#supplierName',
    accountStatementYear:'#accountStatementYear',
    accountStatementMonth:'#accountStatementMonth',
    delFlag: '#delFlag',
    tb: '#tb',
    bodyHeight: document.documentElement.clientHeight
};


$(function () {
    initDict('payment_way')
    initNecessaryDatas('pc/sys/purchaseGroup/rest/listAll,pc/sys/company/rest/listAll')
    initComBoBoxData();

    loadGrid();
});


/**
 * 初始化页面下拉框数据
 */
function initComBoBoxData() {
    // 初始化状态下拉框
    // 0，正常 1 删除 2 全部
    $(el.delFlag).combobox({
        valueField: 'value',
        textField: 'label',
        value: 0,
        data: [
            {label: '全部', value: -1},
            {label: '正常', value: 0},
            {label: '删除', value: 1},
        ],
        editable: false,
        panelHeight: 'auto'
    });

    //初始化日期
    var months = [];

    for (let i = 1; i <= 12; i++) {
        let paddedNumber = i.toString().padStart(2, '0');
        months.push({
            label: paddedNumber,
            value: paddedNumber
        });
    }

    $('#accountStatementMonth').combobox({
        data: months,
        valueField: 'value',
        textField: 'label'
    });
}


/**
 * 初始化页面表格
 */
function loadGrid() {
    var toolBar = [];
    toolBar.push(
        {
            text: '导出',
            iconCls: 'icon-download',
            id: 'btn_export',
            handler: function () {
                detailExport();
            }
        });
    var searchHeight = $(el.searchForm).outerHeight();
    var pager = initGridPagerNew(el.bodyHeight - searchHeight, 37, 40);
    var queryParams = {};
    queryParams.createTimeStart = $(el.createTimeStart).datebox('getValue');
    queryParams.createTimeEnd = $(el.createTimeEnd).datebox('getValue');
    queryParams.supplierCode = $('#supplierCode').val();
    queryParams.supplierName = $('#supplierName').val();
    queryParams.accountStatementNo = $('#accountStatementNo').val();
    queryParams.delFlag = $(el.delFlag).combobox('getValue');
    $(el.tb).datagrid({
        loadMsg: '数据加载中......',
        emptyMsg: '暂无数据',
        url: g.ctx + 'pc/finance/account/statement/rest/salesContractList',
        height: el.bodyHeight - searchHeight,
        singleSelect: false,
        rownumbers: true,
        // 定义从服务器对数据进行排序
        remoteSort: true,
        // 定义是否允许多列排序
        // multiSort: true,
        pagination: true,
        pageSize: pager.pageSize,
        pageList: pager.pageList,
        queryParams: queryParams,
        frozenColumns: [
            [
                {
                    field: 'ck',
                    checkbox: true
                },
                {
                    field: 'delFlag',
                    title: '数据状态',
                    width: 110,
                    sortable: true,
                    formatter: function (value) {
                        switch (value) {
                            case "0":
                                return '正常';
                            case "1":
                                return  '删除';
                            default:
                                return '';
                        }
                    }
                },
                {
                    field: 'accountStatementNo',
                    title: '对账单号',
                    width: 80
                }
            ]
        ],
        columns: [
            [
                {
                    field: 'supplierName',
                    title: '供应商名称',
                    width: 100
                },
                {
                    field: 'supplierCode',
                    title: '供应商代码',
                    width: 100
                },
                {
                    field: 'accountStatementYear',
                    title: '对账年份',
                    width: 100
                },
                {
                    field: 'accountStatementMonth',
                    title: '对账月份',
                    width: 100
                },
                {
                    field: 'contractNumber',
                    title: '合同编号',
                    width: 100
                },
                {
                    field: 'contractAmount',
                    title: '合同金额',
                    width: 100
                } ,
                {
                    field: 'contractDate',
                    title: '合同日期',
                    width: 90
                },
                {
                    field: 'accepPeriod',
                    title: '承兑期限',
                    width: 100
                },
                {
                    field: 'createDate',
                    title: '创建日期',
                    width: 110
                }
            ]
        ],
        toolbar: toolBar,
    });
}



/**
 * 查询参数
 */
function searchAsData() {
    var queryParams = $(el.tb).datagrid('options').queryParams;
    queryParams.accountStatementNo = $.trim($(el.accountStatementNo).val());
    queryParams.supplierName = $.trim($(el.supplierName).val());
    queryParams.supplierCode = $.trim($(el.supplierCode).val());
    queryParams.delFlag = $(el.delFlag).combobox('getValue');
    queryParams.createTimeStart = $(el.createTimeStart).datebox('getValue');
    queryParams.createTimeEnd = $(el.createTimeEnd).datebox('getValue');
    queryParams.accountStatementMonth = $(el.accountStatementMonth).val();
    queryParams.accountStatementYear = $(el.accountStatementYear).val();
    $(el.tb).datagrid('load');
}


/**
 * 重置查询
 */
function resetAsSearch() {
    $(el.createTimeStart).datebox('setValue', '');
    $(el.createTimeEnd).datebox('setValue', '');
    $(el.accountStatementNo).val('');
    $(el.supplierName).val('');
    $(el.supplierCode).val('');
    $(el.createTimeStart).datebox('setValue', '');
    $(el.createTimeEnd).datebox('setValue', '');
    $(el.accountStatementYear).val('');
    $(el.accountStatementMonth).combobox('setValue', "");
    $(el.delFlag).combobox('setValue', "");
    searchAsData();
}

function detailExport(){
    var queryParams = {};
    queryParams.createTimeStart = $(el.createTimeStart).datebox('getValue');
    queryParams.createTimeEnd = $(el.createTimeEnd).datebox('getValue');
    queryParams.supplierCode = $('#supplierCode').val();
    queryParams.supplierName = $('#supplierName').val();
    queryParams.accountStatementNo = $('#accountStatementNo').val();
    queryParams.delFlag = $(el.delFlag).combobox('getValue');
    openExportDialog( "pc/finance/account/statement/rest/detailExport","tb",queryParams)
}


