<?php
	if ( ! defined( 'ABSPATH' ) ) {
		exit;
	}

	if ( ! current_user_can( 'manage_options' ) ) {
		return;
	}
?>
<div class="modern-settings-wrap">
  <h1 class="modern-settings-title"><?php esc_html_e( 'Sales Contract Settings', 'wc-korkmaz-contract' ); ?></h1>
  <form action="options.php" method="post">
	  <?php
		  // Creates nonce and hidden fields for the settings group.
		  settings_fields( 'korkmaz_contract_options_group' );
	  ?>

	<!-- Company Information -->
	<div class="modern-settings-section">
	  <div class="modern-settings-section-header">
		<span class="dashicons dashicons-building"></span>
		<h2><?php esc_html_e( 'Company Information', 'wc-korkmaz-contract' ); ?></h2>
	  </div>
	  <div class="modern-settings-section-body">
		<div class="modern-settings-field">
		  <label for="firmaadi"><?php esc_html_e( 'Company Name', 'wc-korkmaz-contract' ); ?></label>
		  <input type="text" id="firmaadi" name="firmaadi" value="<?php echo esc_attr( get_option( 'firmaadi' ) ); ?>">
		  <p class="field-description"><?php esc_html_e( 'Enter your company name.', 'wc-korkmaz-contract' ); ?></p>
		</div>
		<div class="modern-settings-field">
		  <label for="firmaadresi"><?php esc_html_e( 'Company Address', 'wc-korkmaz-contract' ); ?></label>
		  <input type="text" id="firmaadresi" name="firmaadresi" value="<?php echo esc_attr( get_option( 'firmaadresi' ) ); ?>">
		  <p class="field-description"><?php esc_html_e( 'Enter your company address.', 'wc-korkmaz-contract' ); ?></p>
		</div>
		<div class="modern-settings-field">
		  <label for="firmatelno"><?php esc_html_e( 'Company Phone Number', 'wc-korkmaz-contract' ); ?></label>
		  <input type="text" id="firmatelno" name="firmatelno" value="<?php echo esc_attr( get_option( 'firmatelno' ) ); ?>">
		  <p class="field-description"><?php esc_html_e( 'Enter your company phone number.', 'wc-korkmaz-contract' ); ?></p>
		</div>
		<div class="modern-settings-field">
		  <label for="firmaverdaire"><?php esc_html_e( 'Company Tax Office', 'wc-korkmaz-contract' ); ?></label>
		  <input type="text" id="firmaverdaire" name="firmaverdaire" value="<?php echo esc_attr( get_option( 'firmaverdaire' ) ); ?>">
		  <p class="field-description"><?php esc_html_e( 'Enter your company tax office.', 'wc-korkmaz-contract' ); ?></p>
		</div>
		<div class="modern-settings-field">
		  <label for="firmaverno"><?php esc_html_e( 'Company Tax Number', 'wc-korkmaz-contract' ); ?></label>
		  <input type="text" id="firmaverno" name="firmaverno" value="<?php echo esc_attr( get_option( 'firmaverno' ) ); ?>">
		  <p class="field-description"><?php esc_html_e( 'Enter your company tax number.', 'wc-korkmaz-contract' ); ?></p>
		</div>
	  </div>
	</div>

	<!-- Link Labels -->
	<div class="modern-settings-section">
	  <div class="modern-settings-section-header">
		<span class="dashicons dashicons-admin-links"></span>
		<h2><?php esc_html_e( 'Link Labels', 'wc-korkmaz-contract' ); ?></h2>
	  </div>
	  <div class="modern-settings-section-body">
		<div class="modern-settings-field">
		  <label for="birinci_sozlesme_link_ismi"><?php esc_html_e( 'First Contract Link Label', 'wc-korkmaz-contract' ); ?></label>
		  <input type="text" id="birinci_sozlesme_link_ismi" name="birinci_sozlesme_link_ismi" value="<?php echo esc_attr( get_option( 'birinci_sozlesme_link_ismi' ) ); ?>">
		  <p class="field-description"><?php esc_html_e( 'Enter the label displayed for the first contract link.', 'wc-korkmaz-contract' ); ?></p>
		</div>
		<div class="modern-settings-field">
		  <label for="ikinci_sozlesme_link_ismi"><?php esc_html_e( 'Second Contract Link Label', 'wc-korkmaz-contract' ); ?></label>
		  <input type="text" id="ikinci_sozlesme_link_ismi" name="ikinci_sozlesme_link_ismi" value="<?php echo esc_attr( get_option( 'ikinci_sozlesme_link_ismi' ) ); ?>">
		  <p class="field-description"><?php esc_html_e( 'Enter the label displayed for the second contract link.', 'wc-korkmaz-contract' ); ?></p>
		</div>
	  </div>
	</div>

	<!-- Visibility -->
	<div class="modern-settings-section">
	  <div class="modern-settings-section-header">
		<span class="dashicons dashicons-visibility"></span>
		<h2><?php esc_html_e( 'Visibility', 'wc-korkmaz-contract' ); ?></h2>
	  </div>
	  <div class="modern-settings-section-body">
		<div class="modern-settings-field">
		  <label for="sozlesme_ozellik_3">
			<input type="checkbox" id="sozlesme_ozellik_3" name="sozlesme_ozellik_3" value="1" <?php checked( 1, get_option( 'sozlesme_ozellik_3' ), true ); ?>>
			  <?php esc_html_e( 'Show contracts on the Checkout page', 'wc-korkmaz-contract' ); ?>
		  </label>
		  <p class="field-description"><?php esc_html_e( 'Enable to display the contracts on the checkout page.', 'wc-korkmaz-contract' ); ?></p>
		</div>

		<div class="modern-settings-field">
		  <label for="sozlesme_ozellik_1">
			<input type="checkbox" id="sozlesme_ozellik_1" name="sozlesme_ozellik_1" value="1" <?php checked( 1, get_option( 'sozlesme_ozellik_1' ), true ); ?>>
			  <?php esc_html_e( 'Show contracts in the Orders list', 'wc-korkmaz-contract' ); ?>
		  </label>
		  <p class="field-description"><?php esc_html_e( 'Enable to display contract links in the Orders admin list.', 'wc-korkmaz-contract' ); ?></p>
		</div>

		<div class="modern-settings-field">
		  <label for="sozlesme_ozellik_2">
			<input type="checkbox" id="sozlesme_ozellik_2" name="sozlesme_ozellik_2" value="1" <?php checked( 1, get_option( 'sozlesme_ozellik_2' ), true ); ?>>
			  <?php esc_html_e( 'Show contracts on the Thank You page', 'wc-korkmaz-contract' ); ?>
		  </label>
		  <p class="field-description"><?php esc_html_e( 'Enable to display the contracts on the thank you page.', 'wc-korkmaz-contract' ); ?></p>
		</div>

		<div class="modern-settings-field">
		  <label for="sozlesme_ozellik_5">
			<input type="checkbox" id="sozlesme_ozellik_5" name="sozlesme_ozellik_5" value="1" <?php checked( 1, get_option( 'sozlesme_ozellik_5' ), true ); ?>>
			  <?php esc_html_e( 'Show contracts on the Order Details page', 'wc-korkmaz-contract' ); ?>
		  </label>
		  <p class="field-description"><?php esc_html_e( 'Enable to display the contracts on the order details page.', 'wc-korkmaz-contract' ); ?></p>
		</div>

		<div class="modern-settings-field">
		  <label for="sozlesme_ozellik_6">
			<input type="checkbox" id="sozlesme_ozellik_6" name="sozlesme_ozellik_6" value="1" <?php checked( 1, get_option( 'sozlesme_ozellik_6' ), true ); ?>>
			  <?php esc_html_e( 'Enable National ID/Tax Number system', 'wc-korkmaz-contract' ); ?>
		  </label>
		  <p class="field-description"><?php esc_html_e( 'Enable to activate the National ID/Tax Number fields logic.', 'wc-korkmaz-contract' ); ?></p>
		</div>

		<div class="modern-settings-field">
		  <label for="sozlesme_ozellik_4">
			<input type="checkbox" id="sozlesme_ozellik_4" name="sozlesme_ozellik_4" value="1" <?php checked( 1, get_option( 'sozlesme_ozellik_4' ), true ); ?>>
			  <?php esc_html_e( 'Send contracts via email', 'wc-korkmaz-contract' ); ?>
		  </label>
		  <p class="field-description"><?php esc_html_e( 'Enable to send the contract PDFs via email based on order status.', 'wc-korkmaz-contract' ); ?></p>
		</div>

		<div class="modern-settings-field">
		  <label for="sozlesme_mail_durumu"><?php esc_html_e( 'Send email at which order status?', 'wc-korkmaz-contract' ); ?></label>
		  <select id="sozlesme_mail_durumu" name="sozlesme_mail_durumu">
			  <?php
				  $options = array(
					  'pending'    => __( 'Pending payment', 'wc-korkmaz-contract' ),
					  'processing' => __( 'Processing', 'wc-korkmaz-contract' ),
					  'on-hold'    => __( 'On hold', 'wc-korkmaz-contract' ),
					  'completed'  => __( 'Completed', 'wc-korkmaz-contract' ),
				  );
				  $current = get_option( 'sozlesme_mail_durumu' );
				  foreach ( $options as $value => $label ) :
					  ?>
					<option value="<?php echo esc_attr( $value ); ?>" <?php selected( $current, $value ); ?>>
						<?php echo esc_html( $label ); ?>
					</option>
				  <?php endforeach; ?>
		  </select>
		  <p class="field-description"><?php esc_html_e( 'Configure when emails with contract PDFs should be sent based on the order status.', 'wc-korkmaz-contract' ); ?></p>
		</div>

		<div class="modern-settings-field">
		  <label for="korkmaz_contract_log_enabled">
			<input type="checkbox" id="korkmaz_contract_log_enabled" name="korkmaz_contract_log_enabled" value="1" <?php checked( 1, get_option( 'korkmaz_contract_log_enabled', 0 ), true ); ?>>
			  <?php esc_html_e( 'Enable plugin logging', 'wc-korkmaz-contract' ); ?>
		  </label>
		  <p class="field-description"><?php esc_html_e( 'When enabled, the plugin writes process logs. When disabled, no plugin log is written.', 'wc-korkmaz-contract' ); ?></p>
		</div>

		<?php if ( 1 === (int) get_option( 'korkmaz_contract_log_enabled', 0 ) ) : ?>
			<?php
				$log_path    = $this->get_action_log_path();
				$log_excerpt = $this->get_action_log_excerpt();
			?>
		<div class="modern-settings-field">
		  <label for="korkmaz_contract_log_output"><?php esc_html_e( 'Plugin Log Output', 'wc-korkmaz-contract' ); ?></label>
		  <textarea id="korkmaz_contract_log_output" rows="12" readonly style="width:100%;font-family:monospace;"><?php echo esc_textarea( $log_excerpt ); ?></textarea>
		  <p class="field-description">
			  <?php
				printf(
					/* translators: %s: absolute log file path */
					esc_html__( 'Log file: %s', 'wc-korkmaz-contract' ),
					esc_html( $log_path )
				);
				?>
		  </p>
			<?php if ( '' === $log_excerpt ) : ?>
		  <p class="field-description"><?php esc_html_e( 'No logs found yet.', 'wc-korkmaz-contract' ); ?></p>
			<?php endif; ?>
		</div>
		<?php endif; ?>
	  </div>
	</div>

	<?php
		$pdf_security = $this->get_pdf_storage_security_status();
		$status_class = 'security-status--neutral';
		if ( isset( $pdf_security['status'] ) ) {
			if ( 'protected' === $pdf_security['status'] ) {
				$status_class = 'security-status--ok';
			} elseif ( 'exposed' === $pdf_security['status'] ) {
				$status_class = 'security-status--danger';
			} elseif ( 'unknown' === $pdf_security['status'] ) {
				$status_class = 'security-status--warning';
			}
		}
		$http_code = isset( $pdf_security['http_code'] ) ? (int) $pdf_security['http_code'] : 0;
	?>
	<div class="modern-settings-section">
	  <div class="modern-settings-section-header">
		<span class="dashicons dashicons-shield-alt"></span>
		<h2><?php esc_html_e( 'PDF Storage Security', 'wc-korkmaz-contract' ); ?></h2>
	  </div>
	  <div class="modern-settings-section-body">
		<div class="kc-security-status <?php echo esc_attr( $status_class ); ?>">
		  <strong><?php esc_html_e( 'Direct PDF access status:', 'wc-korkmaz-contract' ); ?></strong>
		  <span><?php echo esc_html( isset( $pdf_security['message'] ) ? $pdf_security['message'] : __( 'Security status could not be checked.', 'wc-korkmaz-contract' ) ); ?></span>
			<?php if ( $http_code > 0 ) : ?>
		  <span class="kc-security-code">
				<?php
				printf(
					/* translators: %d: HTTP status code */
					esc_html__( 'HTTP status: %d', 'wc-korkmaz-contract' ),
					$http_code
				);
				?>
		  </span>
			<?php endif; ?>
		</div>

		<div class="kc-security-grid">
		  <div>
			<strong><?php esc_html_e( 'Web server', 'wc-korkmaz-contract' ); ?></strong>
			<span><?php echo esc_html( isset( $pdf_security['server_label'] ) ? $pdf_security['server_label'] : __( 'Unknown web server', 'wc-korkmaz-contract' ) ); ?></span>
		  </div>
		  <div>
			<strong><?php esc_html_e( 'PDF directory', 'wc-korkmaz-contract' ); ?></strong>
			<span><?php echo esc_html( isset( $pdf_security['directory_path'] ) ? $pdf_security['directory_path'] : '' ); ?></span>
		  </div>
		  <div>
			<strong><?php esc_html_e( 'PDF URL base', 'wc-korkmaz-contract' ); ?></strong>
			<span><?php echo esc_url( isset( $pdf_security['directory_url'] ) ? $pdf_security['directory_url'] : '' ); ?></span>
		  </div>
		  <div>
			<strong><?php esc_html_e( 'Apache .htaccess', 'wc-korkmaz-contract' ); ?></strong>
			<span><?php echo esc_html( isset( $pdf_security['htaccess_message'] ) ? $pdf_security['htaccess_message'] : '' ); ?></span>
		  </div>
		</div>

		<p class="field-description">
			<?php esc_html_e( 'The plugin serves customer/admin contract downloads through a protected WordPress endpoint. The rules below block direct public access to the stored PDF files.', 'wc-korkmaz-contract' ); ?>
		</p>
		<p>
		  <a class="button button-secondary" href="<?php echo esc_url( isset( $pdf_security['refresh_url'] ) ? $pdf_security['refresh_url'] : '#' ); ?>">
			  <?php esc_html_e( 'Run security check again', 'wc-korkmaz-contract' ); ?>
		  </a>
		</p>

		<div class="modern-settings-field">
		  <label for="korkmaz_apache_rule"><?php esc_html_e( 'Apache rule', 'wc-korkmaz-contract' ); ?></label>
		  <textarea id="korkmaz_apache_rule" rows="11" readonly class="kc-security-codebox"><?php echo esc_textarea( isset( $pdf_security['apache_rule'] ) ? $pdf_security['apache_rule'] : '' ); ?></textarea>
		  <p class="field-description"><?php esc_html_e( 'Apache usually uses the .htaccess file that the plugin writes automatically. If direct access is still open, ask the hosting provider to allow .htaccess overrides for this uploads folder.', 'wc-korkmaz-contract' ); ?></p>
		</div>

		<div class="modern-settings-field">
		  <label for="korkmaz_nginx_rule"><?php esc_html_e( 'Nginx rule', 'wc-korkmaz-contract' ); ?></label>
		  <textarea id="korkmaz_nginx_rule" rows="4" readonly class="kc-security-codebox"><?php echo esc_textarea( isset( $pdf_security['nginx_rule'] ) ? $pdf_security['nginx_rule'] : '' ); ?></textarea>
		  <p class="field-description"><?php esc_html_e( 'Nginx does not read .htaccess files. Add this location rule to the site server block and reload Nginx.', 'wc-korkmaz-contract' ); ?></p>
		</div>
	  </div>
	</div>

	  <?php submit_button(); ?>
  </form>
</div>
