<?php

/**
 * The admin-specific functionality of the plugin.
 *
 * @link       https://immowp.fr
 * @since      1.0.0
 *
 * @package    Immowp_Gestion_Immobiliere
 * @subpackage Immowp_Gestion_Immobiliere/admin
 */

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

/**
 * The admin-specific functionality of the plugin.
 *
 * Defines the plugin name, version, and two examples hooks for how to
 * enqueue the admin-specific stylesheet and JavaScript.
 *
 * @package    Immowp_Gestion_Immobiliere
 * @subpackage Immowp_Gestion_Immobiliere/admin
 * @author     ImmoWP <contact@immowp.fr>
 */
class Immowp_Gestion_Immobiliere_Metaboxes {

	/**
	 * The ID of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $plugin_name    The ID of this plugin.
	 */
	private $plugin_name;

	/**
	 * The version of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $version    The current version of this plugin.
	 */
	private $version;

	/**
	 * Initialize the class and set its properties.
	 *
	 * @since    1.0.0
	 * @param      string $plugin_name       The name of this plugin.
	 * @param      string $version    The version of this plugin.
	 */
	public function __construct( $plugin_name, $version ) {
		$this->plugin_name = $plugin_name;
		$this->version     = $version;
	}

	public function agence_add_term_fields( $taxonomy ) {
		?>
			<div class="form-field">
				<label for="agence_adresse">Adresse</label>
				<input type="text" name="agence_adresse" id="agence_adresse" />
			</div>
			<div class="form-field">
				<label for="agence_ville">Ville</label>
				<input type="text" name="agence_ville" id="agence_ville" />
			</div>
			<div class="form-field">
				<label for="agence_cp">Code Postal</label>
				<input type="text" name="agence_cp" id="agence_cp" />
			</div>
			<div class="form-field">
				<label for="agence_telephone">Téléphone</label>
				<input type="text" name="agence_telephone" id="agence_telephone" />
			</div>
			<div class="form-field">
				<label for="agence_email">Email</label>
				<input type="text" name="agence_email" id="agence_email" />
			</div>
			<div class="form-field">
				<label>Image</label>
				<a href="#" class="button rudr-upload">Upload image</a>
				<a href="#" class="rudr-remove" style="display:none">Supprimer image</a>
				<input type="hidden" name="agence_image" value="">
			</div>
			<?php wp_nonce_field( 'immowp_save_agence_meta_nonce', 'immowp_agence_nonce_field' ); ?>
		<?php
	}

	function agence_edit_term_fields( $term, $taxonomy ) {

		// get meta data value
		$agence_adresse   = get_term_meta( $term->term_id, 'agence_adresse', true );
		$agence_ville     = get_term_meta( $term->term_id, 'agence_ville', true );
		$agence_cp        = get_term_meta( $term->term_id, 'agence_cp', true );
		$agence_telephone = get_term_meta( $term->term_id, 'agence_telephone', true );
		$agence_email     = get_term_meta( $term->term_id, 'agence_email', true );
		$agence_image_id  = get_term_meta( $term->term_id, 'agence_image', true );

		?>
		<tr class="form-field">
			<th><label for="agence_adresse">Adresse</label></th>
			<td>
				<input name="agence_adresse" id="agence_adresse" type="text" value="<?php echo esc_attr( $agence_adresse ); ?>" />
			</td>
		</tr>
		<tr class="form-field">
			<th><label for="agence_ville">Ville</label></th>
			<td>
				<input name="agence_ville" id="agence_ville" type="text" value="<?php echo esc_attr( $agence_ville ); ?>" />
			</td>
		</tr>
		<tr class="form-field">
			<th><label for="agence_cp">Code Postal</label></th>
			<td>
				<input name="agence_cp" id="agence_cp" type="text" value="<?php echo esc_attr( $agence_cp ); ?>" />
			</td>
		</tr>
		<tr class="form-field">
			<th><label for="agence_telephone">Téléphone</label></th>
			<td>
				<input name="agence_telephone" id="agence_telephone" type="text" value="<?php echo esc_attr( $agence_telephone ); ?>" />
			</td>
		</tr>
		<tr class="form-field">
			<th><label for="agence_email">Email</label></th>
			<td>
				<input name="agence_email" id="agence_email" type="text" value="<?php echo esc_attr( $agence_email ); ?>" />
			</td>
		</tr>
		<tr class="form-field">
			<th>
				<label for="agence_image">Image</label>
			</th>
			<td>
				<?php $image_html = $agence_image_id ? wp_get_attachment_image( $agence_image_id, 'thumbnail' ) : ''; ?>
				<?php if ( $image_html ) : ?>
					<a href="#" class="button rudr-upload" style="display:none">Choisir l'image</a>
					<a href="#" class="rudr-upload-image">
						<?php echo $image_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- wp_get_attachment_image returns safe HTML ?>
					</a>
					<a href="#" class="rudr-remove">Supprimer l'image</a>
					<input type="hidden" name="agence_image" value="<?php echo absint( $agence_image_id ); ?>">
				<?php else : ?>
					<a href="#" class="button rudr-upload">Choisir l'image</a>
					<a href="#" class="rudr-remove" style="display:none">Supprimer l'image</a>
					<input type="hidden" name="agence_image" value="">
				<?php endif; ?>
			</td>
		</tr>
		<?php wp_nonce_field( 'immowp_save_agence_meta_nonce', 'immowp_agence_nonce_field' ); ?>
		<?php
	}

