Economy.
To quickly summarize what happened is that Erdogan and his AKP party actually did very, very good in the first term or two. The AKP party actually built infrastructure (they still do) like mad, creating jobs, giving back to the poor people with incredible health care. The AKP party made it easier to get mortgages (via a secure and solid system) where mortgages didn't exist in Turkey. The good majority of the people for the first time in their life had fair access to a job, education, healthcare and a roof over there heads. Hope was installed back into the country. Erdogan really was kinda incredible back then, mind you a lot of his policies were carrying over what was already planned by others but had yet to be executed. Ohh and get this, Erdogan wasn't really that pro-religious back then either, I mean sure he was very open about being muslim and so forth but he wasn't as religious back then, more on this later.
Then what happened is as follows, and this is simply my theory, my opinion. Erdogan got the shaft from the west, Europe embarrassed him time after time on the public stage by stalling their European entry candidacy.. As much as the AKP party reformed Turlye into looking and feeling more like the west, the Europeans power that be gave AKP the shaft and said "nah, continue waiting in line while these other poorer countries are allowed in".
...So Erdogan said fuck it, he gave up on the West knowing full well no matter how well Turkey does Europe will never allow the country in. Erdogan began boosting it's allies with the middle-east and Asia. AKP party at this point was very well established, funded and incredibly popular. The AKP party saw the Middle-East movement as an opportunity and started to cater to the audience (the Sunni Muslims) and went to bed with KSA. This is about when the AKP went full stupid with the religion thing. This plan ultimately backfired when Egypt's Muslim brotherhood fell and Syria couldn't be forced into a regime change.
To add to the misery, in order to gain a majority vote in the elections that followed in Turkey, the AKP distanced themselves from the Kurdish HDP party and moved more right wing to the nationalist MHP. This caused a drift between the Kurds (about 20 million Kurds live in Turkey) and the Turks living in Turkey and you now have a cluster fuck of a civil war between the Kurds and the state in south east Turkey.
The icing on the cake is ISIS attacking both the Kurds and the Turks living in Turkey. Finally, the AKP has to now deal with 3 million plus Syrian refugees that's costing the country billions.
tldr: Erdogan tried, the west failed him, so he tries an alternative method, which ends up failing him too.