<?php
/**
 * Inpost international Courier method
 */

namespace Ilabs\Inpost_International\Shipping;

use Ilabs\Inpost_International\Helpers\Helper;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
} // Exit if accessed directly.



/**
 * Shipping_Method_Courier
 */
class Shipping_Method_Courier extends Shipping_Method_Base {


	/**
	 * Constructor.
	 *
	 * @param int $instance_id Shipping method instance.
	 */
	public function __construct( $instance_id = 0 ) {

		parent::__construct();

		$this->instance_id = absint( $instance_id );
		$this->supports    = array(
			'shipping-zones',
			'instance-settings',
		);

		$this->id                 = 'inpost_international_courier';
		$this->method_description = esc_html__(
			'Allows users to receive orders with delivery to their address.',
			'inpost-international'
		);

		$this->method_title = esc_html__( 'InPost International Courier', 'inpost-international' );
		$this->init();
	}

	/**
	 * Init form fields - Child class override
	 *
	 * @return void
	 */
	public function init_form_fields(): void {

		$settings = array(
			array(
				'title'       => esc_html__( 'General settings', 'inpost-international' ),
				'type'        => 'title',
				'description' => '',
				'id'          => 'section_general_settings',
			),
			'logo_selected'                          => array(
				'name'  => '',
				'title' => esc_html__( 'Choose logo', 'inpost-international' ),
				'type'  => 'logo_selected',
				'id'    => 'logo_selected',
			),
			'logo_custom'                            => array(
				'name'              => '',
				'title'             => '',
				'type'              => 'text',
				'id'                => 'logo_custom',
				'custom_attributes' => array(
					'hidden' => true,
				),
			),
			'title'                                  => array(
				'title'    => esc_html__( 'Method title', 'inpost-international' ),
				'type'     => 'text',
				'default'  => esc_html__( 'InPost International Courier', 'inpost-international' ),
				'desc_tip' => false,
			),
            'delivery_terms'                                  => array(
                'title'    => esc_html__( 'Terms of delivery', 'inpost-international' ),
                'type'     => 'text',
                'default'  => '',
                'desc_tip' => false,
            ),
			'insurance_inpost_intl'                  => array(
				'title'       => esc_html__( 'Insurance', 'inpost-international' ),
				'label'       => esc_html__( 'Set from order amount', 'inpost-international' ),
				'type'        => 'checkbox',
				'description' => '',
				'default'     => 'no',
				'desc_tip'    => true,
			),
			'insurance_value_inpost_intl'            => array(
				'title'             => esc_html__( 'Default insurance amount', 'inpost-international' ),
				'type'              => 'number',
				'custom_attributes' => array(
					'step' => 'any',
					'min'  => '0',
				),
				'default'           => '',
				'desc_tip'          => false,
				'placeholder'       => '0.00',
			),
			'free_shipping_cost'                     => array(
				'title'             => esc_html__( 'Free shipping', 'inpost-international' ),
				'type'              => 'number',
				'custom_attributes' => array(
					'step' => 'any',
					'min'  => '0',
				),
				'default'           => '',
				'desc_tip'          => esc_html__(
					'Enter the amount of the order from which the shipping will be free (does not include virtual products). ',
					'inpost-international'
				),
				'placeholder'       => '0.00',
			),
			'show_free_shipping_label'               => array(
				'title'       => '',
				'label'       => esc_html__( 'Add label "(free)" to the end of title of shipping method', 'inpost-international' ),
				'type'        => 'checkbox',
				'description' => '',
				'default'     => 'yes',
				'desc_tip'    => true,
			),
			'apply_minimum_order_rule_before_coupon' => array(
				'title'       => esc_html__( 'Coupons discounts', 'inpost-international' ),
				'label'       => esc_html__( 'Apply minimum order rule before coupon discount', 'inpost-international' ),
				'type'        => 'checkbox',
				'description' => esc_html__( 'If checked, free shipping would be available based on pre-discount order amount.', 'inpost-international' ),
				'default'     => 'no',
				'desc_tip'    => true,
			),
			'flat_rate'                              => array(
				'title'   => esc_html__( 'Flat rate', 'inpost-international' ),
				'type'    => 'checkbox',
				'label'   => esc_html__( 'Set a flat-rate shipping fee for the entire order.', 'inpost-international' ),
				'class'   => INPOST_INTL_PLUGIN_SLUG . '_flat_rate',
				'default' => 'yes',
			),
			'cost_per_order'                         => array(
				'title'             => esc_html__( 'Cost per order', 'inpost-international' ),
				'type'              => 'number',
				'custom_attributes' => array(
					'step' => 'any',
					'min'  => '0',
				),
				'class'             => INPOST_INTL_PLUGIN_SLUG . '_cost_per_order',
				'default'           => '',
				'desc_tip'          => esc_html__(
					'Set a flat-rate shipping for all orders',
					'inpost-international'
				),
				'placeholder'       => '0.00',
			),
			'tax_status'                             => array(
				'title'   => esc_html__( 'Tax status', 'inpost-international' ),
				'type'    => 'select',
				'class'   => 'wc-enhanced-select',
				'default' => 'none',
				'options' => array(
					'none'    => esc_html__( 'None', 'inpost-international' ),
					'taxable' => esc_html__( 'Taxable', 'inpost-international' ),
				),
			),

			array(
				'title'       => esc_html__( 'Rates table', 'inpost-international' ),
				'type'        => 'title',
				'description' => '',
				'id'          => 'section_general_settings',
				'class'       => INPOST_INTL_PLUGIN_SLUG . '_rates_table_title',
			),
			'based_on'                               => array(
				'title'       => esc_html__( 'Based on', 'inpost-international' ),
				'type'        => 'select',
				'desc_tip'    => esc_html__(
					'Select the method of calculating shipping cost. If the cost of shipping is to be calculated based on the weight of the cart and the products do not have a defined weight, the cost will be calculated incorrectly.',
					'inpost-international'
				),
				'description' => '',
				'class'       => 'wc-enhanced-select ' . INPOST_INTL_PLUGIN_SLUG . '_based_on',
				'options'     => array(
					'price'            => esc_html__( 'Price', 'inpost-international' ),
					'weight'           => esc_html__( 'Weight', 'inpost-international' ),
					'product_qty'      => esc_html__( 'Products qty', 'inpost-international' ),
					'parcel_templates' => esc_html__( 'Based on templates', 'inpost-international' ),
				),
			),
			'rates'                                  => array(
				'title'    => '',
				'type'     => 'rates',
				'class'    => INPOST_INTL_PLUGIN_SLUG . '_rates',
				'default'  => '',
				'desc_tip' => '',
			),
		);

		$settings = $this->add_shipping_classes_settings( $settings );

		$this->instance_form_fields = $settings;
		$this->form_fields          = $settings;
	}
}
