<?php
/**
 *
 * Field: Date
 *
 * @link       https://shapedplugin.com/
 *
 * @package Smart_Post_Show_Pro
 * @subpackage Smart_Post_Show_Pro/admin/views
 */

if ( ! defined( 'ABSPATH' ) ) {
	die;
} // Cannot access directly.

if ( ! class_exists( 'SP_PCP_Field_date' ) ) {
	/**
	 *
	 * Field: date
	 *
	 * @since 2.0
	 * @version 2.0
	 */
	class SP_PCP_Field_date extends SP_PCP_Fields {
		/**
		 * The class constructor.
		 *
		 * @param array  $field The field type.
		 * @param string $value The values of the field.
		 * @param string $unique The unique ID for the field.
		 * @param string $where To where show the output CSS.
		 * @param string $parent The parent args.
		 */
		public function __construct( $field, $value = '', $unique = '', $where = '', $parent = '' ) {
			parent::__construct( $field, $value, $unique, $where, $parent );
		}
		/**
		 * The render method.
		 *
		 * @return void
		 */
		public function render() {

			$default_settings = array(
				'dateFormat' => 'mm/dd/yy',
			);

			$view_options = ( ! empty( $this->field['settings'] ) ) ? $this->field['settings'] : array();
			$view_options = wp_parse_args( $view_options, $default_settings );

			echo wp_kses_post( $this->field_before() );

			if ( ! empty( $this->field['from_to'] ) ) {

				$args = wp_parse_args(
					$this->field,
					array(
						'text_from' => 'From',
						'text_to'   => 'To',
					)
				);

				$value = wp_parse_args(
					$this->value,
					array(
						'from' => '',
						'to'   => '',
					)
				);

				echo '<label class="spf--from">' . $args['text_from'] . ' <input type="text" name="' . $this->field_name( '[from]' ) . '" value="' . $value['from'] . '"' . $this->field_attributes() . '/></label>';// phpcs:ignore
				echo '<label class="spf--to">' . $args['text_to'] . ' <input type="text" name="' . $this->field_name( '[to]' ) . '" value="' . $value['to'] . '"' . $this->field_attributes() . '/></label>';// phpcs:ignore

			} else {

				echo '<input type="text" name="' . esc_attr( $this->field_name() ) . '" value="' .esc_attr( $this->value ) . '"' . $this->field_attributes() . '/>';// phpcs:ignore

			}

			echo '<div class="spf-date-settings" data-settings="' . esc_attr( json_encode( $view_options ) ) . '"></div>';

			echo wp_kses_post( $this->field_after() );

		}

		/**
		 * Enqueue
		 *
		 * @return void
		 */
		public function enqueue() {

			if ( ! wp_script_is( 'jquery-ui-datepicker' ) ) {
				wp_enqueue_script( 'jquery-ui-datepicker' );
			}

		}

	}
}
