/**
 * 对账模块-查看预付款申请页面JS
 *
 */
'use strict';
var el = {
    id: '#id',
    dataPanel: '#dataPanel',
    dataDiv: '#dataDiv',
    tb: '#tb',
    tb2: '#tb2',
    bodyHeight: document.documentElement.clientHeight
};


/**
 * 文档就绪
 */
$(function () {
    $(el.dataPanel).panel({
        collapsible: true,
        onCollapse: function () {
            $(el.tb).datagrid('resize', {height: el.bodyHeight - 45});
        },
        onExpand: function () {
            var dataDivHeight = $(el.dataDiv).outerHeight();
            $(el.tb).datagrid('resize', {height: el.bodyHeight - dataDivHeight});
        }
    });
    initPageComBoBox();
    initPageDataGrid();
    initPageDataGrid2();

    $('#tt').tabs({
        onSelect:function(title){
            if('已申请付款信息' === title){
                var rows = $(el.tb).datagrid('getRows');
                let poCodes = [];
                for (let i = 0; i < rows.length; i++) {
                    var item = rows[i];
                    poCodes.push(item.poCode);
                }
                findPaymentDetailByPoCode(poCodes)
            }
        }
    });
});

/**
 * 初始化页面下拉框
 */
function initPageComBoBox() {

    // 初始化付款方式下拉框
    $(el.paymentWayCode).combobox({
        required: true,
        editable: false,
        valueField: 'dicId',
        textField: 'dicTxt',
        panelHeight: 'auto',
        data: [
            {dicTxt: '转账', dicId: "0"},
            {dicTxt: '承兑汇票', dicId: "1"},
            {dicTxt: '承兑汇票加转账', dicId: "2"}
        ],
        prompt: '--- 请选择 ---',
        onSelect: function (data) {
            $(el.paymentWayDesc).val(data.dicTxt);
        }
    });
    // //租聘费类别
    // $('#oaHireType').combobox({
    //     valueField: 'dicId',
    //     textField: 'dicTxt',
    //     data: [
    //         {dicTxt: '员工、经理、总监房租', dicId: "0"},
    //         {dicTxt: '公司日常组织', dicId: "1"},
    //     ],
    //     editable: false,
    //     required: false,
    //     prompt: '--- 请选择 ---',
    //     panelHeight: 'auto',
    //     panelWidth: 300
    // });
    //季度
    $('#oaQuarter').combobox({
        valueField: 'label',
        textField: 'label',
        data: [
            {label: '一季度', value: 0},
            {label: '二季度', value: 1},
            {label: '三季度', value: 2},
            {label: '四季度', value: 3},
            {label: '一月', value: 4},
            {label: '二月', value: 5},
            {label: '三月', value: 6},
            {label: '四月', value: 7},
            {label: '五月', value: 8},
            {label: '六月', value: 9},
            {label: '七月', value: 10},
            {label: '八月', value: 11},
            {label: '九月', value: 12},
            {label: '十月', value: 13},
            {label: '十一月', value: 14},
            {label: '十二月', value: 15}
        ],
        editable: false,
        prompt: '--- 请选择 ---',
        panelHeight: 'auto',
        panelWidth: 300,
    });
    //类别
    $('#oaType').combobox({
        valueField: 'label',
        textField: 'label',
        data: [
            {label: '物料采购预算', value: 0},
            {label: '工程技改预算', value: 1},
            {label: '设备购置预算', value: 2},
            {label: '设备维修预算', value: 3},
            {label: '咨询服务预算', value: 4},
            {label: '日常借款', value: 5}
        ],
        editable: false,
        prompt: '--- 请选择 ---',
        panelHeight: 'auto',
        panelWidth: 300,
    });
    //是否oa补录
    // $('#isOa').combobox({
    //     valueField: 'value',
    //     textField: 'label',
    //     data: [
    //         {label: '否', value: 0},
    //         {label: '是', value: 1},
    //     ],
    //     editable: false,
    //     required:true,
    //     prompt: '--- 请选择 ---',
    //     panelHeight: 'auto',
    //     panelWidth: 300,
    //     onSelect:function (data) {
    //         let value = data.value;
    //         if(value === 1){
    //             $('#oaProcessCode').textbox({'required':true});
    //         }else{
    //             $('#oaProcessCode').textbox({'required':false});
    //         }
    //     }
    // });
    $('#isSafeProductionCost').combobox({
        valueField: 'value',
        textField: 'label',
        data: [
            {label: '否', value: 1},
            {label: '是', value: 0},
        ],
        editable: false,
        required: true,
        prompt: '--- 请选择 ---',
        panelHeight: 'auto',
        panelWidth: 300
    });
    //类别
    $('#processId').combobox({
        valueField: 'value',
        textField: 'label',
        data: [
            {label: '物料采购借款单', value: 1520},
            {label: '工程技改借款单', value: 1522},
            {label: '设备采购借款单', value: 1523},
            {label: '设备维修借款单', value: 1524},
            {label: '服务类借款单', value: 1525},
            {label: '借款单', value: 1223},
            {label: '借款单(金派格)', value: 1499}
        ],
        editable: false,
        prompt: '--- 请选择 ---',
        panelHeight: 'auto',
        panelWidth: 300,
    });
}
/**
 * 初始化页面表格数据
 */
