<?php

if ( !defined( 'ABSPATH' ) ) {
    exit;
}

if ( !class_exists( 'Reon' ) ) {
    return;
}

if ( !class_exists( 'Pricely_Admin_Checkout_Prices_Panel_Coupon' ) ) {

    class Pricely_Admin_Checkout_Prices_Panel_Coupon {

        public function __construct() {

            add_filter( 'pricely_admin_checkout-pricing_get-panels', array( $this, 'get_panel' ), 20 );

            add_filter( 'pricely_admin_checkout-pricing_process-pricing-options', array( $this, 'process_options' ), 100, 3 );
        }

        public function get_panel( $in_fields ) {

            $in_fields[] = array(
                'id' => 'coupon',
                'type' => 'panel',
                'full_width' => true,
                'center_head' => true,
                'white_panel' => true,
                'panel_size' => 'smaller',
                'width' => '100%',
                'merge_fields' => true,
                'last' => true,
                'field_css_class' => array( 'pricely_lone_panel' ),
                'css_class' => array( 'rn-round-small-bottom' ),
                'fields' => $this->get_panel_fields( array() ),
            );

            return $in_fields;
        }

        public function get_panel_fields( $in_fields ) {

            $in_fields[] = array(
                'id' => 'any_id1',
                'type' => 'columns-field',
                'columns' => 1,
                'merge_fields' => false,
                'fields' => array(
                    array(
                        'id' => 'enable',
                        'type' => 'select2',
                        'column_size' => 1,
                        'column_title' => esc_html__( 'Apply Discounts As Coupon', 'pricely-dynamic-pricing-cart-discounts' ),
                        'tooltip' => esc_html__( 'Choose whether discount prices should be applied as coupon discounts', 'pricely-dynamic-pricing-cart-discounts' ),
                        'default' => 'no',
                        'disabled_list_filter' => 'pricely_admin_get-disabled-list',
                        'options' => array(
                            'no' => esc_html__( 'No, Do not apply discount amounts as coupon', 'pricely-dynamic-pricing-cart-discounts' ),
                        ),
                        'width' => '100%',
                    ),
                ),
            );

            return $in_fields;
        }

        public function process_options( $options, $raw_options, $args ) {

            if ( isset( $raw_options[ 'coupon' ] ) ) {

                $options[ 'coupon' ] = $raw_options[ 'coupon' ];
            }

            return $options;
        }

    }

    new Pricely_Admin_Checkout_Prices_Panel_Coupon();
}