<?php

if (class_exists('GF_Field')) {
	class OrderSynthese extends GF_Field_Select
	{
		public $type = 'order_synthese';


		private function buildChoicesArray()
		{

			$ID_FR = 6538;
			$ID_NL = 6541;

			$currentLang = apply_filters('wpml_current_language', null);
			$current_lang_id = apply_filters('wpml_object_id', $ID_FR, 'post', false, $currentLang);

			$document_pdf = get_field('brochure_pdf', $current_lang_id);

			$choices[] = [
				'title' => $document_pdf['title'],
				'text' => $document_pdf['title'],
				'ID_fr' => $ID_FR,
				'ID_nl' => $ID_NL,
			];

			$this->choices = $choices;
			return $choices;
		}

		public function get_form_editor_field_title()
		{
			return esc_attr__('Field de commande de la synthèse des primes', 'homegrade-theme__texte-fonctionnel');
		}
		public function get_form_editor_button()
		{
			return [
				'group' => 'advanced_fields',
				'text'  => $this->get_form_editor_field_title(),
			];
		}
		public function get_form_editor_field_icon()
		{
			return 'dashicons-list-view';
		}
		public function get_form_editor_field_settings()
		{
			return [
				'label_setting',
				'choices_setting',
				'description_setting',
				'rules_setting',
				'error_message_setting',
				'css_class_setting',
				'conditional_logic_field_setting'
			];
		}


		public function is_value_submission_array()
		{
			return true;
		}

		public function get_field_input($form, $value = '', $entry = null)
		{

			// $this->buildChoices();
			$form_id         = $form['id'];
			$id = $this->id;

			ob_start();
			include 'template-field-order-documents.php';
			$table = ob_get_clean();

			return $table;
		}

		private function translateValueArray($value)
		{
			$this->buildChoicesArray();

			$table_value = [];
			$counter = 0;

			foreach ($this->choices as $document) {
				// $isEmpty = empty($value[$counter]) && empty($value[$counter + 1]);

				// if ($isEmpty) {
				// 	$counter += 4;
				// 	continue;
				// }
				$ID_FR = $document['ID_fr'];
				$ID_NL = $document['ID_nl'];

				$table_value[$document['text']]['quantity_fr'] = $value[$counter] ?? 0;
				$table_value[$document['text']]['quantity_nl'] = $value[$counter + 1] ?? 0;

				$table_value[$document['text']]['id_fr'] = 	$ID_FR;
				$table_value[$document['text']]['id_nl'] = 	$ID_NL;
				$counter += 4;
			}
			return $table_value;
		}

		public function get_value_save_entry($value, $form, $input_name, $lead_id, $lead)
		{

			if (empty($value)) {
				$value = '';
			} else {
				$table_value = $this->translateValueArray($value);

				$value = serialize($table_value);
			}

			return $value;
		}


		private function PrettyListDocumentOrder($value)
		{
			$documentOrderPreviewHtml = '<ul>';

			foreach ($value as $brochure => $quantities) {

				$documentOrderAmountHtml = null;


				if ($quantities["quantity_fr"]) {
					$documentOrderAmountHtml .= '<li>' . __("Quantité FR: ", "homegrade-theme__texte-fonctionnel") . '<span class="brochure_quantity">' . $quantities["quantity_fr"]  . '</span></li>';
				}
				if ($quantities["quantity_nl"]) {
					$documentOrderAmountHtml .= '<li>' . __("Quantité NL: ", "homegrade-theme__texte-fonctionnel") . '<span class="brochure_quantity">' .   $quantities["quantity_nl"] . '</span></li>';
				}

				if (!$documentOrderAmountHtml) continue;


				if (!empty($documentOrderAmountHtml)) {
					$documentOrderPreviewHtml .= '<li><h3>' . $brochure . '</h3><ul class="days">' . $documentOrderAmountHtml . '</ul></li>';
				}
			}
			$documentOrderPreviewHtml .= '</ul>';
			return $documentOrderPreviewHtml;
		}


		public function get_value_entry_list($value, $entry, $field_id, $columns, $form)
		{
			return __('Enter details to see delivery details', 'homegrade-theme__texte-fonctionnel');
		}

		public function get_value_entry_detail($value, $currency = '', $use_text = false, $format = 'html', $media = 'screen')
		{
			$value = maybe_unserialize($value);
			if (empty($value)) {
				return;
			}
			$str = $this->PrettyListDocumentOrder($value);

			return $str;
		}

		public function get_value_merge_tag($value, $input_id, $entry, $form, $modifier, $raw_value, $url_encode, $esc_html, $format, $nl2br)
		{
			return $this->PrettyListDocumentOrder($value);
		}
	}
	GF_Fields::register(new OrderSynthese());
}
