$.ajaxSetup({
	cache : false,
	headers : {
		"token" : localStorage.getItem("token")
	},
	error : function(xhr, textStatus, errorThrown) {
		var msg = xhr.responseText;
		var response = JSON.parse(msg);
		var code = response.code;
		var message = response.message;
		if (code == 400) {
			layer.msg(message);
		} else if (code == 401) {
			localStorage.removeItem("token");
			location.href = '/login.html';
		} else if (code == 403) {
			console.log("未授权:" + message);
			layer.msg('未授权');
		} else if (code == 500) {
			layer.msg('系统错误：' + message);
		}
	}
});

/**
 * 删除用户
 * */
function del(id) {
	var data = {"userId":id};
	layer.confirm("确定删除吗？",function () {
		$.ajax({
			type: 'post',
			contentType: "application/json; charset=utf-8",
			url: ctxPath + '/users/deleteUser',
			data: JSON.stringify(data),
			dataType: 'json',
			success: function (data) {
				if (data.resMsg === "删除成功") {
					layer.alert('删除成功', {icon: 1});
					example.ajax.reload(); // 刷新页面
				} else {
					layer.msg("删除失败", {icon: 2});
					example.ajax.reload(); // 刷新页面
				}
			}
		})
	})
}

//删除按键
function buttonDel(id, permission, pers){

	if(permission != ""){
		if ($.inArray(permission, pers) < 0) {
			return "";
		}
	}
	var btn = $("<a style='color: #009688;font-size: 16px;cursor:pointer;' onclick='del(\"" + id +"\")'><span  style='color: #0e0e0e;'> | </span>删除</a>");

	return btn.prop("outerHTML");
}

// 编辑按钮
function buttonEdit(href, permission, pers){
	if(permission != ""){
		if ($.inArray(permission, pers) < 0) {
			return "";
		}
	}
	
	var btn = $("<a style='color: #009688;font-size: 16px;cursor:pointer;' onclick='window.location=\"" + href +"\"'>编辑</a>");
	return btn.prop("outerHTML");
}

//重置按钮
function buttonEdits(id, permission, pers) {
	var btn = $("<a style='color: #FAB528;font-size: 16px;cursor:pointer;' onclick='edit(\"" + id + "\")'>重置密码</a>");
	return btn.prop("outerHTML");
}

function edit(id) {
	var data = {"userId":id};
	$.ajax({
		type: 'post',
		contentType: "application/json; charset=utf-8",
		url: ctxPath + '/users/resetUserPwd',
		data: JSON.stringify(data),
		dataType: 'json',
		success: function (data) {
			if (data.resMsg === "修改成功") {
				layer.alert('重置成功,初始密码为:NXdtxny@123', {icon: 1});
				example.ajax.reload(); // 刷新页面
			} else {
				layer.msg("重置失败", {icon: 2});
				example.ajax.reload(); // 刷新页面
			}
		}
	})

}


function deleteCurrentTab(){
	var lay_id = $(parent.document).find("ul.layui-tab-title").children("li.layui-this").attr("lay-id");
	parent.active.tabDelete(lay_id);
}
