$(function(){
    // resize时设置操作部分的样式(定位的top值)
    $('body').resize(function(){
        $(".projectManagementBottom").css('top',$(".projectManagementTop").height()+$(".businessNavigation").height()+$('.projectManagementBottom .detailContentBox-title').height()+'px');
    });
    $('body').resize();

    // 拖动中间线
    $(".projectManagementBottom .detailContentBox-title").mousedown(function(){
        var isDrag = true;
        $(document).mousemove(function(e){
            if( isDrag ){
                if( e.clientY>=($(".breadcrumb").height()+$(".yk_head").height()) && $('.detailContentBox').height()-e.clientY+$(".breadcrumb").height()+$(".yk_head").height()>200 ){
                    $(".projectManagementTop").css('height',(e.clientY-$(".businessNavigation").height()-$(".breadcrumb").height()-$(".yk_head").height())+'px');
                    $(".projectManagementBottom").css('top',(e.clientY-$(".breadcrumb").height()-$(".yk_head").height())+'px');
                };
            };
        });
        $(document).unbind('mouseup').bind('mouseup',function(){
            $(document).unbind('mousemove');
            isDrag = false;
        });
        return false;
    });

    // 点击历史信息标题展开对应内容
    $(".projectManagementTop").on('click','.detailContentBox-title',function(){
        if( $(this).hasClass('up') ){
            $(this).removeClass('up');
            $(this).nextAll().each(function(i,ele){
                if( !$(ele).hasClass('hide') ){
                    $(ele).show();
                }
            });
        }else{
            $(this).addClass('up');
            $(this).nextAll().hide();
        }
    });

    // 点击导航展开对应部分的内容
    $('.businessNavigation').on("click","li",function(){
        var scrollTopNo = $('.businessNavigation li').index($(this));
        if( $('.projectManagementTop .detailContentBox-title').eq(scrollTopNo).hasClass('up') ){
            $('.projectManagementTop .detailContentBox-title').eq(scrollTopNo).removeClass('up');
            $('.projectManagementTop .detailContentBox-title').eq(scrollTopNo).nextAll().each(function(i,ele){
                if( !$(ele).hasClass('hide') ){
                    $(ele).show();
                }
            });
        };
        var scrollTopHeight = 0;
        if( scrollTopNo == 0 ){
            scrollTopHeight = 0;
        }else{
            for( var i=0;i<scrollTopNo;i++ ){
                scrollTopHeight += $('.projectManagementTop .nodeShow').eq(i).height();
            };
            i = null;
        };
        $('.projectManagementTop .newTabAddUpdateBox').animate({'scrollTop':scrollTopHeight})
    });

    // 加载查看历史信息dom元素
    $(".projectManagementTop .newTabAddUpdateBox").load('/purchInvoice/history',function(){
        var businessKey,async = true;
        if( GetRequest() && GetRequest()['historyId'] ){
            businessKey = GetRequest()['historyId'];
            async = false;
            //window.location.href = window.location.href.split('?')[0]
        }else{
            businessKey = $('[name=businessKey]').val();
        };

        // 获取历史信息
        $.ajax({
            url:'/businessProcessingTask/doBusinessHistoryView',
            type:'get',
            data:{
                'businessKey':businessKey,
                'businessType':'PurchInvoice'
            },
            async:async,
            success:function(data){
                if( data.success ){
                    var contractHistoryData = data.data;
                    for( var i=0;i<contractHistoryData.length;i++ ){
                        // 添加导航dom
                        $(".businessNavigation ul").append('<li><div class="businessCircle">'+(i+1)+'</div><div class="businessWord"><span>'+contractHistoryData[i].formBusinessName+'</span></div></li>');

                        //
                        if( contractHistoryData[i].formBusinessName == '采购发票录入' ){
                            //标题log
                            var logText = '';
                            if(contractHistoryData[i].businessProcessingLogVo) {
                                if (contractHistoryData[i].businessProcessingLogVo.assigneeTypeName) {
                                    logText += '<span style="color: #333;font-size: 0.13rem;font-weight: normal">' + contractHistoryData[i].businessProcessingLogVo.assigneeTypeName + '：' + contractHistoryData[i].businessProcessingLogVo.assigneeName + ' </span>';
                                }

                                if (contractHistoryData[i].businessProcessingLogVo.status) {
                                    logText += '<span style="color: #333;font-size: 0.13rem;font-weight: normal">状态：' + contractHistoryData[i].businessProcessingLogVo.status + ' </span>';
                                }

                                if (contractHistoryData[i].businessProcessingLogVo.startTime) {
                                    logText += '<span style="color: #333;font-size: 0.13rem;font-weight: normal">开始时间：' + contractHistoryData[i].businessProcessingLogVo.startTime + ' </span>';
                                }

                                if (contractHistoryData[i].businessProcessingLogVo.endTime) {
                                    logText += '<span style="color: #333;font-size: 0.13rem;font-weight: normal">完成时间：' + contractHistoryData[i].businessProcessingLogVo.endTime + ' </span>';
                                }
                            }

                            var contractMakingViewBox = $(".historyHideArea .contractMakingViewBox").clone();
                            contractMakingViewBox.find('.contractMakingViewBox-log').append(logText)

                            if( contractHistoryData[i].obj.fileStorage ){
                                var fileList=contractHistoryData[i].obj.fileStorage;
                                var filesHtm='';
                                if( fileList.length>0 ){
                                    //contractMakingViewBox.find(".uploadFile").removeClass("hide");
                                    for(var ii=0;ii<fileList.length;ii++){
                                        filesHtm+='<div class="uploaderFile oldFile">'+
                                            '<input type="hidden" class="fileid" value="'+fileList[ii].id+'">'+
                                            '<div class="fileContent"><div class="file-wrapper">';
                                        var fileType=fileList[ii].fileType;
                                        if(fileType=='.jpg' || fileType=='.jpeg' || fileType=='.png' || fileType=='.bmp'){
                                            filesHtm+='<div class="file-icon" style="color: rgb(119, 31, 173);"><div  class="file-icon-image"'+
                                                'style="background-image:url('+ fileList[ii].path +');"></div></div>';
                                        }else {
                                            filesHtm+='<div class="file-icon" style="color:#5AC4FC;"><div class="file-icon-image"><i class="icon icon-file-pdf"></i></div></div>';
                                        }
                                        filesHtm+='<div class="content">'+
                                            '<div class="file-name">'+fileList[ii].originalName+'</div>'+
                                            '<div class="file-size small text-muted">'+(fileList[ii].size/1024/1024).toFixed(2)+'MB</div>'+
                                            '</div>'+
                                            '<div class="actions" style="width: 1.032rem;">'+
                                            '<a class="btn btn-link btn-preview-file" target="_blank" href="/fileStorage/preview?fileId='+fileList[ii].id+'">'+
                                            '<em style="display:none">预览</em>'+
                                            '<i class="operateIconfont operateIcon-yulan"></i>'+
                                            '</a>'+
                                            '<a class="btn btn-link btn-download-file" style="float:right" href="/fileStorage/downloadFile?fileId='+fileList[ii].id+'">'+
                                            '<em style="display:none">下载</em>'+
                                            '<i class="operateIconfont operateIcon-xiazai"></i>'+
                                            '</a></div></div>';
                                    }
                                    contractMakingViewBox.find(".contractMakingView-invoiceFile").append(filesHtm);
                                }
                            }

                            if( contractHistoryData[i].obj.listOfGoodsFile ){
                                var fileList=contractHistoryData[i].obj.listOfGoodsFile;
                                var filesHtm='';
                                if( fileList.length>0 ){
                                    //contractMakingViewBox.find(".uploadFile").removeClass("hide");
                                    for(var ii=0;ii<fileList.length;ii++){
                                        filesHtm+='<div class="uploaderFile oldFile">'+
                                            '<input type="hidden" class="fileid" value="'+fileList[ii].id+'">'+
                                            '<div class="fileContent"><div class="file-wrapper">';
                                        var fileType=fileList[ii].fileType;
                                        if(fileType=='.jpg' || fileType=='.jpeg' || fileType=='.png' || fileType=='.bmp'){
                                            filesHtm+='<div class="file-icon" style="color: rgb(119, 31, 173);"><div  class="file-icon-image"'+
                                                'style="background-image:url('+ fileList[ii].path +');"></div></div>';
                                        }else {
                                            filesHtm+='<div class="file-icon" style="color:#5AC4FC;"><div class="file-icon-image"><i class="icon icon-file-pdf"></i></div></div>';
                                        }
                                        filesHtm+='<div class="content">'+
                                            '<div class="file-name">'+fileList[ii].originalName+'</div>'+
                                            '<div class="file-size small text-muted">'+(fileList[ii].size/1024/1024).toFixed(2)+'MB</div>'+
                                            '</div>'+
                                            '<div class="actions" style="width: 1.032rem;">'+
                                            '<a class="btn btn-link btn-preview-file" target="_blank" href="/fileStorage/preview?fileId='+fileList[ii].id+'">'+
                                            '<em style="display:none">预览</em>'+
                                            '<i class="operateIconfont operateIcon-yulan"></i>'+
                                            '</a>'+
                                            '<a class="btn btn-link btn-download-file" style="float:right" href="/fileStorage/downloadFile?fileId='+fileList[ii].id+'">'+
                                            '<em style="display:none">下载</em>'+
                                            '<i class="operateIconfont operateIcon-xiazai"></i>'+
                                            '</a></div></div>';
                                    }
                                    contractMakingViewBox.find(".contractMakingView-goodsFile").append(filesHtm);
                                }
                            }

                            contractMakingViewBox.find('.contractMakingView-supplierName').text(contractHistoryData[i].obj.purchInvoiceInfo.invoiceDate);
                            contractMakingViewBox.find('.contractMakingView-purchaserCustomer').text(commaDelimiter(contractHistoryData[i].obj.purchInvoiceInfo.invoiceAmount));
                            contractMakingViewBox.find('.contractMakingView-mortgageNumber').text(contractHistoryData[i].obj.purchInvoiceInfo.invoiceNumber);
                            contractMakingViewBox.find('.contractMakingView-contractName').text(contractHistoryData[i].obj.purchInvoiceInfo.supplier);
                            contractMakingViewBox.find('.contractMakingView-contractNumber').text(contractHistoryData[i].obj.purchInvoiceInfo.invoiceTypeDic.name);
                            contractMakingViewBox.find('.contractMakingView-contractAmount').text((contractHistoryData[i].obj.purchInvoiceInfo.amountKindDic.name));

                            //货物信息
                            var goodsInfoList=contractHistoryData[i].obj.purchInvoiceGoodsList;
                            if(contractHistoryData[i].obj.purchInvoiceInfo.amountKindDic.name=='货款') {
                                if( goodsInfoList ){
                                    var trs='';
                                    for(var ii=0;ii<goodsInfoList.length;ii++ ){
                                        var num=ii+1;
                                        trs+='<tr>'+
                                            '<td  class="enter">'+num+'</td>'+
                                            '<td  class="enter">'+goodsInfoList[ii].goodsName+'</td>'+
                                            '<td  class="enter">'+goodsInfoList[ii].modelNumber+'</td>'+
                                            '<td  class="enter">'+goodsInfoList[ii].units+'</td>'+
                                            '<td  class="enter">'+goodsInfoList[ii].quantity+'</td>'+
                                            '<td  class="enter">'+commaDelimiter(goodsInfoList[ii].unitPrice)+'</td>'+
                                            '<td  class="enter">'+commaDelimiter(goodsInfoList[ii].totalPrice)+'</td>'+
                                            '<td  class="enter">'+commaDelimiter(goodsInfoList[ii].invoicedQuantity)+'</td>'+
                                            '<td  class="enter">'+commaDelimiter(goodsInfoList[ii].openQuantity)+'</td>'+
                                            '<td  class="enter">'+commaDelimiter(goodsInfoList[ii].openAmount)+'</td>'+
                                            '</tr>';
                                    }
                                    ii=null;
                                    contractMakingViewBox.find(".goodsInfoList_header").after(trs);

                                }
                            }else if(contractHistoryData[i].obj.purchInvoiceInfo.amountKindDic.name=='贴现费用') {
                                if( goodsInfoList ){
                                    var trs='';
                                    for(var ii=0;ii<goodsInfoList.length;ii++ ){
                                        var num=ii+1;
                                        trs+='<tr>'+
                                            '<td  class="enter">'+num+'</td>'+
                                            '<td  class="enter">'+goodsInfoList[ii].goodsName+'</td>'+
                                            '<td  class="enter">'+goodsInfoList[ii].modelNumber+'</td>'+
                                            '<td  class="enter">'+goodsInfoList[ii].units+'</td>'+
                                            '<td  class="enter">'+commaDelimiter(goodsInfoList[ii].openAmount)+'</td>'+
                                            '</tr>';
                                    }
                                    ii=null;
                                    contractMakingViewBox.find(".tiexianList_header").after(trs);

                                }

                            }else if(contractHistoryData[i].obj.purchInvoiceInfo.amountKindDic.name=='资金占用费') {
                                if( goodsInfoList ){
                                    var trs='';
                                    for(var ii=0;ii<goodsInfoList.length;ii++ ){
                                        var num=ii+1;
                                        trs+='<tr>'+
                                            '<td  class="enter">'+num+'</td>'+
                                            '<td  class="enter">'+goodsInfoList[ii].supplier+'</td>'+
                                            '<td  class="enter">'+commaDelimiter(goodsInfoList[ii].openAmount)+'</td>'+
                                            '</tr>';
                                    }
                                    ii=null;
                                    contractMakingViewBox.find(".zijinList_header").after(trs);

                                }

                            }

                            $(".showarea").append(contractMakingViewBox.show());
                            if(contractHistoryData[i].obj.purchInvoiceInfo.amountKindDic.name=='货款') {
                                contractMakingViewBox.find(".tiexianList").remove();
                                contractMakingViewBox.find(".supplierList").remove();
                            }else if(contractHistoryData[i].obj.purchInvoiceInfo.amountKindDic.name=='贴现费用') {
                                contractMakingViewBox.find(".goodsList").remove();
                                contractMakingViewBox.find(".supplierList").remove();
                            }else if(contractHistoryData[i].obj.purchInvoiceInfo.amountKindDic.name=='资金占用费') {
                                contractMakingViewBox.find(".goodsList").remove();
                                contractMakingViewBox.find(".tiexianList").remove();
                            }

                        };

                        //审核
                        if( contractHistoryData[i].formBusinessName == '财务审核' ){
                            //标题log
                            var logText = '';
                            if(contractHistoryData[i].businessProcessingLogVo) {
                                if (contractHistoryData[i].businessProcessingLogVo.assigneeTypeName) {
                                    logText += '<span style="color: #333;font-size: 0.13rem;font-weight: normal">' + contractHistoryData[i].businessProcessingLogVo.assigneeTypeName + '：' + contractHistoryData[i].businessProcessingLogVo.assigneeName + ' </span>';
                                }

                                if (contractHistoryData[i].businessProcessingLogVo.status) {
                                    logText += '<span style="color: #333;font-size: 0.13rem;font-weight: normal">状态：' + contractHistoryData[i].businessProcessingLogVo.status + ' </span>';
                                }

                                if (contractHistoryData[i].businessProcessingLogVo.startTime) {
                                    logText += '<span style="color: #333;font-size: 0.13rem;font-weight: normal">开始时间：' + contractHistoryData[i].businessProcessingLogVo.startTime + ' </span>';
                                }

                                if (contractHistoryData[i].businessProcessingLogVo.endTime) {
                                    logText += '<span style="color: #333;font-size: 0.13rem;font-weight: normal">完成时间：' + contractHistoryData[i].businessProcessingLogVo.endTime + ' </span>';
                                }
                                // $('.terminationContractApprovalViewBox-log').append(logText)
                            }

                            var terminationContractApprovalViewBox = $(".historyHideArea .terminationContractApprovalViewBox").clone();
                            terminationContractApprovalViewBox.find('.terminationContractApprovalViewBox-log').append(logText)

                            if(contractHistoryData[i].formBusinessName == '财务审核') {
                                terminationContractApprovalViewBox.find(".detailContentBox-title-small").text('财务审核')
                            }else if(contractHistoryData[i].formBusinessName == '财务审核变更'){
                                terminationContractApprovalViewBox.find(".detailContentBox-title-small").text('财务审核变更')
                            }

                            //审批结论
                            terminationContractApprovalViewBox.find(".terminationContractApprovalView-approvalConclusion").text(contractHistoryData[i].obj.approvalConclusion);

                            //审批意见
                            if( contractHistoryData[i].obj.approvalComment ){
                                terminationContractApprovalViewBox.find(".terminationContractApprovalView-approvalOpinion").text(contractHistoryData[i].obj.approvalComment);
                            }

                            $(".showarea").append(terminationContractApprovalViewBox.show());
                        }

                    };
                    i = null;
                }else{
                    if( data.msg != '返回结果为空' ){
                        new $.zui.Messager('获取历史信息失败:'+data.msg, {
                            type: 'danger'
                        }).show();
                    };
                };

                // 导航排序
                $('.businessNavigation .businessCircle').each(function(i,ele){
                    $(ele).text(i+1)
                })

                // 历史信息排序
                $(".projectManagementTop .detailContentBox-title").each(function(i,ele){
                    $(ele).text( (i+1)+'、'+$(ele).text() );
                    $(ele).addClass('up');
                    $(ele).nextAll().hide();
                });

                if( $('.projectManagementTop .showarea .nodeShow').length<=0 ){
                    $('.projectManagementBottom').css('top','0')
                };

                // 新窗口查看时需要计算高度
                $(".projectManagementTop.historyTop").css('height',$("body").height()-$(".breadcrumb").height()-$(".businessNavigation").height()-15 )
                $(window).bind('resize',function(){
                    $(".projectManagementTop.historyTop").css('height',$("body").height()-$(".breadcrumb").height()-$(".businessNavigation").height()-15 )
                })
            },
            error:function(status){
                new $.zui.Messager('获取历史信息时发生错误:'+status, {
                    type: 'danger'
                }).show();
            }
        });

    });


})