<?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly.
/**
 *
 * Field: map
 *
 * @since 1.0.0
 * @version 1.0.0
 *
 */
if ( ! class_exists( 'DSPILOT_Field_map' ) ) {
  class DSPILOT_Field_map extends DSPILOT_Fields {

    public $version = '1.9.4';

    public function __construct( $field, $value = '', $unique = '', $where = '', $parent = '' ) {
      parent::__construct( $field, $value, $unique, $where, $parent );
    }

    public function render() {

      $args              = wp_parse_args( $this->field, array(
        'placeholder'    => esc_html__( 'Search...', 'discountpilot-dynamic-pricing' ),
        'latitude_text'  => esc_html__( 'Latitude', 'discountpilot-dynamic-pricing' ),
        'longitude_text' => esc_html__( 'Longitude', 'discountpilot-dynamic-pricing' ),
        'address_field'  => '',
        'height'         => '',
      ) );

      $value             = wp_parse_args( $this->value, array(
        'address'        => '',
        'latitude'       => '20',
        'longitude'      => '0',
        'zoom'           => '2',
      ) );

      $default_settings   = array(
        'center'          => array( $value['latitude'], $value['longitude'] ),
        'zoom'            => $value['zoom'],
        'scrollWheelZoom' => false,
      );

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

      $style_attr  = ( ! empty( $args['height'] ) ) ? ' style="min-height:'. esc_attr( $args['height'] ) .';"' : '';
      $placeholder = ( ! empty( $args['placeholder'] ) ) ? array( 'placeholder' => $args['placeholder'] ) : '';

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

      if ( empty( $args['address_field'] ) ) {
        echo '<div class="dspilot--map-search">';
        echo '<input type="text" name="'. esc_attr( $this->field_name( '[address]' ) ) .'" value="'. esc_attr( $value['address'] ) .'"'. esc_attr($this->field_attributes( $placeholder )) .' />';
        echo '</div>';
      } else {
        echo '<div class="dspilot--address-field" data-address-field="'. esc_attr( $args['address_field'] ) .'"></div>';
      }

      echo '<div class="dspilot--map-osm-wrap"><div class="dspilot--map-osm" data-map="'. esc_attr( wp_json_encode( $settings ) ) .'"'. esc_attr($style_attr) .'></div></div>';

      echo '<div class="dspilot--map-inputs">';

        echo '<div class="dspilot--map-input">';
        echo '<label>'. esc_attr( $args['latitude_text'] ) .'</label>';
        echo '<input type="text" name="'. esc_attr( $this->field_name( '[latitude]' ) ) .'" value="'. esc_attr( $value['latitude'] ) .'" class="dspilot--latitude" />';
        echo '</div>';

        echo '<div class="dspilot--map-input">';
        echo '<label>'. esc_attr( $args['longitude_text'] ) .'</label>';
        echo '<input type="text" name="'. esc_attr( $this->field_name( '[longitude]' ) ) .'" value="'. esc_attr( $value['longitude'] ) .'" class="dspilot--longitude" />';
        echo '</div>';

      echo '</div>';

      echo '<input type="hidden" name="'. esc_attr( $this->field_name( '[zoom]' ) ) .'" value="'. esc_attr( $value['zoom'] ) .'" class="dspilot--zoom" />';

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

    }

    public function enqueue() {

      if ( ! wp_script_is( 'dspilot-leaflet' ) ) {
        wp_enqueue_script( 'dspilot-leaflet', DSPILOT_PL_PATH . 'admin/includes/framework/assets/js/leaflet.js', array( 'discountpilot-dynamic-pricing' ), $this->version, true );
      }

      if ( ! wp_style_is( 'dspilot-leaflet' ) ) {
        wp_enqueue_style( 'dspilot-leaflet', DSPILOT_PL_PATH . 'admin/includes/framework/assets/js/leaflet.css', array(), $this->version );
      }

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

    }

  }
}
