A man who averted a potential bombing by befriending the attacker told a 999 call handler "he was about to kill a lot of people", a court has heard.
Mohammed Farooq, 28, who is accused of planning a terror attack at St James's Hospital in Leeds, was "agitated" when Nathan Newby spotted him in the early hours of 20 January.
Mr Newby asked him if he was "alright" and then proceeded to talk to him for five hours, Sheffield Crown Court heard in a police interview with him.
During what Mr Newby described as "totally normal chat", Farooq allegedly said he wanted to exact revenge on the hospital.
Farooq told him: "They've stabbed me in the back they've f****d me over. He wanted to get them back - basically they had f****d his life up."
Shortly afterwards Mr Newby said Farooq revealed he had the explosives on him.
Farooq "just came out with it, absolutely normal, and said 'oh it's just a bomb'", said Mr Newby.
Mr Newby asked him "what's your plans?" and Farooq allegedly said he was going to "walk through the main doors, past the lifts, straight to the canteen at the back" and wait for the canteen to fill with nurses and then light the fuse and walk out.
"I kept him talking, trying to keep him calm. The priority was to try and get him away from the hospital", Mr Newby added.
999 call made off Farooq's phone
Mr Newby, who was a patient at the hospital, spent several hours talking and eventually it was Farooq who asked him to call 999, only for Mr Newby to discover his phone had run out of battery.
When Farooq offered his phone, Mr Newby became concerned: "I said it's not going to set that off is it?". He asked Farooq to type the number in and put it on speaker.
Mr Newby told police: "I was shocked I had managed to talk him out of it. I reached out my hand, I gave him a hug and said 'mate you've done the right thing', to try and keep him calm.
"I thought what would have happened if I had wrestled him to the floor and he got agitated - a lot of what ifs."
In the 999 call, played to the jury, Mr Newby could be heard saying: "I'm a patient, I have been at hospital and bumped into a guy. How long have we been having a chat for? A few hours.
"He's a good lad. I am sat with him now... He's just pulled a gun out."
Mr Newby said he did not realise the gun only fired blanks and persuaded Farooq to place it on a nearby bench.
Later he told Farooq: "You're doing the right thing mate, think about your kids and that. You're doing the best thing mate, you are."
Farooq, a student nurse from Roundhay in Leeds, was arrested outside the hospital with a viable bomb, manufactured from a pressure cooker and 9.9kg of low explosives.
He had been viewing extremist videos on TikTok, and had also conducted a reconnaissance trip around RAF Menwith Hill, the court was told.
The clinical support worker had pleaded guilty to terrorism offences ahead of his trial for allegedly planning the attack on the hospital and the RAF station in North Yorkshire.
Farooq admits taking the bomb to the hospital but denies preparing acts of terrorism between 12 July 2021 and 21 January 2023.
Gul Nawaz Hussain KC, defending, told the court his client was "ready and willing" to detonate the home-made bomb at the hospital because of a "sense of anger and grievance" towards work colleagues but was not motivated by Islamist extremism and not radicalised.
The trial continues.