let personalId = ''
// 修改信息
function updateBusiness(id) {
  personalId = id
  $('#updataPlatModal').modal('show')
	// $("#cardType").val('')
	$("#name").val('')
	// $("#socialCreditCode").val('')
	$("#mobile").val('')
	$.ajax({
		url:'/electronic/signature/personal/get',
		type:'get',
    data:{
      'id':id
    },
		success:function(row){
      $("#name").val(row.data.name)
      // $("#cardType").val(row.data.cardType)
			// $("#socialCreditCode").val(row.data.socialCreditCode)
			$("#mobile").val(row.data.mobile)
		},
		error:function(status){
			new $.zui.Messager('获取个人信息:'+status, {
				type: 'danger'
			}).show();
		}
	})
}

//复制链接
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 refreshAuthLink(id) {
// 	$.ajax({
// 		url:"/customer/flushUrl?id="+id,
// 		type:'get',
// 		processData:false,
// 		contentType:false,
// 		success: function (data) {
// 			if(data.success) {
//         $(".yk_rightcontent").load('/customer/personalList');
// 			}
// 		},
// 		error:function(status){
// 			new $.zui.Messager('刷新时发生错误:'+status, {
// 					type: 'danger'
// 			}).show();
// 		}
// 	});
// }

function refreshAuthLink(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) {
				platformQuery.listPage = $('#personalList').getGridParam('page');
				$.ajax({
					url:"electronic/signature/personal/flushUrl?id="+id,
					type:'get',
          processData:false,
		      contentType:false,
					success:function(data){
            if( data.success ){
							if( data.data ){
								new $.zui.Messager('重新生成认证链接成功', {
								    type: 'success'
								}).show();
								//刷新表格(当前页)
								//刷新表格
								$('#personalList').setGridParam({
									mtype:'GET', 
									url : '/electronic/signature/personal/findByName',
									datatype : 'json',
									postData: {
										'name':platformQuery.searchName,
									},
									page: platformQuery.listPage
								});
								$('#personalList').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 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) {
				platformQuery.listPage = $('#personalList').getGridParam('page');
				$.ajax({
					url:'electronic/signature/personal/delete',
					type:'get',
					data:{
						'id':id
					},
					success:function(data){
						if( data.success ){
							if( data.data ){
								new $.zui.Messager('删除成功', {
								    type: 'success'
								}).show();
								//刷新表格(当前页)
								//刷新表格
								$('#personalList').setGridParam({
									mtype:'GET', 
									url : '/electronic/signature/personal/findByName',
									datatype : 'json',
									postData: {
										'name':platformQuery.searchName,
									},
									page: platformQuery.listPage
								});
								$('#personalList').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(){
    $('#personalList').setGridParam({
      mtype:'GET',
      url : '/electronic/signature/personal/findByName',
      datatype : 'json',
      postData: {
        'name':platformQuery.searchName,
      },
      page : 1
    });
    $('#personalList').trigger('reloadGrid');
  };

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

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

  // 操作列
  function operateBusiness(cellValue,options,rowObject){
    var btnstr = '';
    if( $('#electronicSignaturePersonalUpdate').length>0 ){
      if(rowObject.authStatus == 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($('#electronicSignaturePersonalFlushUrl').length>0) {
			if(rowObject.authStatus == 1) {
				btnstr += '<p><span>重新生成认证链接</span><i class="iconfont icon-zhongxinshengchengchushimima" onclick="refreshAuthLink(\''+rowObject.id+'\')"></i></p>';
			}
		}
    
		if($('#electronicSignaturePersonalDelete').length>0){
			btnstr += '<p><span>删除</span><i class="operateIconfont operateIcon-shanchu" onclick="deleteBusiness(\''+rowObject.id+'\')"></i></p>';
		}
    return '<div class="operateIcon noHover">'+btnstr+'</div>';
  };

  sxUtil.jqGrid('#personalList', {
    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: 'name', label: '姓名', sortable: false,align: 'center', title: false},
      {name: 'cardType', label: '证件类型', sortable: false,align: 'center', title: false},
      {name: 'socialCreditCode', label: '证件号码', sortable: false,align: 'center', title: false},
      {name: 'mobile', label: '电话', sortable: false,align: 'center', title: false},
      {name: 'authStatus', label: '认证状态', sortable: false, align: 'center',  title: false,formatter:status},
      {name: 'gmtCreate', 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: '/electronic/signature/personal/findByName',
    pager: "#personalListPage",
    multiselect: false,
    rownumbers: false,
    postData: {},
    page: platformQuery.listPage,
    loadError: function () {
      new $.zui.Messager('列表加载失败', {
        type: 'warning'
      }).show();
    }
  }, true);

  //表单验证
	function addPersonalFormValid(e){
		$("#addPersonalForm").validate({
			promptPosition : "bottomLeft",
			submitHandler : function(form) {
				let postdata = new FormData();
				postdata.append('name', $("#name").val())
				// postdata.append('socialCreditCode',  $("#socialCreditCode").val())
				postdata.append('mobile', $("#mobile").val())
				// postdata.append('cardTypeId', $("[name=cardType]").val())
				postdata.append('id', personalId)
				var formchoose = '#updataPlatModal'
				// $('.addPlatSure').addClass("loadingCircle");
				$.ajax({
					url: '/electronic/signature/personal/update',
					type: 'post',
					data: postdata,
					contentType: false,
   				processData: false,
					success: function(data){
						if( data.success ){
							new $.zui.Messager('修改个人成功', {
								type: 'success'
							}).show();
							//刷新表格(当前页)
								//刷新表格
							// $('#personalList').setGridParam({
							// 	mtype:'GET', 
							// 	url : '/electronic/signature/personal/findByName',
							// 	datatype : 'json',
							// 	postData: {
							// 		'name':platformQuery.searchName,
							// 	},
							// 	page: platformQuery.listPage
							// });
							// $('#personalList').trigger('reloadGrid');
							$('.addPlatSure').removeClass("loadingCircle");
							//关闭弹框
							$(formchoose).modal("hide");
							$('.modal-backdrop').remove();
							//弹框被隐藏, 且过渡效果执行完毕
							$(".yk_rightcontent").load('/customer/personalList');

						}else{ 
							new $.zui.Messager('修改个人失败:' + data.msg, {
								type: 'danger'
							}).show();
							$('.addPlatSure').removeClass("loadingCircle");
							//关闭弹框
							$(formchoose).modal("hide");
							$('.modal-backdrop').remove();
						}
					},
					error:function(status){
						new $.zui.Messager('修改个人时出错:'+status, {
							type: 'danger'
						}).show();
						$('.addPlatSure').removeClass("loadingCircle");
						//关闭弹框
						$(formchoose).modal("hide");
						$('.modal-backdrop').remove();
					}
				})
			},
			rules : {
				'name':{
					required : true
				},
				'socialCreditCode':{
					required : true
				},
				'mobile':{
					required : true
				},
				// 'cardType': {
				// 	required : true
				// },
			},
			messages : {
				'name':{
					required:'请输入姓名'
				},
				'socialCreditCode':{
					required:'请输入身份证号'
				},
				'mobile':{
					required:'请输入电话'
				},
				// 'cardType': {
				// 	required:'请选择证件类型'
				// },

			}
		})
	};

  //确定保存按钮
	$(".addPlatSure").click(addPersonalFormValid);

  //切换表格
  $(".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');
    }
  });

})
