var el = {
    searchForm: '#searchForm',
    tb: '#tb',
    purchaseGroupCode: '#purchaseGroupCode',
    status: '#status',
    postStatus: '#postStatus',
    type: '#type',
    customs_declaration_add: '#customs_declaration_add',
    customs_declaration_edit: '#customs_declaration_edit',
    customs_declaration_search: '#customs_declaration_search',
    customs_declaration_push_confirm: '#customs_declaration_push_confirm',
    btn_finance_confirm: '#btn_finance_confirm',
    btn_remove: '#btn_remove',
    bodyHeight: document.documentElement.clientHeight
};
$(function () {
    initDict('unit,currency,payment_method,fyppd_type')
    initNecessaryDatas('pc/sys/purchaseGroup/rest/listAll');
    initCombo();
    loadGrid();
});

function initCombo(){
    $(el.status).combobox({
        editable: false,
        panelHeight: 'auto',
        valueField: 'value',
        textField: 'label',
        prompt: '--- 请选择 ---',
        data: [
            // {label: '暂存', value: 1},
            {label: '待财务确认', value: 7},
            {label: '财务拒绝', value: 8},
            {label: '已确认', value: 9}
        ],
    });

    $(el.postStatus).combobox({
        editable: false,
        panelHeight: 'auto',
        valueField: 'value',
        textField: 'label',
        prompt: '--- 请选择 ---',
        data: [
            {label: '未过账', value: 1},
            {label: '预制失败', value: 3},
            {label: '预制成功', value: 4},
            {label: '过账成功', value: 5}
        ],
    });

    $(el.type).combobox({
        panelHeight: 'auto',
        limitToList: 'true',
        prompt: '-- 请选择 --',
        method: 'GET',
        valueField: 'code',
        textField: 'name',
        data: getDictList('fyppd_type')
    });

    $(el.purchaseGroupCode).combobox({
        valueField: 'code',
        textField: 'codeWithDesc',
        method: 'POST',
        prompt: '--- 请选择 ---',
        data: getNecessaryDataList('pc/sys/purchaseGroup/rest/listAll')
    });
}


function loadGrid() {
    var searchHeight = $(el.searchForm).outerHeight();
    var pager = initGridPagerNew(el.bodyHeight - searchHeight - 20, 37, 40);
    $(el.tb).datagrid({
        height: el.bodyHeight - searchHeight,
        url: g.ctx + 'pc/finance/payment/matching/rest/list?statusListStr=7,8,9',
        singleSelect: true,
        pagination: true,
        pageSize: pager.pageSize,
        pageList: pager.pageList,
        loadMsg: '数据加载中......',
        rownumbers: true,
        columns: [[
            {
                field: 'ck',
                checkbox: true
            },
            {
                field: 'postStatus',
                title: '状态',
                width: '120',
                exportEnum:[
                    {code:1,codeWithDesc:'未过账'},
                    {code:3,codeWithDesc:'预制失败'},
                    {code:4,codeWithDesc:'预制成功'},
                    {code:5,codeWithDesc:'过账成功'},
                ],
                formatter: function (value) {
                    switch (value) {
                        case 1:
                            return '未过账';
                        case 3:
                            return '<span style="color: red;">预制失败</span>';
                        case 4:
                            return '<span style="color: green;">预制成功</span>';
                        case 5:
                            return '<span style="color: green;">过账成功</span>';
                        default:
                            return '';
                    }
                }
            },
            {
                field: 'type',
                title: '单据类型',
                width: '120',
                exportEnum:getDictList('fyppd_type'),
                formatter: function (value) {
                    return getDicName('fyppd_type',value);
                }
            },
            {
                field: 'code',
                title: '单据流水号',
                width: '120',
                align: 'left'
            },
            // {
            //     field: 'purchaseGroupCode',
            //     title: '采购组',
            //     width: '120',
            //     align: 'left',
            //     formatter: function (value) {
            //         return getNecessaryDataName('pc/sys/purchaseGroup/rest/listAll',value);
            //     }
            // },
            {
                field: 'supplierCode',
                title: '供应商编码',
                width: '100',
                align: 'left'
            },
            {
                field: 'supplierName',
                title: '供应商名称',
                width: '200',
                align: 'left'
            },
            {
                field: 'taxAmount',
                title: '进口总金额',
                width: '100',
                align: 'left'
            },
            // {
            //     field: 'currencyCode',
            //     title: '币种',
            //     width: '120',
            //     align: 'left'
            // },
            {
                field: 'companyCode',
                title: '交易主体',
                width: '120',
                align: 'left'
            },
            {
                field: 'remark',
                title: '备注',
                width: '120',
                align: 'left'
            },
            {
                field: 'createUserName',
                title: '创建人',
                width: '120',
                align: 'left'
            },
            {
                field: 'createTime',
                title: '创建时间',
                width: '120',
                align: 'center',
                formatter: function (value) {
                    if (value) {
                        return moment(value).format('YYYY-MM-DD HH:mm');
                    }
                    return '';
                }
            },
            {
                field: 'invoicedCode',
                title: 'MIRO凭证号',
                width: '120',
                align: 'left'
            },
            {
                field: 'voucherYear',
                title: '发票年度',
                width: '120',
                align: 'left'
            },
            {
                field: 'postDate',
                title: '过账日期',
                width: '120',
                align: 'center',
                formatter: function (value) {
                    if (value) {
                        return moment(value).format('YYYY-MM-DD');
                    }
                    return '';
                }
            },
            {
                field: 'attachmentFiles', title: '附件', width: 120, align: 'left', formatter: function (value, row) {
                    if (value !== "") {
                        return "<a href='javascript:void(0)' onclick=\"viewAttachmentFileDetail('" + row.id + "', '" + row.code +"')\">" + "点击查看(已上传)" + "</a>";
                    } else {
                        return "<a style='color:red;' href='javascript:void(0)' onclick=\"viewAttachmentFileDetail('" + row.id + "', '" + row.code +"')\">" + "点击上传(未上传)" + "</a>";
                    }
                }
            },
        ]],
        toolbar: [
            {
                text: '确认',
                iconCls: 'icon-ok',
                id: 'btn_finance_confirm',
                handler: function () {
                    detail(5);
                }
            }
        ],
        onBeforeLoad:function () {
            displayButtonByUserRole('customs_declaration_finance_list');
        },
        onCheck: function (rowIndex) {
            var rows = $(el.tb).datagrid('getRows');
            var status = rows[rowIndex].status;
            //数据状态 1:暂存 2:待采购确认 3:采购拒绝 4:待推送OA审批 5:OA审批中 6:OA审批不通过 7:待财务确认 8:财务拒绝 9:确认通过
            switch (Number(status)) {
                case 0:
                    $(el.btn_finance_confirm).linkbutton('disable');
                    break;
                case 7:
                    $(el.btn_finance_confirm).linkbutton('enable');
                    break;
                case 8:
                    $(el.btn_finance_confirm).linkbutton('disable');
                    break;
                case 9:
                    $(el.btn_finance_confirm).linkbutton('disable');
                    break;
                default:
                    break;
            }
        }
    });
}


