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

if ( !class_exists( 'EMD_MB_Datetime_Field' ) )
{
	class EMD_MB_Datetime_Field extends EMD_MB_Field
	{
		/**
		 * Enqueue scripts and styles
		 *
		 * @return void
		 */
		static function admin_enqueue_scripts()
		{
			$url_css = EMD_MB_CSS_URL . 'jqueryui';
			wp_register_script( 'jquery-ui-timepicker', EMD_MB_JS_URL . 'jqueryui/jquery-ui-timepicker-addon.js', array( 'jquery-ui-datepicker', 'jquery-ui-slider' ), '0.9.7', true );
			wp_enqueue_style( 'jquery-ui-timepicker-css', "{$url_css}/jquery-ui-timepicker-addon.css");
			$deps = array( 'jquery-ui-datepicker', 'jquery-ui-timepicker' );

                        $locale = get_locale();
                        $date_vars['closeText'] = __('Done','employee-spotlight');
                        $date_vars['prevText'] = __('Prev','employee-spotlight');
                        $date_vars['nextText'] = __('Next','employee-spotlight');
                        $date_vars['currentText'] = __('Today','employee-spotlight');
                        $date_vars['monthNames'] = Array(__('January','employee-spotlight'),__('February','employee-spotlight'),__('March','employee-spotlight'),__('April','employee-spotlight'),__('May','employee-spotlight'),__('June','employee-spotlight'),__('July','employee-spotlight'),__('August','employee-spotlight'),__('September','employee-spotlight'),__('October','employee-spotlight'),__('November','employee-spotlight'),__('December','employee-spotlight'));
                        $date_vars['monthNamesShort'] = Array(__('Jan','employee-spotlight'),__('Feb','employee-spotlight'),__('Mar','employee-spotlight'),__('Apr','employee-spotlight'),__('May','employee-spotlight'),__('Jun','employee-spotlight'),__('Jul','employee-spotlight'),__('Aug','employee-spotlight'),__('Sep','employee-spotlight'),__('Oct','employee-spotlight'),__('Nov','employee-spotlight'),__('Dec','employee-spotlight'));
                        $date_vars['dayNames'] = Array(__('Sunday','employee-spotlight'),__('Monday','employee-spotlight'),__('Tuesday','employee-spotlight'),__('Wednesday','employee-spotlight'),__('Thursday','employee-spotlight'),__('Friday','employee-spotlight'),__('Saturday','employee-spotlight'));
                        $date_vars['dayNamesShort'] = Array(__('Sun','employee-spotlight'),__('Mon','employee-spotlight'),__('Tue','employee-spotlight'),__('Wed','employee-spotlight'),__('Thu','employee-spotlight'),__('Fri','employee-spotlight'),__('Sat','employee-spotlight'));
                        $date_vars['dayNamesMin'] = Array(__('Su','employee-spotlight'),__('Mo','employee-spotlight'),__('Tu','employee-spotlight'),__('We','employee-spotlight'),__('Th','employee-spotlight'),__('Fr','employee-spotlight'),__('Sa','employee-spotlight'));
                        $date_vars['weekHeader'] = __('Wk','employee-spotlight');

			$time_vars['timeOnlyTitle'] = __('Choose Time','employee-spotlight');
			$time_vars['timeText'] = __('Time','employee-spotlight');
			$time_vars['hourText'] = __('Hour','employee-spotlight');
			$time_vars['minuteText'] = __('Minute','employee-spotlight');
			$time_vars['secondText'] = __('Second','employee-spotlight');
			$time_vars['millisecText'] = __('Millisecond','employee-spotlight');
			$time_vars['timezoneText'] = __('Time Zone','employee-spotlight');
			$time_vars['currentText'] = __('Now','employee-spotlight');
			$time_vars['closeText'] = __('Done','employee-spotlight');

                        $vars['date'] = $date_vars;
                        $vars['time'] = $time_vars;
                        $vars['locale'] = $locale;

			wp_enqueue_script( 'emd-mb-datetime', EMD_MB_JS_URL . 'datetime.js', $deps, EMD_MB_VER, true );
                        wp_localize_script( 'emd-mb-datetime', 'dtvars', $vars);
		}

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

		/**
		 * Calculates the timestamp from the datetime string and returns it
		 * if $field['timestamp'] is set or the datetime string if not
		 *
		 * @param mixed $new
		 * @param mixed $old
		 * @param int   $post_id
		 * @param array $field
		 *
		 * @return string|int
		 */
		/*static function value( $new, $old, $post_id, $field )
		{
			if ( !$field['timestamp'] )
				return $new;

			$d = DateTime::createFromFormat( self::translate_format( $field ), $new );
			return $d ? $d->getTimestamp() : 0;
		}*/

		/**
		 * 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(),
				'timestamp'  => false,
			) );

			// 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'],
				'timeFormat'      => 'hh:mm:ss',
				'showButtonPanel' => true,
				'separator'       => ' ',
				'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 )
				. $field['js_options']['separator']
				. strtr( $field['js_options']['timeFormat'], self::$time_format_translation );
		}
		static function save( $new, $old, $post_id, $field )
                {
                        $name = $field['id'];
                        if ( '' === $new)
                        {
                                delete_post_meta( $post_id, $name );
                                return;
                        }
                        if($field['js_options']['timeFormat'] == 'hh:mm')
                        {
                                $getformat = 'Y-m-d H:i';
                        }
                        else
                        {
                                $getformat = 'Y-m-d H:i:s';
                        }
			if(DateTime::createFromFormat(self::translate_format($field), $new)){
                        	$new = DateTime::createFromFormat(self::translate_format($field), $new)->format($getformat);
                        	update_post_meta( $post_id, $name, $new );
			}
                }
	}
}
