<?php
/**
 * Smart_Custom_Fields_Field_Number
 */
class Smart_Custom_Fields_Field_Number extends Smart_Custom_Fields_Field_Base {

	/**
	 * Set the required items
	 *
	 * @return array
	 */
	protected function init() {
		return array(
			'type'         => 'number',
			'display-name' => __( 'Number', 'smart-custom-fields' ),
			'optgroup'     => 'basic-fields',
		);
	}

	/**
	 * Set the non required items
	 *
	 * @return array
	 */
	protected function options() {
		return array(
			'default'     => '',
			'min'     		=> '',
			'max'     		=> '',
			'step'     		=> '1',
			'prefix'		=> '',
			'postfix'		=> '',
			'instruction' => '',
			'notes'       => '',
		);
	}

	/**
	 * Getting the field
	 *
	 * @param int    $index
	 * @param string $value
	 * @return string html
	 */
	public function get_field( $index, $value ) {
		$name     = $this->get_field_name_in_editor( $index );
		$disabled = $this->get_disable_attribute( $index );
		return sprintf(
			'%s<input type="number" name="%s" value="%s" min="%s" max="%s" step="%s" %s />%s',
			$this->get( 'prefix' ),
			esc_attr( $name ),
			esc_attr( $value ),
			$this->get( 'min' ),
			$this->get( 'max' ),
			$this->get( 'step' ),
			disabled( true, $disabled, false ),
			$this->get( 'postfix' )
		);
	}

	/**
	 * Displaying the option fields in custom field settings page
	 *
	 * @param int $group_key
	 * @param int $field_key
	 */
	public function display_field_options( $group_key, $field_key ) {
		$this->display_label_option( $group_key, $field_key );
		$this->display_name_option( $group_key, $field_key );
		?>
		<tr>
			<th><?php esc_html_e( 'Default', 'smart-custom-fields' ); ?></th>
			<td>
				<input type="number"
					name="<?php echo esc_attr( $this->get_field_name_in_setting( $group_key, $field_key, 'default' ) ); ?>"
					class="widefat"
					value="<?php echo esc_attr( $this->get( 'default' ) ); ?>" />
			</td>
		</tr>
		<tr>
			<th><?php esc_html_e( 'Min', 'smart-custom-fields' ); ?></th>
			<td>
				<input type="number"
					name="<?php echo esc_attr( $this->get_field_name_in_setting( $group_key, $field_key, 'min' ) ); ?>"
					class="widefat"
					value="<?php echo esc_attr( $this->get( 'min' ) ); ?>" />
			</td>
		</tr>
		<tr>
			<th><?php esc_html_e( 'Max', 'smart-custom-fields' ); ?></th>
			<td>
				<input type="number"
					name="<?php echo esc_attr( $this->get_field_name_in_setting( $group_key, $field_key, 'max' ) ); ?>"
					class="widefat"
					value="<?php echo esc_attr( $this->get( 'max' ) ); ?>" />
			</td>
		</tr>
		<tr>
			<th><?php esc_html_e( 'Step', 'smart-custom-fields' ); ?></th>
			<td>
				<input type="number"
					name="<?php echo esc_attr( $this->get_field_name_in_setting( $group_key, $field_key, 'step' ) ); ?>"
					class="widefat"
					value="<?php echo esc_attr( $this->get( 'step' ) ); ?>" />
			</td>
		</tr>
		<tr>
			<th>
				<?php esc_html_e( 'Prefix', 'smart-custom-fields' ); ?>
				<div style="font-size: smaller;">
					入力欄の先頭に表示されます
				</div>
			</th>
			<td>
				<input type="text"
					name="<?php echo esc_attr( $this->get_field_name_in_setting( $group_key, $field_key, 'prefix' ) ); ?>"
					class="widefat"
					value="<?php echo esc_attr( $this->get( 'prefix' ) ); ?>" />
			</td>
		</tr>
		<tr>
			<th>
				<?php esc_html_e( 'Postfix', 'smart-custom-fields' ); ?>
				<div style="font-size: smaller;">
					入力欄の末尾に表示されます
				</div>
			</th>
			<td>
				<input type="text"
					name="<?php echo esc_attr( $this->get_field_name_in_setting( $group_key, $field_key, 'postfix' ) ); ?>"
					class="widefat"
					value="<?php echo esc_attr( $this->get( 'postfix' ) ); ?>" />
			</td>
		</tr>
		<tr>
			<th><?php esc_html_e( 'Instruction', 'smart-custom-fields' ); ?></th>
			<td>
				<textarea name="<?php echo esc_attr( $this->get_field_name_in_setting( $group_key, $field_key, 'instruction' ) ); ?>"
					class="widefat" rows="5"><?php echo esc_attr( $this->get( 'instruction' ) ); ?></textarea>
			</td>
		</tr>
		<tr>
			<th><?php esc_html_e( 'Notes', 'smart-custom-fields' ); ?></th>
			<td>
				<input type="text"
					name="<?php echo esc_attr( $this->get_field_name_in_setting( $group_key, $field_key, 'notes' ) ); ?>"
					class="widefat"
					value="<?php echo esc_attr( $this->get( 'notes' ) ); ?>"
				/>
			</td>
		</tr>
		<?php
	}
}
