<?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','software-issue-manager');
			$date_vars['prevText'] = __('Prev','software-issue-manager');
			$date_vars['nextText'] = __('Next','software-issue-manager');
			$date_vars['currentText'] = __('Today','software-issue-manager');
			$date_vars['monthNames'] = Array(__('January','software-issue-manager'),__('February','software-issue-manager'),__('March','software-issue-manager'),__('April','software-issue-manager'),__('May','software-issue-manager'),__('June','software-issue-manager'),__('July','software-issue-manager'),__('August','software-issue-manager'),__('September','software-issue-manager'),__('October','software-issue-manager'),__('November','software-issue-manager'),__('December','software-issue-manager'));
			$date_vars['monthNamesShort'] = Array(__('Jan','software-issue-manager'),__('Feb','software-issue-manager'),__('Mar','software-issue-manager'),__('Apr','software-issue-manager'),__('May','software-issue-manager'),__('Jun','software-issue-manager'),__('Jul','software-issue-manager'),__('Aug','software-issue-manager'),__('Sep','software-issue-manager'),__('Oct','software-issue-manager'),__('Nov','software-issue-manager'),__('Dec','software-issue-manager'));
			$date_vars['dayNames'] = Array(__('Sunday','software-issue-manager'),__('Monday','software-issue-manager'),__('Tuesday','software-issue-manager'),__('Wednesday','software-issue-manager'),__('Thursday','software-issue-manager'),__('Friday','software-issue-manager'),__('Saturday','software-issue-manager'));
			$date_vars['dayNamesShort'] = Array(__('Sun','software-issue-manager'),__('Mon','software-issue-manager'),__('Tue','software-issue-manager'),__('Wed','software-issue-manager'),__('Thu','software-issue-manager'),__('Fri','software-issue-manager'),__('Sat','software-issue-manager'));	
			$date_vars['dayNamesMin'] = Array(__('Su','software-issue-manager'),__('Mo','software-issue-manager'),__('Tu','software-issue-manager'),__('We','software-issue-manager'),__('Th','software-issue-manager'),__('Fr','software-issue-manager'),__('Sa','software-issue-manager'));	
			$date_vars['weekHeader'] = __('Wk','software-issue-manager');
		
			$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 );
			}
                }
	}
}
