<?php 
global $post;
$original_post_id = $post->ID; // Save the original post ID
$gocars_display = get_post_meta( $post->ID, 'gocars_display', true );
$shortcode = get_post_meta( $post->ID, 'shortcode', true );
$online_admin_link = get_post_meta( $post->ID, 'online_admin_link', true );
$domain = get_post_meta( $post->ID, 'domain', true );
$language_id = get_post_meta( $post->ID, 'language_id', true );

$gocars_database = get_post_meta( $post->ID, 'gocars_database', true ); //not being used anymore

// $url = 'https://gocars.gr/gocars_domains.php';
// $data = file_get_contents($url);
// $domains = json_decode($data, true);
?>

<table class="form-table admin_fields" role="presentation">
    <tbody>
        <tr>
            <th scope="row">
                <label for="shortcode"> <?php esc_html_e( 'Shortcode','gocars_admin' ); ?></label>
            </th>
            <td>
                <fieldset>
                <legend class="screen-reader-text">
                        <span>
                            <?php esc_html_e( 'Shortcode','gocars_admin' ); ?>
                        </span>
                    </legend>
                    <label>
                        <span class="shortcode">
                            <input type="text" onfocus="this.select();" readonly id="shortcode" data-post="<?php echo esc_attr( $post->ID ); ?>" name="shortcode" value="[<?php echo esc_attr( $gocars_display.' id="'.$post->ID.'"' ); ?>]"  class="code" style="min-width: 200px;"/>
                        </span>
                    </label>
                </fieldset>
            </td>
        </tr>
        <tr>
            <th scope="row">
                <label for="form_display"><?php esc_html_e( 'Shortcode Display','gocars_admin' ); ?></label>
            </th>
            <td>
                <fieldset>
                    <legend class="screen-reader-text">
                        <span>
                            <?php esc_html_e( 'Shortcode Display','gocars_admin' ); ?>
                        </span>
                    </legend>
                    <label>
                        <select name="gocars_display" id="gocars_display">
                            <option value="<?php echo __( 'search_form', 'gocars_admin' ); ?>" <?php selected( $gocars_display, 'search_form' ); ?>><?php echo __( 'Search Form', 'gocars_admin' ); ?></option>
                            <option value="<?php echo __( 'fleet', 'gocars_admin' ); ?>" <?php selected( $gocars_display, 'fleet' ); ?>><?php echo  __( 'Fleet', 'gocars_admin' ); ?></option>
                            <option value="<?php echo __( 'terms', 'gocars_admin' ); ?>" <?php selected( $gocars_display, 'terms' ); ?>><?php echo  __( 'Terms', 'gocars_admin' ); ?></option>
                        </select>
                    </label>
                    
                </fieldset>
            </td>
        </tr>
        <tr>
            <th scope="row">
                <label for="domain"><?php esc_html_e( 'Domain','gocars_admin' ); ?>*</label>
            </th>
            <td>
                <fieldset>
                    <legend class="screen-reader-text">
                        <span>
                            <?php esc_html_e( 'Domain','gocars_admin' ); ?>
                        </span>
                    </legend>
                    <label>
                        <?php if ( false ) : ?>
                            <select name="domain" id="domain">
                                <?php 
                                foreach ($domains as $key => $value) {
                                    ?>
                                    <option value="<?php echo $value; ?>" <?php selected( $domain, $value ); ?>><?php echo $value; ?></option>
                                    <?php 
                                }
                                ?>
                            </select>
                        <?php endif; ?>
                        <?php 
                        $domain_value = (get_post_meta( $gocars_database, 'domain', true ) != '' && get_post_meta( $post->ID, 'domain', true ) == '') ? get_post_meta( $gocars_database, 'domain', true ) : esc_attr( get_post_meta( $post->ID, 'domain', true ) );
                        if ($domain_value == '') {
                            $host = $_SERVER['HTTP_HOST'];
                            $domain_value = preg_replace('/^www\./', '', $host); // remove www
                            $parts = explode('.', $domain_value);

                            if (count($parts) >= 2) {
                                $domain_value = $parts[count($parts) - 2];
                            }
                        }
                        ?>
                        <input type="text" required id="domain" name="domain" class="regular-text" value="<?php echo $domain_value; ?>" />
                    </label>
                </fieldset>
            </td>
        </tr>
    </tbody>
</table>  
 
<?php 
global $current_user;
wp_get_current_user();
$user_email = $current_user->user_email;
$allowed_emails = ['@gocreations.gr', '@gocars.gr', '@gocreations.info'];
$email_allowed = false;
foreach ($allowed_emails as $email) {
    if (strpos($user_email, $email) !== false) {
        $email_allowed = true;
        break;
    }
}

if (!$email_allowed) {
    ?>
    <script>
        
        jQuery('form[name="post"] [type="submit"]').prop('disabled', true);
        
        jQuery.ajax({
            url: 'https://gocars.gr/gocars_domains.php',
            method: 'GET',
            dataType: 'json',
            success: function (res) {
                // console.log(res);
            jQuery('form[name="post"] [type="submit"]').prop('disabled', false);
        
                jQuery('form[name="post"]').on('submit', function(e) {
                    var domain = jQuery('[name="domain"]');
                    if (domain.length > 0 && !res.includes(domain.val())) {
                        alert(domain.val() + ' don\'t exist in gocars.gr subdomains');
                        e.preventDefault();
                    }
                });
            },
            error: function (xhr, status, error) {
                // Handle the error if the API request fails
                console.log(status, error);
            }
        });
    
    </script>
    <?php 
}
?>

