function signContractConfirm() {
    bootbox.confirm({
        title : '签署已完成',
        message : '<div style="text-align:left;font-size:0.12rem">流程已进入后续环节，请知悉</div>',
        size : 'small',
        buttons : {
            cancel :  {
                className:  'delete-cancel-btn'
            },
            confirm : {
                label : '确定',
                className:'btn-white btn-info colorbtn'
            }
        },
        callback:function(result){
            if (result) {
                $(".yk_rightcontent").load('/purchContract/list');
            };
        }
    })
}

let submitStatus = ''

$(function () {
    // 贸理通3.1逻辑 - 开始
    $('[name=conclusion]').get(0).checked = true

    uploadFileMethod({
        mainDom: '#contractSign-form',
        delegateDom: '.choiceFile',
        insideDelegateDom: '.fileContent',
        delegateDomInput: '.fileListBox .fileinput',
        depositDataDom: '.fileListBox',
        delegateLabel: '.fileListBox',
        displayListDom: '.uploaderFile',
        isCustomUploadBtn: false,
    })

    // 图片/视频 - 回显
    if($('.purchContractSignFileStorage').length) {
        showOriginalFile($('.purchContractSignFileStorage'),  $('.purchContractSignTd').find('.fileListBox'), 'deleteAndView')
    }

    // 贸理通3.1逻辑 - 结束

    // 获取签署状态
    function getSignStatus(params) {
        var postdata = new FormData();
        postdata.append('electronicInfoId', params.electronicInfoId)
        postdata.append('contractType', params.type)
        // postdata.append('customerId',$('[name=customerId]').val())
        postdata.append('basicId',params.basicId)
        postdata.append('taskId',$('[name=taskId]').val())
        $.ajax({
            url:'/purchContract/confirm/signType',
            type:'post',
            data:postdata,
            processData:false,
            contentType:false,
            success: function (data) {
                if(data.data) {
                    console.log(params.dom, 'dom')
                    // signContractConfirm()
                    params.dom.val('已签署')
                    $('[name=authStatus]').val('已认证')
                }
            },
            error:function(status){
                new $.zui.Messager('刷新签署状态时发生错误:'+status, {
                    type: 'danger'
                }).show();
            }
        });
    }


    $('.signRefresh').on('click',function () {
		var signType = $('[name=signContractType]').val() === '签署' ? 1 : 2
        const params = {
            electronicInfoId:  $(this).attr('purchContractBasicInfoId'),
            basicId: $('[name=signContractType]').val() === '签署' ? $(this).attr('basicId') : $('[name=basicId]').val(),
            type: signType,
            dom: $(this).parent().find('[name=signStatus]')
        }
        getSignStatus(params)
	})

    //获取认证状态刷新
    function getAuthRefresh() {
        $.ajax({
            url:'/purchContract/confirm/getAuthStatus?customerId='+$('[name=customerId]').val(),
            type:'get',
            data:{},
            processData:false,
            contentType:false,
            success: function (data) {
                if(data.data) {
                    // signContractConfirm()
                    $('[name=authStatus]').val('已认证')
                }
            },
            error:function(status){
                new $.zui.Messager(tipword+'时发生错误:'+status, {
                    type: 'danger'
                }).show();
            }
        });s
    }
    $('.authRefresh').on('click',function () {
        getAuthRefresh()
	})

    //提交
    function submitFunction(e){
        // $(".newTabAddUpdateBtnBox .save,.newTabAddUpdateBtnBox .btn-cancel").addClass("loadingCircle");
        var postdata = new FormData(),tipword="提交";
        postdata.append('taskId',$('[name=taskId]').val())
        postdata.append('businessKey',$('[name=businessKey]').val())

        postdata.append('remark',$('[name=remark]').val())
        postdata.append('conclusion',$('[name=conclusion]:checked').val())
        if($('[name=electronType]').val() == '1') {
            postdata.append('visible', 1)
        }else {
            postdata.append('id',$('[name=purchContractSignConclusionId]').val())
            const fileList =  $('.photoImgVideo').find('.fileListBox').data('filearray')
            const fileListId = $('.photoImgVideo').find('.fileListBox .file-name').attr('originalId')

            if(submitStatus  === 'temp') {
                postdata.append('visible', 0)
                tipword = '暂存'
            }else if(submitStatus  === 'save') {
                postdata.append('visible', 1)
                if(!fileList.length && !fileListId) {
                    new $.zui.Messager('请上传图片/视频', {
                        type: 'danger'
                    }).show();
                    return
                }
            }

            // 附件信息
            for( let m = 0; m < fileList.length; m++ ){
                postdata.append(`files[${m}]`,fileList[m]);
            };
            const deletefileList =  $('.photoImgVideo').find('.fileListBox').data('deletefileid')
            if(deletefileList.length) {
                for (let i = 0; i < deletefileList.length; i++) {
                    postdata.append(`deleteIds[${i}]`,deletefileList[i]);
                }

            }
        }

        let url = ''
        if($('[name=signContractType]').val() == '签署') {
            url = '/purchContract/confirm/add/sign'
        }else if($('[name=signContractType]').val() == '签署变更'){
            url = 'purchContract/change/add/sign'
        }
        $.ajax({
            url: url,
            type:'post',
            data:postdata,
            processData:false,
            contentType:false,
            success:function(data){
                if( data.success ){
                    new $.zui.Messager(tipword+'成功', {
                        type: 'success'
                    }).show();
                    if(submitStatus  === 'temp') {
                        // $(".yk_rightcontent").load(`/businessProcessingTask/dispatchMyTask?taskId=${$('[name=taskId]').val()}&businessKey=${$('[name=businessKey]').val()}`)
                        $(".yk_rightcontent").load('/purchContract/list');
                    }else {
                        $(".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 = {};

    var formMessages = {};


    //表单验证
    function addNewCustomer(e){
        $("#contractSign-form").validate({
            promptPosition : "bottomLeft",
            submitHandler : function(form) {
                if( $('.error.shows').length>0 ){
                    $('.error.shows:first').prev().focus();
                    return;
                };
                submitFunction(e);
            },
            rules:formRules,
            messages : formMessages
        });
        return false
    };

    // 暂存按钮
    $(".yk_addNewMsg .temp").click(function(e){
        if( !$(this).hasClass('loadingCircle') ){
            submitStatus = 'temp'
            submitFunction(e);

        };
        return false
    });

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

        };
    });

    $('#copyAuthUrl').click(function () {
        $('[name=authUrl]').select();
        if(document.execCommand('copy')) {
            document.execCommand('copy');
            new $.zui.Messager('复制成功。', {
                type: 'success'
            }).show();
        }
    })
    $('#copyConUrl').click(function () {
        $('[name=signUrl]').select();
        if(document.execCommand('copy')) {
            document.execCommand('copy');
            new $.zui.Messager('复制成功。', {
                type: 'success'
            }).show();
        }
    })

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

})
