/**
 * 对账单采购审批页面中选择扣款信息弹框JS
 *
 * Created by li.kui on 2020/8/4.
 */
'use strict';
var el = {
    searchForm: '#searchForm',
    companyCode: '#companyCode',
    supplierCode: '#supplierCode',
    selectedDmId: '#selectedDmId',
    tempDelDmId: '#tempDelDmId',
    deductionNo: '#deductionNo',
    deductionType: '#deductionType',
    taxCode: '#taxCode',
    startDate: '#startDate',
    endDate: '#endDate',
    createTimeStart: '#createTimeStart',
    createTimeEnd: '#createTimeEnd',
    dedRangeStart: '#dedRangeStart',
    dedRangeEnd: '#dedRangeEnd',
    tb: '#tb',
    bodyHeight: document.documentElement.clientHeight
};

$(function () {
    initPageComBoBox();

    initDataGrid();
});


/**
 * 初始化页面下拉框
 */
function initPageComBoBox() {
    // 初始化扣款类型下拉框
    $(el.deductionType).combobox({
        editable: false,
        url: g.ctx + 'dic/get/by/type?dictTypeValue=back_type',
        panelHeight: 'auto',
        valueField: 'itemValue',
        textField: 'itemName',
        prompt: '--- 请选择 ---'
    });


    // 初始化税率代码下拉框
    $(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/deduction/manage/rest/list',
        height: el.bodyHeight - searchHeight,
        rownumbers: true,
        singleSelect: false,
        pagination: false,
        queryParams: {
            page: 1,
            rows: 1000,
            whether: 0,
            queryForm: 2,
            companyCode: $(el.companyCode).val(),
            supplierCode: $(el.supplierCode).val(),
            tempDelDmId: $(el.tempDelDmId).val()
        },
        frozenColumns: [
            [
                {
                    field: 'ck',
                    checkbox: true
                },
                {
                    field: 'deductionNo',
                    title: '扣款编号',
                    width: 140
                },

                {
                    field: 'deductionTypeDesc',
                    title: '扣款类型',
                    width: 90
                },
                {
                    field: 'deductionAmount',
                    title: '含税扣款金额(元)',
                    width: 110,
                    formatter: function (value) {
                        if (!value) {
                            return '';
                        }
                        return formatNumber(value, 2, 1);
                    }
                }
            ]
        ],
        columns: [
            [
                {
                    field: 'taxCodeDesc',
                    title: '税率',
                    width: 160
                },
                {
                    field: 'deductionTime',
                    title: '扣款时间',
                    width: 90,
                    formatter: function (value) {
                        if (value) {
                            return moment(value).format("YYYY-MM-DD");
                        }
                        return '';
                    }
                },
                {
                    field: 'deductionReason',
                    title: '扣款原因',
                    width: 300
                },
                {
                    field: 'associatedNo',
                    title: 'SRM业务单据',
                    width: 140,
                    formatter: function (value, row) {
                        if (!row.associatedRowNo) {
                            return value;
                        } else {
                            return value + '|' + row.associatedRowNo;
                        }
                    }
                },
                {
                    field: 'associatedOaNo',
                    title: 'OA流程单号',
                    width: 120
                },
                {
                    field: 'companyCode',
                    title: '公司名称',
                    width: 70
                },
                {
                    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 selectedDmId = $(el.selectedDmId).val().split(',');
            var rows = data.rows;
            for (var i = 0; i < selectedDmId.length; i++) {
                for (var j = 0; j < rows.length; j++) {
                    if (selectedDmId[i] === rows[j].id) {
                        $(el.tb).datagrid('selectRow', j);
                    }
                }
            }
        }
    });
}


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


/**
 * 数据查询
 */
function searchDeductionData() {
    var queryParams = $(el.tb).datagrid('options').queryParams;
    queryParams.deductionNo = $.trim($(el.deductionNo).val());
    queryParams.deductionType = $(el.deductionType).combobox('getValue');
    queryParams.taxCode = $(el.taxCode).combobox('getValue');
    queryParams.dedRangeStart = $.trim($(el.dedRangeStart).numberbox('getValue'));
    queryParams.dedRangeEnd = $.trim($(el.dedRangeEnd).numberbox('getValue'));
    queryParams.startDate = $(el.startDate).datebox('getValue');
    queryParams.endDate = $(el.endDate).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 resetDeductionSearch() {
    $(el.deductionNo).val('');
    $(el.deductionType).combobox('setValue', '');
    $(el.taxCode).combobox('setValue', '');
    $(el.dedRangeStart).numberbox('setValue', '');
    $(el.dedRangeEnd).numberbox('setValue', '');
    $(el.startDate).datebox('setValue', '');
    $(el.endDate).datebox('setValue', '');
    $(el.createTimeStart).datebox('setValue', '');
    $(el.createTimeEnd).datebox('setValue', '');

    searchRebateData();
}