function initPageDataGrid() {
    var dataDivHeight = $(el.dataDiv).outerHeight(), id = $(el.id).val();

    $(el.tb).datagrid({
        loadMsg: '数据加载中......',
        url: g.ctx + 'pc/finance/paymentAdvance/apply/detail/rest/find/paa/detail',
        height: el.bodyHeight - dataDivHeight,
        singleSelect: true,
        rownumbers: true,
        // 定义从服务器对数据进行排序
        remoteSort: false,
        // 定义是否允许多列排序
        // multiSort: true,
        pagination: false,
        showFooter: true,
        queryParams: {
            applyId: id,
            dataShowType: 2
        },
        frozenColumns: [
            [
                {
                    field: 'poCode',
                    title: '采购订单',
                    width: 100,
                    sortable: true,
                },
                {
                    field: 'poItemNo',
                    title: '订单行号',
                    width: 65,
                    sortable: true
                },
                {
                    field: 'subjectTypeName',
                    title: '科目分配类别',
                    width: 65,
                    sortable: true
                },
                {
                    field: 'poNum',
                    title: '订单数量',
                    width: 65,
                    sortable: true
                }
            ]
        ],
        columns: [
            [
                {
                    field: 'factoryCode',
                    title: '工厂',
                    width: 80,
                    sortable: true
                },
                {
                    field: 'materialDesc',
                    title: '物料描述',
                    width: 260,
                    sortable: true
                },
                {
                    field: 'taxCodeDesc',
                    title: '税码',
                    width: 160,
                    sortable: true
                },
                {
                    field: 'itPrice',
                    title: '含税单价',
                    width: 100,
                    sortable: true,
                    formatter:function (value) {
                        if (!value) {
                            return 0;
                        }
                        return  formatNumber(value, 4, 1);
                    }
                },
                {
                    field: 'itAmount',
                    title: '订单含税金额',
                    width: 100,
                    sortable: true,
                    formatter:function (value) {
                        if (!value) {
                            return 0;
                        }
                        return  formatNumber(value, 4, 1);
                    }
                },
                // {
                //     field: 'budInnerMoney',
                //     title: '预算内申请金额',
                //     width: 100,
                //     sortable: true,
                //     formatter:function (value) {
                //         if (!value) {
                //             return 0;
                //         }
                //         return  formatNumber(value, 4, 1);
                //     }
                // },
                {
                    field: 'budOutMoney',
                    title: '本次申请金额',
                    width: 100,
                    sortable: true,
                    formatter:function (value) {
                        if (!value) {
                            return 0;
                        }
                        return  formatNumber(value, 4, 1);
                    }
                },
                {
                    field: 'alreadyAdvanceAmount',
                    title: '已预付金额',
                    width: 100,
                    sortable: true
                },
                {
                    field: 'reversalMoney',
                    title: '已冲账金额',
                    width: 100,
                    sortable: true
                },
                {
                    field: 'pcCodeName',
                    title: '付款条件名称',
                    width: 200,
                    sortable: true
                },
                {
                    field: 'itemRemark',
                    title: '摘要',
                    width: 200
                },
                {
                    field: 'contractNo',
                    title: '合同号',
                    width:120
                },
                {
                    field: 'contractName',
                    title: '合同名称',
                    width:120
                },
                {
                    field: 'emptyColumn',
                    title: '',
                    width: 20
                }
            ]
        ],
        toolbar: [
            {
                text: '导出',
                iconCls: 'icon-download',
                handler: function () {
                    itemDataExport();
                }
            }
        ],
        onLoadSuccess: function () {
            $(el.tb).datagrid('clearSelections');
        }
    });
}

