NO this is not legal as the rule is you have to come back to the country of origin and then applied for the work permit and then go back