The Mental Health Act 1983 allows detention and treatment of mental disorder without consent. It does not authorise treatment of physical conditions unrelated to the mental disorder. A detained patient who needs surgery is treated with consent if they have capacity, or under the Mental Capacity Act in their best interests if they lack it.
The Mental Capacity Act 2005 covers decisions for people who lack capacity for any reason (delirium, dementia, head injury, intoxication). It is the usual legal basis for treating confused surgical patients.