/**
 * 对账单新增编辑页面中选择返利信息弹框JS
 *
 * Created by li.kui on 2020/8/2.
 */
'use strict';
var el = {
    searchForm: '#searchForm',
    rebateNo: '#rebateNo',
    rebateType: '#rebateType',
    taxCode: '#taxCode',
    rebateTimeStart: '#rebateTimeStart',
    rebateTimeEnd: '#rebateTimeEnd',
    createTimeStart: '#createTimeStart',
    createTimeEnd: '#createTimeEnd',
    raRangeStart: '#raRangeStart',
    raRangeEnd: '#raRangeEnd',
    companyCode: '#companyCode',
    supplierCode: '#supplierCode',
    selectedRmId: '#selectedRmId',
    tempDelRmId: '#tempDelRmId',
    tb: '#tb',
    bodyHeight: document.documentElement.clientHeight
};


$(function () {
    initPageComBoBox();

    initDataGrid();
});


/**
 * 初始化页面下拉框
 */
function initPageComBoBox() {
    // 初始化返利类型下拉框
    $(el.rebateType).combobox({
        valueField: 'value',
        textField: 'label',
        value: -1,
        data: [
            {label: '全部', value: -1},
            {label: '购方对供方返利', value: 1},
            {label: '供方对购方返利', value: 2}
        ],
        editable: false,
        panelHeight: 'auto'
    });


    // 初始化公司代码下拉框
    $(el.taxCode).combobox({
        url: g.ctx + 'pc/sys/tax/rest/listAll',
        panelHeight: 200,
        valueField: 'code',
        textField: 'codeWithDesc',
        prompt: '--- 请选择 ---'
    });
}


/**
 * 初始化返利数据列表
 */
function initDataGrid() {
    var searchHeight = $(el.searchForm).outerHeight();

    $(el.tb).datagrid({
        loadMsg: '数据加载中, 请耐心等待......',
        url: g.ctx + 'fin/rebate/manage/rest/list',
        rownumbers: true,
        singleSelect: false,
        pagination: false,
        height: el.bodyHeight - searchHeight,
        queryParams: {
            page: 1,
            rows: 1000,
            whether: 0,
            companyCode: $(el.companyCode).val(),
            supplierCode: $(el.supplierCode).val(),
            tempDelRmId: $(el.tempDelRmId).val()
        },
        frozenColumns: [
            [
                {
                    field: 'ck',
                    checkbox: true
                },
                {
                    field: 'rebateNo',
                    title: '返利单号',
                    width: 140
                },
                {
                    field: 'rebateAmount',
                    title: '含税返利金额(元)',
                    width: 110,
                    formatter: function (value) {
                        if (!value) {
                            return '';
                        }
                        return formatNumber(value, 2, 1);
                    }
                }
            ]
        ],
        columns: [
            [
                {
                    field: 'taxCodeDesc',
                    title: '税率',
                    width: 160
                },
                {
                    field: 'rebateType',
                    title: '返利类型',
                    width: 120,
                    formatter: function (value) {
                        if(Number(value) === 1){
                            return '购方对供方返利';
                        }else {
                            return '供方对购方返利';
                        }
                    }
                },
                {
                    field: 'rebateTime',
                    title: '返利时间',
                    width: 110,
                    formatter: function (value) {
                        if (value) {
                            return moment(value).format("YYYY-MM-DD");
                        }
                        return '';
                    }
                },
                {
                    field: 'rebateReason',
                    title: '返利原因',
                    width: 300
                },
                {
                    field: 'companyName',
                    title: '公司名称',
                    width: 180
                },
                {
                    field: 'supplierCode',
                    title: '供应商编码',
                    width: 90
                },
                {
                    field: 'supplierName',
                    title: '供应商名称',
                    width: 270
                },
                {
                    field: 'createTime',
                    title: '创建时间',
                    width: 140,
                    formatter: function (value) {
                        if (value) {
                            return moment(value).format("YYYY-MM-DD HH:mm:ss");
                        }
                        return '';
                    }
                }
            ]
        ],
        onLoadSuccess: function (data) {
            var selectedRmId = $(el.selectedRmId).val().split(',');
            var rows = data.rows;
            for(var i = 0; i < selectedRmId.length; i++){
                for(var j = 0; j < rows.length; j++){
                    if(selectedRmId[i] === rows[j].id){
                        $(el.tb).datagrid('selectRow', j);
                    }
                }
            }
        }
    });
}


/**
 * 确认选择
 */
function confirmChose(callback) {
    callback($(el.tb).datagrid('getSelections'));
}


/**
 * 查询返利数据
 */
function searchRebateData() {
    var queryParams = $(el.tb).datagrid('options').queryParams;
    queryParams.rebateNo = $.trim($(el.rebateNo).val());
    queryParams.rebateType = $(el.rebateType).combobox('getValue');
    queryParams.taxCode = $(el.taxCode).combobox('getValue');
    queryParams.raRangeStart = $.trim($(el.raRangeStart).numberbox('getValue'));
    queryParams.raRangeEnd = $.trim($(el.raRangeEnd).numberbox('getValue'));
    queryParams.rebateTimeStart = $(el.rebateTimeStart).datebox('getValue');
    queryParams.rebateTimeEnd = $(el.rebateTimeEnd).datebox('getValue');
    queryParams.createTimeStart = $(el.createTimeStart).datebox('getValue');
    queryParams.createTimeEnd = $(el.createTimeEnd).datebox('getValue');
    queryParams.whether = 0;
    queryParams.companyCode = $(el.companyCode).val();
    queryParams.supplierCode = $(el.supplierCode).val();
    queryParams.tempDelRmId = $(el.tempDelRmId).val();
    $(el.tb).datagrid('load');
}


/**
 * 清空返利数据查询
 */
function resetRebateSearch() {
    $(el.rebateNo).val('');
    $(el.rebateType).combobox('setValue', -1);
    $(el.taxCode).combobox('setValue', '');
    $(el.raRangeStart).numberbox('setValue', '');
    $(el.raRangeEnd).numberbox('setValue', '');
    $(el.rebateTimeStart).datebox('setValue', '');
    $(el.rebateTimeEnd).datebox('setValue', '');
    $(el.createTimeStart).datebox('setValue', '');
    $(el.createTimeEnd).datebox('setValue', '');

    searchRebateData();
}