How to return to nearest way point after stopped pursing player???

trying to get my enemy to go to the nearest waypoint after he loses the player from sight.

using UnityEngine;
using System.Collections;

public class chase : MonoBehaviour {

public Transform player;
NavMeshAgent agent;
public Transform target;
Animator anim;

string state = "patrol";
public GameObject[] waypoints;
int currentWP = 0;
float rotSpeed = 2f;
float speed = 5f;
float accuracyWP = 1.0f;

// Use this for initialization
void Start () 
{
	anim = GetComponent<Animator>();
}

// Update is called once per frame
void Update () 
{

	Vector3 direction = player.position - this.transform.position;	
	float angle = Vector3.Angle(direction,this.transform.forward);
	
	if(state == "patrol" && waypoints.Length > 0)
	{
		anim.SetBool("isIdle",false);
		anim.SetBool("isWalking",true);
		anim.SetBool("isRunning",false);
		if(Vector3.Distance(waypoints[currentWP].transform.position,transform.position) < accuracyWP)
		{
			currentWP++;
			if(currentWP >= waypoints.Length)
			{
				currentWP = Random.Range(0,waypoints.Length);
			}
		}
		//rotation towards waypoints
		direction = waypoints[currentWP].transform.position - transform.position;
		this.transform.rotation = Quaternion.Slerp(transform.rotation,
							Quaternion.LookRotation(direction),rotSpeed * Time.deltaTime);
		this.transform.Translate(0,0,.1f);
	}
	
	if(Vector3.Distance(player.position, this.transform.position) < 15 && (angle < 90 || state == "pursuing")) 
	{
	
		
		state = "pursuing";
		
		direction.y = 0;

		this.transform.rotation = Quaternion.Slerp(this.transform.rotation,
		                                           Quaternion.LookRotation (direction), rotSpeed * Time.deltaTime);
		
		agent = GetComponent<NavMeshAgent> ();
		agent.SetDestination(target.position);
											   
		if(direction.magnitude > 1) 
		{
			this.transform.Translate(0,0,.05f); 
			anim.SetBool("isRunning",true);
			anim.SetBool("isWalking",false);
			anim.SetBool("isAttacking",false);
			
		}									
		else
		{
			anim.SetBool("isAttacking",true);
			anim.SetBool("isRunning",false);
			anim.SetBool("isWalking",false);
		}	   
	
	}
	else
	{
		anim.SetBool("isWalking",true);
		anim.SetBool("isRunning",false);
		anim.SetBool("isAttacking",false);
		state = "patrol";

	}
	
}

}

Just store the latest player vector3 position when enemy sees it? And when enemy no longer sees it go to that position.