<?php
// Prevent loading this file directly
defined( 'ABSPATH' ) || exit;

if ( !class_exists( 'EMD_MB_Date_Field' ) )
{
	class EMD_MB_Date_Field extends EMD_MB_Field
	{
		/**
		 * Enqueue scripts and styles
		 *
		 * @return void
		 */
		static function admin_enqueue_scripts()
		{
			$deps = array( 'jquery-ui-datepicker' );
			$locale = get_locale();
			$date_vars['closeText'] = __('Done','knowledge-center');
			$date_vars['prevText'] = __('Prev','knowledge-center');
			$date_vars['nextText'] = __('Next','knowledge-center');
			$date_vars['currentText'] = __('Today','knowledge-center');
			$date_vars['monthNames'] = Array(__('January','knowledge-center'),__('February','knowledge-center'),__('March','knowledge-center'),__('April','knowledge-center'),__('May','knowledge-center'),__('June','knowledge-center'),__('July','knowledge-center'),__('August','knowledge-center'),__('September','knowledge-center'),__('October','knowledge-center'),__('November','knowledge-center'),__('December','knowledge-center'));
			$date_vars['monthNamesShort'] = Array(__('Jan','knowledge-center'),__('Feb','knowledge-center'),__('Mar','knowledge-center'),__('Apr','knowledge-center'),__('May','knowledge-center'),__('Jun','knowledge-center'),__('Jul','knowledge-center'),__('Aug','knowledge-center'),__('Sep','knowledge-center'),__('Oct','knowledge-center'),__('Nov','knowledge-center'),__('Dec','knowledge-center'));
			$date_vars['dayNames'] = Array(__('Sunday','knowledge-center'),__('Monday','knowledge-center'),__('Tuesday','knowledge-center'),__('Wednesday','knowledge-center'),__('Thursday','knowledge-center'),__('Friday','knowledge-center'),__('Saturday','knowledge-center'));
			$date_vars['dayNamesShort'] = Array(__('Sun','knowledge-center'),__('Mon','knowledge-center'),__('Tue','knowledge-center'),__('Wed','knowledge-center'),__('Thu','knowledge-center'),__('Fri','knowledge-center'),__('Sat','knowledge-center'));	
			$date_vars['dayNamesMin'] = Array(__('Su','knowledge-center'),__('Mo','knowledge-center'),__('Tu','knowledge-center'),__('We','knowledge-center'),__('Th','knowledge-center'),__('Fr','knowledge-center'),__('Sa','knowledge-center'));	
			$date_vars['weekHeader'] = __('Wk','knowledge-center');
		
			$vars['date'] = $date_vars;
			$vars['locale'] = $locale;	
			wp_enqueue_script( 'emd-mb-date', EMD_MB_JS_URL . 'date.js', $deps, EMD_MB_VER, true );
			wp_localize_script( 'emd-mb-date', 'vars', $vars);
		}

		/**
		 * Get field HTML
		 *
		 * @param mixed  $meta
		 * @param array  $field
		 *
		 * @return string
		 */
		static function html( $meta, $field )
		{
			if($meta != '')
                        {
				if(DateTime::createFromFormat('Y-m-d',$meta)){
                                	$meta = DateTime::createFromFormat('Y-m-d',$meta)->format(self::translate_format($field));
				}
                        }
			return sprintf(
				'<input type="text" class="emd-mb-date" name="%s" value="%s" id="%s" size="%s" data-options="%s" %s readonly/>',
				$field['field_name'],
				$meta,
				isset( $field['clone'] ) && $field['clone'] ? '' : $field['id'],
				$field['size'],
				esc_attr( wp_json_encode( $field['js_options'] ) ),
				isset($field['data-cell']) ? "data-cell='{$field['data-cell']}'" : ''
			);
		}

		/**
		 * Normalize parameters for field
		 *
		 * @param array $field
		 *
		 * @return array
		 */
		static function normalize_field( $field )
		{
			$field = wp_parse_args( $field, array(
				'size'       => 30,
				'js_options' => array(),
			) );

			// Deprecate 'format', but keep it for backward compatible
			// Use 'js_options' instead
			$field['js_options'] = wp_parse_args( $field['js_options'], array(
				'dateFormat'      => empty( $field['format'] ) ? 'yy-mm-dd' : $field['format'],
				'showButtonPanel' => true,
				'changeMonth' => true,
				'changeYear' => true,
				'yearRange' => '-100:+10'
			) );

			return $field;
		}
	
                /**
                 * Returns a date() compatible format string from the JavaScript format
                 *
                 * @see http://www.php.net/manual/en/function.date.php
                 *
                 * @param array $field
                 *
                 * @return string
                 */
                static function translate_format( $field )
                {
                        return strtr( $field['js_options']['dateFormat'], self::$date_format_translation );
                }

                static function save( $new, $old, $post_id, $field )
                {
                        $name = $field['id'];
                        if ( '' === $new)
                        {
                                delete_post_meta( $post_id, $name );
                                return;
                        }
			if(DateTime::createFromFormat(self::translate_format($field), $new)){
                        	$new = DateTime::createFromFormat(self::translate_format($field), $new)->format('Y-m-d');
                        	update_post_meta( $post_id, $name, $new );
			}
                }
	}
}
