<?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_Annonce {

	/**
	 * 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 create_image_gallery_metabox() {
		// Geenral
		add_meta_box(
			'general_metabox',
			'Général',
			array( $this, 'display_general_metabox' ),
			'immowp_gestion_immo',
			'normal',
			'high'
		);

		add_meta_box(
			'image_gallery_metabox',
			'Galerie images',
			array( $this, 'display_image_gallery_metabox' ),
			'immowp_gestion_immo',
			'normal',
			'high'
		);

		add_meta_box(
			'finance_metabox',
			'Finance',
			array( $this, 'display_finance_metabox' ),
			'immowp_gestion_immo',
			'normal',
			'high'
		);

		add_meta_box(
			'surfaces_metabox',
			'Surfaces',
			array( $this, 'display_surfaces_metabox' ),
			'immowp_gestion_immo',
			'normal',
			'high'
		);

		add_meta_box(
			'localisation_metabox',
			'Localisation',
			array( $this, 'display_localisation_metabox' ),
			'immowp_gestion_immo',
			'normal',
			'high'
		);

		add_meta_box(
			'diagnostics_metabox',
			'Diagnostics/Alur',
			array( $this, 'display_diagnostics_metabox' ),
			'immowp_gestion_immo',
			'normal',
			'high'
		);
	}


	public function display_image_gallery_metabox( $post ) {
		$array = get_post_meta( $post->ID, 'image_ids', true );
		if ( is_array( $array ) ) {
			$image_ids = $array;
		} else {
			$image_ids = explode( ',', $array );
		}

		?>
			<?php wp_nonce_field( 'immowp_save_annonce_metabox_nonce', 'immowp_annonce_nonce_field' ); ?>
			<div id="image-gallery-metabox">
				<ul class="image-gallery-items">
				<?php
				if ( $image_ids ) :
					foreach ( $image_ids as $image_id ) :
						$image_html = wp_get_attachment_image( $image_id, 'thumbnail' );
						?>
					<li class="image-gallery-item">
						<input type="hidden" name="image_ids[]" value="<?php echo esc_attr( $image_id ); ?>">
						<?php echo $image_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- generated markup ?>
						<p>
						<button type="button" class="button remove-image-button">X</button>
						</p>
					</li>
						<?php
						endforeach;
					endif;
				?>
				</ul>
				<p>
				<button type="button" class="button button-primary button-large add_image_button" data-uploader-title="Add image(s) to gallery" data-uploader-button-text="Add image(s)">Ajouter des images</button>
				</p>
			</div>
		<?php
	}

	public function display_general_metabox( $post ) {
		$affId             = get_post_meta( $post->ID, 'affId', true ) ?? '';
		$affNum            = get_post_meta( $post->ID, 'affNum', true ) ?? '';
		$video             = get_post_meta( $post->ID, 'video', true ) ?? '';
		$visiteVirtuelle   = get_post_meta( $post->ID, 'visiteVirtuelle', true ) ?? '';
		$mandatNumero      = get_post_meta( $post->ID, 'mandatNumero', true ) ?? '';
		$mandatType        = get_post_meta( $post->ID, 'mandatType', true ) ?? '';
		$exclusivite       = get_post_meta( $post->ID, 'exclusivite', true ) ?? '';
		$vendu             = get_post_meta( $post->ID, 'vendu', true ) ?? '';
		$anneeConstruction = get_post_meta( $post->ID, 'anneeConstruction', true ) ?? '';
		$etat              = get_post_meta( $post->ID, 'etat', true ) ?? '';
		$exposition        = get_post_meta( $post->ID, 'exposition', true ) ?? '';
		$vue               = get_post_meta( $post->ID, 'vue', true ) ?? '';
		$chauffage         = get_post_meta( $post->ID, 'chauffage', true ) ?? '';
		$cuisine           = get_post_meta( $post->ID, 'cuisine', true ) ?? '';

		?>
		<table class="form-table">
			<tr>
				<th scope="row"><label for="affId">Unique ID</label></th>
				<td><input type="text" name="affId" id="affId" value="<?php echo esc_attr( $affId ); ?>"></td>
			</tr>
			<tr>
				<th scope="row"><label for="affNum">Référence</label></th>
				<td><input type="text" name="affNum" id="affNum" value="<?php echo esc_attr( $affNum ); ?>"></td>
			</tr>
			<tr>
				<th scope="row"><label for="video">Vidéo (url)</label></th>
				<td><input type="text" name="video" id="video" value="<?php echo esc_attr( $video ); ?>"></td>
			</tr>
			<tr>
				<th scope="row"><label for="visiteVirtuelle">Visite virtuelle (url)</label></th>
				<td><input type="text" name="visiteVirtuelle" id="visiteVirtuelle" value="<?php echo esc_attr( $visiteVirtuelle ); ?>"></td>
			</tr>
			<tr>
				<th scope="row"><label for="mandatNumero">Mandat numéro</label></th>
				<td><input type="text" name="mandatNumero" id="mandatNumero" value="<?php echo esc_attr( $mandatNumero ); ?>"></td>
			</tr>
			<tr>
				<th scope="row"><label for="mandatType">Type mandat</label></th>
				<td><input type="text" name="mandatType" id="mandatType" value="<?php echo esc_attr( $mandatType ); ?>"></td>
			</tr>
			<tr>
				<th scope="row"><label for="exclusivite">Exclusivité</label></th>
				<td><input type="checkbox" name="exclusivite" id="exclusivite" value="1" <?php checked( $exclusivite, 1 ); ?>></td>
			</tr>
			<tr>
				<th scope="row"><label for="vendu">Vendu</label></th>
				<td><input type="checkbox" name="vendu" id="vendu" value="1" <?php checked( $vendu, 1 ); ?>></td>
			</tr>
			<tr>
				<th scope="row"><label for="anneeConstruction">Année de construction</label></th>
				<td><input type="text" name="anneeConstruction" id="anneeConstruction" value="<?php echo esc_attr( $anneeConstruction ); ?>"></td>
			</tr>
			<tr>
				<th scope="row"><label for="etat">Etat</label></th>
				<td><input type="text" name="etat" id="etat" value="<?php echo esc_attr( $etat ); ?>"></td>
			</tr>
			<tr>
				<th scope="row"><label for="exposition">Exposition</label></th>
				<td><input type="text" name="exposition" id="exposition" value="<?php echo esc_attr( $exposition ); ?>"></td>
			</tr>
			<tr>
				<th scope="row"><label for="vue">Vue</label></th>
				<td><input type="text" name="vue" id="vue" value="<?php echo esc_attr( $vue ); ?>"></td>
			</tr>
			<tr>
				<th scope="row"><label for="chauffage">Chauffage</label></th>
				<td><input type="text" name="chauffage" id="chauffage" value="<?php echo esc_attr( $chauffage ); ?>"></td>
			</tr>
			<tr>
				<th scope="row"><label for="cuisine">Cuisine</label></th>
				<td><input type="text" name="cuisine" id="cuisine" value="<?php echo esc_attr( $cuisine ); ?>"></td>
			</tr>
		</table>
		<?php
	}

	public function display_finance_metabox( $post ) {
		$prix                = get_post_meta( $post->ID, 'prix', true ) ?? '';
		$prixHorsHono        = get_post_meta( $post->ID, 'prixHorsHono', true ) ?? '';
		$pourcentageHono     = get_post_meta( $post->ID, 'pourcentageHono', true ) ?? '';
		$typeLoyer           = get_post_meta( $post->ID, 'typeLoyer', true ) ?? '';
		$chargesHonoraires   = get_post_meta( $post->ID, 'chargesHonoraires', true ) ?? '';
		$provisionSurCharges = get_post_meta( $post->ID, 'provisionSurCharges', true ) ?? '';
		$honoEDL             = get_post_meta( $post->ID, 'honoEDL', true ) ?? '';
		$taxeFonciere        = get_post_meta( $post->ID, 'taxeFonciere', true ) ?? '';
		$fraisAgence         = get_post_meta( $post->ID, 'fraisAgence', true ) ?? '';
		$depotGarantie       = get_post_meta( $post->ID, 'depotGarantie', true ) ?? '';

		?>
		<table class="form-table">
			<tr>
				<th scope="row"><label for="prix">Prix ou loyer</label></th>
				<td><input type="text" name="prix" id="prix" value="<?php echo esc_attr( $prix ); ?>"></td>
			</tr>
			<tr>
				<th scope="row"><label for="prixHorsHono">Prix hors honoraires</label></th>
				<td><input type="text" name="prixHorsHono" id="prixHorsHono" value="<?php echo esc_attr( $prixHorsHono ); ?>"></td>
			</tr>
			<tr>
				<th scope="row"><label for="pourcentageHono">Pourcentage honoraires</label></th>
				<td><input type="text" name="pourcentageHono" id="pourcentageHono" value="<?php echo esc_attr( $pourcentageHono ); ?>"></td>
			</tr>
			<tr>
				<th scope="row"><label for="chargesHonoraires">Charges Honoraires</label></th>
				<td>
					<select name="chargesHonoraires" id="chargesHonoraires">
						<option value=""></option>
						<option value="vendeur" <?php selected( $chargesHonoraires, 'vendeur' ); ?>>Vendeur</option>
						<option value="acheteur" <?php selected( $chargesHonoraires, 'acheteur' ); ?>>Acheteur</option>
						<option value="both" <?php selected( $chargesHonoraires, 'both' ); ?>>Acheteur & Vendeur</option>
					</select>
				</td>
			</tr>
			<tr>
				<th scope="row"><label for="typeLoyer">Type Loyer</label></th>
				<td>
					<select name="typeLoyer" id="typeLoyer">
						<option value=""></option>
						<option value="cc" <?php selected( $typeLoyer, 'cc' ); ?>>Charges comprises</option>
						<option value="hc" <?php selected( $typeLoyer, 'hc' ); ?>>Hors charges</option>
					</select>
				</td>
			</tr>
			<tr>
				<th scope="row"><label for="provisionSurCharges">Charges par mois</label></th>
				<td><input type="text" name="provisionSurCharges" id="provisionSurCharges" value="<?php echo esc_attr( $provisionSurCharges ); ?>"></td>
			</tr>
			<tr>
				<th scope="row"><label for="honoEDL">Honoraires etat des lieux</label></th>
				<td><input type="text" name="honoEDL" id="honoEDL" value="<?php echo esc_attr( $honoEDL ); ?>"></td>
			</tr>
			<tr>
				<th scope="row"><label for="taxeFonciere">Taxe Foncière</label></th>
				<td><input type="text" name="taxeFonciere" id="taxeFonciere" value="<?php echo esc_attr( $taxeFonciere ); ?>"></td>
			</tr>
			<tr>
				<th scope="row"><label for="fraisAgence">Frais agence</label></th>
				<td><input type="text" name="fraisAgence" id="fraisAgence" value="<?php echo esc_attr( $fraisAgence ); ?>"></td>
			</tr>
			<tr>
				<th scope="row"><label for="depotGarantie">Dépot garantie</label></th>
				<td><input type="text" name="depotGarantie" id="depotGarantie" value="<?php echo esc_attr( $depotGarantie ); ?>"></td>
			</tr>

		</table>
		<?php
	}

	public function display_localisation_metabox( $post ) {
		$adresse     = get_post_meta( $post->ID, 'adresse', true ) ?? '';
		$cp          = get_post_meta( $post->ID, 'cp', true ) ?? '';
		$ville       = get_post_meta( $post->ID, 'ville', true ) ?? '';
		$pays        = get_post_meta( $post->ID, 'pays', true ) ?? '';
		$departement = get_post_meta( $post->ID, 'departement', true ) ?? '';
		$secteur     = get_post_meta( $post->ID, 'secteur', true ) ?? '';

		?>
		<table class="form-table">
			<tr>
				<th scope="row"><label for="adresse">Adresse</label></th>
				<td><input type="text" name="adresse" id="adresse" value="<?php echo esc_attr( $adresse ); ?>"></td>
			</tr>
			<tr>
				<th scope="row"><label for="cp">Code Postal</label></th>
				<td><input type="text" name="cp" id="cp" value="<?php echo esc_attr( $cp ); ?>"></td>
			</tr>
			<tr>
				<th scope="row"><label for="ville">Ville</label></th>
				<td><input type="text" name="ville" id="ville" value="<?php echo esc_attr( $ville ); ?>"></td>
			</tr>
			<tr>
				<th scope="row"><label for="pays">Pays</label></th>
				<td><input type="text" name="pays" id="pays" value="<?php echo esc_attr( $pays ); ?>"></td>
			</tr>
			<tr>
				<th scope="row"><label for="departement">Département</label></th>
				<td><input type="text" name="departement" id="departement" value="<?php echo esc_attr( $departement ); ?>"></td>
			</tr>
			<tr>
				<th scope="row"><label for="secteur">Secteur</label></th>
				<td><input type="text" name="secteur" id="secteur" value="<?php echo esc_attr( $secteur ); ?>"></td>
			</tr>
		</table>
		<?php
	}

	public function display_surfaces_metabox( $post ) {
		$nbrePieces     = get_post_meta( $post->ID, 'nbrePieces', true ) ?? '';
		$surface        = get_post_meta( $post->ID, 'surface', true ) ?? '';
		$surfaceTerrain = get_post_meta( $post->ID, 'surfaceTerrain', true ) ?? '';
		$nbreChambres   = get_post_meta( $post->ID, 'nbreChambres', true ) ?? '';
		$nbreSDB        = get_post_meta( $post->ID, 'nbreSDB', true ) ?? '';
		$nbreSDE        = get_post_meta( $post->ID, 'nbreSDE', true ) ?? '';
		$nbreWC         = get_post_meta( $post->ID, 'nbreWC', true ) ?? '';
		$nbreEtage      = get_post_meta( $post->ID, 'nbreEtage', true ) ?? '';
		$numEtage       = get_post_meta( $post->ID, 'numEtage', true ) ?? '';

		?>
		<table class="form-table">
			<tr>
				<th scope="row"><label for="nbrePieces">Nombre de pièces</label></th>
				<td><input type="number" name="nbrePieces" id="nbrePieces" value="<?php echo esc_attr( $nbrePieces ); ?>"></td>
			</tr>
			<tr>
				<th scope="row"><label for="surface">Surface</label></th>
				<td><input type="number" name="surface" id="surface" value="<?php echo esc_attr( $surface ); ?>"></td>
			</tr>
			<tr>
				<th scope="row"><label for="surfaceTerrain">Surface jardin</label></th>
				<td><input type="number" name="surfaceTerrain" id="surfaceTerrain" value="<?php echo esc_attr( $surfaceTerrain ); ?>"></td>
			</tr>
			<tr>
				<th scope="row"><label for="nbreChambres">Nombre de Chambres</label></th>
				<td><input type="number" name="nbreChambres" id="nbreChambres" value="<?php echo esc_attr( $nbreChambres ); ?>"></td>
			</tr>
			<tr>
				<th scope="row"><label for="nbreSDB">Nombre de Salle de Bains</label></th>
				<td><input type="number" name="nbreSDB" id="nbreSDB" value="<?php echo esc_attr( $nbreSDB ); ?>"></td>
			</tr>
			<tr>
				<th scope="row"><label for="nbreSDE">Nombre de Salle d'eau</label></th>
				<td><input type="number" name="nbreSDE" id="nbreSDE" value="<?php echo esc_attr( $nbreSDE ); ?>"></td>
			</tr>
			<tr>
				<th scope="row"><label for="nbreWC">Nombre de WC</label></th>
				<td><input type="number" name="nbreWC" id="nbreWC" value="<?php echo esc_attr( $nbreWC ); ?>"></td>
			</tr>
			<tr>
				<th scope="row"><label for="nbreEtage">Nombre d'étages</label></th>
				<td><input type="number" name="nbreEtage" id="nbreEtage" value="<?php echo esc_attr( $nbreEtage ); ?>"></td>
			</tr>
			<tr>
				<th scope="row"><label for="numEtage">Numéro étage</label></th>
				<td><input type="number" name="numEtage" id="numEtage" value="<?php echo esc_attr( $numEtage ); ?>"></td>
			</tr>
		</table>
		<?php
	}

	public function display_diagnostics_metabox( $post ) {
		$dpe                            = get_post_meta( $post->ID, 'dpe', true ) ?? '';
		$dpeNumber                      = get_post_meta( $post->ID, 'dpeNumber', true ) ?? '';
		$ges                            = get_post_meta( $post->ID, 'ges', true ) ?? '';
		$gesNumber                      = get_post_meta( $post->ID, 'gesNumber', true ) ?? '';
		$montantEnergieMin              = get_post_meta( $post->ID, 'montantEnergieMin', true ) ?? '';
		$montantEnergieMax              = get_post_meta( $post->ID, 'montantEnergieMax', true ) ?? '';
		$montant_depense_energie_estime = get_post_meta( $post->ID, 'montant_depense_energie_estime', true ) ?? '';
		$dateDpe                        = get_post_meta( $post->ID, 'dateDpe', true ) ?? '';
		$dateDpeReference               = get_post_meta( $post->ID, 'dateDpeReference', true ) ?? '';
		$chargesAnnuelles               = get_post_meta( $post->ID, 'chargesAnnuelles', true ) ?? '';
		$nomProcedures                  = get_post_meta( $post->ID, 'nomProcedures', true ) ?? '';
		$procedures                     = get_post_meta( $post->ID, 'procedures', true ) ?? '';
		$nbLots                         = get_post_meta( $post->ID, 'nbLots', true ) ?? '';
		$copro                          = get_post_meta( $post->ID, 'copro', true ) ?? '';

		?>
		<table class="form-table">
			<tr>
				<th scope="row"><label for="dpe">Étiquette DPE (lettre)</label></th>
				<td><input type="text" name="dpe" id="dpe" value="<?php echo esc_attr( $dpe ); ?>">
				<p class="description">
					Classe énergétique du bien dépendant de la valeur relevée lors du diagnostic et de sa date de réalisation.
				</p>
				</td>
			</tr>
			<tr>
				<th scope="row"><label for="dpeNumber">Consommation énergétique en kWh/m²/an (DPE)</label></th>
				<td><input type="number" name="dpeNumber" id="dpeNumber" value="<?php echo esc_attr( $dpeNumber ); ?>"></td>
			</tr>
			<tr>
				<th scope="row"><label for="ges">Étiquette GES (lettre)</label></th>
				<td><input type="text" name="ges" id="ges" value="<?php echo esc_attr( $ges ); ?>">
				<p class="description">
					Classe climatique (émission de Gaz à Effet de Serre) dépendant de la valeur relevée lors du diagnostic et de sa date de réalisation.
				</p>
				</td>
			</tr>
			<tr>
				<th scope="row"><label for="gesNumber">Émission en CO2/m²/an (GES)</label></th>
				<td><input type="number" name="gesNumber" id="gesNumber" value="<?php echo esc_attr( $gesNumber ); ?>"></td>
			</tr>
			<tr>
				<th scope="row"><label for="montantEnergieMin">Montant dépenses énergétiques min</label></th>
				<td><input type="text" name="montantEnergieMin" id="montantEnergieMin" value="<?php echo esc_attr( $montantEnergieMin ); ?>">
				<p class="description">
					Montant minimum estimé de la consommation énergétique annuelle du bien dans un usage standard d’habitation
				</p>
				</td>
			</tr>
			<tr>
				<th scope="row"><label for="montantEnergieMax">Montant dépenses énergétiques max</label></th>
				<td><input type="text" name="montantEnergieMax" id="montantEnergieMax" value="<?php echo esc_attr( $montantEnergieMax ); ?>">
				<p class="description">
					Montant maximum estimé de la consommation énergétique annuelle du bien dans un usage standard d’habitation
				</p>
				</td>
			</tr>
			<tr>
				<th scope="row"><label for="montant_depense_energie_estime">Montant dépense énergie estimé</label></th>
				<td><input type="text" name="montant_depense_energie_estime" id="montant_depense_energie_estime" value="<?php echo esc_attr( $montant_depense_energie_estime ); ?>">
				<p class="description">
					Montant estimé de consommation énergétique annuelle du logement (pour les DPE réalisés avant juillet 2021)
				</p>
				</td>
			</tr>
			<tr>
				<th scope="row"><label for="dateDpe">Date de réalisation du DPE</label></th>
				<td><input type="text" name="dateDpe" id="dateDpe" value="<?php echo esc_attr( $dateDpe ); ?>"></td>
			</tr>
			<tr>
				<th scope="row"><label for="dateDpeReference">Date indice prix énergie</label></th>
				<td><input type="text" name="dateDpeReference" id="dateDpeReference" value="<?php echo esc_attr( $dateDpeReference ); ?>">
					<p class="description">
						Date de référence des prix utilisés pour déterminer le montant des dépenses énergétiques annuelles du bien dans un usage standard d’habitation
					</p>
				</td>
			</tr>
			<tr>
				<th scope="row"><label for="copro">Co-Propriété</label></th>
				<td><input type="checkbox" name="copro" id="copro" value="1" <?php checked( $copro, 1 ); ?>></td>
			</tr>
			<tr>
				<th scope="row"><label for="nbLots">Nombre de lots</label></th>
				<td><input type="text" name="nbLots" id="nbLots" value="<?php echo esc_attr( $nbLots ); ?>"></td>
			</tr>
			<tr>
				<th scope="row"><label for="procedures">Procédures</label></th>
				<td><input type="text" name="procedures" id="procedures" value="<?php echo esc_attr( $procedures ); ?>"></td>
			</tr>
			<tr>
				<th scope="row"><label for="nomProcedures">Nom procédures</label></th>
				<td><input type="text" name="nomProcedures" id="nomProcedures" value="<?php echo esc_attr( $nomProcedures ); ?>"></td>
			</tr>
			<tr>
				<th scope="row"><label for="chargesAnnuelles">Charges annuelles (€)</label></th>
				<td><input type="text" name="chargesAnnuelles" id="chargesAnnuelles" value="<?php echo esc_attr( $chargesAnnuelles ); ?>"></td>
			</tr>
		</table>
		<?php
	}

	public function save_metabox_values( $post_id ) {
		if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
			return;
		}
		if ( ! current_user_can( 'edit_post', $post_id ) ) {
			return;
		}

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

		if ( isset( $_POST['image_ids'] ) && is_array( $_POST['image_ids'] ) ) {
			update_post_meta( $post_id, 'image_ids', array_map( 'absint', wp_unslash( $_POST['image_ids'] ) ) );
		} else {
			delete_post_meta( $post_id, 'image_ids' );
		}

		// general
		if ( isset( $_POST['affId'] ) ) {
			update_post_meta( $post_id, 'affId', sanitize_text_field( wp_unslash( $_POST['affId'] ) ) );
		}

		if ( isset( $_POST['affNum'] ) ) {
			update_post_meta( $post_id, 'affNum', sanitize_text_field( wp_unslash( $_POST['affNum'] ) ) );
		}

		if ( isset( $_POST['video'] ) ) {
			update_post_meta( $post_id, 'video', esc_url_raw( wp_unslash( $_POST['video'] ) ) );
		}

		if ( isset( $_POST['visiteVirtuelle'] ) ) {
			update_post_meta( $post_id, 'visiteVirtuelle', esc_url_raw( wp_unslash( $_POST['visiteVirtuelle'] ) ) );
		}

		if ( isset( $_POST['mandatNumero'] ) ) {
			update_post_meta( $post_id, 'mandatNumero', sanitize_text_field( wp_unslash( $_POST['mandatNumero'] ) ) );
		}

		if ( isset( $_POST['mandatType'] ) ) {
			update_post_meta( $post_id, 'mandatType', sanitize_text_field( wp_unslash( $_POST['mandatType'] ) ) );
		}

		if ( isset( $_POST['select_field'] ) ) {
			update_post_meta( $post_id, 'select_field', sanitize_text_field( wp_unslash( $_POST['select_field'] ) ) );
		}

		if ( isset( $_POST['exclusivite'] ) ) {
			update_post_meta( $post_id, 'exclusivite', sanitize_text_field( wp_unslash( $_POST['exclusivite'] ) ) );
		}

		if ( isset( $_POST['vendu'] ) ) {
			update_post_meta( $post_id, 'vendu', sanitize_text_field( wp_unslash( $_POST['vendu'] ) ) );
		}

		if ( isset( $_POST['anneeConstruction'] ) ) {
			update_post_meta( $post_id, 'anneeConstruction', sanitize_text_field( wp_unslash( $_POST['anneeConstruction'] ) ) );
		}

		if ( isset( $_POST['etat'] ) ) {
			update_post_meta( $post_id, 'etat', sanitize_text_field( wp_unslash( $_POST['etat'] ) ) );
		}

		if ( isset( $_POST['exposition'] ) ) {
			update_post_meta( $post_id, 'exposition', sanitize_text_field( wp_unslash( $_POST['exposition'] ) ) );
		}

		if ( isset( $_POST['vue'] ) ) {
			update_post_meta( $post_id, 'vue', sanitize_text_field( wp_unslash( $_POST['vue'] ) ) );
		}

		if ( isset( $_POST['chauffage'] ) ) {
			update_post_meta( $post_id, 'chauffage', sanitize_text_field( wp_unslash( $_POST['chauffage'] ) ) );
		}

		if ( isset( $_POST['cuisine'] ) ) {
			update_post_meta( $post_id, 'cuisine', sanitize_text_field( wp_unslash( $_POST['cuisine'] ) ) );
		}

		// finance
		if ( isset( $_POST['prix'] ) ) {
			update_post_meta( $post_id, 'prix', sanitize_text_field( wp_unslash( $_POST['prix'] ) ) );
		}

		if ( isset( $_POST['prixHorsHono'] ) ) {
			update_post_meta( $post_id, 'prixHorsHono', sanitize_text_field( wp_unslash( $_POST['prixHorsHono'] ) ) );
		}

		if ( isset( $_POST['pourcentageHono'] ) ) {
			update_post_meta( $post_id, 'pourcentageHono', sanitize_text_field( wp_unslash( $_POST['pourcentageHono'] ) ) );
		}

		if ( isset( $_POST['typeLoyer'] ) ) {
			update_post_meta( $post_id, 'typeLoyer', sanitize_text_field( wp_unslash( $_POST['typeLoyer'] ) ) );
		}

		if ( isset( $_POST['chargesHonoraires'] ) ) {
			update_post_meta( $post_id, 'chargesHonoraires', sanitize_text_field( wp_unslash( $_POST['chargesHonoraires'] ) ) );
		}

		if ( isset( $_POST['provisionSurCharges'] ) ) {
			update_post_meta( $post_id, 'provisionSurCharges', sanitize_text_field( wp_unslash( $_POST['provisionSurCharges'] ) ) );
		}

		if ( isset( $_POST['honoEDL'] ) ) {
			update_post_meta( $post_id, 'honoEDL', sanitize_text_field( wp_unslash( $_POST['honoEDL'] ) ) );
		}

		if ( isset( $_POST['taxeFonciere'] ) ) {
			update_post_meta( $post_id, 'taxeFonciere', sanitize_text_field( wp_unslash( $_POST['taxeFonciere'] ) ) );
		}

		if ( isset( $_POST['fraisAgence'] ) ) {
			update_post_meta( $post_id, 'fraisAgence', sanitize_text_field( wp_unslash( $_POST['fraisAgence'] ) ) );
		}

		if ( isset( $_POST['depotGarantie'] ) ) {
			update_post_meta( $post_id, 'depotGarantie', sanitize_text_field( wp_unslash( $_POST['depotGarantie'] ) ) );
		}

		// localisation
		if ( isset( $_POST['adresse'] ) ) {
			update_post_meta( $post_id, 'adresse', sanitize_text_field( wp_unslash( $_POST['adresse'] ) ) );
		}

		if ( isset( $_POST['cp'] ) ) {
			update_post_meta( $post_id, 'cp', sanitize_text_field( wp_unslash( $_POST['cp'] ) ) );
		}

		if ( isset( $_POST['ville'] ) ) {
			update_post_meta( $post_id, 'ville', sanitize_text_field( wp_unslash( $_POST['ville'] ) ) );
		}

		if ( isset( $_POST['pays'] ) ) {
			update_post_meta( $post_id, 'pays', sanitize_text_field( wp_unslash( $_POST['pays'] ) ) );
		}

		if ( isset( $_POST['departement'] ) ) {
			update_post_meta( $post_id, 'departement', sanitize_text_field( wp_unslash( $_POST['departement'] ) ) );
		}

		if ( isset( $_POST['secteur'] ) ) {
			update_post_meta( $post_id, 'secteur', sanitize_text_field( wp_unslash( $_POST['secteur'] ) ) );
		}

		// surface
		if ( isset( $_POST['nbrePieces'] ) ) {
			update_post_meta( $post_id, 'nbrePieces', sanitize_text_field( wp_unslash( $_POST['nbrePieces'] ) ) );
		}

		if ( isset( $_POST['surface'] ) ) {
			update_post_meta( $post_id, 'surface', sanitize_text_field( wp_unslash( $_POST['surface'] ) ) );
		}

		if ( isset( $_POST['surfaceTerrain'] ) ) {
			update_post_meta( $post_id, 'surfaceTerrain', sanitize_text_field( wp_unslash( $_POST['surfaceTerrain'] ) ) );
		}

		if ( isset( $_POST['nbreChambres'] ) ) {
			update_post_meta( $post_id, 'nbreChambres', sanitize_text_field( wp_unslash( $_POST['nbreChambres'] ) ) );
		}

		if ( isset( $_POST['nbreSDB'] ) ) {
			update_post_meta( $post_id, 'nbreSDB', sanitize_text_field( wp_unslash( $_POST['nbreSDB'] ) ) );
		}

		if ( isset( $_POST['nbreSDE'] ) ) {
			update_post_meta( $post_id, 'nbreSDE', sanitize_text_field( wp_unslash( $_POST['nbreSDE'] ) ) );
		}

		if ( isset( $_POST['nbreWC'] ) ) {
			update_post_meta( $post_id, 'nbreWC', sanitize_text_field( wp_unslash( $_POST['nbreWC'] ) ) );
		}

		if ( isset( $_POST['nbreEtage'] ) ) {
			update_post_meta( $post_id, 'nbreEtage', sanitize_text_field( wp_unslash( $_POST['nbreEtage'] ) ) );
		}

		if ( isset( $_POST['numEtage'] ) ) {
			update_post_meta( $post_id, 'numEtage', sanitize_text_field( wp_unslash( $_POST['numEtage'] ) ) );
		}

		// diagnostics
		if ( isset( $_POST['dpe'] ) ) {
			update_post_meta( $post_id, 'dpe', sanitize_text_field( wp_unslash( $_POST['dpe'] ) ) );
		}

		if ( isset( $_POST['dpeNumber'] ) ) {
			update_post_meta( $post_id, 'dpeNumber', sanitize_text_field( wp_unslash( $_POST['dpeNumber'] ) ) );
		}

		if ( isset( $_POST['ges'] ) ) {
			update_post_meta( $post_id, 'ges', sanitize_text_field( wp_unslash( $_POST['ges'] ) ) );
		}

		if ( isset( $_POST['gesNumber'] ) ) {
			update_post_meta( $post_id, 'gesNumber', sanitize_text_field( wp_unslash( $_POST['gesNumber'] ) ) );
		}

		if ( isset( $_POST['montantEnergieMin'] ) ) {
			update_post_meta( $post_id, 'montantEnergieMin', sanitize_text_field( wp_unslash( $_POST['montantEnergieMin'] ) ) );
		}

		if ( isset( $_POST['montantEnergieMax'] ) ) {
			update_post_meta( $post_id, 'montantEnergieMax', sanitize_text_field( wp_unslash( $_POST['montantEnergieMax'] ) ) );
		}

		if ( isset( $_POST['montant_depense_energie_estime'] ) ) {
			update_post_meta( $post_id, 'montant_depense_energie_estime', sanitize_text_field( wp_unslash( $_POST['montant_depense_energie_estime'] ) ) );
		}

		if ( isset( $_POST['dateDpe'] ) ) {
			update_post_meta( $post_id, 'dateDpe', sanitize_text_field( wp_unslash( $_POST['dateDpe'] ) ) );
		}

		if ( isset( $_POST['dateDpeReference'] ) ) {
			update_post_meta( $post_id, 'dateDpeReference', sanitize_text_field( wp_unslash( $_POST['dateDpeReference'] ) ) );
		}

		if ( isset( $_POST['chargesAnnuelles'] ) ) {
			update_post_meta( $post_id, 'chargesAnnuelles', sanitize_text_field( wp_unslash( $_POST['chargesAnnuelles'] ) ) );
		}

		if ( isset( $_POST['nomProcedures'] ) ) {
			update_post_meta( $post_id, 'nomProcedures', sanitize_text_field( wp_unslash( $_POST['nomProcedures'] ) ) );
		}

		if ( isset( $_POST['procedures'] ) ) {
			update_post_meta( $post_id, 'procedures', sanitize_text_field( wp_unslash( $_POST['procedures'] ) ) );
		}

		if ( isset( $_POST['nbLots'] ) ) {
			update_post_meta( $post_id, 'nbLots', sanitize_text_field( wp_unslash( $_POST['nbLots'] ) ) );
		}

		if ( isset( $_POST['copro'] ) ) {
			update_post_meta( $post_id, 'copro', sanitize_text_field( wp_unslash( $_POST['copro'] ) ) );
		}
	}
}
