// 平台管理--修改业务
function updateBusiness(id) {
    platformQuery.listPage = $('#businessList').getGridParam('page');
    $('#enterPriseBusinessUpdateDialog').load('/platform/toUpdatePage?id='+id,function(){
        $("#myModal-updateEnterPriseBusinessEntry").modal("show")
    })
}

// 平台管理--开票信息维护
function information(id) {
    platformQuery.listPage = $('#businessList').getGridParam('page');
    $.get('/platform/editInvoice?id='+id,function(data){
        $('.yk_rightcontent').empty().html(data)
    });
}

//复制链接
function copyUrl(url) {
    const input = document.createElement('input');
    document.body.appendChild(input);
    input.setAttribute('value', url);
    input.select();
    if (document.execCommand('copy')) {
        document.execCommand('copy');
        new $.zui.Messager('复制认证链接成功。', {
            type: 'success'
        }).show();
    }
    document.body.removeChild(input);
}
//复制签署链接
function copySignUrl(url) {
    const input = document.createElement('input');
    document.body.appendChild(input);
    input.setAttribute('value', url);
    input.select();
    if (document.execCommand('copy')) {
        document.execCommand('copy');
        new $.zui.Messager('复制自动签署链接成功。', {
            type: 'success'
        }).show();
    }
    document.body.removeChild(input);
}

function refreshAuthLink(id) {
	$.ajax({
		url:"/platform/flushUrl?id="+id,
		type:'get',
		processData:false,
		contentType:false,
		success: function (data) {
			if(data.success) {
                $(".yk_rightcontent").load('/platform/platformList');
			}
		},
		error:function(status){
			new $.zui.Messager('刷新时发生错误:'+status, {
					type: 'danger'
			}).show();
		}
	});
}

$(function () {

    // 重新加载列表
    function reloadList(){
        $('#businessList').setGridParam({
            mtype:'GET',
            url : '/platform/findPage',
            datatype : 'json',
            postData: {

            },
            page : 1
        });
        $('#businessList').trigger('reloadGrid');
    };

    function status(cellValue,options,rowObject) {
        if(rowObject.authStatus) {
            return '已认证'
        }else {
            return '未认证'
        }
    }
    function signStatus(cellValue,options,rowObject) {
        if(rowObject.autoSignStatus) {
            return '已签署'
        }else {
            return '未签署'
        }
    }
    function interest(cellValue,options,rowObject) {
        if(rowObject.interestBearing) {
            return '是'
        }else {
            return '否'
        }
    }


    // 操作列
    function operateBusiness(cellValue,options,rowObject){
        var btnstr = '';
        if( $('#updateBusiness').length>0 ){
            btnstr += '<p><span>修改</span><i class="operateIconfont operateIcon-bianji" onclick="updateBusiness('+rowObject.id+')"></i></p>';
        };
        if( $('#copyAuthLink').length>0 ){
            btnstr += '<p><span>复制认证链接</span><i class="operateIconfont operateIcon-fuzhi" onclick="copyUrl(\''+rowObject.shortAuthUrl+'\')"></i></p>';
        };
        if( $('#copySignLink').length>0 ){
            btnstr += '<p><span>复制自动签署链接</span><i class="operateIconfont operateIcon-fuzhi" onclick="copySignUrl(\''+rowObject.shortAutoSignUrl+'\')"></i></p>';
        };
        if( $('#editInvoiceInfo').length>0 ){
            btnstr += '<p><span>开票信息维护</span><i class="operateIconfont operateIcon-chuli" onclick="information(\''+rowObject.id+'\')"></i></p>';
        };
        if($('#platformFlushAuthUrl').length) {
            btnstr += '<p><span>刷新认证和签署链接</span><i class="iconfont icon-shuaxin" onclick="refreshAuthLink(\''+rowObject.id+'\')"></i></p>';
        }
        return '<div class="operateIcon noHover">'+btnstr+'</div>';
    };

    sxUtil.jqGrid('#businessList', {
        colNames: ['ID', '序号','平台名称',  '认证状态', '自动签署状态', '创建时间 ','认证链接','签署链接', '操作'],
        colModel: [
            {name: 'id', label: 'ID', hidedlg: true, hidden: true, sortable: false, title: false},
            {name: 'orderNumber', label: '序号', sortable: false, width: 50, align: 'center', title: false},
            {name: 'platformName', label: '平台名称', sortable: false,align: 'center', title: false},
            // {name: 'interestBearing', label: '投放日是否计息', sortable: false, align: 'center', title: false,formatter:interest},
            {name: 'status', label: '认证状态', sortable: false, align: 'center',  title: false,formatter:status},
            {name: 'status', label: '自动签署状态', sortable: false, align: 'center',  title: false,formatter:signStatus},
            {name: 'gmtCreate', label: '创建时间', sortable: false, align: 'center',title: false},
            {name: 'authUrl', label: '认证链接', hidedlg: true, hidden: true, sortable: false, title: false},
            {name: 'autoSignUrl', label: '签署链接', hidedlg: true, hidden: true, sortable: false, title: false},
            {
                name: 'remark',
                label: '操作',
                sortable: false,
                align: 'left',
                width: 140,
                title: false,
                formatter: operateBusiness
            }
        ],
        height: 'auto',
        mtype: 'GET',
        url: '/platform/findPage',
        pager: "#businessListPage",
        multiselect: false,
        rownumbers: false,
        postData: {

        },
        page: platformQuery.listPage,
        loadError: function () {
            new $.zui.Messager('列表加载失败', {
                type: 'warning'
            }).show();
        }
    }, true);

    //添加平台
    $("#addBusiness").click(function () {
        $('#enterPriseBusinessDialog').load('/platform/addPage',function(){
            $("#myModal-addEnterPriseBusinessEntry").modal("show")
        })
    })

    //切换表格
    $(".projectListTab li").click(function () {

        if ($(this).text() == '供应商') {
            $(".yk_rightcontent").load('/customer/listPage');
        } else if ($(this).text() == '核心企业') {
            $(".yk_rightcontent").load('/customer/enterpriseList');
        }else if ($(this).text() == '贸易平台') {
            $(".yk_rightcontent").load('/platform/platformList');
        }else if ($(this).text() == '电子签个人管理') {
            $(".yk_rightcontent").load('/customer/personalList');
        }
    });

})
