<script src="{$BASE_PATH_JS}/StatesDropdown.js"></script>
{assign var="contactsCount" value=$contacts|@count}
{assign var="enabledPreferenceCount" value=0}
{foreach $formdata.emailPreferences as $emailType => $value}
    {if $value}
        {assign var="enabledPreferenceCount" value=$enabledPreferenceCount+1}
    {/if}
{/foreach}

<section class="dashboard-page-section">
    <section class="dashboard-analytics-grid dashboard-analytics-grid--three">
        <article class="dashboard-analytics-card dashboard-analytics-card--green dashboard-analytics-card--spark">
            <span class="dashboard-analytics-card__icon"><i class="fas fa-address-book"></i></span>
            <div class="dashboard-analytics-card__copy">
                <span class="dashboard-analytics-card__label">{lang key='navContacts'}</span>
                <strong class="dashboard-analytics-card__value">{$contactsCount}</strong>
                <span class="dashboard-analytics-card__meta">{lang key='contacts'}</span>
            </div>
            <span class="dashboard-analytics-card__line dashboard-analytics-card__line--green"></span>
        </article>
        <article class="dashboard-analytics-card dashboard-analytics-card--mint dashboard-analytics-card--spark">
            <span class="dashboard-analytics-card__icon"><i class="fas fa-envelope-open-text"></i></span>
            <div class="dashboard-analytics-card__copy">
                <span class="dashboard-analytics-card__label">{lang key='clientareaemail'}</span>
                <strong class="dashboard-analytics-card__value">{$formdata.email|default:'-'}</strong>
                <span class="dashboard-analytics-card__meta">{lang key='contactDetails'}</span>
            </div>
            <span class="dashboard-analytics-card__line dashboard-analytics-card__line--mint"></span>
        </article>
        <article class="dashboard-analytics-card dashboard-analytics-card--amber dashboard-analytics-card--spark">
            <span class="dashboard-analytics-card__icon"><i class="fas fa-bell"></i></span>
            <div class="dashboard-analytics-card__copy">
                <span class="dashboard-analytics-card__label">{lang key='clientareacontactsemails'}</span>
                <strong class="dashboard-analytics-card__value">{$enabledPreferenceCount}</strong>
                <span class="dashboard-analytics-card__meta">{lang key='notifications'}</span>
            </div>
            <span class="dashboard-analytics-card__line dashboard-analytics-card__line--amber"></span>
        </article>
    </section>

    <section class="dashboard-panel dashboard-page-table-panel">
        <div class="dashboard-page-table-toolbar">
            <form role="form" method="post" action="{routePath('account-contacts')}" class="w-100">
                <div class="row g-3 align-items-end">
                    <div class="col-lg-8">
                        <label for="inputContactId" class="col-form-label">{lang key='clientareachoosecontact'}</label>
                        <select name="contactid" id="inputContactId" onchange="submit()" class="form-control custom-select">
                            {foreach $contacts as $contact}
                                <option value="{$contact.id}"{if $contact.id eq $contactid} selected="selected"{/if}>{$contact.name} - {$contact.email}</option>
                            {/foreach}
                            <option value="new">{lang key='clientareanavaddcontact'}</option>
                        </select>
                    </div>
                    <div class="col-lg-4 text-lg-right">
                        <button type="submit" class="btn btn-default mt-2 mt-lg-0">
                            {lang key='go'}
                        </button>
                    </div>
                </div>
            </form>
        </div>

        {include file="$template/includes/flashmessage.tpl"}
        {if $errorMessageHtml}
            {include file="$template/includes/alert.tpl" type="error" errorshtml=$errorMessageHtml}
        {/if}

        <form role="form" method="post" action="{routePath('account-contacts-save')}">
            <input type="hidden" name="contactid" value="{$contactid}" />

            <div class="row g-4">
                <div class="col-xl-8">
                    <div class="card dashboard-settings-card dashboard-settings-card--primary h-100">
                        <div class="card-body">
                            <div class="dashboard-settings-card__heading">
                                <span class="dashboard-page-heading__icon dashboard-page-heading__icon--mint">
                                    <i class="far fa-user"></i>
                                </span>
                                <div>
                                    <h3 class="card-title">Contact Details</h3>
                                    <p class="dashboard-panel__copy">{lang key='clientareaupdateyourdetails'}</p>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label for="inputFirstName" class="col-form-label">{lang key='clientareafirstname'}</label>
                                        <input type="text" name="firstname" id="inputFirstName" value="{$formdata.firstname}" class="form-control" />
                                    </div>
                                    <div class="form-group">
                                        <label for="inputLastName" class="col-form-label">{lang key='clientarealastname'}</label>
                                        <input type="text" name="lastname" id="inputLastName" value="{$formdata.lastname}" class="form-control" />
                                    </div>
                                    <div class="form-group">
                                        <label for="inputCompanyName" class="col-form-label">{lang key='clientareacompanyname'}</label>
                                        <input type="text" name="companyname" id="inputCompanyName" value="{$formdata.companyname}" class="form-control" />
                                    </div>
                                    <div class="form-group">
                                        <label for="inputEmail" class="col-form-label">{lang key='clientareaemail'}</label>
                                        <input type="email" name="email" id="inputEmail" value="{$formdata.email}" class="form-control" />
                                    </div>
                                    <div class="form-group">
                                        <label for="inputPhone" class="col-form-label">{lang key='clientareaphonenumber'}</label>
                                        <input type="tel" name="phonenumber" id="inputPhone" value="{$formdata.phonenumber}" class="form-control" />
                                    </div>
                                    <div class="form-group">
                                        <label for="inputTaxId" class="col-form-label">{lang key=$taxIdLabel}</label>
                                        <input type="text" name="tax_id" id="inputTaxId" class="form-control" value="{$formdata.tax_id}" />
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label for="inputAddress1" class="col-form-label">{lang key='clientareaaddress1'}</label>
                                        <input type="text" name="address1" id="inputAddress1" value="{$formdata.address1}" class="form-control" />
                                    </div>
                                    <div class="form-group">
                                        <label for="inputAddress2" class="col-form-label">{lang key='clientareaaddress2'}</label>
                                        <input type="text" name="address2" id="inputAddress2" value="{$formdata.address2}" class="form-control" />
                                    </div>
                                    <div class="form-group">
                                        <label for="inputCity" class="col-form-label">{lang key='clientareacity'}</label>
                                        <input type="text" name="city" id="inputCity" value="{$formdata.city}" class="form-control" />
                                    </div>
                                    <div class="form-group">
                                        <label for="inputState" class="col-form-label">{lang key='clientareastate'}</label>
                                        <input type="text" name="state" id="inputState" value="{$formdata.state}" class="form-control" />
                                    </div>
                                    <div class="form-group">
                                        <label for="inputPostcode" class="col-form-label">{lang key='clientareapostcode'}</label>
                                        <input type="text" name="postcode" id="inputPostcode" value="{$formdata.postcode}" class="form-control" />
                                    </div>
                                    <div class="form-group">
                                        <label class="col-form-label" for="country">{lang key='clientareacountry'}</label>
                                        {$countriesdropdown}
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="col-xl-4">
                    <div class="card dashboard-settings-card dashboard-settings-card--preferences h-100">
                        <div class="card-body">
                            <div class="dashboard-settings-card__heading">
                                <span class="dashboard-page-heading__icon dashboard-page-heading__icon--violet">
                                    <i class="far fa-envelope"></i>
                                </span>
                                <div>
                                    <h3 class="card-title">Email Preferences</h3>
                                    <p class="dashboard-panel__copy">Choose which account, billing, and service notifications this contact should receive.</p>
                                </div>
                            </div>
                            <div class="controls form-check dashboard-email-preferences dashboard-email-preferences--grid">
                                {foreach $formdata.emailPreferences as $emailType => $value}
                                    <label class="dashboard-email-preference dashboard-email-preference--card">
                                        <input type="hidden" name="email_preferences[{$emailType}]" value="0">
                                        <input type="checkbox" class="form-check-input" name="email_preferences[{$emailType}]" id="{$emailType}emails" value="1"{if $value} checked="checked"{/if} />
                                        <span class="dashboard-email-preference__label">{lang key="clientareacontactsemails"|cat:$emailType}</span>
                                    </label>
                                {/foreach}
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="form-group text-center mt-4">
                <input class="btn btn-primary" type="submit" name="save" value="{lang key='clientareasavechanges'}" />
                <input class="btn btn-default" type="reset" value="{lang key='cancel'}" />
                <button type="button" class="btn btn-danger" data-toggle="modal" data-target="#modalDeleteContact">{lang key='clientareadeletecontact'}</button>
            </div>
        </form>
    </section>
</section>

<form method="post" action="{routePath('account-contacts-delete')}">
    <input type="hidden" name="contactid" value="{$contactid}">
    <div class="modal fade" id="modalDeleteContact">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header card-header">
                    <h4 class="modal-title">
                        {lang key="clientareadeletecontact"}
                    </h4>
                    <button type="button" class="close" data-dismiss="modal" aria-label="{lang key='close'}"><span aria-hidden="true">&times;</span></button>
                </div>
                <div class="modal-body">
                    <p>{lang key="clientareadeletecontactareyousure"}</p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">
                        {lang key="cancel"}
                    </button>
                    <button type="submit" class="btn btn-primary" id="btnCancelInviteConfirm">
                        {lang key="confirm"}
                    </button>
                </div>
            </div>
        </div>
    </div>
</form>
