$('[name=approvalConclusion]').change(function () {
    if($('[name=approvalConclusion]:checked').val() == 'pass') {
        $('.approvalCommentTitle').removeClass('required_fileds_star')
    }else {
        $('.approvalCommentTitle').addClass('required_fileds_star')
    }
})
$(function () {

      // 附件相关dom
    var fileWrapperbasic = `<div class="file-wrapper">
        <div class="file-icon" style="color: #5AC4FC;"><div class="file-icon-image"></div></div>
        <div class="content">
            <div class="file-name"></div>
            <div class="file-size small text-muted"></div>
        </div>
        <div class="actions" style="width: 1.032rem">
            <a class="btn btn-link btn-yulan-file" target="_blank">
                <em>预览</em>
                <i class="operateIconfont operateIcon-yulan"></i>
            </a>
        </div>
    </div>`;

       // 展示原先有的附件
       function showOriginalFile(source, insert) {
         for (var j = 0; j < $(source).length; j++) {
             var originalFileWrapper = $(fileWrapperbasic).clone();
             originalFileWrapper.find('.file-name').text($(source).eq(j).find('.originalName').val());
             originalFileWrapper.find('.file-name').attr('originalid', $(source).eq(j).find('.originalId').val());
             if (parseFloat($(source).eq(j).find('.originalSize').val()) < 1048576) {
                 originalFileWrapper.find('.file-size').text(($(source).eq(j).find('.originalSize').val() / 1024).toFixed(2) + "KB");
             } else {
                 originalFileWrapper.find('.file-size').text(($(source).eq(j).find('.originalSize').val() / 1024 / 1024).toFixed(2) + "MB");
             }
             ;
             var originaltype = $(source).eq(j).find('.originalType').val().toUpperCase();
             if (originaltype == '.JPG' || originaltype == '.JPEG' || originaltype == '.PNG' || originaltype == '.BMP') {
                 originalFileWrapper.find('.file-icon-image').css('background-image', 'url(' + $(source).eq(j).find('.originalUrl').val() + ')');
             } else {
                 originalFileWrapper.find('.file-icon-image').append('<i class="icon icon-file-pdf"></i>');
             }
             ;
             originalFileWrapper.find('.btn-delete-file').addClass('btn-delete-originalfile').removeClass("btn-delete-file");

             originalFileWrapper.find('.btn-yulan-file').attr('href', '/fileStorage/preview?fileId=' + $(source).eq(j).find('.originalId').val());
             originalFileWrapper.find('.btn-xiazai-file').attr('href', '/fileStorage/downloadFile?fileId=' + $(source).eq(j).find('.originalId').val());
             $(insert).find('.fileContent').append(originalFileWrapper)
         };
         j = null;
         if ($(insert).find('.file-wrapper').length > 0) {
             $(insert).find('.spanHolder').remove();
         }
         ;
     };
    // 签署文件回显
    if($('.fileInfoListOriginal').length) {
        // 展示原附件
        showOriginalFile('.fileInfoListOriginal', '.addFileList');
    }
    //提交
    function submitFunction(e){
        // $(".newTabAddUpdateBtnBox .save,.newTabAddUpdateBtnBox .btn-cancel").addClass("loadingCircle");
        var postdata = {}, tipword="提交";
        if($('[name=approvalConclusion]:checked').val() == 'back' && !$('[name=approvalComment]').val()) {
            new $.zui.Messager('请输入审批意见', {
                type: 'danger'
            }).show();
            return
        }
        postdata = {
            'taskId': $('[name=taskId]').val() === 'null' ? '' : $('[name=taskId]').val(),
            'businessKey': $('[name=businessKey]').val(),
            'approvalConclusion':$('[name=approvalConclusion]:checked').val(),
            'approvalComment':$('[name=approvalComment]').val()
        }
        var url = ''
        if($('[name=signChangetype]').val() === '合同变更审核') {
            url = '/purchContract/change/approval/doAudit'
        } else if($('[name=signChangetype]').val() === '签署合同变更审核') {
            url = '/purchContract/change/final/sign/audit'
        } else if($('[name=signChangetype]').val() === '签署审核'){
            url = '/purchContract/final/approval/doAudit'
        }
        // var url = $('[name=signChangetype]').val() === '签署合同变更审核' ? '/purchContract/change/final/sign/audit' : '/purchContract/final/approval/doAudit'
        $.ajax({
            url: url,
            type:'post',
            data:postdata,
            success:function(data){
                if( data.success ){
                    new $.zui.Messager(tipword+'成功', {
                        type: 'success'
                    }).show();

                    $(".yk_rightcontent").load('/purchContract/list');

                }else{
                    new $.zui.Messager(tipword+'失败:'+data.msg, {
                        type: 'danger'
                    }).show();
                    // $(".newTabAddUpdateBtnBox .save,.newTabAddUpdateBtnBox .btn-cancel").removeClass("loadingCircle");
                }
            },
            error:function(status){
                new $.zui.Messager(tipword+'时发生错误:'+status, {
                    type: 'danger'
                }).show();
                // $(".newTabAddUpdateBtnBox .save,.newTabAddUpdateBtnBox .btn-cancel").removeClass("loadingCircle");
            }
        });
        return false;
    };

    var formRules = {
        approvalConclusion:{
            required:true
        }

    };

    var formMessages = {
        approvalConclusion:{
            required:'请选择审批结论'
        }

    };


    //表单验证
    function addNewCustomer(e){

        $("#contractSignCheck-form").validate({
            promptPosition : "bottomLeft",
            submitHandler : function(form) {
                submitFunction(e);
            },
            rules:formRules,
            messages : formMessages
        });
    };

    // 提交按钮
    //$(".yk_addNewMsg .save").click(addNewCustomer);
    $(".yk_addNewMsg .save").click(function(e){
        if( !$(this).hasClass('loadingCircle') ){
            addNewCustomer(e);
        };
    });

    // 关闭按钮
    $(".yk_addNewMsg .btn-cancel,.newBackBtn").click(function(){
        if( !$(this).hasClass('loadingCircle') ){
            $(".yk_rightcontent").load('/purchContract/list');

        };
        return false;
    });



})
