WASHINGTON (Reuters) - The seven-year Republican quest to scrap Obamacare, a major campaign vow by President Donald Trump, lay in ruins on Friday after the Senate failed to dismantle the healthcare law, with congressional leaders now planning to move on to other matters.

John McCain, the maverick 80-year-old senator and 2008 Republican presidential nominee, cast the deciding vote in the dramatic early-morning showdown on the Senate floor as a bill to repeal key elements of Obamacare was defeated, 51-49, dealing Trump a crushing political setback.

McCain, who flew from Arizona this week after being diagnosed with brain cancer and was heading back for further treatment starting on Monday, joined fellow Republicans Susan Collins and Lisa Murkowski in voting with Senate Democrats unified against the legislation.

“It’s time to move on,” Senate Majority Leader Mitch McConnell, whose reputation as a master strategist was diminished, said on the Senate floor after the vote at roughly 1:30 a.m. (0530 GMT).

While House of Representatives Speaker Paul Ryan said fellow Republicans should not give up on healthcare, he cited other pressing issues that needed attention, including major tax-cut legislation sought by Trump.

“We have so much work still to do,” Ryan said in a statement.

The Senate’s healthcare failure called into question the Republican Party’s ability to govern even as it controls the White House, Senate and House of Representatives.

Trump has not had a major legislative victory after more than six months in office, and his administration is mired in investigations into contacts between his election campaign and Russia and high-level White House staff infighting. He had promised to get major healthcare legislation, tax cuts and a boost in infrastructure spending through Congress in short order.

Also on the legislative agenda are spending bills for the fiscal year beginning Oct. 1 to avoid a government shutdown and raising the U.S. debt limit.

Speaking in Brentwood, New York, on Long Island, Trump expressed dismay at the bill’s failure, saying, “I said from the beginning, let Obamacare implode and then do it (pass legislation). I turned out to be right. Let Obamacare implode.”

Trump, who earlier on Twitter said the three Republicans who voted no “let America down,” again took aim at lawmakers in his own party. “Boy, oh boy, they’ve been working on that one for seven years,” he said in Brentwood. “Can you believe that? The swamp. But we’ll get it done.”

Some lawmakers urged a bipartisan effort to buttress the existing healthcare system. With the partisan divide as wide as ever in Washington, it remained to be seen if a bipartisan approach can get off the ground.

McCain said the defeated bill did not offer meaningful reform and that its defeat presents “an opportunity to start fresh” on legislation crafted by lawmakers in both parties.

“I encourage my colleagues on both sides of the aisle to trust each other, stop the political gamesmanship and put the healthcare needs of the American people first,” McCain said.

Top congressional Democrats urged a bipartisan effort to fix problems in the Obamacare law without repealing it. “Change it, improve it, but don’t just take a knife and try to destroy it and put nothing in its place,” top Senate Democrat Chuck Schumer said.

Democratic Senator Bill Nelson said he was working with Republican Collins on a bipartisan effort on healthcare.

While Ryan was able to secure House passage of a comprehensive bill to gut Obamacare in May, McConnell earlier in the week was unable to win passage of similarly broad healthcare legislation amid intraparty squabbling and competing demands by hard-line conservatives and moderates. On Friday morning, he failed to get even a stripped-down, so-called skinny bill over the finish line.

Slideshow ( 17 images )

BACKLASH FEARED

Killing the Affordable Care Act, Democratic former President Barack Obama’s signature domestic policy achievement dubbed Obamacare, has been a passion for Republicans since its 2010 enactment over their unified opposition, and was a key campaign promise by Trump last year.

Republicans lawmakers, some of whom have been gleeful about razing Obama’s presidential legacy, now fear a backlash from their conservative political base that could affect the 2018 congressional elections.

For the moment, the Affordable Care Act, which extended health insurance to 20 million people and drove the percentage of uninsured people to historic lows, remains in place and must be overseen by an administration that is hostile to it.

Slideshow ( 17 images )

This leaves health insurers unsure of how long the administration will continue to make billions of dollars in Obamacare payments that help cover out-of-pocket medical expenses for low-income Americans.

Schumer warned against any efforts to sabotage the law.

The Blue Cross Blue Shield Association, which represents insurers across the country, said it would work to ensure a smooth open enrollment period from Nov. 1-Dec. 15 and stabilize the individual insurance market under Obamacare for the long term.

Shares of health insurers, which had fought against the bill’s proposed repeal of the mandate that Americans obtain insurance, were up. Aetna Inc rose 0.9 percent, Anthem Inc gained 2.3 percent and Humana Inc edged up 0.2 percent.

On Wall Street, shares of hospitals were mostly higher because of the dwindling prospects for big cuts in the Medicaid insurance program for the poor and disabled. Community Health Systems Inc rose 2.2 percent, HCA Healthcare Inc gained 0.6 percent and Tenet Healthcare Corp edged up 0.1 percent. Republicans have long denounced Obamacare - which expanded Medicaid and created online marketplaces for individuals to obtain coverage - as an intrusion by government on people’s healthcare decisions.

Veteran House Republican Tom Cole said he thought there were “a number” of other Republican senators who were uncomfortable with the Senate’s healthcare legislation but were able to vote “yes” knowing McCain’s vote would kill it.

Republican Senator Lindsey Graham, a close friend of McCain, met on Friday with Trump to discuss Graham’s proposal to take tax money raised by Obamacare and send it back to the states in the form of healthcare block grants, the senator’s office said.

The skinny bill, released just three hours before voting began, would have retroactively repealed Obamacare’s penalty on individuals who do not obtain health insurance, repealed for eight years a penalty on certain businesses that do not provide employees with insurance and repealed a tax on medical devices until 2020.