/**
 * 根据采购订单编号获取付款信息
 * @param poCodes
 */
function findPaymentDetailByPoCode(poCodes) {
    $.ajax({
        type: 'POST',
        url: g.ctx + 'pc/finance/paymentAdvance/apply/detail/rest/find/payment/detail',
        data:{
            'poCodeList': poCodes
        },
        dataType:'json',
        success: function (data) {
            $(el.tb2).datagrid('loadData', data);
        },
        error: function (data) {
            console.log(data);
            topWin.layer.alert('数据获取失败, 请联系管理员', {icon: 0});
        }
    });
}

/**
 * 初始化申请付款信息
 */
function initPageDataGrid2() {
    var dataDivHeight = $(el.dataDiv).outerHeight(), id = $(el.id).val();

    $(el.tb2).datagrid({
        loadMsg: '数据加载中......',
        //url: g.ctx + 'fin/payment/advance/apply/rest/find/payment/detail',
        height: el.bodyHeight - dataDivHeight,
        singleSelect: true,
        rownumbers: true,
        // 定义从服务器对数据进行排序
        remoteSort: false,
        // 定义是否允许多列排序
        // multiSort: true,
        pagination: false,
        showFooter: false,
        view: detailview,
        queryParams: {
            applyId: id,
            type: 'advance_payment'
        },
        columns: [
            [
                {
                    field: 'poCode',
                    title: '采购订单',
                    width: 150
                },
                {
                    field: 'prepaid',
                    title: '已申请预付金额',
                    width: 180
                },
                {
                    field: 'actualPrepaid',
                    title: '实际预付金额',
                    width: 180
                },
                {
                    field: 'deductionPrepaid',
                    title: '已抵扣预付金额',
                    width: 180,
                    // formatter:function (value) {
                    //     if (!value) {
                    //         return 0;
                    //     }
                    //     return  formatNumber(value, 4, 1);
                    // }
                },
                {
                    field: 'ticketPrepaid',
                    title: '票到付款申请金额',
                    width: 180,
                    // formatter:function (value) {
                    //     if (!value) {
                    //         return 0;
                    //     }
                    //     return  formatNumber(value, 4, 1);
                    // }
                },
                {
                    field: 'actualTicketPrepaid',
                    title: '实际票到付款金额',
                    width: 180,
                    // formatter:function (value) {
                    //     if (!value) {
                    //         return 0;
                    //     }
                    //     return  formatNumber(value, 4, 1);
                    // }
                },
                {
                    field: 'totalPayment',
                    title: '累计已付款金额',
                    width: 180,
                    // formatter:function (value) {
                    //     if (!value) {
                    //         return 0;
                    //     }
                    //     return  formatNumber(value, 4, 1);
                    // }
                },
                {
                    field: 'remaining',
                    title: '剩余付款金额',
                    width: 180
                },
                {
                    field: 'buyer',
                    title: '采购员',
                    width: 200
                }
            ]
        ],
        // onLoadSuccess: function () {
        //     $(el.tb).datagrid('clearSelections');
        // },
        //构造一个满足Datagrid的div
        detailFormatter: function (index) {
            return '<div id="ddv-' + index + '" style="padding: 5px;"></div>';
        },
        onExpandRow: function (index, row) {
            //el.dateViewTotal.push(index);
            catPaymentDetail(index,row);
        },
    });
}

/**
 * 查看每个订单下的付款详细数据
 * @param index 行索引
 * @param row 行数据
 */
