Erdogan suffers blow as ruling party defeated