Before he gunned down a Chicago police officer, a doctor and a pharmacy resident at Mercy Hospital, Juan Lopez made threats, harassed his estranged wife and slept with a gun under his pillow, court records show.

And when his wife prepared to leave him in 2014, complaining that he once pointed a gun at a real-estate agent, she said he threatened to show up at her workplace and “cause a scene” — an eerie hint of things to come.

It turns out the man who took three lives at Mercy before he died during a shootout with police had actually worked as a security guard for another hospital. Not only that, he held a valid Firearm Owners Identification card and a concealed-carry license.

But authorities say there was nothing in his background to prevent Lopez from obtaining guns.

State law bars people with existing court orders of protection against them from getting a FOID card. Lopez’s former wife did obtain an emergency protection order against him in late 2014, but records show it was in effect for only 16 days — not long enough to affect his FOID status. Court records show it was dismissed for “want of prosecution.”

Lopez was never charged with a crime in connection with that protection order.

Police agencies can file objections with a state board to prevent potentially dangerous people from getting concealed-carry permits, but it doesn’t appear Lopez was even on the Chicago Police Department’s radar until the shooting on Monday.

The Chicago Housing Authority said in a statement it hired Lopez in February, and he worked there as an associate program specialist at its customer care center in the Loop. CHA said he underwent typical background checks before he was hired, and it received no complaints about him.

Then, on Monday afternoon, Lopez showed up at Mercy to demand an engagement ring back from emergency room doctor Tamara O’Neal, police said. Months earlier, she had broken off her engagement with Lopez, her father told reporters, and Lopez “couldn’t let it go.”

Police said Lopez shot O’Neal, who had just enough time to call 911 to report that her ex-fiance had shown up with a gun. He allegedly then opened fire on officers arriving at the hospital, ran inside and fatally wounded pharmacy resident Dayna Less, and then ran back outside to again fire at police who had used a squad car to shield O’Neal’s body.

Lopez also shot and killed Officer Samuel Jimenez, who was among the officers who arrived at the hospital. That made Monday’s fatal shooting the worst in 30 years to involve a police officer.

Sources say the gun Lopez used in the killings was a Glock 9mm semiautomatic handgun bought at Shore Galleries in Lincolnwood in 2012. No one at the gun store was available to talk about Lopez’s purchase. It was among four guns Lopez bought over a five-year period, police say.

A spokeswoman for Ingalls Memorial Hospital confirmed Lopez had worked as a security guard there in 2013 and 2014, and separately for several months in 2015. DePaul University also confirmed that he had been enrolled as a graduate student, seeking a degree in public service. He earned a bachelor’s degree at the school in 2013.

His history of threats dates back to around that time, according to court records and comments by authorities Tuesday.

Five years ago, while Lopez was a trainee with the Chicago Fire Department, he was disciplined for “improper conduct toward women,” said department spokesman Larry Langford.

While Langford originally said Lopez had also allegedly threatened to shoot up the Chicago Fire Academy, Langford said late Tuesday that there was no record of the threat in Lopez’s personnel file.

Then, in late 2014, the woman Lopez had married in 2008 sought an order of protection against him, records show. Not only did it say she had begun to separate from him, but it said he had been sleeping with pistol under his pillow and left the gun where the couple’s child could reach it.

The woman claimed in her petition that Lopez had pulled a gun multiple times. Once, she said, he ran outside with a gun to look for a neighbor he felt threatened by. Then, on Nov. 22, 2014, he allegedly “forgot about a scheduled apartment appraisal and pulled the gun out and pointed it to the Realtor.”

Finally, when he realized she was preparing to leave him, Lopez began to threaten her, the woman said. On Dec. 1, 2014, the day she sought the order of protection, she said he sent her a text message. She said he threatened “to come to my job and cause a scene.”

Contributing: Mitch Dudek, Rachel Hinton and Andy Grimm

RELATED

• Father of slain Mercy Hospital doctor: ‘Tammy’s profession was to save lives’

• Young pharmacy resident — a portrait in courage

• ‘This tears at the soul of our city’

• Hero cop married less than a year before he was slain at Mercy Hospital

• Slain Mercy Hospital doctor called 911 before ex-fiance killed her

• EDITORIAL: NRA says docs should ‘stay in their lane’ on guns; tell that to Mercy Hospital, CPD