{# @var craft \craft\web\twig\variables\CraftVariable #}
{% from "seomatic/settings/_includes/macros.twig" import configWarning %}
{% import "_includes/forms" as forms %}

{% switch pageContext %}
{% case "identity" %}
    {% set websiteVerb = "owns" %}

{% case "creator" %}
    {% set websiteVerb = "created" %}

{% default %}
    {% set websiteVerb = "" %}
{% endswitch %}

{# ############## General Info for all types ############## #}

<h2>{{ "General Info" |t("seomatic") }}</h2>

{{ forms.textField({
    label: "Entity Name"|t("seomatic"),
    instructions: "The name of the entity that #{websiteVerb} the website" |t("seomatic"),
    id: "genericName",
    class: "nicetext",
    name: "genericName",
    value: entity.genericName,
    required: true,
    errors: entity.getErrors("genericName"),
}) }}

{{ forms.textField({
    label: "Alternate Entity Name"|t("seomatic"),
    instructions: "An alternate or nickname for the entity that #{websiteVerb} the website" |t("seomatic"),
    id: "genericAlternateName",
    class: "nicetext",
    name: "genericAlternateName",
    value: entity.genericAlternateName,
    required: false,
    errors: entity.getErrors("genericAlternateName"),
}) }}

<div class="field" id="genericDescription-field">
    <div class="heading">
        <label for="genericDescription">{{ "Entity Description"|t("seomatic") }}</label>
    </div>
    <div class="instructions"><p>{{ "A description of the entity that #{websiteVerb} the website" |t("seomatic") }}</p>
    </div>
    {{ forms.textarea({
        label: "Entity Description"|t("seomatic"),
        instructions: "A description of the entity that #{websiteVerb} the website" |t("seomatic"),
        id: "genericDescription",
        class: "nicetext",
        name: "genericDescription",
        value: entity.genericDescription,
        maxlength: 1024,
        showCharsLeft: true,
        required: false,
        errors: entity.getErrors("genericDescription"),
    }) }}
</div>

{{ forms.autosuggestField({
    label: "Entity URL"|t("seomatic"),
    instructions: "A URL for the entity that #{websiteVerb} the website" |t("seomatic"),
    suggestEnvVars: true,
    suggestAliases: true,
    id: "genericUrl",
    class: "nicetext",
    name: "genericUrl",
    value: entity.genericUrl,
    required: false,
    errors: entity.getErrors("genericUrl"),
}) }}

{{ forms.elementSelectfield({
    label: "Entity Brand"|t("seomatic"),
    instructions: "An image or logo that represents the entity that #{websiteVerb} the website.  The image must be in JPG, PNG, or GIF format." |t("seomatic"),
    id: "genericImageIds",
    name: "genericImageIds",
    viewMode: "large",
    elements: genericImageElements,
    elementType: elementType,
    criteria: {
        kind: ["image"],
    },
    jsClass: "Craft.AssetSelectInput",
    selectionLabel: "Select Brand Image"|t("seomatic"),
    sources: null,
    limit: 1,
    warning: false,
    errors: entity.getErrors("genericImageIds"),
}) }}

{{ forms.textField({
    label: "Entity Telephone"|t("seomatic"),
    instructions: "The primary contact telephone number for the entity that #{websiteVerb} the website" |t("seomatic"),
    id: "genericTelephone",
    class: "nicetext",
    name: "genericTelephone",
    value: entity.genericTelephone,
    required: false,
    errors: entity.getErrors("genericTelephone"),
}) }}

{{ forms.textField({
    label: "Entity Email"|t("seomatic"),
    instructions: "The primary contact email address for the entity that #{websiteVerb} the website" |t("seomatic"),
    id: "genericEmail",
    class: "nicetext",
    name: "genericEmail",
    value: entity.genericEmail,
    required: false,
    errors: entity.getErrors("genericEmail"),
}) }}

<!-- Location -->
<h2>{{ "Location Info" |t("seomatic") }}</h2>

{{ forms.textField({
    label: "Entity Street Address"|t("seomatic"),
    instructions: "The street address of the entity that #{websiteVerb} the website, e.g.: 123 Main Street" |t("seomatic"),
    id: "genericStreetAddress",
    class: "nicetext",
    name: "genericStreetAddress",
    value: entity.genericStreetAddress,
    required: false,
    errors: entity.getErrors("genericStreetAddress"),
}) }}

<div class="field">
    <div class="half-width-field half-width-field-left">
        {{ forms.textField({
            label: "Entity Locality"|t("seomatic"),
            instructions: "The locality of the entity that #{websiteVerb} the website, e.g.: Portchester" |t("seomatic"),
            id: "genericAddressLocality",
            class: "nicetext",
            name: "genericAddressLocality",
            value: entity.genericAddressLocality,
            required: false,
            errors: entity.getErrors("genericAddressLocality"),
        }) }}
    </div>
    <div class="half-width-field">
        {{ forms.textField({
            label: "Entity Region"|t("seomatic"),
            instructions: "The region of the entity that #{websiteVerb} the website, e.g.: New York or NY" |t("seomatic"),
            id: "genericAddressRegion",
            class: "nicetext",
            name: "genericAddressRegion",
            value: entity.genericAddressRegion,
            required: false,
            errors: entity.getErrors("genericAddressRegion"),
        }) }}
    </div>
</div>

<div class="field">
    <div class="half-width-field half-width-field-left">
        {{ forms.textField({
            label: "Entity Postal Code"|t("seomatic"),
            instructions: "The postal code of the entity that #{websiteVerb} the website, e.g.: 14580" |t("seomatic"),
            id: "genericPostalCode",
            class: "nicetext",
            name: "genericPostalCode",
            value: entity.genericPostalCode,
            required: false,
            errors: entity.getErrors("genericPostalCode"),
        }) }}
    </div>
    <div class="half-width-field">
        {{ forms.textField({
            label: "Entity Country"|t("seomatic"),
            instructions: "The country in which the entity that #{websiteVerb} the website is located, e.g.: US" |t("seomatic"),
            id: "genericAddressCountry",
            class: "nicetext",
            name: "genericAddressCountry",
            value: entity.genericAddressCountry,
            required: false,
            errors: entity.getErrors("genericAddressCountry"),
        }) }}
    </div>
</div>

<div class="field">
    <div class="half-width-field half-width-field-left">
        {{ forms.textField({
            label: "Entity Latitude"|t("seomatic"),
            instructions: "The latitude of the location of the entity that #{websiteVerb} the website, e.g.: -120.5436367" |t("seomatic"),
            id: "genericGeoLatitude",
            class: "nicetext",
            name: "genericGeoLatitude",
            value: entity.genericGeoLatitude,
            required: false,
            errors: entity.getErrors("genericGeoLatitude"),
        }) }}
    </div>

    <div class="half-width-field">
        {{ forms.textField({
            label: "Entity Longitude"|t("seomatic"),
            instructions: "The longitude of the location of the entity that #{websiteVerb} the website, e.g.: 80.6033588" |t("seomatic"),
            id: "genericGeoLongitude",
            class: "nicetext",
            name: "genericGeoLongitude",
            value: entity.genericGeoLongitude,
            required: false,
            errors: entity.getErrors("genericGeoLongitude"),
        }) }}
    </div>
</div>

<div class="buttons">
    <div class="btngroup first">
        <input id="geolookup" type="button" class="btn" value="{{ "Look up Latitude/Longitude"|t("seomatic") }}">
    </div>
</div>
<div id="geolookup-errors" style="display: none;">
    <ul class="errors">
        <li>{{ "No location found.  Perhaps the address is wrong/incomplete?" |t("seomatic") }}</li>
    </ul>
</div>

{# ############## Entity metaPane fields ############## #}

{# ############## Person type specific fields ############## #}

<div class="metaPane field Person">
    <h2>{{ "Person Info" |t("seomatic") }}</h2>
    <div class="field">
        <div class="half-width-field half-width-field-left">
            {{ forms.selectField({
                label: "Person Gender"|t("seomatic"),
                instructions: "The gender of the person" |t("seomatic"),
                id: "personGender",
                class: "nicetext",
                options: {
                    "Male": "Male"|t("seomatic"),
                    "Female": "Female"|t("seomatic"),
                    "Other": "Other"|t("seomatic"),
                },
                name: "personGender",
                value: entity.personGender,
                required: false,
                errors: entity.getErrors("personGender"),
            }) }}
        </div>
        <div class="half-width-field">
            {{ forms.textField({
                label: "Person Birth Place"|t("seomatic"),
                instructions: "The place where the person was born" |t("seomatic"),
                id: "personBirthPlace",
                class: "nicetext",
                name: "personBirthPlace",
                value: entity.personBirthPlace,
                required: false,
                errors: entity.getErrors("personBirthPlace"),
            }) }}
        </div>
    </div>
</div>

{# ############## Organization type specific fields ############## #}

<div class="metaPane field Organization">
    <h2>{{ "Organization Info" |t("seomatic") }}</h2>

    <div class="field">
        <div class="half-width-field half-width-field-left">
            {{ forms.textField({
                label: "Organization DUNS Number"|t("seomatic"),
                instructions: "The DUNS (Dunn & Bradstreet) number of the organization that #{websiteVerb} the website" |t("seomatic"),
                id: "organizationDuns",
                class: "nicetext",
                name: "organizationDuns",
                value: entity.organizationDuns,
                required: false,
                errors: entity.getErrors("organizationDuns"),
            }) }}
        </div>
        <div class="half-width-field">
            {{ forms.textField({
                label: "Organization Founder"|t("seomatic"),
                instructions: "The name of the founder of the organization" |t("seomatic"),
                id: "organizationFounder",
                class: "nicetext",
                name: "organizationFounder",
                value: entity.organizationFounder,
                required: false,
                errors: entity.getErrors("organizationFounder"),
            }) }}
        </div>
    </div>

    <div class="field">
        <div class="half-width-field half-width-field-left">
            {{ forms.textField({
                label: "Organization Founding Date"|t("seomatic"),
                instructions: "The date the organization was founded, in ISO 8601 date format, e.g.: `2018-03-26`. [Learn More](http://schema.org/Date)" |t("seomatic"),
                id: "organizationFoundingDate",
                class: "nicetext",
                name: "organizationFoundingDate",
                value: entity.organizationFoundingDate,
                required: false,
                errors: entity.getErrors("organizationFoundingDate"),
            }) }}
        </div>
        <div class="half-width-field">
            {{ forms.textField({
                label: "Organization Founding Location"|t("seomatic"),
                instructions: "The location where the organization was founded" |t("seomatic"),
                id: "organizationFoundingLocation",
                class: "nicetext",
                name: "organizationFoundingLocation",
                value: entity.organizationFoundingLocation,
                required: false,
                errors: entity.getErrors("organizationFoundingLocation"),
            }) }}
        </div>
    </div>

    <h2>{{ "Organization Contact Points" |t("seomatic") }}</h2>

    {{ forms.editableTableField({
        label: "Contact Points"|t("seomatic"),
        instructions: "Use organization contact points to add your organizations's contact information to the Google Knowledge panel in some searches.  The Telephone Number **must be** an internationalized version of the phone number, starting with the '+' symbol and country code (+1 in the US and Canada).  e.g.: `+1-800-555-1212` or `+44-2078225951`. [Learn More](https://developers.google.com/structured-data/customize/contact-points)"|t("seomatic") |md,
        id: "organizationContactPoints",
        name: "organizationContactPoints",
        allowAdd: true,
        allowDelete: true,
        allowReorder: true,
        required: false,
        cols: {
            telephone: {
                heading: "Telephone Number" |t("seomatic"),
                type: "singleline" |t("seomatic"),
            },
            contactType: {
                heading: "Contact Type" |t("seomatic"),
                type: "select" |t("seomatic"),
                options: {
                    "customer support": "Customer Support",
                    "technical support": "Technical Support",
                    "billing support": "Billing Support",
                    "bill payment": "Bill Payment",
                    "sales": "Sales",
                    "reservations": "Reservations",
                    "credit card support": "Credit Card Support",
                    "emergency": "Emergency",
                    "baggage tracking": "Baggage Tracking",
                    "roadside assistance": "Roadside Assistance",
                    "package tracking": "Package Tracking",
                }
            }
        },
        rows: entity.organizationContactPoints,
        errors: entity.getErrors("organizationContactPoints"),
    }) }}

</div>

{# ############## Entity metaSubPane fields ############## #}

{# ############## Airline type specific fields ############## #}

<div class="metaSubPane field Airline">
</div>

{# ############## Corporation type specific fields ############## #}

<div class="metaSubPane field Corporation">
    <h2>{{ "Corporation Info" |t("seomatic") }}</h2>
    {{ forms.textField({
        label: "Corporation Ticker Symbol"|t("seomatic"),
        instructions: "The exchange ticker symbol of the corporation" |t("seomatic"),
        id: "corporationTickerSymbol",
        class: "nicetext",
        name: "corporationTickerSymbol",
        value: entity.corporationTickerSymbol,
        required: false,
        errors: entity.getErrors("corporationTickerSymbol"),
    }) }}
</div>

{# ############## EducationalOrganization type specific fields ############## #}

<div class="metaSubPane field EducationalOrganization">
</div>

{# ############## GovernmentOrganization type specific fields ############## #}

<div class="metaSubPane field GovernmentOrganization">
</div>

{# ############## LocalBusiness type specific fields ############## #}

<div class="metaSubPane field LocalBusiness">
    <h2>{{ "Local Business Info" |t("seomatic") }}</h2>

    {{ forms.selectField({
        label: "Price Range"|t("seomatic"),
        instructions: "The approximate price range of the goods or services offered by this local business."|t("seomatic"),
        id: "localBusinessPriceRange",
        options: {
            "$": "$",
            "$$": "$$",
            "$$$": "$$$",
            "$$$$": "$$$$",
            "$$$$$": "$$$$$",
        },
        name: "localBusinessPriceRange",
        value: entity.localBusinessPriceRange,
        errors: entity.getErrors("localBusinessPriceRange"),
    }) }}

    <div class="field" id="localBusinessOpeningHours-field">
        <div class="heading">
            <label for="localBusinessOpeningHours">{{ "Opening Hours"|t("seomatic") }}</label>
        </div>
        <div class="instructions">
            <p>{{ "The opening hours for this local business.  If the business is closed on a given day, just leave the hours for that day blank.  If you leave everything blank, no opening hours will be listed. [Learn More](https://developers.google.com/search/docs/data-types/local-business)" |t("seomatic") |md }}</p>
        </div>

        {% macro day(id, name, value, day) %}
            {% from "_includes/forms" import time %}
            <tr>
                <th>
                    {{ craft.app.locale.getWeekDayName(day) }}
                </th>
                <td>
                    {{ time({
                        id: id~"-"~day~"-open",
                        name: name~"["~day~"][open]",
                        value: value[day].open.date ?? ""
                    }) }}
                </td>
                <td>
                    {{ time({
                        id: id~"-"~day~"-close",
                        name: name~"["~day~"][close]",
                        value: value[day].close.date ?? ""
                    }) }}
                </td>
            </tr>
        {% endmacro %}

        {% from _self import day %}

        {% set startDay = currentUser.getPreference("weekStartDay") ?? craft.app.config.general.defaultWeekStartDay %}
        {% set days = (startDay..6) %}

        {% if startDay != 0 %}
            {% set days = days|merge(0..(startDay -1)) %}
        {% endif %}

        <table class="data">
            <thead>
            <tr>
                <td></td>
                <th>{{ "Opening Time"|t("store-hours") }}</th>
                <th>{{ "Closing Time"|t("store-hours") }}</th>
            </tr>
            </thead>
            <tbody>
            {% for i in days %}
                {{ day("localBusinessOpeningHours", "localBusinessOpeningHours", entity.localBusinessOpeningHours, i) }}
            {% endfor %}
            </tbody>
        </table>
    </div>
</div>

{# ############## NGO type specific fields ############## #}

<div class="metaSubPane field NGO">
</div>

{# ############## PerformingGroup type specific fields ############## #}

<div class="metaSubPane field PerformingGroup">
</div>

{# ############## SportsOrganization type specific fields ############## #}

<div class="metaSubPane field SportsOrganization">
</div>

{# ############## Entity metaSpecificPane fields ############## #}

{# ############## FoodEstablishment type specific fields ############## #}

<div
    class="metaSpecificPane field FoodEstablishment Bakery BarOrPub Brewery CafeOrCoffeeShop FastFoodRestaurant IceCreamShop Restaurant Winery">
    <h2>{{ "Food Establishment Info" |t("seomatic") }}</h2>
    {{ forms.textField({
        label: "Food Establishment Cuisine"|t("seomatic"),
        instructions: "The primary type of cuisine that the food establishment serves" |t("seomatic"),
        id: "restaurantServesCuisine",
        class: "nicetext",
        name: "restaurantServesCuisine",
        value: entity.restaurantServesCuisine,
        required: false,
        errors: entity.getErrors("restaurantServesCuisine"),
    }) }}

    {{ forms.textField({
        label: "Food Establishment Menu URL"|t("seomatic"),
        instructions: "URL to the food establishment's menu" |t("seomatic"),
        id: "restaurantMenuUrl",
        class: "nicetext",
        name: "restaurantMenuUrl",
        value: entity.restaurantMenuUrl,
        required: false,
        errors: entity.getErrors("restaurantMenuUrl"),
    }) }}

    {{ forms.textField({
        label: "Food Establishment Reservations URL"|t("seomatic"),
        instructions: "URL to the food establishment's reservations page" |t("seomatic"),
        id: "restaurantReservationsUrl",
        class: "nicetext",
        name: "restaurantReservationsUrl",
        value: entity.restaurantReservationsUrl,
        required: false,
        errors: entity.getErrors("restaurantReservationsUrl"),
    }) }}
</div>

{% js %}
// Handle looking up the lattitude/longitude
$('#{{ "geolookup" |namespaceInputId }}').on('click', function(e) {
        window.open('https://www.latlong.net/convert-address-to-lat-long.html','_blank');
    });
{% endjs %}
