<?php
namespace SabaiApps\Directories\Component\Field\Widget;

use SabaiApps\Directories\Component\Entity;
use SabaiApps\Directories\Component\Field\IField;

class DateRangeWidget extends AbstractWidget {

	protected function _fieldWidgetInfo() {
		return array(
			'label'            => __( 'Date range picker', 'directories-pro' ),
			'field_types'      => array( $this->_name ),
			'default_settings' => array(
				'default_year' => null,
			),
			'repeatable'       => true,
		);
	}

	public function fieldWidgetSettingsForm( $fieldType, Entity\Model\Bundle $bundle, array $settings, array $parents = array(), array $rootParents = array() ) {
		return array(
			'default_year' => array(
				'#title'         => __( 'Default year', 'directories-pro' ),
				'#type'          => 'number',
				'#integer'       => true,
				'#min_value'     => 0,
				'#default_value' => $settings['default_year'],
			),
		);
	}

	public function fieldWidgetForm( IField $field, array $settings, $value = null, ?Entity\Type\IEntity $entity = null, array $parents = array(), $language = null ) {
		$field_settings = $field->getFieldSettings();
		return array(
			'#type'                  => 'daterangepicker',
			'#current_date_selected' => false,
			'#min_date'              => ! empty( $field_settings['date_range_enable'] ) ? @$field_settings['date_range'][0] : null,
			'#max_date'              => ! empty( $field_settings['date_range_enable'] ) ? @$field_settings['date_range'][1] : null,
			'#disable_time'          => true,
			'#default_value'         => isset( $value ) ? array( $value['min'], $value['max'] ) : null,
			'#default_year'          => empty( $settings['default_year'] ) ? null : $settings['default_year'],
		);
	}
}
