<?php namespace ProcessWire;

require_once(__DIR__ . '/InputfieldPageListSelectCommon.php');

/**
 * ProcessWire Page List Select Multiple Inputfield module
 *
 * A Page List Selector for selecting multiple pages
 *
 * ProcessWire 3.x, Copyright 2023 by Ryan Cramer
 * https://processwire.com
 * 
 * @property string $removeLabel
 * @property string $moreLabel
 * @property string $unselectLabel
 * @property string $selectLabel
 * @property string $selectedLabel
 * @property string $cancelLabel
 * @property string $startLabel
 * @property string $labelFieldName
 * @property int $parent_id
 * 
 *
 */

class InputfieldPageListSelectMultiple extends Inputfield 
	implements InputfieldHasArrayValue, InputfieldPageListSelection, InputfieldHasSortableValue {

	use InputfieldPageListSelectCommon;

	public static function getModuleInfo() {
		return array(
			'title' => __('Page List Select Multiple', __FILE__), // Module Title
			'summary' => __('Selection of multiple pages from a ProcessWire page tree list', __FILE__), // Module Summary
			'version' => 103,
			'permanent' => true, 
		);
	}

	public function init() {
		parent::init();
		
		$this->set('parent_id', 0); 
		$this->set('labelFieldName', 'title');
		$this->set('startLabel', $this->_('Add'));
		$this->set('cancelLabel', $this->_('Close'));
		$this->set('selectLabel', $this->_('Select'));
		$this->set('selectedLabel', $this->_('Selected'));
		$this->set('unselectLabel', $this->_('Unselect'));
		$this->set('moreLabel', $this->_('More'));
		$this->set('removeLabel', $this->_('Remove'));
	}

	/**
	 * @param string $label Entity encoded label text
	 * @param int $value
	 * @param string $class
	 * @return string
	 * @throws WireException
	 * 
	 */
	protected function renderListItem($label, $value, $class = '') {
		if($class) $class = " $class";
		$sanitizer = $this->wire()->sanitizer;
		$sortIcon = wireIconMarkup('arrows itemSort');
		$trashIcon = wireIconMarkup('trash');
		$trashLabel = $sanitizer->entities1($this->removeLabel);
		$out = 	
			"<li class='ui-state-default$class'>" . 
				"$sortIcon " . 
				"<span class='itemValue'>$value</span>" .
				"<span class='itemLabel'>$label</span> " . 
				"<a class='itemRemove' title='$trashLabel' href='#'>$trashIcon</a>" . 
			"</li>";
		return $out; 
	}

	public function renderReady(?Inputfield $parent = null, $renderValueMode = false) {
		$this->pageListReady($this->attr('name'), $this->labelFieldName);
		return parent::renderReady($parent, $renderValueMode);
	}
	
	public function ___render() {
		
		$pages = $this->wire()->pages;

		if(!strlen("$this->parent_id")) return $this->renderParentError();

		$out = "<ol id='{$this->id}_items'>" . $this->renderListItem("Label", "1", "itemTemplate"); 

		foreach($this->value as $page_id) {
			$page = $pages->get((int) $page_id); 
			if(!$page || !$page->id) continue; 
			$label = $page->getText($this->labelFieldName, true, true);
			if(!strlen($label)) $label = $page->name;
			$out .= $this->renderListItem($label, $page->id); 
		}
		
		$out .= "</ol>";

		$this->addClass('InputfieldPageListSelectMultipleData');
		$attrs = $this->getAttributes();
		unset($attrs['value']);
		
		$attrs['data-root'] = $this->parent_id; // rootPageID
		$attrs['data-href'] = "#wrap_{$this->id}"; // selectSelectHref
		$attrs['data-start'] = $this->startLabel; // selectStartLabel
		$attrs['data-cancel'] = $this->cancelLabel; // selectCancelLabel
		$attrs['data-select'] = $this->selectLabel; // selectSelectLabel
		$attrs['data-selected'] = $this->selectedLabel;
		$attrs['data-unselect'] = $this->unselectLabel; // selectUnselectLabel
		$attrs['data-more'] = $this->moreLabel; // moreLabel
		$attrs['data-labelName'] = $this->attr('name');
	
		$attrStr = $this->getAttributesString($attrs);
		$attrStr = "value='" . implode(',', $this->value) . "' $attrStr";
		
		$out .=	"<input type='text' $attrStr />";

		return $out; 
	}

	public function ___renderValue() {
		return $this->renderMarkupValue($this->val());
	}

	/**
	 * Convert the CSV string provide in the $input to an array of ints needed for this fieldtype
	 * 
	 * @param WireInputData $input
	 * @return $this
	 *
	 */
	public function ___processInput(WireInputData $input) {

		parent::___processInput($input); 

		$value = $this->attr('value');	
		if(is_array($value)) $value = reset($value); 
		$value = trim($value); 

		if(strpos($value, ",") !== false) $value = explode(",", $value); 
			else if($value) $value = array($value);
			else $value = array();

		foreach($value as $k => $v) {
			$value[$k] = (int) $v; 
		}

		$this->attr('value', $value); 

		return $this; 
	}
	
}
