<?php

if (! class_exists( 'GFForms' ) )  {

	die();
}

class Wiaas_Delivery_Process_Addon extends Gravity_Flow_Extension {

	private static $_instance = null;

	protected $_slug = 'wiaas_delivery_process';

	protected $_title = 'Delivery Settings';

	protected $_short_title = 'Delivery Settings';


	public static function get_instance() {
		if ( self::$_instance == null ) {
			self::$_instance = new Wiaas_Delivery_Process_Addon();
		}

		return self::$_instance;
	}

	public function init() {

	    parent::init();

		add_filter( 'gform_enqueue_scripts', array( $this, 'filter_gform_enqueue_scripts' ) );
    }

    public function filter_gform_enqueue_scripts( ) {

	    wp_enqueue_script( 'gform_datepicker_init' );
    }

	/**
	 * Extends Gravity Form entry metadata with 'wiaas_delivery_process_id'
	 *
	 * @param array $entry_meta
	 * @param int $form_id
	 *
	 * @return array
	 */
	public function get_entry_meta( $entry_meta, $form_id ) {
		$entry_meta[ 'wiaas_delivery_process_id' ] = array(
			'label'                      => 'Wiaas Delivery Process Id',
			'is_numeric'                 => true,
			'update_entry_meta_callback' => array( __CLASS__, 'update_entry_delivery_process_id' ),
			'is_default_column'          => false, // this column will be displayed by default on the entry list
			'filter'                     => array(
				'operators' => array( 'is' ),
			),
		);

		$entry_meta[ 'wiaas_delivery_order_id' ] = array(
			'label'                      => 'Wiaas Delivery Process Order Id',
			'is_numeric'                 => true,
			'update_entry_meta_callback' => null,
			'is_default_column'          => false, // this column will be displayed by default on the entry list
			'filter'                     => array(
				'operators' => array( 'is' ),
			),
		);

		$entry_meta[ 'wiaas_delivery_step_name' ] = array(
			'label'                      => 'Wiaas Delivery Step name',
			'is_numeric'                 => false,
			'update_entry_meta_callback' => null,
			'is_default_column'          => false, // this column will be displayed by default on the entry list
			'filter'                     => array(
				'operators' => array( 'is' ),
			),
		);

		return $entry_meta;
	}

	public static function update_entry_delivery_process_id($key, $entry, $form) {

		if ( isset( $_REQUEST['wiaas_delivery_process_id'] ) ) {
			return absint( $_REQUEST['wiaas_delivery_process_id'] );
		}

		if ( isset( $entry[ $key ] ) ) {
			return $entry[ $key ];
		}

		return '';
	}


	public function scripts() {

		$plugin_url = untrailingslashit( plugins_url( '/', WIAAS_FILE ) );

		$scripts = array(

			array(
				'handle' => 'wiaas_form_editor_js',
				'src' => $plugin_url . '/assets/js/wiaas-form-editor.js',
				'enqueue'  => array(
					array(
						'admin_page' => array('form_editor'),
					),
				),
			)
		);

		return array_merge( parent::scripts(), $scripts );
	}


	/**
	 * Add settings menu for form delivery process
	 *
	 * @param $tabs
	 * @param $form_id
	 *
	 * @return array
	 */
	public function add_form_settings_menu( $tabs, $form_id ) {

		$tabs[] = array(
			'name'  => $this->_slug,
			'label' => esc_html__( 'Delivery Settings', 'wiaas' ),
			'query' => array( 'fid' => null )
		);

		return $tabs;
	}


	/**
	 * Add settings field for delivery process settings menu
	 *
	 * @param $form
	 *
	 * @return array
	 */
	public function form_settings_fields($form) {

		return array(

			array(
				'title' => esc_html__( 'Delivery Process', 'wiaas' ),
				'fields' => array(
					array(
						'name' => 'delivery_process',
						'label' => esc_html__( 'Delivery Form Type', 'wiaas' ),
						'type'  => 'delivery_process',
					)
				)
			)
		);
	}

	public function settings_delivery_process() {

		$this->settings_select(array(
			'name' => 'delivery_form_type',
			'choices' => array(
				array( 'value' => 'process', 'label' => 'Process Form' ),
				array( 'value' => 'action', 'label' => 'Action Form' ),
			),
			'after_select' => '<p class="description"> Choose if this form will be used as process form or action form.</p>' .
			                  '<p class="description"> <strong>Process form</strong> defines order delivery process workflow.</p>' .
			                  '<p class="description"> <strong>Action form</strong> defines custom order data that is collected from delivery process participants.</p>'
		));


		?>
        <br />  <br />  <br />
		<?php


		$settings = $this->get_current_settings();

		if ($settings['delivery_form_type'] !== 'process') {

			$this->settings_select(array(
				'name' => 'delivery_action_code',
				'choices' => array(
					array( 'value' => '', 'label' => 'Select action code ...' ),
					array( 'value' => 'customer-acceptance', 'label' => 'Customer acceptance' ),
					array( 'value' => 'validate-questionnaire', 'label' => 'Validate Questionnaire' ),
					array( 'value' => 'schedule-meeting', 'label' => 'Schedule meeting' ),
				),
				'after_select' => '<p class="description"> Choose action code for action form.</p>'
			));


			$this->settings_checkbox_and_select(array(
				'checkbox' => array(
					'label'          => esc_html__( 'Automatic', 'wiaas' ),
					'name'           => 'automatic_action_entries_enabled',
					'default_value' => '0',
				),
				'select'   => array(
					'name'    => 'automatic_action_entries_type',
					'choices' => array(
						array(
							'value' => 'single',
							'label' => esc_html__( 'Single entry', 'wiaas' ),
						),
						array(
							'value' => 'bundle',
							'label' => esc_html__( 'Entry per bundle', 'wiaas' ),
						)
					),
					'after_select' => '<p class="description">Automatic entries can be created once per order or per every bundle in order.</p>' .
					                  '<p class="description">Automatic entry will not be created if any required field cannot be populated.</p>',
				)
			));

			return;
		}

		$countries = Wiaas_Countries::get_available_countries();

		$countries_choices = array();
		foreach ($countries as $country) {
		    $countries_choices[] = array( 'value' => $country['code'] , 'label' => $country['name'] );
        }

		$this->settings_select(array(
			'name' => 'delivery_country',
			'choices' => $countries_choices,
			'after_select' => '<p class="description"> Choose country for which this process is defined.</p>'
		));
	}
}