Self-consistent theory of localization for the Anderson model