In their role as president or during their time in office has any president ever not left the country, in regards to a foreign visit? if there is more than one who is the most recent?
Oh my yes. In fact the MAJORITY of Presidents did not travel abroad during their time in office. The first time a President left the US while in office was when Teddy Roosevelt, the 25th of 45 so far, inspected construction efforts on the Panama Canal in 1906. And even that trip was only to US held territory.
Taft then visited Mexico, but the first President to travel to Europe and the first to attend any sort of major conference was Wilson when he went to the Paris Peace Conference following WW1. On that trip he visited multiple foreign capitals and heads of state, including London for the PM and the King, Rome and The Vatican, Brussels, and of course Paris.
The Depression era Presidents had rather scant travel, Harding made a single stop in Canada while in office, Coolidge one long weekend in Havana, and Hoover a month in South America while he was still President-Elect and nothing while in office.
Since then all Presidents starting with FDR have traveled extensively, in part thanks to improving infrastructure and technology(airplanes). But also with more engaged, globally focused US foreign policy called for them to be and be seen in many places. While the US's place as a superpower and Leader of the Free World made a visit from the President a far bigger thing and more desirable by many foreign governments than it had been in decades before.
The State Department keeps an excellent list of each President's trips abroad with location, date, and a short bit about what it was for. https://history.state.gov/departmenthistory/travels/president