function catPaymentDetail(index, row) {
    $('#ddv-' + index).datagrid({
        loadMsg: '数据加载中......',
        url: g.ctx + 'pc/finance/paymentAdvance/apply/detail/rest/find/payment/detailBypoCode',
        singleSelect: false,
        height: 'auto',
        pagination: false,
        width: screenWidth - 10,
        page: 1,
        pageSize: 1000,
        queryParams: {
            poCode: row.poCode
        },
        columns: [[
            {
                field: 'status',
                title: '付款状态',
                width: 120,
                formatter: function (value, row, index) {
                    switch (value){
                        case 3:
                            return '审批中';
                        case 5:
                            return '付款完成';
                    }
                }
            },
            {
                field: 'amountType',
                title: '付款类型',
                width: 120,
                formatter: function (value, row, index) {
                    // if (value == undefined) {
                    //     value = "";
                    // }
                    // if (Number(row.is_require_cost) === 1) {
                    //     return '<a href="javascript:void(0);" style="color: blue;" onclick="costBaoJia(this)" data-index="' + index + '" data-value="' + row.taxfree_quote + '" data-sid="' + row.id + '" data-mc="' + row.material_code + '" data-mn="' + row.material_name + '">' + value + '</a>';
                    // }
                    // return value;
                    switch (value) {
                        case 'Z001':
                            return '预付款';
                        case 'Z003':
                            return '票到付款';
                    }
                }
            },
            {
                field: 'paymentNo',
                title: '付款单号',
                width: 150
            },
            {
                field: 'poCode',
                title: '订单号',
                width: 150
            },
            {
                field: 'itemNo',
                title: '订单行号',
                width: 150
            },
            {
                field: 'contractNo',
                title: '合同号',
                width: 170
            },
            {
                field: 'invoiceNo',
                title: '发票号',
                width: 170
            },
            {
                field: 'invoiceItemNo',
                title: '发票行号',
                width: 150,
                // formatter: function (value, row, rec) {
                //     var desc = BJGL.BJGL__SHANGC;
                //     if (row.supplier_annex) {
                //         value = row.supplier_annex;
                //         var supplierAnnexNum = row.supplier_annex.split(',');
                //         desc = BJGL.BJGL__YSC + supplierAnnexNum.length + '个';
                //     } else {
                //         value = -100;
                //     }
                //     if (row.material_name != "<b>合计</b>") {
                //
                //         return '<a href="javascript:void(0);" style="color: blue;" onclick="uploadRequireFileUI(this)" data-index="' + rec + '" data-value="' + value + '">' + desc + '</a>';
                //     }
                // },
                // styler: function (value, row) {
                //     var prohibitQuoteFlag = row.prohibit_quote_flag;
                //     if (Number(prohibitQuoteFlag) === 1) {
                //         return 'background-color:#92b3d0;';
                //     } else {
                //         if (value) {
                //             return 'background-color:#fff;';
                //         }
                //         return 'background-color:#f8d594;';
                //     }
                // }
            },
            {
                field: 'prepaid',
                title: '申请付款金额',
                width: 100
            },
            {
                field: 'actualPrepaid',
                title: '实际付款金额',
                width: 100,
            },
            {
                field: 'deductionPrepaid',
                title: '抵扣预付金额',
                width: 100
            },
            {
                field: 'buyer',
                title: '采购员',
                width: 100
            }
        ]],
        onLoadSuccess: function (data) {
            $(el.tb2).datagrid('fixDetailRowHeight', index);
            var buyer = row.buyer;
            var rows = data.rows;
            for (let i = 0; i < rows.length; i++) {
                $('#ddv-' + index).datagrid('updateRow',{
                    index: i,
                    row: {
                        buyer: buyer,
                    }
                });
            }
            //$('#supplierTb').datagrid('fixDetailRowHeight', index);
        },
        // onClickRow: function (detailIndex, rowData) {
        //     if (supplierEditIndex === undefined) {
        //         el.tableDom = '#ddv-' + index;
        //         $(el.tableDom).datagrid('beginEdit', detailIndex);
        //         supplierEditIndex = detailIndex;
        //     } else {
        //         supplierEndEditor();
        //         el.tableDom = '#ddv-' + index;
        //         $(el.tableDom).datagrid('beginEdit', detailIndex);
        //         supplierEditIndex = detailIndex;
        //     }
        // },
        onResize: function () {
            $(el.tb2).datagrid('fixDetailRowHeight', index);
        }
        // onCheck: function (rowIndex, rowData) {
        //     $(el.tb).datagrid('unselectAll');
        // },
        // onCheckAll: function (rows) {
        //     $(el.tb).datagrid('unselectAll');
        // }
    });
}


/**
 * 数据导出
 */
function itemDataExport() {
    var rows = $(el.tb).datagrid('getRows');
    for(var i = 0; i < rows.length; i++){
        var item = rows[i];
        var rowIndex = $(el.tb).datagrid('getRowIndex', item);
        $(el.tb).datagrid('endEdit', rowIndex);
    }

    $(el.tb).datagrid('toExcel', {
        filename: '预付款申请明细数据.xls',
        rows: rows,
        worksheet: '预付款申请明细'
    });
}
