If you leave a game you are given a loss and the position you left in e.g if there are 2 others left in the game when you leave, you are 3rd. The AI that takes over for you has no baring on your position. If your AI wins, you do not win and are still given the 3rd place when you left.
As for what happens when you are the last human player and leave. If you leave or disconnect when you are the only human player in the game, the game will close and you are not given a win or loss. This occurs because a game must always have a host and once you as the only available host leave, the game ends.
In that situation the player would receive 2nd position as they are meant to for being eliminated by the last remaining opponent in this case, the AI.
Yes you still get credit for beating them as if they were human players. If you had left, no you would not been given the win.
The easiest way to think about it is. If a player leaves, they loose, but you still have to beat their AI to get the win.
The green CPU gets the win, but the player who left does not. You need to actually win the game to get a win.