<script>
const receiverCCode = "<%= trip?.receiver?.c_code || '' %>";

fetch("/countryCode.json")
  .then(res => res.json())
  .then(data => {
    const $select = $('#receiverCountryCode');
    const savedCode = "<%= trip?.receiver?.c_code || '' %>";

    data.forEach(country => {
      const flag = [...country.code.toUpperCase()]
        .map(char => 127397 + char.charCodeAt())
        .map(code => String.fromCodePoint(code))
        .join('');

      const value = `+${country.id}`;
      const option = new Option(
        `${flag} +${country.id} (${country.name})`,
        value,
        false,
        false
      );

      $select.append(option);

      if (savedCode && savedCode == country.id) {
        option.selected = true;
      }
    });

    $select.trigger('change');
  });
</script>