	function agence_save_term_fields( $term_id ) {

		if ( ! isset( $_POST['immowp_agence_nonce_field'] ) || ! wp_verify_nonce( sanitize_text_field(wp_unslash($_POST['immowp_agence_nonce_field'])), 'immowp_save_agence_meta_nonce' ) ) {
			return;
		}

		$agence_adresse_value = isset($_POST['agence_adresse']) ? sanitize_text_field(wp_unslash($_POST['agence_adresse'])) : '';
		update_term_meta(
			$term_id,
			'agence_adresse',
			$agence_adresse_value
		);

		$agence_ville_value = isset($_POST['agence_ville']) ? sanitize_text_field(wp_unslash($_POST['agence_ville'])) : '';
		update_term_meta(
			$term_id,
			'agence_ville',
			$agence_ville_value
		);

		$agence_cp_value = isset($_POST['agence_cp']) ? sanitize_text_field(wp_unslash($_POST['agence_cp'])) : '';
		update_term_meta(
			$term_id,
			'agence_cp',
			$agence_cp_value
		);

		$agence_telephone_value = isset($_POST['agence_telephone']) ? sanitize_text_field(wp_unslash($_POST['agence_telephone'])) : '';
		update_term_meta(
			$term_id,
			'agence_telephone',
			$agence_telephone_value
		);

		$agence_email_value = isset($_POST['agence_email']) ? sanitize_email(wp_unslash($_POST['agence_email'])) : '';
		update_term_meta(
			$term_id,
			'agence_email',
			$agence_email_value
		);

		$agence_image_value = isset($_POST['agence_image']) ? absint(wp_unslash($_POST['agence_image'])) : 0; // Default to 0 for absint if not set
		update_term_meta(
			$term_id,
			'agence_image',
			$agence_image_value
		);
	}

	public function agent_add_term_fields( $taxonomy ) {
		?>
			<div class="form-field">
				<label for="agent_tel">Téléphone</label>
				<input type="text" name="agent_tel" id="agent_tel" />
			</div>
			<div class="form-field">
				<label for="agent_email">Email</label>
				<input type="text" name="agent_email" id="agent_email" />
			</div>
			<div class="form-field">
				<label>Image</label>
				<a href="#" class="button rudr-upload">Upload image</a>
				<a href="#" class="rudr-remove" style="display:none">Supprimer image</a>
				<input type="hidden" name="agent_image" value="">
			</div>
			<?php wp_nonce_field( 'immowp_save_agent_meta_nonce', 'immowp_agent_nonce_field' ); ?>
		<?php
	}

	function agent_edit_term_fields( $term, $taxonomy ) {

		// get meta data value
		$agent_tel      = get_term_meta( $term->term_id, 'agent_tel', true );
		$agent_email    = get_term_meta( $term->term_id, 'agent_email', true );
		$agent_image_id = get_term_meta( $term->term_id, 'agent_image', true );

		?>
		<tr class="form-field">
			<th><label for="agent_tel">Téléphone</label></th>
			<td>
				<input name="agent_tel" id="agent_tel" type="text" value="<?php echo esc_attr( $agent_tel ); ?>" />
			</td>
		</tr>
		<tr class="form-field">
			<th><label for="agent_email">Email</label></th>
			<td>
				<input name="agent_email" id="agent_email" type="text" value="<?php echo esc_attr( $agent_email ); ?>" />
			</td>
		</tr>
		<tr class="form-field">
			<th>
				<label for="agent_image">Image</label>
			</th>
			<td>
				<?php $agent_image_html = $agent_image_id ? wp_get_attachment_image( $agent_image_id, 'thumbnail' ) : ''; ?>
				<?php if ( $agent_image_html ) : ?>
					<a href="#" class="button rudr-upload" style="display:none">Choisir l'image</a>
					<a href="#" class="rudr-upload-image">
						<?php echo $agent_image_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- wp_get_attachment_image returns safe HTML ?>
					</a>
					<a href="#" class="rudr-remove">Supprimer l'image</a>
					<input type="hidden" name="agent_image" value="<?php echo absint( $agent_image_id ); ?>">
				<?php else : ?>
					<a href="#" class="button rudr-upload">Choisir l'image</a>
					<a href="#" class="rudr-remove" style="display:none">Supprimer l'image</a>
					<input type="hidden" name="agent_image" value="">
				<?php endif; ?>
		</tr>
		<?php wp_nonce_field( 'immowp_save_agent_meta_nonce', 'immowp_agent_nonce_field' ); ?>
		<?php
	}

	function agent_save_term_fields( $term_id ) {

		if ( ! isset( $_POST['immowp_agent_nonce_field'] ) || ! wp_verify_nonce( sanitize_text_field(wp_unslash($_POST['immowp_agent_nonce_field'])), 'immowp_save_agent_meta_nonce' ) ) {
			return;
		}

		$agent_tel_value = isset($_POST['agent_tel']) ? sanitize_text_field(wp_unslash($_POST['agent_tel'])) : '';
		update_term_meta(
			$term_id,
			'agent_tel',
			$agent_tel_value
		);

		$agent_email_value = isset($_POST['agent_email']) ? sanitize_email(wp_unslash($_POST['agent_email'])) : '';
		update_term_meta(
			$term_id,
			'agent_email',
			$agent_email_value
		);

		$agent_image_value = isset($_POST['agent_image']) ? absint(wp_unslash($_POST['agent_image'])) : 0; // Default to 0 for absint if not set
		update_term_meta(
			$term_id,
			'agent_image',
			$agent_image_value
		);
	}
}
