<?php

    if ( ! defined( 'ABSPATH' ) ) exit;

    require_once __DIR__ . "/../Classes/DigitalSignage4_FormBuilder.php";
    require_once __DIR__ . "/../Classes/DigitalSignage4_Tools.php";
    require_once __DIR__ . "/../Kernel/Translations/DigitalSignage4_Translate.php";
    require_once __DIR__ . "/../Classes/DigitalSignage4_caller.php";
    require_once __DIR__ . "/../Kernel/API/Manager/DigitalSignage4_DemandeManager.php";
    require_once __DIR__ . "/../Kernel/API/Manager/DigitalSignage4_Exception.php";
    require_once __DIR__ . "/../Kernel/API/Manager/DigitalSignage4_ForfaitManager.php";
    require_once "DigitalSignage4_Controller.php";
	
    class DigitalSignage4_ParametreController extends DigitalSignage4_Controller
    {
        public function generatePage()
        {
            $this->route();
        }

        private function route()
        {
            if (isset($_POST)) {
                if (sizeof($_POST) >0) {
                    check_admin_referer( 'ds4_form_nonce' );
                }
            }
            try
            {
                $forfait = DigitalSignage4_ForfaitManager::instance()->getForfait(DigitalSignage4_ForfaitManager::instance()->getToken(), true);

                $this->handleDisqueFreeSpaceFormResponse();
                $this->handleYoutubeFormResponse();
                $this->handleWatermarkFormResponse();
                $this->handleResolutionFormResponse();

                // Deconnéxion de l'utilisateur
                $form = new DigitalSignage4_FormBuilder();
                $form->addExtraLink(DigitalSignage4_Translate::getTraduction("Deconnexion"), (isset($_SERVER['HTTP_HOST'])?sanitize_text_field(wp_unslash($_SERVER['HTTP_HOST'])):"") . (isset($_SERVER['REQUEST_URI'])?sanitize_text_field(wp_unslash($_SERVER['REQUEST_URI'])):""), "DigitalSignage4_logout", true, false);
                if ($form->submitExtraLink("DigitalSignage4_logout")) {
                    DigitalSignage4_caller::getInstance()->delete_data(DigitalSignage4_caller::FORFAIT);
                    DigitalSignage4_Tools::redirectAdmin('DigitalSignageSettings');
                }
				
				$trial="no";
				if ($forfait->getTrial() == true) {
					$trial="yes";
				}
                $moduleInfos = DigitalSignage4_Tools::getModuleInformations();
                $comtpeOnlineDs4Link = (!$forfait) ? "" : DigitalSignage4_Translate::getTraduction("Compte_online_ds4") . "<a target='_blank' href='" . DigitalSignage4_Tools::getAcaciaRedirectionPageUrl("account&accountId=" . $forfait->getCompte()->getId() . "&forfaitId=" . $forfait->getId()."&token=".DigitalSignage4_ForfaitManager::instance()->getToken(),$trial) . "'> " . DigitalSignage4_Translate::getTraduction("Cliquez_ici") . "</a>";

                //Affichage de la vue de connexion
                $this->assign(array(
                    'pluginName'          => $moduleInfos['Name'], //name du module
                    'pluginurl'           => $moduleInfos['PluginURI'], //url du module
                    'pluginVersion'       => $moduleInfos['Version'], //Version du module
                    'pluginAuthor'        => $moduleInfos['AuthorName'], //author du module
                    'pluginAuthorUrl'     => $moduleInfos['AuthorURI'], //author url du module
                    'comtpeOnlineDs4'     => $comtpeOnlineDs4Link,
                    'actualiteUrl'        => DigitalSignage4_Translate::getTraduction("Actualitees_site", DigitalSignage4_Tools::getAcaciaRedirectionPageUrl("actu"."&token=".DigitalSignage4_ForfaitManager::instance()->getToken(),$trial)),
                    //Cliquez_ici
                    'formConnection'      => $this->formConnexion(),
                    'formLogout'          => $form, // Déconnexion
                    'forfait'             => $forfait,
                    'resolution'          => DigitalSignage4_caller::getInstance()->get_data(DigitalSignage4_caller::RESOLUTION),
                    'compte'              => (!$forfait) ? "" : $forfait->getCompte(),
                    'urlAndNameForm'      => $this->formSetUrlAndSiteName(),
                    'prixDisplaySelector' => $this->formPrixDisplaySelector(),

                    //Changement url et nom du site pour les videos
                    'espaceDisk'          => array( // Variable pour formulaire d'espace disk
                                                    'totalFreeSpace' => round(disk_free_space("/") / 1024 / 1024 / 1024),
                                                    'totalDiskSpace' => round(disk_total_space("/") / 1024 / 1024 / 1024),
                                                    'currentLimit'   => $this->getCurrentSpaceSafe(),
                    ),
                    'watermark'           => $this->getCurrentWatermark(),// formulaire de création du watermark
                    'youtubeData'		  => $this->getYoutubeData(),
                    'youtubeAPIUrl'		=> DigitalSignage4_API_Linker::API_URL . 'Youtube/',
                    'placements'          => $this->placementForm(), // Placement des videos a afficher automatiquement
                    'watermark_nonce' => wp_nonce_field( 'ds4_form_nonce' ),

                ));
            } catch (DigitalSignage4_Exception $e) {
                $e->echoError();
            }

            $this->setWPTemplate("Parametres/parametres");
        }

        private function formConnexion()
        {
            $form = new DigitalSignage4_FormBuilder("formualireDeConnexion", DigitalSignage4_Translate::getTraduction("Connexion"));

            //Ajout des champs du formulaire
            $form->addChamp($form->generateField()->setId("userToken")->setLabel(DigitalSignage4_Translate::getTraduction("Entrer_token_connexion"))->setName("userToken")->setType("text")->setValue(""));

            //ajout de l'oubli de mot de passe et de la création de contre
			$trial="no";
            try {
                $forfait = DigitalSignage4_ForfaitManager::instance()->getForfait(DigitalSignage4_ForfaitManager::instance()->getToken(), true);
                if ($forfait) {
					
                    if ($forfait->getTrial() == true) {
                            $trial="yes";
                    }
                    else
                    {
                        $form->addExtraText("<br>" . DigitalSignage4_Translate::getTraduction("Credit_dispo_trial") . ":" . ($forfait->getCredit() - $forfait->getCreditUsedPeriod()));
                    }
                    $form->addExtraLink(DigitalSignage4_Translate::getTraduction("Creer_compte_ds4"), DigitalSignage4_Tools::getAcaciaRedirectionPageUrl("newAccount"."&token=".DigitalSignage4_ForfaitManager::instance()->getToken(),$trial));
                }
            } catch (DigitalSignage4_Exception $e) {
            }

            $form->addExtraText(DigitalSignage4_Translate::getTraduction("Actualitees_site", DigitalSignage4_Tools::getAcaciaRedirectionPageUrl("actu"."&token=".DigitalSignage4_ForfaitManager::instance()->getToken(),$trial)));

            //Récuperation du submit
            if ($form->isSubmit()) {
                $token = $form->getDatas();
                $token = $token['userToken'];

                try {
                    DigitalSignage4_ForfaitManager::instance()->clearUserMessages();
                    $user = DigitalSignage4_ForfaitManager::instance()->getForfait($token, true);

                    if ($user) {
                        $_SESSION['Success'][] = DigitalSignage4_Translate::getTraduction("Connexion_compte_etablie");
                        DigitalSignage4_caller::getInstance()->save_data(DigitalSignage4_caller::FORFAIT, $user);
                        $_SESSION['Errors'] = array();
                        DigitalSignage4_Tools::redirectAdmin("DigitalSignageHome");
                    } else {
                        $_SESSION['Errors'] = array_merge((isset($_SESSION['Errors'])) ? map_deep($_SESSION['Errors'], "sanitize_text_field") : array(), DigitalSignage4_ForfaitManager::instance()->getUserMessages());
                    }
                } catch (DigitalSignage4_Exception $e) {
                    $e->echoError();
                }
            }

            return $form;
        }

        /**
         * Récuperation de la limite d'espace disque
         */
        private function handleDisqueFreeSpaceFormResponse()
        {
            if (isset($_POST)) {
                if (isset($_POST['setDiskSpaceSafe'])) {
                    if (isset($_POST['activerLaSecurisation'])) {
                        $limit = (isset($_POST['limitSpace'])?sanitize_text_field(wp_unslash($_POST['limitSpace'])):"");
                        unset($_POST['setDiskSpaceSafe'], $_POST['limitSpace'], $_POST['activerLaSecurisation']);
                    } else {
                        $limit = 0;
                    }
                    if (DigitalSignage4_caller::getInstance()->get_data(DigitalSignage4_caller::DISK_FREE_SPACE) == $limit) {
                        $_SESSION['Success'][] = DigitalSignage4_Translate::getTraduction("Parametres_inchangé");
                    } else {
                        if (DigitalSignage4_caller::getInstance()->save_data(DigitalSignage4_caller::DISK_FREE_SPACE, $limit)) {
                            $_SESSION['Success'][] = DigitalSignage4_Translate::getTraduction("Espace_disque_save");
                        } else {
                            $_SESSION['Errors'][] = DigitalSignage4_Translate::getTraduction("Espace_disque_non_save");
                        }
                    }

                    $this->refreshParametresPage();
                }
            }
        }

        /**
         * Récupération du watermark et son emplacement
         */
        private function handleWatermarkFormResponse()
        {
            if (isset($_POST)) {
                if (isset($_POST['WaterMark_button'])) {
                    $watermarkUploaded = false;
                    $newWatermarkFileUploaded = false;
                    //$uploaddir = DigitalSignage4_Tools::getModuleImgDir();
					
					$upload_dir_global = wp_upload_dir();
					$uploaddir =$upload_dir_global['basedir'] . '/' . 'ds4' . '/Files';
					
					if (!file_exists($uploaddir)) {
						wp_mkdir_p($uploaddir);
					}
					$uploaddir .='/';
					
			
                    $uploadfile = $uploaddir . "watermark.png";
					
					

                    if (isset($_FILES)) {
                        if (isset($_FILES['Watermark']) && isset($_FILES['Watermark']['size'])) {
                            if ($_FILES['Watermark']['size'] > 0) {
                                //configurer options wp_handle_upload
                                $overrides = [ 'test_form' => false ];
                                //appeler wp_handle_upload
                                $movefile = wp_handle_upload($_FILES['Watermark'] , $overrides );
                                //si le fichier est correct, le déplacer dans $uploadfile
                                if ( $movefile && empty( $movefile['error'] ) ) {
                                    $file_path = $movefile['file'];
                                    chmod($uploadfile, 0777);
                                    wp_delete_file($uploadfile);
                                    copy($file_path,$uploadfile);
                                    $newWatermarkFileUploaded = true;
                                    chmod($uploadfile, 0777);
                                    $watermarkUploaded = true;
                                }
                                //$watermarkUploaded = move_uploaded_file($_FILES['Watermark']['tmp_name'], $uploadfile);
                            } else {
                                $watermarkUploaded = false; // aucun changement de watermark demandé
                            }
                        }
                    }

                    $watermarkDatas = array(
                        "watermark_active" => isset($_POST['activ_watermark']),
                        "watermark_place"  => isset($_POST['place'])?sanitize_text_field(wp_unslash($_POST['place'])):"",
                    );

                    if ($watermarkDatas['watermark_active'] && !$watermarkUploaded) {
                        $_SESSION['Errors'][] = DigitalSignage4_Translate::getTraduction("image_erreur_download");
                        $this->refreshParametresPage();
                    } elseif ($watermarkDatas['watermark_active'] == false) {
                        wp_delete_file($uploadfile);
                    }

                    if (DigitalSignage4_caller::getInstance()->get_data(DigitalSignage4_caller::WATERMARK) == $watermarkDatas && !$newWatermarkFileUploaded) {
                        $_SESSION['Success'][] = DigitalSignage4_Translate::getTraduction("Parametres_inchangé");
                    } else {
                        if (DigitalSignage4_caller::getInstance()->save_data(DigitalSignage4_caller::WATERMARK, $watermarkDatas)) {
                            $_SESSION['Success'][] = DigitalSignage4_Translate::getTraduction("Filigrane_save");
                        } else {
                            $_SESSION['Errors'][] = DigitalSignage4_Translate::getTraduction("Filigrane_non_save");
                        }
                    }

                    $this->refreshParametresPage();
                }
            }
        }

        /**
         * @return DigitalSignage4_FormBuilder
         */
        private function formSetUrlAndSiteName()
        {
            $form = new DigitalSignage4_FormBuilder("urlForm", DigitalSignage4_Translate::getTraduction("Enregistrer"));

            $currentInfos = $this->getCurrentInfoSite();

            $form->addChamp($form->generateField()->setId("urlSite")->setLabel(DigitalSignage4_Translate::getTraduction("Url_site"))->setName("urlSite")->setType("url")->setValue($currentInfos['urlSite']));

            $form->addChamp($form->generateField()->setId("nameSite")->setLabel(DigitalSignage4_Translate::getTraduction("Nom_site"))->setName("nameSite")->setType("text")->setValue($currentInfos['nameSite']));

            if ($form->isSubmit()) {
                $datas = $form->getDatas();

                if (DigitalSignage4_caller::getInstance()->get_data(DigitalSignage4_caller::SITE) == $datas) {
                    $_SESSION['Success'][] = DigitalSignage4_Translate::getTraduction("Parametres_inchangé");
                } else {
                    if (DigitalSignage4_caller::getInstance()->save_data(DigitalSignage4_caller::SITE, $datas)) {
                        $_SESSION['Success'][] = DigitalSignage4_Translate::getTraduction("Parametres_save");
                    } else {
                        $_SESSION['Errors'][] = DigitalSignage4_Translate::getTraduction("Parametres_non_save");
                    }
                }

                $this->refreshParametresPage();
            }

            return $form;
        }

        private function getCurrentInfoSite()
        {
            if ($DigitalSignage4_infos = DigitalSignage4_caller::getInstance()->get_data(DigitalSignage4_caller::SITE)) {
                if ($DigitalSignage4_infos != "1") {
                    if (isset($DigitalSignage4_infos['urlSite'], $DigitalSignage4_infos['nameSite'])) {
                        return $DigitalSignage4_infos;
                    }
                }
            }

            return array(
                "urlSite"  => get_site_url(),
                "nameSite" => get_bloginfo('name'),
            );
        }

        private function getYoutubeData()
        {
            if (DigitalSignage4_caller::getInstance()->get_data(DigitalSignage4_caller::YOUTUBE_JSON)) {
                return DigitalSignage4_caller::getInstance()->get_data(DigitalSignage4_caller::YOUTUBE_JSON);
            }

            return array(
                "youtube_active" => false,
                "youtube_nb_max_envoi_jour" => 10,
                "youtube_json"  => 'coucou',
            );
        }

        private function getCurrentSpaceSafe()
        {
            if (DigitalSignage4_caller::getInstance()->get_data(DigitalSignage4_caller::DISK_FREE_SPACE)) {
                return DigitalSignage4_caller::getInstance()->get_data(DigitalSignage4_caller::DISK_FREE_SPACE);
            }

            return 0;
        }


        private function getCurrentWatermark()
        {
            if ($DigitalSignage4_watermark = DigitalSignage4_caller::getInstance()->get_data(DigitalSignage4_caller::WATERMARK)) {
                if ($DigitalSignage4_watermark !== "1") {
                    if (isset($DigitalSignage4_watermark['watermark_active'], $DigitalSignage4_watermark['watermark_place'])) {
                        return $DigitalSignage4_watermark;
                    }
                }
            }

            return array(
                "watermark_active" => false,
                "watermark_place"  => '',
            );
        }


        private function placementForm()
        {
            $DigitalSignage4_infos = DigitalSignage4_caller::getInstance()->get_data(DigitalSignage4_caller::PLACEMENT);

            if (!is_array($DigitalSignage4_infos)) {
                $DigitalSignage4_infos = array($DigitalSignage4_infos);
            }

            $form = new DigitalSignage4_FormBuilder("placementForm", DigitalSignage4_Translate::getTraduction("Enregistrer"));



            $form->addChamp($form->generateField()->setId("placement_1")->setName("placement_1")->setType("checkbox")->setChecked(isset($DigitalSignage4_infos['placement_1'])));

            $form->addChamp($form->generateField()->setId("placement_2")->setName("placement_2")->setType("checkbox")->setChecked(isset($DigitalSignage4_infos['placement_2'])));

            $form->addChamp($form->generateField()->setId("placement_3")->setName("placement_3")->setType("checkbox")->setChecked(isset($DigitalSignage4_infos['placement_3'])));

            $form->addChamp($form->generateField()->setId("placement_4")->setName("placement_4")->setType("checkbox")->setChecked(isset($DigitalSignage4_infos['placement_4'])));

            $form->addChamp($form->generateField()->setId("placement_5")->setName("placement_5")->setType("checkbox")->setChecked(isset($DigitalSignage4_infos['placement_5'])));

            $form->addChamp($form->generateField()->setId("placement_6")->setName("placement_6")->setType("checkbox")->setChecked(isset($DigitalSignage4_infos['placement_6'])));


            if ($form->isSubmit()) {
                $datas = $form->getDatas();

                if (DigitalSignage4_caller::getInstance()->get_data(DigitalSignage4_caller::PLACEMENT) == $datas) {
                    $_SESSION['Success'][] = DigitalSignage4_Translate::getTraduction("Parametres_inchangé");
                } else {
                    if (DigitalSignage4_caller::getInstance()->save_data(DigitalSignage4_caller::PLACEMENT, $datas)) {
                        $_SESSION['Success'][] = DigitalSignage4_Translate::getTraduction("Emplacement_save");
                    } else {
                        $_SESSION['Errors'][] = DigitalSignage4_Translate::getTraduction("Emplacement_non_save");
                    }
                }

                $this->refreshParametresPage();
            }

            return $form;
        }

        private function refreshParametresPage()
        {
            header('Location: ' . (isset($_SERVER['REQUEST_URI'])?sanitize_text_field(wp_unslash($_SERVER['REQUEST_URI'])):""));
            exit();
        }

        private function formPrixDisplaySelector()
        {
            if (!$DigitalSignage4_prix_display = DigitalSignage4_caller::getInstance()->get_data(DigitalSignage4_caller::PRIX_FORMAT)) {
                $DigitalSignage4_prix_display = 0;
                DigitalSignage4_caller::getInstance()->save_data(DigitalSignage4_caller::PRIX_FORMAT, $DigitalSignage4_prix_display);
            }

            $device = DigitalSignage4_Tools::getWooCommerceDevice();
            $pleinPrice = 10;
            $floatPrice = 99;
            $separator = ',';
            $separatorDot = '.';


            $formTableau='';
			$formTableau.='
            <h2>'.esc_html(DigitalSignage4_Translate::getTraduction("Affichage du prix")).' </h2>
            <table class="priceFormat" style="font-size: 1.2em;">
                <tr>

                    <td>
                        <label style="font-weight: inherit;"><input type="radio"
                                                                    name="format" '.(($DigitalSignage4_prix_display == 0) ? "checked" : "").'
                                                                    value="0"/> '."<strong style='margin: 0px 3px;font-weight: 600;'>" . esc_html($device) . "</strong>" . $pleinPrice . "<strong style='margin: 0px 3px;'>" . esc_html($separatorDot) . "</strong>" .esc_html($floatPrice).'
                        </label><br>
                        <label style="font-weight: inherit;"><input type="radio"
                                                                    name="format" '.(($DigitalSignage4_prix_display == 1) ? "checked" : "") .'
                                                                    value="1"/> '."<strong style='margin: 0px 3px;font-weight: 600;'>" . esc_html($device) . "</strong>" . esc_html($pleinPrice)."<strong style='margin: 0px 3px;'>" . esc_html($separatorDot) . "</strong>" . "<sup>" . esc_html($floatPrice) . "</sup>".'
                        </label><br>
                        <label style="font-weight: inherit;"><input type="radio"
                                                                    name="format" '. (($DigitalSignage4_prix_display == 2) ? "checked" : "").'
                                                                    value="2"/> '."<strong style='margin: 0px 3px;font-weight: 600;'>" . esc_html($device) . "</strong>" . esc_html($pleinPrice) . "<strong style='margin: 0px 3px;'>" . esc_html($separatorDot) . "</strong>" . "<sub>" . esc_html($floatPrice) . "</sub>".'
                        </label>
                    </td>

                    <td>
                        <label style="font-weight: inherit;"><input type="radio"
                                                                    name="format" '.(($DigitalSignage4_prix_display == 3) ? "checked" : "").'
                                                                    value="3"/> '."<strong style='margin: 0px 3px;font-weight: 600;'>" . esc_html($device) . "</strong>" . esc_html($pleinPrice) . "<strong style='margin: 0px 3px;'>" . esc_html($separator) . "</strong>" . esc_html($floatPrice).' 
                        </label><br>
                        <label style="font-weight: inherit;"><input type="radio"
                                                                    name="format" '.(($DigitalSignage4_prix_display == 4) ? "checked" : "").'
                                                                    value="4"/> '."<strong style='margin: 0px 3px;font-weight: 600;'>" . esc_html($device) . "</strong>" . esc_html($pleinPrice) . "<strong style='margin: 0px 3px;'>" . esc_html($separator) . "</strong>" . "<sup>" . esc_html($floatPrice) . "</sup>".'
                        </label><br>
                        <label style="font-weight: inherit;"><input type="radio"
                                                                    name="format" '.(($DigitalSignage4_prix_display == 5) ? "checked" : "").'
                                                                    value="5"/> '."<strong style='margin: 0px 3px;font-weight: 600;'>" . esc_html($device) . "</strong>" . esc_html($pleinPrice) . "<strong style='margin: 0px 3px;'>" . esc_html($separator) . "</strong>" . "<sub>" . esc_html($floatPrice) . "</sub>".'
                        </label>
                    </td>

                    <td>
                        <label style="font-weight: inherit;"><input type="radio"
                                                                    name="format" '.(($DigitalSignage4_prix_display == 6) ? "checked" : "").'
                                                                    value="6"/> '."<strong style='margin: 0px 3px;font-weight: 600;'>" . esc_html($device) . "</strong>" . esc_html($pleinPrice) . "<sup>" . esc_html($floatPrice) . "</sup>".'
                        </label><br>
                        <label style="font-weight: inherit;"><input type="radio"
                                                                    name="format" '.(($DigitalSignage4_prix_display == 7) ? "checked" : "").'
                                                                    value="7"/> '."<strong style='margin: 0px 3px;font-weight: 600;'>" . esc_html($device) . "</strong>" . esc_html($pleinPrice) . "<sub>" . esc_html($floatPrice) . "</sub>".'
                        </label><br>
                    </td>


                </tr>';
				
				$formTableau.='
                <tr>


                    <td>
                        <label style="font-weight: inherit;"><input type="radio"
                                                                    name="format" '.(($DigitalSignage4_prix_display == 8) ? "checked" : "").'
                                                                    value="8"/> '.esc_html($pleinPrice) . "<strong style='margin: 0px 3px;'>" . esc_html($separatorDot) . "</strong>" . esc_html($floatPrice) . "<strong style='margin: 0px 3px;font-weight: 600;'>" . esc_html($device) . "</strong>".'
                        </label><br>
                        <label style="font-weight: inherit;"><input type="radio"
                                                                    name="format" '.(($DigitalSignage4_prix_display == 9) ? "checked" : "").'
                                                                    value="9"/> '.esc_html($pleinPrice) . "<strong style='margin: 0px 3px;'>" . esc_html($separatorDot) . "</strong>" . "<sup>" . esc_html($floatPrice) . "</sup>" . "<strong style='margin: 0px 3px;font-weight: 600;'>" . esc_html($device) . "</strong>".'
                        </label><br>
                        <label style="font-weight: inherit;"><input type="radio"
                                                                    name="format" '.(($DigitalSignage4_prix_display == 10) ? "checked" : "").' 
                                                                    value="10"/> '.esc_html($pleinPrice) . "<strong style='margin: 0px 3px;'>" . esc_html($separatorDot) . "</strong>" . "<sub>" . esc_html($floatPrice) . "</sub>" . "<strong style='margin: 0px 3px;font-weight: 600;'>" . esc_html($device) . "</strong>".'
                        </label>
                    </td>

                    <td>
                        <label style="font-weight: inherit;"><input type="radio"
                                                                    name="format" '.(($DigitalSignage4_prix_display == 11) ? "checked" : "").' 
                                                                    value="11"/> '. esc_html($pleinPrice) . "<strong style='margin: 0px 3px;'>" . esc_html($separator) . "</strong>" . esc_html($floatPrice) . "<strong style='margin: 0px 3px;font-weight: 600;'>" . esc_html($device) . "</strong>" .'
                        </label><br>
                        <label style="font-weight: inherit;"><input type="radio"
                                                                    name="format" '.(($DigitalSignage4_prix_display == 12) ? "checked" : "").'
                                                                    value="12"/> '. esc_html($pleinPrice) . "<strong style='margin: 0px 3px;'>" . esc_html($separator) . "</strong>" . "<sup>" . esc_html($floatPrice) . "</sup>" . "<strong style='margin: 0px 3px;font-weight: 600;'>" . esc_html($device) . "</strong>" .'
                        </label><br>
                        <label style="font-weight: inherit;"><input type="radio"
                                                                    name="format" '.(($DigitalSignage4_prix_display == 13) ? "checked" : "").'
                                                                    value="13"/> '.esc_html($pleinPrice) . "<strong style='margin: 0px 3px;'>" . esc_html($separator) . "</strong>" . "<sub>" . esc_html($floatPrice) . "</sub>" . "<strong style='margin: 0px 3px;font-weight: 600;'>" . esc_html($device) . "</strong>" .'
                        </label>
                    </td>

                    <td>
                        <label style="font-weight: inherit;"><input type="radio"
                                                                    name="format" '.(($DigitalSignage4_prix_display == 14) ? "checked" : "").'
                                                                    value="14"/> '.esc_html($pleinPrice) . "<sup>" . esc_html($floatPrice) . "</sup>" . "<strong style='margin: 0px 3px;font-weight: 600;'>" . esc_html($device) . "</strong>" .'
                        </label><br>
                        <label style="font-weight: inherit;"><input type="radio"
                                                                    name="format" '.(($DigitalSignage4_prix_display == 15) ? "checked" : "").'
                                                                    value="15"/> '.esc_html($pleinPrice) . "<sub>" . esc_html($floatPrice) . "</sub>" . "<strong style='margin: 0px 3px;font-weight: 600;'>" . esc_html($device) . "</strong>" .'
                        </label><br>
                    </td>


                </tr>

                <tr>
                    <td>
                        <label style="font-weight: inherit;"><input type="radio"
                                                                    name="format" '.(($DigitalSignage4_prix_display == 16) ? "checked" : "").'
                                                                    value="16"/> '.esc_html($pleinPrice) . "<strong style='margin: 0px 3px;font-weight: 600;'>" . esc_html($device) . "</strong>" . esc_html($floatPrice).' 
                        </label><br>
                        <label style="font-weight: inherit;"><input type="radio"
                                                                    name="format" '.(($DigitalSignage4_prix_display == 17) ? "checked" : "").' 
                                                                    value="17"/> '.esc_html($pleinPrice) . "<strong style='margin: 0px 3px;font-weight: 600;'>" . esc_html($device) . "</strong>" . "<sup>" . esc_html($floatPrice) . "</sup>" .'
                        </label><br>
                        <label style="font-weight: inherit;"><input type="radio"
                                                                    name="format" '.(($DigitalSignage4_prix_display == 18) ? "checked" : "").'
                                                                    value="18"/> '.esc_html($pleinPrice) . "<strong style='margin: 0px 3px;font-weight: 600;'>" . esc_html($device) . "</strong>" . "<sub>" . esc_html($floatPrice) . "</sub>" .'
                        </label>
                    </td>
                    <td></td>
                    <td></td>
                </tr>


            </table>';

            

            

            $form = new DigitalSignage4_FormBuilder("priceFormatForm", DigitalSignage4_Translate::getTraduction("Enregistrer"));
            $form->setClass("specialTable");
            $form->setHtml($formTableau);

            if ($form->isSubmit()) {
                $datas = $form->getDatas();
                $datas = $datas['format'];

                if (DigitalSignage4_caller::getInstance()->get_data(DigitalSignage4_caller::PRIX_FORMAT) == $datas) {
                    $_SESSION['Success'][] = DigitalSignage4_Translate::getTraduction("Parametres_save");
                } else {
                    if (DigitalSignage4_caller::getInstance()->save_data(DigitalSignage4_caller::PRIX_FORMAT, $datas)) {
                        $_SESSION['Success'][] = DigitalSignage4_Translate::getTraduction("Parametres_save");
                    } else {
                        $_SESSION['Errors'][] = DigitalSignage4_Translate::getTraduction("Parametres_non_save");
                    }
                }

                $this->refreshParametresPage();
            }

            return $form;
        }
        private function handleYoutubeFormResponse()
        {
            if (isset($_POST)) {
                if (isset($_POST['Youtube_button'])) {
                    $youtubeActive = false;
                    $youtubeDatas = array(
                        "youtube_active" => isset($_POST['activ_youtube']),
                        "youtube_json"  => isset($_POST['youtube_json'])?sanitize_text_field(wp_unslash($_POST['youtube_json'])):"",
                        "youtube_nb_max_envoi_jour"  => isset($_POST['youtube_nb_max_envoi_jour'])?sanitize_text_field(wp_unslash($_POST['youtube_nb_max_envoi_jour'])):"",
                    );
                    //pousser a l'api le nouveau youtube_nb_max_envoi_jour


                    DigitalSignage4_ForfaitManager::instance()->update_compteur_youtube_max(isset($_POST['youtube_nb_max_envoi_jour'])?sanitize_text_field(wp_unslash($_POST['youtube_nb_max_envoi_jour'])):"", DigitalSignage4_ForfaitManager::instance()->getToken());

                    if (DigitalSignage4_caller::getInstance()->save_data(DigitalSignage4_caller::YOUTUBE_JSON, $youtubeDatas)) {
                        $_SESSION['Success'][] = DigitalSignage4_Translate::getTraduction("Youtube_save");
                    } else {
                        $_SESSION['Errors'][] = DigitalSignage4_Translate::getTraduction("Youtube_non_save");
                    }
                    $this->refreshParametresPage();
                }
            }
        }

        private function handleResolutionFormResponse()
        {
            if (isset($_POST['setResolution'])) {
                if (DigitalSignage4_caller::getInstance()->get_data(DigitalSignage4_caller::RESOLUTION) == isset($_POST['resolution'])?sanitize_text_field(wp_unslash($_POST['resolution'])):"" || // Si les parametres so nt les meme que avant
                    DigitalSignage4_caller::getInstance()->save_data(DigitalSignage4_caller::RESOLUTION, isset($_POST['resolution'])?sanitize_text_field(wp_unslash($_POST['resolution'])):"") // si les nouveaux parametres sont enregistrait
                ) {
                    $_SESSION['Success'][] = DigitalSignage4_Translate::getTraduction("Parametres_save");
                } else {
                    $_SESSION['Errors'][] = DigitalSignage4_Translate::getTraduction("Parametres_non_save");
                }
            }
        }
    }
