var el = {
    searchForm: '#searchForm',
    tb: '#tb',
    purchaseGroupCode: '#purchaseGroupCode',
    status: '#status',
    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_revoke_fc: '#btn_revoke_fc',
    btn_remove: '#btn_remove',
    bodyHeight: document.documentElement.clientHeight
};
$(function () {
    initDict('unit,currency,payment_method,fktzd_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.type).combobox({
        panelHeight: 'auto',
        limitToList: 'true',
        prompt: '-- 请选择 --',
        method: 'GET',
        valueField: 'code',
        textField: 'name',
        data: getDictList('fktzd_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/notice/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: 'status',
                title: '状态',
                width: '120',
                exportEnum:[
                    {code:1,codeWithDesc:'暂存'},
                    {code:7,codeWithDesc:'待财务确认'},
                    {code:8,codeWithDesc:'财务拒绝'},
                    {code:9,codeWithDesc:'已确认'},
                ],
                formatter: function (value) {
                    switch (value) {
                        case 1:
                            return '暂存';
                        case 2:
                            return '<span style="color: blue;">待采购确认</span>';
                        case 3:
                            return '<span style="color: red;">采购拒绝</span>';
                        case 7:
                            return '<span style="color: orange;">待财务确认</span>';
                        case 8:
                            return '<span style="color: red;">财务拒绝</span>';
                        case 9:
                            return '<span style="color: green;">已确认</span>';
                        default:
                            return '';
                    }
                }
            },
            {
                field: 'type',
                title: '单据类型',
                width: '120',
                exportEnum:getDictList('fktzd_type'),
                formatter: function (value) {
                    return getDicName('fktzd_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: '180',
                align: 'left'
            },
            {
                field: 'createTime',
                title: '创建时间',
                width: '180',
                align: 'center',
                formatter: function (value) {
                    if (value) {
                        return moment(value).format('YYYY-MM-DD HH:mm');
                    }
                    return '';
                }
            }
        ]],
        toolbar: [
            {
                text: '确认',
                iconCls: 'icon-ok',
                id: 'btn_finance_confirm',
                handler: function () {
                    detail(5);
                }
            },
            '-',
            {
                text: '财务撤销确认',
                iconCls: 'icon-redo',
                id: 'btn_revoke_fc',
                handler: function () {
                    revokeFinConfirmed();
                }
            },
            "-",
            {
                text: '查看',
                iconCls: 'icon-search',
                id:'payment_notice_search',
                handler: function () {
                    detail(2);
                }
            }

        ],
        onBeforeLoad:function () {
            displayButtonByUserRole('payment_notice_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 1:
                    $(el.btn_finance_confirm).linkbutton('disable');
                    $(el.btn_revoke_fc).linkbutton('disable');
                    break;
                case 7:
                    $(el.btn_finance_confirm).linkbutton('enable');
                    $(el.btn_revoke_fc).linkbutton('disable');
                    break;
                case 8:
                    $(el.btn_finance_confirm).linkbutton('disable');
                    $(el.btn_revoke_fc).linkbutton('disable');
                    break;
                case 9:
                    $(el.btn_finance_confirm).linkbutton('disable');
                    $(el.btn_revoke_fc).linkbutton('enable');
                    break;
                default:
                    break;
            }
        }
    });
}


/**
 * 撤销财务已确认
 */
function revokeFinConfirmed() {
    var row = $(el.tb).datagrid('getSelected');
    if (!row) {
        topWin.layer.alert('请选择一条单据进行操作', {icon: 0});
        return false;
    }
    if(Number(row.status) !== 9){
        topWin.layer.alert('只有财务已确认状态的才能撤销', {icon: 0});
        return false;
    }

    topWin.layer.prompt({title: '请填写撤销原因', formType: 2, maxlength: 255}, function (text, index) {
        var loading = topWin.layer.msg('数据保存中, 请耐心等待......', {icon: 16, time: 0, shade: 0.1});
        $.ajax({
            url: g.ctx + 'pc/finance/payment/notice/rest/revoke/fin/confirmed',
            type: 'POST',
            data: {
                id: row.id,
                revokeReason: text
            },
            success: function () {
                topWin.layer.close(index);
                topWin.layer.close(loading);
                topWin.layer.msg('撤销成功', {icon: 1});
                $(el.tb).datagrid('reload');
            },
            error: function (data) {
                topWin.layer.close(loading);
                topWin.layer.alert(JSON.parse(data.responseText).message,{icon: 2, shadeClose: true, title: '温馨提示'});
            }
        });
    });
}

