<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Prevent direct access to the script.
}

use Gigneo\Modules;

$profile_id  = $args['profile_id'] ?? gigneo_active_profile_id();
$social_list = gigneo_social_profiles( 'active' );
$socials     = gigneo_post_meta( $profile_id, '_gigneo_pm_socials' );
$socials     = ! empty( $socials ) ? $socials : array();

if ( ! in_array( 'social_profiles', gigneo_active_modules(), true ) ) {
	return;
}
?>
<div id="gn-profile-socials" class="gn-setting-section">
	<div class="gn-head">
		<h3 class="gn-title"><?php echo esc_html__( 'Socials', 'gigneo' ); ?></h3>
		<button class="gn-link gn-link-primary" data-gn-modal-toggle="gn-edit-profile-socials">
			<i class="hi hi-pencil-edit" aria-hidden="true"></i>
		</button>
	</div>
	<?php if ( ! empty( $socials ) ) { ?>
		<div class="gn-body">
			<div class="gn-profile-social-list gn-tags">
				<?php
				foreach ( $socials as $social ) {
					$social_type = isset( $social['type'] ) && isset( $social_list[ $social['type'] ] ) ? $social_list[ $social['type'] ] : '';
					$social_url  = ! empty( $social['url'] ) ? $social['url'] : '';
					if ( ! empty( $social_type ) && ! empty( $social_url ) ) {
						$social_type_icon = strtolower( $social_type );
						$logo_path = GIGNEO_DIR_PATH . "assets/img/icon/$social_type_icon.png";
						$logo_url  = GIGNEO_DIR_URL . "assets/img/icon/$social_type_icon.png";
						$logo      = file_exists( $logo_path ) ? $logo_url : GIGNEO_DIR_URL . 'assets/img/icon/link.png';
						?>
						<a href="<?php echo esc_url( $social_url ); ?>" target="_blank" class="gn-social-tag gn-social-<?php echo esc_attr( $social['type'] ); ?>">
							<img src="<?php echo esc_url( $logo ); ?>" alt="<?php echo esc_attr( $social_type ); ?>">
							<?php echo esc_html( $social_type ); ?>
						</a>
						<?php
					}
				}
				?>
			</div>
		</div>
	<?php } ?>
	<!--Edit Socials Modal-->
	<div id="gn-edit-profile-socials" class="gn-modal-wrapper gn-profile-setting-modal gn-edit-profile-socials">
		<div class="gn-modal-dialog">
			<div class="gn-modal-content">
				<div class="gn-modal-info">
					<div class="gn-modal-header">
						<h4 class="gn-title"><?php echo esc_html__( 'Social profiles', 'gigneo' ); ?></h4>
						<p class="gn-description"><?php echo esc_html__( 'Manage and update your social profiles.', 'gigneo' ); ?></p>
						<button type="button" data-gn-modal-close="gn-edit-profile-socials" class="gn-modal-close">
							<i class="hi hi-x-mark" aria-hidden="true"></i>
						</button>
					</div>
					<form id="gn-save-profile-setting-form" class="gn-theme-form gn-save-profile-setting">
						<div class="gn-modal-body gn-custom-scrollbar">
							<fieldset class="gn-form-fields">
								<div class="row g-4 align-items-end">
									<div class="col-md-6">
										<div class="gn-form-field gn-select2">
											<label for="gn-social-type"><?php echo esc_html__( 'Type', 'gigneo' ); ?></label>
											<select id="gn-social-type" name="social_type" placeholder="<?php echo esc_html__( 'Select Type', 'gigneo' ); ?>">
												<?php
												if ( ! empty( $social_list ) ) {
													foreach ( $social_list as $social_slug => $social_type ) {
														?>
														<option value="<?php echo esc_attr( $social_slug ); ?>" <?php selected( in_array( $social_slug, array_column( $socials, 'type' ), true ) ); ?>><?php echo esc_html( $social_type ); ?></option>
														<?php
													}
												}
												?>
											</select>
										</div>
									</div>
									<div class="col-md-6">
										<div class="gn-form-field gn-select2">
											<label for="gn-social-url"><?php echo esc_html__( 'URL', 'gigneo' ); ?></label>
											<input id="gn-social-url" type="text" name="social_url" autocomplete="off" value="" placeholder="<?php echo esc_attr__( 'Enter URL', 'gigneo' ); ?>">
										</div>
									</div>
									<div class="col-md-12">
										<button id="gn-add-social-btn" type="button" class="gn-link gn-link-primary">
											<i class="hi hi-plus" aria-hidden="true"></i>
											<?php echo esc_html__( 'Add social profile', 'gigneo' ); ?>
										</button>
									</div>
								</div>
							</fieldset>
							<ul class="gn-repeater-tags">
							<?php
							foreach ( $socials as $key => $social ) {
								$social_type = isset( $social['type'] ) && isset( $social_list[ $social['type'] ] ) ? $social_list[ $social['type'] ] : '';
								$social_url  = ! empty( $social['url'] ) ? $social['url'] : '';
								if ( ! empty( $social_type ) && ! empty( $social_url ) ) {
									?>
										<li class="gn-tag gn-social-<?php echo esc_attr( $social['type'] ); ?>">
											<div class="gn-tag-info">
												<h6 class="gn-name"><?php echo esc_html( $social_type ); ?></h6>
												<input type="hidden" name="socials[<?php echo esc_attr( $key ); ?>][type]" value="<?php echo esc_attr( $social['type'] ); ?>">
												<input type="hidden" name="socials[<?php echo esc_attr( $key ); ?>][url]" value="<?php echo esc_attr( $social['url'] ); ?>">
											</div>
											<span class="gn-remove-tag">
												<i class="hi hi-x-mark" aria-hidden="true"></i>
											</span>
										</li>
									<?php
								}
							}
							?>
							</ul>
						</div>
						<div class="gn-modal-footer">
							<input type="hidden" name="type" value="socials">
							<button type="button" class="gn-btn gn-btn-outline-grey" data-gn-modal-close="gn-edit-profile-socials"><?php echo esc_html__( 'Close', 'gigneo' ); ?></button>
							<button type="submit" class="gn-btn gn-btn-primary"><?php echo esc_html__( 'Save', 'gigneo' ); ?></button>
						</div>
					</form>
				</div>
			</div>
		</div>
	</div>
</div>
