A unified picture for gamma-ray burst prompt and X-ray afterglow emissions