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

global $current_user;

$profile_id   = $args['profile_id'] ?? gigneo_active_profile_id();
$profile_type = $args['profile_type'] ?? gigneo_profile_type();
$date_format  = $args['date_format'] ?? get_option( 'date_format' );
$max_date     = $args['max_date'] ?? gigneo_datetime( gmdate( 'Y-m-d' ), 'Y-m-d' );

$profile_name   = gigneo_profile_name( $profile_id );
$genders        = gigneo_genders( false );
$qualifications = gigneo_qualifications();

// Post Meta
$tagline_raw            = gigneo_post_meta( $profile_id, '_gigneo_pm_tagline' );
$tagline                = ! empty( $tagline_raw ) ? $tagline_raw : esc_html__( 'None', 'gigneo' );
$phone_raw              = gigneo_post_meta( $profile_id, '_gigneo_pm_phone' );
$phone                  = ! empty( $phone_raw ) ? $phone_raw : esc_html__( 'None', 'gigneo' );
$dob_raw                = gigneo_post_meta( $profile_id, '_gigneo_pm_dob' );
$dob                    = ! empty( $dob_raw ) ? date_i18n( $date_format, strtotime( $dob_raw ), true ) : esc_html__( 'None', 'gigneo' );
$first_name             = gigneo_post_meta( $profile_id, '_gigneo_pm_first_name' );
$first_name             = ! empty( $first_name ) ? $first_name : $current_user->first_name;
$last_name              = gigneo_post_meta( $profile_id, '_gigneo_pm_last_name' );
$last_name              = ! empty( $last_name ) ? $last_name : $current_user->last_name;
$gender_raw             = gigneo_post_meta( $profile_id, '_gigneo_pm_gender' );
$gender                 = ! empty( $genders[ $gender_raw ] ) ? $genders[ $gender_raw ] : esc_html__( 'None', 'gigneo' );
$profile_visibility_raw = gigneo_post_meta( $profile_id, '_gigneo_pm_visibility' );
$profile_visibility     = 'public' === $profile_visibility_raw ? esc_html__( 'Public', 'gigneo' ) : esc_html__( 'Private', 'gigneo' );
$qualification_raw      = gigneo_post_meta( $profile_id, '_gigneo_pm_qualification' );
$qualification          = ! empty( $qualifications[ $qualification_raw ] ) ? $qualifications[ $qualification_raw ] : esc_html__( 'None', 'gigneo' );
?>
<div id="gn-profile-personal-info" class="gn-setting-section">
	<div class="gn-head">
		<h3 class="gn-title"><?php echo esc_html__( 'Personal information', 'gigneo' ); ?></h3>
		<button class="gn-link gn-link-primary" data-gn-modal-toggle="gn-edit-profile-personal-info">
			<i class="hi hi-pencil-edit" aria-hidden="true"></i>
			<?php echo esc_html__( 'Edit', 'gigneo' ); ?>
		</button>
	</div>
	<div class="gn-body">
		<div class="row g-4">
			<div class="col-sm-6">
				<div class="gn-info-grid">
					<span class="gn-icon">
						<i class="hi hi-briefcase" aria-hidden="true"></i>
					</span>
					<div class="gn-info">
						<h5 class="gn-title"><?php echo esc_html( $tagline ); ?></h5>
						<p class="gn-label"><?php echo esc_html__( 'Tagline', 'gigneo' ); ?></p>
					</div>
				</div>
			</div>
			<div class="col-sm-6">
				<div class="gn-info-grid">
					<span class="gn-icon">
						<i class="hi hi-users" aria-hidden="true"></i>
					</span>
					<div class="gn-info">
						<h5 class="gn-title"><?php echo esc_html( $gender ); ?></h5>
						<p class="gn-label"><?php echo esc_html__( 'Gender', 'gigneo' ); ?></p>
					</div>
				</div>
			</div>
			<div class="col-sm-6">
				<div class="gn-info-grid">
					<span class="gn-icon">
						<i class="hi hi-calendar" aria-hidden="true"></i>
					</span>
					<div class="gn-info">
						<h5 class="gn-title"><?php echo esc_html( $dob ); ?></h5>
						<p class="gn-label"><?php echo esc_html__( 'Date of birth', 'gigneo' ); ?></p>
					</div>
				</div>
			</div>
			<div class="col-sm-6">
				<div class="gn-info-grid">
					<span class="gn-icon">
						<i class="hi hi-academic-cap" aria-hidden="true"></i>
					</span>
					<div class="gn-info">
						<h5 class="gn-title">
							<?php echo esc_html( $qualification ); ?>
						</h5>
						<p class="gn-label"><?php echo esc_html__( 'Qualification', 'gigneo' ); ?></p>
					</div>
				</div>
			</div>
			<div class="col-sm-6">
				<div class="gn-info-grid">
					<span class="gn-icon">
						<i class="hi hi-device-phone-mobile" aria-hidden="true"></i>
					</span>
					<div class="gn-info">
						<h5 class="gn-title"><?php echo esc_html( $phone ); ?></h5>
						<p class="gn-label"><?php echo esc_html__( 'Phone', 'gigneo' ); ?></p>
					</div>
				</div>
			</div>
			<div class="col-sm-6">
				<div class="gn-info-grid">
					<span class="gn-icon">
						<i class="hi hi-eye" aria-hidden="true"></i>
					</span>
					<div class="gn-info">
						<h5 class="gn-title">
							<?php echo esc_html( $profile_visibility ); ?>
						</h5>
						<p class="gn-label"><?php echo esc_html__( 'Profile visibility', 'gigneo' ); ?></p>
					</div>
				</div>
			</div>
		</div>
	</div>
	<!--Edit Personal Info Modal-->
	<div id="gn-edit-profile-personal-info" class="gn-modal-wrapper gn-profile-setting-modal">
		<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__( 'Personal information', 'gigneo' ); ?></h4>
						<p class="gn-description"><?php echo esc_html__( 'Manage or update your personal details.', 'gigneo' ); ?></p>
						<button type="button" data-gn-modal-close="gn-edit-profile-personal-info" 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">
									<div class="col-md-6">
										<div class="gn-form-field">
											<label for="gn-first-name" class="gn-required-field-label"><?php echo esc_html__( 'First name', 'gigneo' ); ?></label>
											<input id="gn-first-name" type="text" name="first_name" class="gn-required-field" autocomplete="off" value="<?php echo esc_attr( $first_name ); ?>" placeholder="<?php echo esc_attr__( 'Enter first name', 'gigneo' ); ?>">
										</div>
									</div>
									<div class="col-md-6">
										<div class="gn-form-field">
											<label for="gn-last-name" class="gn-required-field-label"><?php echo esc_html__( 'Last name', 'gigneo' ); ?></label>
											<input id="gn-last-name" type="text" name="last_name" class="gn-required-field" autocomplete="off" value="<?php echo esc_attr( $last_name ); ?>" placeholder="<?php echo esc_attr__( 'Enter last name', 'gigneo' ); ?>">
										</div>
									</div>
									<div class="col-md-12">
										<div class="gn-form-field">
											<label for="gn-tagline"><?php echo esc_html__( 'Tagline', 'gigneo' ); ?></label>
											<input id="gn-tagline" type="text" name="tagline" value="<?php echo esc_attr( $tagline_raw ); ?>" placeholder="<?php echo esc_attr__( 'Enter tagline', 'gigneo' ); ?>">
										</div>
									</div>
									<div class="col-md-6">
										<div class="gn-form-field gn-select2">
											<label for="gn-gender"><?php echo esc_html__( 'Gender', 'gigneo' ); ?></label>
											<select id="gn-gender" name="gender" data-minimum-results-for-search="Infinity">
												<?php foreach ( $genders as $gender_slug => $gender_name ) { ?>
													<option<?php echo esc_attr( $gender_slug === $gender_raw ? ' selected' : '' ); ?> value="<?php echo esc_attr( $gender_slug ); ?>"><?php echo esc_html( $gender_name ); ?></option>
												<?php } ?>
											</select>
										</div>
									</div>
									<div class="col-md-6">
										<div class="gn-form-field gn-datepicker">
											<label for="gn-dob"><?php echo esc_html__( 'Date of birth', 'gigneo' ); ?></label>
											<input id="gn-dob" class="flatpickr-input" data-single="true" data-max-date="<?php echo esc_attr( $max_date ); ?>" type="hidden" name="dob" autocomplete="off" value="<?php echo esc_attr( $dob_raw ); ?>" placeholder="<?php echo esc_attr__( 'Enter date of birth', 'gigneo' ); ?>">
										</div>
									</div>
									<div class="col-md-6">
										<div class="gn-form-field gn-select2">
											<label for="gn-qualification"><?php echo esc_html__( 'Qualification', 'gigneo' ); ?></label>
											<select id="gn-qualification" name="qualification" data-minimum-results-for-search="Infinity">
												<?php
												foreach ( $qualifications as $qualification_slug => $qualification ) {
													$selected = selected( $qualification_raw, $qualification_slug, false );
													?>
													<option value="<?php echo esc_attr( $qualification_slug ); ?>"<?php echo esc_attr( $selected ? ' ' . $selected : '' ); ?>><?php echo esc_html( $qualification ); ?></option>
												<?php } ?>
											</select>
										</div>
									</div>
									<div class="col-md-6">
										<div class="gn-form-field gn-select2">
											<label for="gn-visibility"><?php echo esc_html__( 'Profile visibility', 'gigneo' ); ?></label>
											<select id="gn-visibility" name="visibility" data-minimum-results-for-search="Infinity">
												<option value="public" <?php selected( $profile_visibility_raw, 'public' ); ?>><?php echo esc_html__( 'Public', 'gigneo' ); ?></option>
												<option value="private" <?php selected( $profile_visibility_raw, 'private' ); ?>><?php echo esc_html__( 'Private', 'gigneo' ); ?></option>
											</select>
										</div>
									</div>
									<div class="col-md-12">
										<div class="gn-form-field">
											<label for="gn-phone"><?php echo esc_html__( 'Phone', 'gigneo' ); ?></label>
											<input id="gn-phone" type="number" name="phone"  pattern="[0-9+\-\s()]{7,15}"  autocomplete="off" value="<?php echo esc_attr( $phone_raw ); ?>" placeholder="<?php echo esc_attr__( 'Enter phone number', 'gigneo' ); ?>">
										</div>
									</div>
								</div>
							</fieldset>
						</div>
						<div class="gn-modal-footer">
							<input type="hidden" name="type" value="personal-info">
							<button type="button" class="gn-btn gn-btn-outline-grey" data-gn-modal-close="gn-edit-profile-personal-info"><?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>
