Hey, it's because 2 of those are always going to be true at the same time and you can't simulate in 2 directions at once, it picks the last events as they run last so up/down. You'll need to apply some different logic for that so for example when the Y is the same then it starts adjusting the x to move left/right.
To stop and attacking you can use distance(player.x,player.x,enemy.x,enemy.y) is a value, and when it hits that value you set the enemy to a different state. This could be an instance variable that changes between 'moving' and 'attacking'.
Then for the movement logic you add a condition, enemy.state is not 'attacking'. This allows the enemy to stop moving and attack instead.