<?php namespace ProcessWire;

/**
 * ProcessWire Fieldset Tabs
 *
 * These are the same as FieldsetOpen except that they are displayed in tabs rather than normal fieldsets. 
 *
 * For documentation about the fields used in this class, please see:  
 * /wire/core/Fieldtype.php
 * 
 * ProcessWire 3.x, Copyright 2022 by Ryan Cramer
 * https://processwire.com
 *
 *
 */

require_once(__DIR__ . "/FieldtypeFieldsetOpen.module"); 

class InputfieldFieldsetTabOpen extends InputfieldFieldsetOpen { 
	
	public function __construct() {
		$this->set('modal', false); // only applicable to ProcessPageEdit at present
		parent::__construct();
	}
	
	public function ___getConfigInputfields() {
		$inputfields = parent::___getConfigInputfields();
		
		$in = $inputfields->getChildByName('columnWidth');
		if($in) $inputfields->remove($in);
	
		/** @var InputfieldSelect $in */
		$in = $inputfields->getChildByName('collapsed'); 
		//if($in->parent) $in->parent->set('collapsed', Inputfield::collapsedYes); 
		if($in) {
			foreach($in->getOptions() as $key => $value) {
				// tabs may not be collapsed
				if($key != Inputfield::collapsedNo && $key != Inputfield::collapsedYesAjax) {
					$in->removeOption($key);
				}
			}
		}
		// tabs don't support showIf
		$in = $inputfields->getChildByName('showIf'); 
		if($in) $in->getParent()->remove($in); 
		
		return $inputfields; 
	}
	
} 

class FieldtypeFieldsetTabOpen extends FieldtypeFieldsetOpen {

	public static function getModuleInfo() {
		return array(
			'title' => 'Fieldset in Tab (Open)',
			'version' => 100,
			'summary' => 'Open a fieldset to group fields. Same as Fieldset (Open) except that it displays in a tab instead.',
			'permanent' => true, 
		);
	}
	
	public function getInputfield(Page $page, Field $field) {
		/** @var InputfieldFieldsetTabOpen $inputfield */
		$inputfield = $this->wire(new InputfieldFieldsetTabOpen());
		$inputfield->class = $this->className();
		if($field->get('modal')) {
			$inputfield->set('modal', true);
		} else if($field->collapsed == Inputfield::collapsedYesAjax || $field->collapsed == Inputfield::collapsedTabAjax) {
			$inputfield->collapsed = $field->collapsed;
		}
		return $inputfield; 
	}
	
	public function ___getConfigInputfields(Field $field) {
		$inputfields = parent::___getConfigInputfields($field); 
		/** @var InputfieldCheckbox $in */
		$in = $this->wire()->modules->get('InputfieldCheckbox');
		$in->attr('name', 'modal');
		$in->label = $this->_('Open in modal window?');
		$in->description = $this->_('Check the box to make this tab open in its own modal window. This can improve performance with large forms.');
		$in->notes = $this->_('To solve a similar need, you might instead consider the AJAX option, available at: Input (tab) > Visibility > Presentation.');
		if($field->get('modal')) $in->attr('checked', 'checked');
		$inputfields->add($in);
		return $inputfields;
	}

	public function ___getConfigAllowContext(Field $field) {
		return array('modal');
	}


}


