<?php
/**
 * Modales B2B agence — préférences email (3 toggles), suppression compte (sans rétention).
 *
 * @package Avision
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

$avi_agency_modal_radar    = 'agency_tier1';
$avi_agency_modal_guardian = 0;
$avi_agency_modal_bundle   = '';

$agency_uid_modal = 0;
if ( isset( $agency_admin_user_id ) && (int) $agency_admin_user_id > 0 ) {
	$agency_uid_modal = (int) $agency_admin_user_id;
} elseif ( isset( $agency_user_id ) && (int) $agency_user_id > 0 ) {
	$agency_uid_modal = (int) $agency_user_id;
}
if ( $agency_uid_modal > 0 && isset( $GLOBALS['wpdb'] ) && is_object( $GLOBALS['wpdb'] ) ) {
	global $wpdb;
	$tp = $wpdb->prefix . 'avi_saas_profiles';
	if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $tp ) ) === $tp ) {
		// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- nom de table préfixé.
		$row = $wpdb->get_row(
			$wpdb->prepare(
				"SELECT radar_plan, guardian_active, avi_bundle FROM `{$tp}` WHERE user_id = %d LIMIT 1",
				$agency_uid_modal
			),
			ARRAY_A
		);
		if ( is_array( $row ) ) {
			if ( isset( $row['radar_plan'] ) && (string) $row['radar_plan'] !== '' ) {
				$avi_agency_modal_radar = (string) $row['radar_plan'];
			}
			$avi_agency_modal_guardian = isset( $row['guardian_active'] ) ? (int) $row['guardian_active'] : 0;
			$avi_agency_modal_bundle   = isset( $row['avi_bundle'] ) ? (string) $row['avi_bundle'] : '';
		}
	}
}
?>

<!-- ═══ MODAL EMAIL PREFERENCES B2B ══════════════════════════════ -->
<div id="aviEmailPrefsModal"
	data-radar="<?php echo esc_attr( $avi_agency_modal_radar ); ?>"
	data-guardian="<?php echo esc_attr( (string) (int) $avi_agency_modal_guardian ); ?>"
	data-bundle="<?php echo esc_attr( $avi_agency_modal_bundle ); ?>"
	style="display:none;position:fixed;inset:0;background:rgba(15,23,42,.6);
	z-index:99999999;align-items:center;justify-content:center;padding:16px;"
	role="dialog" aria-modal="true" aria-labelledby="aviEmailPrefsTitleAgency">
  <div style="background:#fff;border-radius:16px;padding:32px;max-width:500px;
			  width:100%;box-shadow:0 20px 60px rgba(0,0,0,.25);max-height:90vh;
			  overflow-y:auto;">
	<div style="display:flex;justify-content:space-between;align-items:center;
				margin-bottom:24px;">
	  <h3 id="aviEmailPrefsTitleAgency"
		  style="font-size:18px;font-weight:700;color:#0f172a;margin:0;">
		📧 Email preferences
	  </h3>
	  <button type="button" id="aviEmailPrefsCloseAgency"
			  style="background:none;border:none;font-size:20px;
					 color:#94a3b8;cursor:pointer;padding:4px;">✕</button>
	</div>
	<p style="font-size:14px;color:#64748b;margin:0 0 24px;">
	  Choose which emails you want to receive from Avision.
	</p>

	<div id="aviEmailPrefsListAgency" style="display:flex;flex-direction:column;gap:0;">
	  <label style="display:flex;justify-content:space-between;align-items:center;padding:12px 0;border-bottom:1px solid #f1f5f9;cursor:pointer;">
		<span style="font-size:14px;color:#374151;">Fleet alerts</span>
		<input type="checkbox" id="aviEmailPrefFleet" name="avi_email_pref_fleet" data-pref="fleet_alerts" value="1">
	  </label>
	  <label style="display:flex;justify-content:space-between;align-items:center;padding:12px 0;border-bottom:1px solid #f1f5f9;cursor:pointer;">
		<span style="font-size:14px;color:#374151;">Re-engagement</span>
		<input type="checkbox" id="aviEmailPrefReengage" name="avi_email_pref_reengage" data-pref="reactivation" value="1">
	  </label>
	  <label style="display:flex;justify-content:space-between;align-items:center;padding:12px 0;border-bottom:1px solid #f1f5f9;cursor:pointer;">
		<span style="font-size:14px;color:#374151;">Reports</span>
		<input type="checkbox" id="aviEmailPrefReports" name="avi_email_pref_reports" data-pref="reports" value="1">
	  </label>
	</div>

	<div style="display:flex;gap:10px;justify-content:flex-end;margin-top:24px;
				padding-top:20px;border-top:1px solid #f1f5f9;">
	  <button type="button" id="aviEmailPrefsCancelAgency"
			  style="background:#f1f5f9;border:none;color:#374151;font-size:14px;
					 font-weight:600;padding:10px 20px;border-radius:8px;cursor:pointer;">
		Cancel
	  </button>
	  <button type="button" id="aviEmailPrefsSaveAgency"
			  style="background:#0f172a;border:none;color:#fff;font-size:14px;
					 font-weight:600;padding:10px 24px;border-radius:8px;cursor:pointer;">
		Save preferences
	  </button>
	</div>

	<div style="margin-top:24px;padding-top:20px;border-top:1px solid #f1f5f9;">
	  <button type="button" id="aviDeleteAccountBtnAgency"
			  style="background:none;border:none;color:#ef4444;font-size:13px;
					 cursor:pointer;text-decoration:underline;padding:0;">
		Delete my account
	  </button>
	</div>
  </div>
</div>

<!-- ═══ MODAL SUPPRESSION COMPTE B2B ═══════════════════════════════ -->
<div id="aviDeleteModalAgency"
	 style="display:none;position:fixed;inset:0;background:rgba(15,23,42,.6);
			z-index:99999999;align-items:center;justify-content:center;padding:16px;"
	 role="dialog" aria-modal="true">
  <div style="background:#fff;border-radius:16px;padding:32px;max-width:480px;
			  width:100%;box-shadow:0 20px 60px rgba(0,0,0,.25);">
	<h3 style="font-size:18px;font-weight:700;color:#0f172a;margin:0 0 8px;">
	  We're sorry to see you go
	</h3>
	<p style="font-size:14px;color:#64748b;margin:0 0 24px;">
	  Help us improve — 3 quick questions before we proceed.
	</p>

	<p style="font-size:14px;font-weight:600;color:#374151;margin:0 0 10px;">
	  1. Why are you leaving?
	</p>
	<div style="display:flex;flex-direction:column;gap:8px;margin-bottom:20px;">
	  <?php foreach ( array(
		'too_expensive'    => 'Too expensive',
		'clients_no_value' => "Clients don't see the value",
		'missing_features' => 'Missing features',
		'switching_tool'   => 'Switching to another tool',
		'closing_agency'   => 'Closing the agency',
		'other'            => 'Other',
	  ) as $val => $label ) : ?>
	  <label style="font-size:14px;color:#374151;display:flex;
					align-items:center;gap:8px;cursor:pointer;">
		<input type="radio" name="avi_del_reason_agency" value="<?php echo esc_attr( $val ); ?>">
		<?php echo esc_html( $label ); ?>
	  </label>
	  <?php endforeach; ?>
	</div>

	<p style="font-size:14px;font-weight:600;color:#374151;margin:0 0 10px;">
	  2. What feature did you miss most?
	</p>
	<div style="display:flex;flex-direction:column;gap:8px;margin-bottom:20px;">
	  <?php foreach ( array(
		'bulk_management'  => 'Bulk management',
		'client_reporting' => 'Client reporting',
		'white_label'      => 'White label',
		'api_access'       => 'API access',
		'more_automation'  => 'More automation',
	  ) as $val => $label ) : ?>
	  <label style="font-size:14px;color:#374151;display:flex;
					align-items:center;gap:8px;cursor:pointer;">
		<input type="checkbox" name="avi_del_feature[]" value="<?php echo esc_attr( $val ); ?>">
		<?php echo esc_html( $label ); ?>
	  </label>
	  <?php endforeach; ?>
	</div>

	<p style="font-size:14px;font-weight:600;color:#374151;margin:0 0 10px;">
	  3. Would you recommend Avision to a colleague?
	</p>
	<div style="display:flex;flex-direction:column;gap:8px;margin-bottom:24px;">
	  <?php foreach ( array(
		'yes'   => 'Yes',
		'maybe' => 'Maybe',
		'no'    => 'No',
	  ) as $val => $label ) : ?>
	  <label style="font-size:14px;color:#374151;display:flex;
					align-items:center;gap:8px;cursor:pointer;">
		<input type="radio" name="avi_del_recommend" value="<?php echo esc_attr( $val ); ?>">
		<?php echo esc_html( $label ); ?>
	  </label>
	  <?php endforeach; ?>
	</div>

	<div style="display:flex;gap:10px;justify-content:flex-end;">
	  <button type="button" id="aviDeleteCancelAgency"
			  style="background:#f1f5f9;border:none;color:#374151;font-size:14px;
					 font-weight:600;padding:10px 20px;border-radius:8px;cursor:pointer;">
		Keep my account
	  </button>
	  <button type="button" id="aviDeleteConfirmAgency"
			  style="background:#ef4444;border:none;color:#fff;font-size:14px;
					 font-weight:600;padding:10px 20px;border-radius:8px;
					 cursor:pointer;opacity:.4;" disabled>
		Delete my account
	  </button>
	</div>
  </div>
</div>
