“Idhā” introduces the conditional situation; “afāqa” is the verb and “al-majnūn” its subject. “Qaḍā” gives the consequent ruling.
The pronoun in “minhu” refers back to Ramadan. Thus “mā maḍā minhu” denotes the elapsed portion of that month, not a separate unrelated period.