<?php

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

//Submit form
if ( isset( $_SERVER['REQUEST_METHOD'] ) && 'POST' === $_SERVER['REQUEST_METHOD'] ) {

	// Verify nonce
	$nonce = isset( $_POST['gigneo_form_nonce_field'] ) ? sanitize_text_field( wp_unslash( $_POST['gigneo_form_nonce_field'] ) ) : '';
	if ( empty( $nonce ) || ! wp_verify_nonce( $nonce, 'gigneo_form_nonce' ) ) {
		wp_die( esc_html__( 'Nonce verification failed.', 'gigneo' ) );
	}

	// Check if user has permission
	if ( ! current_user_can( 'manage_options' ) ) {
		wp_die( esc_html__( 'You do not have permission to perform this action.', 'gigneo' ) );
	}

	$freelancer_toolbar = isset( $_POST['gigneo_freelancer_show_toolbar'] ) ? sanitize_text_field( wp_unslash( $_POST['gigneo_freelancer_show_toolbar'] ) ) : '';
	$employer_toolbar   = isset( $_POST['gigneo_employer_show_toolbar'] ) ? sanitize_text_field( wp_unslash( $_POST['gigneo_employer_show_toolbar'] ) ) : '';
	$allow_tracking     = isset( $_POST['gigneo_allow_tracking'] ) ? sanitize_text_field( wp_unslash( $_POST['gigneo_allow_tracking'] ) ) : '';

	update_option( '_gigneo_freelancer_show_toolbar', 'yes' === $freelancer_toolbar );
	update_option( '_gigneo_employer_show_toolbar', 'yes' === $employer_toolbar );
	update_option( '_gigneo_allow_tracking', 'yes' === $allow_tracking );
}

$freelancer_toolbar = get_option( '_gigneo_freelancer_show_toolbar', false );
$employer_toolbar   = get_option( '_gigneo_employer_show_toolbar', false );
$allow_tracking     = get_option( '_gigneo_allow_tracking', false );

?>
<div id="gn-general-tab" class="gn-setting-tab gn-setting-tab-general">
	<h2 class="gn-title"><?php echo esc_html__( 'Basic options', 'gigneo' ); ?></h2>
	<p class="gn-description"><?php echo esc_html__( 'Configure site basic settings here.', 'gigneo' ); ?></p>
	<form id="gn-setting-general-form" action="" method="post" enctype="multipart/form-data">
		<table class="form-table">
			<tbody>
				<tr>
					<th scope="row"><?php echo esc_html__( 'Toolbar', 'gigneo' ); ?></th>
					<td>
						<fieldset>
							<label>
								<input type="checkbox" id="gigneo_freelancer_show_toolbar"
									name="gigneo_freelancer_show_toolbar" <?php echo $freelancer_toolbar ? ' checked="checked"' : ''; ?> value="yes">
								<?php
								printf(
									// translators: %s: Freelancer singular label
									esc_html__( 'Show the Toolbar for logged-in %s', 'gigneo' ),
									esc_html( gigneo_freelancer_singular() )
								);
								?>
							</label>
						</fieldset>
						<fieldset>
							<label>
								<input type="checkbox" id="gigneo_employer_show_toolbar"
									name="gigneo_employer_show_toolbar" <?php echo $employer_toolbar ? ' checked="checked"' : ''; ?> value="yes">
								<?php
								printf(
									// translators: %s: Employer singular label
									esc_html__( 'Show the Toolbar for logged-in %s', 'gigneo' ),
									esc_html( gigneo_employer_singular() )
								);
								?>
							</label>
						</fieldset>
					</td>
				</tr>
			</tbody>
		</table>
		<?php wp_nonce_field( 'gigneo_form_nonce', 'gigneo_form_nonce_field' ); ?>
		<!--Submit-->
		<button type="submit" class="gn-btn gn-btn-primary"><?php echo esc_html__( 'Save changes', 'gigneo' ); ?></button>
	</form>
</div>