/**
 * 删除
 */
function delInvoiceData() {
    endEditor();
    var rows = $(el.tb).datagrid('getSelections');
    if(rows.length === 0){
        topWin.layer.alert('请选择您要删除的行数据', {icon: 0});
        return false;
    }
    let Z001Selections=rows.filter(function(item) {
        return item.amountType=="Z001"
    });
    if(Z001Selections.length!=0){
        layer.msg("预付款行不能单独勾选删除");
        return ;
    }
    for(var i = 0; i < rows.length; i++){
        var row = rows[i];
        if('Z001' === row.amountType){
            layer.msg("预付款行不允许删除");
            break;
        }
        let poCode=row.poCode;
        let poItemNo=row.poItemNo;
        if(row.id !== 0){
            let obj = {};
            obj.id = row.id;
            obj.sourceId = row.detailId;
            obj.amountType = row.amountType;
            el.tempDelTbData.push(obj);
        }
        var rowIndex = $(el.tb).datagrid('getRowIndex', row);
        $(el.tb).datagrid('deleteRow', rowIndex);
        el.existedData.remove(row.dataKey)
        let rows2 = $('#tb').datagrid('getRows');
        //票到付款数据
        let daoRows = rows2.filter(function(e) {
           return e.amountType == "Z003"&&poCode == e.poCode && poItemNo == e.poItemNo;
        });
        if (daoRows.length == 0) {
            //预付款数据
            let deleteRows=rows2.filter(function(e) {
                return e.amountType=="Z001"&& poCode == e.poCode && poItemNo == e.poItemNo
            })||[];
            //如果有相应的预付款数据删除该预付款
            delRows(deleteRows);
        }
    }
    summaryItemAmount();
    $(el.tb).datagrid('clearSelections');
}


function delRows(deleteRows) {
    for (let j = 0; j < deleteRows.length; j++) {
        var row=deleteRows[j];
        var index = $('#tb').datagrid('getRowIndex',deleteRows[j]);
        $(el.tb).datagrid('deleteRow', index);
        if(row.id !== 0){
            let obj = {};
            obj.id = row.id;
            obj.sourceId = row.detailId;
            obj.amountType = row.amountType;
            el.tempDelTbData.push(obj);
        }
        el.existedData.remove(deleteRows[j].dataKey)
    }
}

//文件上传点击事件
function initFileBox() {

    $('#codFileInput').filebox({
        onChange: function () {
            var obj = $(this);
            var addFiles = [];
            var files = $('#codFileInput').filebox('files'), formData = new FormData();

            for (var r = 0; r < files.length; r++) {
                addFiles.push(files[r]);
            }

            if (addFiles.length !== 0) {
                for (var n = 0; n < addFiles.length; n++) {
                    formData.append('files', addFiles[n]);
                }
                formData.append('type', 1);

                var index = layer.load(1, {shade: [0.1, '#eee']});
                $.ajax({
                    url: g.ctx + 'finPaymentAttachment/rest/file/upload',
                    data: formData,
                    type: 'post',
                    contentType: false,
                    processData: false,
                    success: function (files) {
                        layer.close(index);
                        let fjId = [], fileMessages = '';
                        if ($('#codFjid').val()) {
                            fjId = $('#codFjid').val().split(',')
                        }
                        for (var n = 0; n < files.length; n++) {
                            var item = files[n];
                            fjId.push(item.id);
                            fileMessages += '<a style="margin-left: 10px" href="/files/download?fileName=' + encodeURI(item.filePath) + '&realName=' + encodeURI(item.fileName)+ '&ctype=allPath">' + item.fileName + '</a><a id="' + item.id + '" class="deleteFile" onclick="deleteAdviceFile(\'#codMessages\',\'#codFjid\')"><img title="删除" src="/static/img/89-Minus.png" style="width: 20px;height: 20px;"></a>';
                        }
                        $('#codFjid').val(fjId.join(','));
                        $('#codMessages').append(fileMessages);
                    }
                });
            }
            //清空文件dom，不清空则不能再次选择文件
            obj.filebox('reset');
            obj.filebox('setValue', '');
            //取消校验
            obj.filebox('disableValidation');
        }
    });
}
//上传
function uploadCodFile() {
    $('#codFileUpload').find('.filebox-label').click();
}
//下载
function downloadCodFile(filePath,fileName) {
    var link = document.getElementById('a-upload');
    $(link).attr("href", "/files/download?fileName="+encodeURI(filePath)+"&realName="+encodeURI(fileName)+"&ctype=allPath");
    link.click();
}
//全部删除
function deleteAllFileCod(idDome,fileDome) {
    let fileId = $('#'+fileDome).val();
    layer.confirm('是否全部删除？', function (index) {
        $.ajax({
            type: 'POST',
            data: {fileId: fileId},
            url: g.ctx + 'finPaymentAttachment/rest/deleteFile.do',
            success: function (data) {
                if (data === 'ok') {
                    layer.msg('删除成功');
                    $('#'+idDome).html('<input type="hidden" class="ui-form-item" id="'+fileDome+'" name="'+fileDome+'">');
                    layer.close(index);
                } else {
                    layer.alert(common_opt_msg.opt_fail_msg);
                }
            },
            error: function (data) {
                console.log(data);
                layer.alert(common_opt_msg.opt_fail_msg);
            }
        });
    });
}
function getAName(){
    return "alreadyPaymentAmount";
}