Praise be to Allah.
Thinking to oneself when praying about matters that have nothing to do with the prayer detracts from its reward, because it undermines the proper focus (khushoo‘) that is prescribed in prayer. However, it does not invalidate it altogether. Ibn ‘Uthaymeen (may Allah have mercy on him) said:
Wandering of the mind, heedlessness, waswasah [whispers from the Shaytaan or intrusive thoughts], and thinking to oneself do not invalidate the prayer, but they detract greatly from it, to the extent that the person may finish his prayer with nothing recorded for him of it except half of it, or one quarter, or one tenth.
The Sahaabah complained about that to the Messenger of Allah (blessings and peace of Allah be upon him), and he instructed them, if they experienced that, to spit dryly to the left three times and seek refuge with Allah from the accursed Shaytaan.
The man who narrated that, and was affected by it, said: I did that, and Allah took away from me what I had been experiencing.
The remedy for that waswaas is for the person to spit dryly to his left three times, and say: A‘oodhu Billahi min ash-Shaytaan ir-rajeem (I seek refuge with Allah from the accursed Shaytaan). If he does that, Allah will take that away from him. End quote.
Fataawa Noor ‘ala ad-Darb (8/2).
With regard to what is mentioned in the question, that the Prophet (blessings and peace of Allah be upon him) said to his companions one day: Whoever among you prays without thinking [of anything else] during his prayer, I will give him my abayah. So ‘Ali prayed and when he had said the salaam, the Prophet (blessings and peace of Allah be upon him) asked him, and he said that he had thought during the prayer about which of his two abayahs the Prophet (blessings and peace of Allah be upon him) would give him,
we could not find any basis for this story, and we could not find any of the scholars who mentioned it, so we should not pay any attention to it, because it comes under the heading of telling lies about the Messenger of Allah (blessings and peace of Allah be upon him) and his companions, which is a major sin.
The Muslim should be keen to focus on his prayer with proper humility (khushoo‘), thinking about his prayer, and he should not be distracted by anything outside of that. He should reflect upon what he is reciting of Qur’an and remember death, and he should be keen to do the prayer, with all its Sunnah and obligatory parts, and not look around whilst praying. If anything of the whispers of the Shaytaan come to him, he should seek refuge with Allah from the accursed Shaytaan and spit dryly to his left three times.
And Allah knows best.