//查看业务
function viewBusiness(id) {
    supplierQuery.listPage = $('#businessList').getGridParam('page');
    $.get('/customer/findDetail?id='+id,function(data){
        $('.yk_rightcontent').empty().html(data)
    });
}

function viewBank(id) {
    supplierQuery.listPage = $('#businessList').getGridParam('page');
    $.get('/customer/findBankDetail?id='+id,function(data){
        $('.yk_rightcontent').empty().html(data)
    });
}

// 刷新认证状态
function getAuthRefresh(id) {
    $.ajax({
        url:'/purchContract/confirm/getAuthStatus?customerId='+id,
        type:'get',
        processData:false,
        contentType:false,
        success: function (data) {
            $("#searchBusiness").click()
        },
        error:function(status){
            new $.zui.Messager(tipword+'时发生错误:'+status, {
                type: 'danger'
            }).show();
        }
    });
}

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

//删除按钮
function deleteBusiness(id){
	bootbox.confirm({
		title : '删除',
		message : '<div style="text-align:left;font-size:0.12rem">确认删除吗？</div>',
		size : 'small',
		buttons : {
			cancel : {
				label : '取消',
				className:'btn-white btn-info'
			},
			confirm : {
				label : '确定',
				className:'btn-white btn-info colorbtn'
			}
		},
		callback:function(result){
			if (result) {
				supplierQuery.listPage = $('#businessList').getGridParam('page');
				$.ajax({
					url:'/customer/deleteCustomer',
					type:'post',
					data:{
						'id':id
					},
					success:function(data){
						if( data.success ){
							if( data.data ){
								new $.zui.Messager('删除成功', {
								    type: 'success'
								}).show();
								//刷新表格(当前页)
								//刷新表格
								$('#businessList').setGridParam({
									mtype:'GET',
									url : '/customer/findList',
									datatype : 'json',
									postData: {
										'judge':true,
                                        'condition':supplierQuery.searchName
									},
									page: supplierQuery.listPage
								});
								$('#businessList').trigger('reloadGrid');
							}else{
								new $.zui.Messager('删除失败', {
								    type: 'danger'
								}).show();
							}
						}else{
							new $.zui.Messager('删除请求失败:'+data.msg, {
							    type: 'danger'
							}).show();
						}
					},
					error:function(status){
						new $.zui.Messager('删除时发生错误:'+status, {
						    type: 'danger'
						}).show();
					}
				})
			}
		}
	})
};

$(function () {

    // 重新加载列表
    function reloadList(){
        $('#businessList').setGridParam({
            mtype:'GET',
            url : '/customer/findList',
            datatype : 'json',
            postData: {
                'judge':true,
                'condition':supplierQuery.searchName
            },
            page : 1
        });
        $('#businessList').trigger('reloadGrid');
    };

    $("#searchName").val(supplierQuery.searchName);

    function status(cellValue,options,rowObject) {
        if(rowObject.authStatus) {
            return '已认证'
        }else {
            return '未认证'
        }
    }


    // 操作列
    function operateBusiness(cellValue,options,rowObject){
        var btnstr = '';
        if( $('#viewBusiness').length>0 ){
            btnstr += '<p><span>查看</span><i class="operateIconfont operateIcon-chakan" onclick="viewBusiness('+rowObject.id+')"></i></p>';
        };
        if($('#BankAccountInformationMaintenance').length > 0) {
            btnstr += '<p><span>银行账户信息</span><i class="operateIconfont operateIcon-xiangqing" onclick="viewBank('+rowObject.id+')"></i></p>';
        }
        if(!rowObject.authStatus) {
            btnstr += '<p><span>刷新认证状态</span><i class="iconfont icon-shuaxin" onclick="getAuthRefresh(\''+rowObject.fddCustomerId+'\')"></i></p>';
        }
        if($('#deleteSupplier').length>0){
            if(rowObject.deleteFlag) {
                btnstr += '<p><span>删除</span><i class="operateIconfont operateIcon-shanchu" onclick="deleteBusiness(\''+rowObject.id+'\')"></i></p>';
            }
        }
        if($('#customerFlushUrl').length) {
            btnstr += '<p><span>重新生成认证链接</span><i class="iconfont icon-zhongxinshengchengchushimima" onclick="refreshAuthLink(\''+rowObject.id+'\')"></i></p>';
        }
        return '<div class="operateIcon noHover">'+btnstr+'</div>';
    };

    sxUtil.jqGrid('#businessList', {
        colNames: ['ID','fddCustomerId', '序号','企业名称', '认证状态', '最近更新时间', '操作'],
        colModel: [
            {name: 'id', label: 'ID', hidedlg: true, hidden: true, sortable: false, title: false},
            {name: 'fddCustomerId', label: 'fddCustomerId', hidedlg: true, hidden: true, sortable: false, title: false},
            {name: 'orderNumber', label: '序号', sortable: false, width: 50, align: 'center', title: false},
            {name: 'enterpriseName', label: '企业名称', sortable: false,align: 'center', title: false},
            {name: 'authStatus', label: '认证状态', sortable: false, align: 'center', title: false,formatter:status},
            {name: 'synchronizeTime', label: '最近更新时间', sortable: false, align: 'center', title: false},
            {
                name: 'remark',
                label: '操作',
                sortable: false,
                align: 'left',
                width: 140,
                title: false,
                formatter: operateBusiness
            }
        ],
        height: 'auto',
        mtype: 'GET',
        url: '/customer/findList',
        pager: "#businessListPage",
        multiselect: false,
        rownumbers: false,
        postData: {
            'judge':true,
            'condition':supplierQuery.searchName
        },
        page: supplierQuery.listPage,
        loadError: function () {
            new $.zui.Messager('列表加载失败', {
                type: 'warning'
            }).show();
        }
    }, true);

    //搜索
    $("#searchBusiness").click(function () {
        supplierQuery.searchName = $("#searchName").val();
        supplierQuery.listPage = 1;
        reloadList();
    })

    //刷新
    $("#refreshBusiness").click(function () {
        supplierQuery.searchName = '';
        supplierQuery.listPage = 1;
        $("#searchName").val("");
        reloadList();
    })

    //同步
    $("#conformBusiness").click(function () {
        $.ajax({
            url:'/customer/flush',
            type:'get',
            data:{},
            success:function(data){
                if(data.success) {
                    new $.zui.Messager('同步成功', {
                        type: 'success'
                    }).show();
                    $("#refreshBusiness").click();
                }
            },
            error:function(status){
                new $.zui.Messager('同步时出错:'+status, {
                    type: 'danger'
                }).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');
        }
    });

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