<?php
/**
 * Sender templates page (Sender Templates).
 *
 * @package Inspire_Labs\Alsendo_Connect
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
} // Exit if accessed directly

use Inspire_Labs\Alsendo_Connect\Sender_Template_Manager;
use Inspire_Labs\Alsendo_Connect\Plugin;

$template_manager = Sender_Template_Manager::get_instance();
$templates        = $template_manager->get_templates( true );
$_nonce           = wp_create_nonce( 'alsendo_connect_ajax_sender_template' );
$_set_default_nonce = wp_create_nonce( 'alsendo_connect_ajax_set_default_sender_template' );
$default_slug     = Sender_Template_Manager::get_default_template_slug();
$api_type         = alsendo_connect()->get_main_settings_api();
$is_ecolet_api    = ( 'ecolet' === $api_type );

$sender_fields    = Sender_Template_Manager::get_sender_fields();

$address_type_options = array(
    '0' => esc_html__( 'Company', 'alsendo-connect' ),
    '1' => esc_html__( 'Private', 'alsendo-connect' ),
);

$country_options = array(
    'AT' => esc_html__( 'Austria', 'alsendo-connect' ),
    'BE' => esc_html__( 'Belgium', 'alsendo-connect' ),
    'BG' => esc_html__( 'Bulgaria', 'alsendo-connect' ),
    'HR' => esc_html__( 'Croatia', 'alsendo-connect' ),
    'CY' => esc_html__( 'Cyprus', 'alsendo-connect' ),
    'CZ' => esc_html__( 'Czech Republic', 'alsendo-connect' ),
    'DK' => esc_html__( 'Denmark', 'alsendo-connect' ),
    'EE' => esc_html__( 'Estonia', 'alsendo-connect' ),
    'FI' => esc_html__( 'Finland', 'alsendo-connect' ),
    'FR' => esc_html__( 'France', 'alsendo-connect' ),
    'DE' => esc_html__( 'Germany', 'alsendo-connect' ),
    'GR' => esc_html__( 'Greece', 'alsendo-connect' ),
    'HU' => esc_html__( 'Hungary', 'alsendo-connect' ),
    'IE' => esc_html__( 'Ireland', 'alsendo-connect' ),
    'IT' => esc_html__( 'Italy', 'alsendo-connect' ),
    'LV' => esc_html__( 'Latvia', 'alsendo-connect' ),
    'LT' => esc_html__( 'Lithuania', 'alsendo-connect' ),
    'LU' => esc_html__( 'Luxembourg', 'alsendo-connect' ),
    'MT' => esc_html__( 'Malta', 'alsendo-connect' ),
    'NL' => esc_html__( 'Netherlands', 'alsendo-connect' ),
    'PL' => esc_html__( 'Poland', 'alsendo-connect' ),
    'PT' => esc_html__( 'Portugal', 'alsendo-connect' ),
    'RO' => esc_html__( 'Romania', 'alsendo-connect' ),
    'SK' => esc_html__( 'Slovakia', 'alsendo-connect' ),
    'SI' => esc_html__( 'Slovenia', 'alsendo-connect' ),
    'ES' => esc_html__( 'Spain', 'alsendo-connect' ),
    'SE' => esc_html__( 'Sweden', 'alsendo-connect' ),
);

if ( 1 === count( $templates ) && empty( $default_slug ) ) {
    $only_item  = reset( $templates );
    $only_slug  = ! empty( $only_item['template_slug'] ) ? $only_item['template_slug'] : sanitize_title( $only_item['name'] );
    update_option( Sender_Template_Manager::DEFAULT_TEMPLATE_OPTION, $only_slug );
    $default_slug = $only_slug;
}
?>
<style>
    #alsendoSenderTemplateDialog { font-size: 13px; }
    #alsendoSenderTemplateDialog .alsendo-stpl-row {
        display: flex;
        flex-wrap: wrap;
        gap: 5px;
        margin-bottom: 6px;
    }
    #alsendoSenderTemplateDialog .alsendo-stpl-field {
        display: flex;
        flex-direction: column;
        flex: 1 1 0;
        min-width: 0;
    }
    #alsendoSenderTemplateDialog .alsendo-stpl-field.full {
        flex: 1 1 100%;
    }
    #alsendoSenderTemplateDialog .alsendo-stpl-field.half {
        flex: 1 1 calc(50% - 3px);
    }
    #alsendoSenderTemplateDialog .alsendo-stpl-field label {
        font-weight: 600;
        margin-bottom: 2px;
        font-size: 12px;
        color: #1d2327;
    }
    #alsendoSenderTemplateDialog .alsendo-stpl-field input[type="text"],
    #alsendoSenderTemplateDialog .alsendo-stpl-field input[type="email"],
    #alsendoSenderTemplateDialog .alsendo-stpl-field select {
        width: 100%;
        padding: 3px 6px;
        box-sizing: border-box;
        font-size: 13px;
        height: 26px;
    }
    #alsendoSenderTemplateDialog .alsendo-stpl-separator {
        border-top: 1px solid #ddd;
        margin: 8px 0 6px;
    }
    .alsendo-stpl-description-cell {
        max-width: 400px;
        word-break: break-word;
    }
    body.woocommerce_page_wc-settings p.submit {
        display: none !important;
    }
    .alsendo_connect_set_default_sender_template.is-default {
        color: #fff !important;
        background: #00a32a !important;
        border-color: #00a32a !important;
        box-shadow: none !important;
        text-shadow: none !important;
        cursor: default;
        pointer-events: none;
    }
</style>

<div class="wrap">
    <h1 class="wp-heading-inline"><?php esc_html_e( 'Sender templates', 'alsendo-connect' ); ?></h1>
    <a id="alsendo_connect_add_sender_template" class="page-title-action"><?php esc_html_e( 'Add template', 'alsendo-connect' ); ?></a>
    <hr class="wp-header-end">
    <table id="alsendo_sender_templates_table" class="wp-list-table widefat fixed striped table-view-list">
        <thead>
        <tr>
            <th scope="col"><?php esc_html_e( 'Name', 'alsendo-connect' ); ?></th>
            <th scope="col"><?php esc_html_e( 'Description', 'alsendo-connect' ); ?></th>
            <th scope="col"><?php esc_html_e( 'Options', 'alsendo-connect' ); ?></th>
        </tr>
        </thead>
        <tbody id="the-list">
        <?php if ( 0 === count( $templates ) ) : ?>
            <tr class="no-items">
                <td class="colspanchange" colspan="3"><?php esc_html_e( 'No templates were found.', 'alsendo-connect' ); ?></td>
            </tr>
        <?php else : ?>
            <?php foreach ( $templates as $item ) :
                $item_slug  = ! empty( $item['template_slug'] ) ? $item['template_slug'] : sanitize_title( $item['name'] );
                $is_default = ( $item_slug === $default_slug );
                ?>
                <tr data-template="<?php echo esc_attr( wp_json_encode( $item ) ); ?>">
                    <td><?php echo esc_html( $item['name'] ); ?></td>
                    <td class="alsendo-stpl-description-cell">
                        <?php
                        $desc_parts = array();
                        if ( ! empty( $item['company_name'] ) ) {
                            $desc_parts[] = esc_html( $item['company_name'] );
                        }
                        if ( ! empty( $item['city'] ) ) {
                            $desc_parts[] = esc_html( $item['city'] );
                        }
                        if ( ! empty( $item['country'] ) ) {
                            $desc_parts[] = esc_html( strtoupper( $item['country'] ) );
                        }
                        if ( ! empty( $item['phone'] ) ) {
                            $desc_parts[] = esc_html( $item['phone'] );
                        }
                        echo implode( ' | ', $desc_parts ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                        ?>
                    </td>
                    <td>
                        <?php if ( $is_default ) : ?>
                            <a class="button alsendo_connect_set_default_sender_template is-default" data-slug="<?php echo esc_attr( $item_slug ); ?>"><?php esc_html_e( 'Default template', 'alsendo-connect' ); ?></a>
                        <?php else : ?>
                            <a class="button alsendo_connect_set_default_sender_template" data-slug="<?php echo esc_attr( $item_slug ); ?>"><?php esc_html_e( 'Set as default template', 'alsendo-connect' ); ?></a>
                        <?php endif; ?>
                        <a class="button alsendo_connect_edit_sender_template"><?php esc_html_e( 'Edit', 'alsendo-connect' ); ?></a>
                        <?php if ( ! $is_default ) : ?>
                            <a class="button alsendo_connect_delete_sender_template"><?php esc_html_e( 'Delete', 'alsendo-connect' ); ?></a>
                        <?php endif; ?>
                    </td>
                </tr>
            <?php endforeach; ?>
        <?php endif; ?>
        </tbody>
        <tfoot>
        <tr>
            <th scope="col"><?php esc_html_e( 'Name', 'alsendo-connect' ); ?></th>
            <th scope="col"><?php esc_html_e( 'Description', 'alsendo-connect' ); ?></th>
            <th scope="col"><?php esc_html_e( 'Options', 'alsendo-connect' ); ?></th>
        </tr>
        </tfoot>
    </table>
    <div class="clear"></div>
</div>

<div id="alsendoSenderTemplateDialog" title="Template" style="display: none;">
    <div id="alsendoSenderTemplateFormFields">
        <input type="hidden" name="action" value="alsendo_connect_sender_template">
        <input type="hidden" name="template[_nonce]" value="<?php echo esc_attr( $_nonce ); ?>">
        <input id="alsendo_sender_template_id" type="hidden" name="template[id]" value="">
        <input id="alsendo_sender_template_legacy_slug" type="hidden" name="template[_legacy_slug]" value="">

        <!-- Name -->
        <div class="alsendo-stpl-row">
            <div class="alsendo-stpl-field full">
                <label for="alsendo_sender_template_name"><?php esc_html_e( 'Template name', 'alsendo-connect' ); ?></label>
                <input id="alsendo_sender_template_name" type="text" name="template[name]" value="" required="required">
            </div>
        </div>

        <!-- Address type -->
        <div class="alsendo-stpl-row">
            <div class="alsendo-stpl-field half">
                <label for="alsendo_sender_template_is_residential"><?php esc_html_e( 'Address type', 'alsendo-connect' ); ?></label>
                <select id="alsendo_sender_template_is_residential" name="template[is_residential]">
                    <?php foreach ( $address_type_options as $value => $label ) : ?>
                        <option value="<?php echo esc_attr( $value ); ?>"><?php echo esc_html( $label ); ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="alsendo-stpl-field half">
                <label for="alsendo_sender_template_country"><?php esc_html_e( 'Country', 'alsendo-connect' ); ?></label>
                <select id="alsendo_sender_template_country" name="template[country]">
                    <option value=""><?php esc_html_e( '— Select —', 'alsendo-connect' ); ?></option>
                    <?php foreach ( $country_options as $value => $label ) : ?>
                        <option value="<?php echo esc_attr( $value ); ?>"><?php echo esc_html( $label ); ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
        </div>

        <!-- Company name -->
        <div class="alsendo-stpl-row">
            <div class="alsendo-stpl-field full">
                <label for="alsendo_sender_template_company_name"><?php esc_html_e( 'Company name', 'alsendo-connect' ); ?></label>
                <input id="alsendo_sender_template_company_name" type="text" name="template[company_name]" value="" maxlength="50">
            </div>
        </div>

        <!-- First name + Last name -->
        <div class="alsendo-stpl-row">
            <div class="alsendo-stpl-field half">
                <label for="alsendo_sender_template_first_name"><?php esc_html_e( 'First Name', 'alsendo-connect' ); ?></label>
                <input id="alsendo_sender_template_first_name" type="text" name="template[first_name]" value="" maxlength="50">
            </div>
            <div class="alsendo-stpl-field half">
                <label for="alsendo_sender_template_last_name"><?php esc_html_e( 'Last name', 'alsendo-connect' ); ?></label>
                <input id="alsendo_sender_template_last_name" type="text" name="template[last_name]" value="" maxlength="50">
            </div>
        </div>

        <!-- Street + Building + Apartment -->
        <div class="alsendo-stpl-row">
            <div class="alsendo-stpl-field">
                <label for="alsendo_sender_template_street">
					<?php echo $is_ecolet_api ? esc_html__( 'Street name', 'alsendo-connect' ) : esc_html__( 'Street', 'alsendo-connect' ); ?>
				</label>
                <input id="alsendo_sender_template_street" type="text" name="template[street]" value="">
            </div>
            <div class="alsendo-stpl-field">
                <label for="alsendo_sender_template_building_number">
					<?php echo $is_ecolet_api ? esc_html__( 'Street number', 'alsendo-connect' ) : esc_html__( 'Building number', 'alsendo-connect' ); ?>
				</label>
                <input id="alsendo_sender_template_building_number" type="text" name="template[building_number]" value="" maxlength="10">
            </div>
			<?php if ( ! $is_ecolet_api ) : ?>
                <div class="alsendo-stpl-field">
                    <label for="alsendo_sender_template_apartment_number"><?php esc_html_e( 'Apartment number', 'alsendo-connect' ); ?></label>
                    <input id="alsendo_sender_template_apartment_number" type="text" name="template[apartment_number]" value="" maxlength="10">
                </div>
			<?php endif; ?>
        </div>
		<?php if ( $is_ecolet_api ) : ?>
            <!-- Block + Entrance + Floor + Apartment (Ecolet) -->
            <div class="alsendo-stpl-row">
                <div class="alsendo-stpl-field">
                    <label for="alsendo_sender_template_block"><?php esc_html_e( 'Block', 'alsendo-connect' ); ?></label>
                    <input id="alsendo_sender_template_block" type="text" name="template[block]" value="" maxlength="10">
                </div>
                <div class="alsendo-stpl-field">
                    <label for="alsendo_sender_template_entrance"><?php esc_html_e( 'Entrance', 'alsendo-connect' ); ?></label>
                    <input id="alsendo_sender_template_entrance" type="text" name="template[entrance]" value="" maxlength="10">
                </div>
                <div class="alsendo-stpl-field">
                    <label for="alsendo_sender_template_floor"><?php esc_html_e( 'Floor', 'alsendo-connect' ); ?></label>
                    <input id="alsendo_sender_template_floor" type="text" name="template[floor]" value="" maxlength="10">
                </div>
                <div class="alsendo-stpl-field">
                    <label for="alsendo_sender_template_apartment_number"><?php esc_html_e( 'Apartment', 'alsendo-connect' ); ?></label>
                    <input id="alsendo_sender_template_apartment_number" type="text" name="template[apartment_number]" value="" maxlength="10">
                </div>
            </div>
		<?php endif; ?>

        <!-- Postal code + Province + City -->
        <div class="alsendo-stpl-row">
            <div class="alsendo-stpl-field">
                <label for="alsendo_sender_template_postal_code"><?php esc_html_e( 'Postal code', 'alsendo-connect' ); ?></label>
                <input id="alsendo_sender_template_postal_code" type="text" name="template[postal_code]" value="">
            </div>
            <div class="alsendo-stpl-field">
                <label for="alsendo_sender_template_province"><?php esc_html_e( 'County', 'alsendo-connect' ); ?></label>
                <input id="alsendo_sender_template_province" type="text" name="template[province]" value="" maxlength="100">
            </div>
            <div class="alsendo-stpl-field">
                <label for="alsendo_sender_template_city"><?php esc_html_e( 'City', 'alsendo-connect' ); ?></label>
                <input id="alsendo_sender_template_city" type="text" name="template[city]" value="" maxlength="50">
            </div>
        </div>

        <div class="alsendo-stpl-separator"></div>

        <!-- Contact person + Phone + Email -->
        <div class="alsendo-stpl-row">
            <div class="alsendo-stpl-field">
                <label for="alsendo_sender_template_contact_person"><?php esc_html_e( 'Contact person', 'alsendo-connect' ); ?></label>
                <input id="alsendo_sender_template_contact_person" type="text" name="template[contact_person]" value="" maxlength="50">
            </div>
            <div class="alsendo-stpl-field">
                <label for="alsendo_sender_template_phone"><?php esc_html_e( 'Phone', 'alsendo-connect' ); ?></label>
                <input id="alsendo_sender_template_phone" type="text" name="template[phone]" value="">
            </div>
            <div class="alsendo-stpl-field">
                <label for="alsendo_sender_template_email"><?php esc_html_e( 'E-mail', 'alsendo-connect' ); ?></label>
                <input id="alsendo_sender_template_email" type="email" name="template[email]" value="">
            </div>
        </div>

        <!-- Bank account number -->
        <div class="alsendo-stpl-row">
            <div class="alsendo-stpl-field full">
                <label for="alsendo_sender_template_bank_account_number"><?php esc_html_e( 'Bank account number', 'alsendo-connect' ); ?></label>
                <input id="alsendo_sender_template_bank_account_number" type="text" name="template[bank_account_number]" value="">
            </div>
        </div>
    </div>
</div>
