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

require_once dirname( __FILE__ ) . '/class-pages-breadcrumbs.php';
/**
 * *This loads one calculator, settings, translation and preview
 * todo: must inherit PagesBreadcrumbs class
 * @param id_form
 */
class PageEditTabs extends PagesBreadcrumbs {

	public function __construct() {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die( esc_html__( 'You do not have permission to edit calculators.', 'scc' ) );
		}

		require dirname( __DIR__, 1 ) . '/formController.php';
		$formC = new formController();
		$form_id = 0;
		if ( isset( $_GET['id_form'] ) && ! is_array( $_GET['id_form'] ) ) {
			$form_id = absint( wp_unslash( $_GET['id_form'] ) );
		}

		wp_enqueue_style( 'wp-color-picker' );
		wp_enqueue_script( 'wp-color-picker' );

		wp_register_script( 'scc-sortable', SCC_URL . 'lib/sortable/sortable.min.js', array( 'jquery' ), STYLISH_COST_CALCULATOR_VERSION, true );
		wp_enqueue_script( 'scc-sortable' );
		wp_enqueue_script( 'jquery-effects-core' );
		wp_enqueue_script( 'jquery-ui-autocomplete' );
        wp_enqueue_script( 'jquery-ui-tooltip' );
        wp_enqueue_style( 'scc-admin-style' );

		//added to load shortcode
		wp_enqueue_style( 'scc-checkbox1' );
		wp_enqueue_script( 'scc-tom-select' );
		wp_enqueue_style( 'scc-bootstrapslider-css' );
		wp_enqueue_script( 'scc-bootstrapslider-js' );
		wp_enqueue_script( 'scc-nouislider' );
		wp_enqueue_script( 'scc-frontend' );
		wp_enqueue_script( 'wp-util' );
		wp_enqueue_script( 'scc-translate-js' );
		wp_enqueue_script( 'scc-settings-modal-react' );

		$lang = isset( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ? substr( $_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2 ) : 'en';
		
		// Define the path to the l10n files
		if ( $lang === 'en') {
			$lang = 'default';
		}

		$l10n_path = SCC_URL . 'lib/flatpickr/js/l10n/';
		$flatpickr_path = SCC_URL . 'lib/flatpickr/js/';
	 
		wp_register_style( 'scc-flatpickr', SCC_URL . 'lib/flatpickr/css/flatpickr.min.css', [], scc_get_file_version( SCC_DIR . '/lib/flatpickr/css/flatpickr.min.css' ) );
		wp_enqueue_style( 'scc-flatpickr' );
        wp_enqueue_script( 'scc-flatpickr', $flatpickr_path . 'flatpickr.min.js', [], scc_get_file_version( SCC_DIR . '/lib/flatpickr/js/flatpickr.min.js' ), true );

		// Check if the l10n file exists for the language
		if ( file_exists( SCC_DIR . '/lib/flatpickr/js/l10n/' . $lang . '.js' ) ) {
			wp_enqueue_script( 'scc-flatpickr-' . $lang, $l10n_path . $lang . '.js', [], scc_get_file_version( SCC_DIR . '/lib/flatpickr/js/l10n/' . $lang . '.js' ), true );
		}

		wp_register_style( 'scc-toast', SCC_URL . 'assets/css/modals/_toast.css', [], scc_get_file_version( SCC_DIR . '/assets/css/modals/_toast.css' ) );
		wp_enqueue_style( 'scc-toast' );
		wp_register_style( 'scc-banner', SCC_URL . 'assets/css/modals/_banner.css', [], scc_get_file_version( SCC_DIR . '/assets/css/modals/_banner.css' ) );
		wp_enqueue_style( 'scc-banner' );
		wp_register_style( 'scc-modal', SCC_URL . 'assets/css/modals/_modal.css', [], scc_get_file_version( SCC_DIR . '/assets/css/modals/_toast.css' ) );
		wp_enqueue_style( 'scc-modal' );
		
		$scc_json_encode_flags = JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT;
		$scc_currencies        = require SCC_DIR . '/lib/currency_data.php';
		$currencies_array      = 'window["scc_currencies"] = ' . wp_json_encode( $scc_currencies, $scc_json_encode_flags );
		wp_add_inline_script( 'scc-frontend', $currencies_array );

		add_thickbox();
		
		$f1          = $form_id > 0 ? $formC->readWithRelations( $form_id ) : null;
		$isActivated = get_option( 'df_scc_licensed', 0 ) ? true : false;

		parent::__construct();
		wp_enqueue_media();

		if ( ! $f1 ) {
			return $this->show_eror( $form_id );
		}

		$this->updateCalculatorUsageStat();

		require dirname( __DIR__, 2 ) . '/views/adminHeader.php';
		require dirname( __DIR__, 2 ) . '/views/searchAndTabs.php';
		require dirname( __DIR__, 2 ) . '/models/editElementModel.php';
		require dirname( __DIR__, 2 ) . '/views/editCalcualtor.php';
		require dirname( __DIR__, 2 ) . '/views/extraSettings.php';
		
		// Load wizard quiz script AFTER the HTML with choices-data is generated
		wp_enqueue_script( 'scc-wizard-quiz' );
		
		require dirname( __DIR__, 2 ) . '/views/adminFooter.php';
	}

	function show_eror( $form_id = 0 ) {
		?>
		<script>
			jQuery(document).ready(function() {
				Swal.fire({
					icon: 'error',
					allowOutsideClick: false,
					title: 'Oops...',
					confirmButtonText: '<i class="fa fa-thumbs-up"></i> Great!',
					text: "Calculator with ID <?php echo intval( $form_id ); ?> doesn't exist, or has been deleted. Please recheck your shortcode ",
				}).then((result) => {
					if (result.isConfirmed) {
						location.href = '<?php echo admin_url( 'admin.php?page=scc-list-all-calculator-forms' ); ?>'
					}
				})
			})
		</script>
		<?php
	}
	function activation_prompt() {
		?>
		<script>
			jQuery(document).ready(function() {
				Swal.fire({
					icon: 'warning',
					allowOutsideClick: false,
					title: 'Pending License Activation',
					confirmButtonText: 'Activate Now',
					text: "Please activate Stylish Cost Calculator with a license key",
				}).then((result) => {
					if (result.isConfirmed) {
						location.href = '<?php echo esc_url( admin_url( 'admin.php?page=stylish_cost_calculator_premium_settings' ) ); ?>'
					}
				})
			})
		</script>
		<?php
	}

	private function updateCalculatorUsageStat() {
		if ( get_option( 'df-scc-save-count', null ) !== null ) {
			$save_count = intval( get_option( 'df-scc-save-count' ) );
			update_option( 'df-scc-save-count', ( $save_count + 1 ) );
		} else {
			update_option( 'df-scc-save-count', 0 );
		}
	}
}
new PageEditTabs();
