<?php

/**
 * Simple Product Subscription Fields
 *
 * @package ArraySubs\Features\SubscriptionProducts
 * @var int $arraysubs_post_id Product post ID
 */

defined('ABSPATH') || exit;

$arraysubs_post_id = $post->ID ?? 0;
$arraysubs_is_subscription = get_post_meta($arraysubs_post_id, '_is_subscription', true);
$arraysubs_subscription_period = get_post_meta($arraysubs_post_id, '_subscription_period', true) ?: 'month';
$arraysubs_subscription_interval = get_post_meta($arraysubs_post_id, '_subscription_interval', true) ?: 1;
$arraysubs_subscription_length = get_post_meta($arraysubs_post_id, '_subscription_length', true) ?: 0;
$arraysubs_enable_renewal_price = get_post_meta($arraysubs_post_id, '_enable_renewal_price', true);
$arraysubs_renewal_price = get_post_meta($arraysubs_post_id, '_renewal_price', true);
$arraysubs_renewal_price_after = get_post_meta($arraysubs_post_id, '_renewal_price_after', true);
if (empty($arraysubs_renewal_price_after)) {
    $arraysubs_renewal_price_after = 1; // Default: after first period
}
?>

<div id="arraysubs_product_data" class="panel woocommerce_options_panel">
    <div class="options_group show_if_subscription" style="<?php echo $arraysubs_is_subscription !== 'yes' ? 'display:none;' : ''; ?>">
        <?php
        // Get product prices and sale schedule
        // Note: WooCommerce's get_price() returns the active price based on sale schedule
        $arraysubs_product_obj = wc_get_product($arraysubs_post_id);
        $arraysubs_regular_price = $arraysubs_product_obj ? $arraysubs_product_obj->get_regular_price() : '';
        $arraysubs_sale_price = $arraysubs_product_obj ? $arraysubs_product_obj->get_sale_price() : '';
        $arraysubs_sale_price_dates_from = $arraysubs_product_obj ? $arraysubs_product_obj->get_date_on_sale_from('edit') : null;
        $arraysubs_sale_price_dates_to = $arraysubs_product_obj ? $arraysubs_product_obj->get_date_on_sale_to('edit') : null;

        // Check if sale is currently active
        $arraysubs_is_on_sale = $arraysubs_product_obj ? $arraysubs_product_obj->is_on_sale() : false;
        ?>
        <p class="form-field">
            <label><?php echo esc_html__('Recurring Price per Billing Cycle', 'arraysubs'); ?></label>
            <span class="description" style="display: block; margin: 0 0 10px 0;">
                <?php echo esc_html__('The subscription will use the product\'s regular price and sale price (if set) for recurring billing.', 'arraysubs'); ?>
            </span>
            <span id="arraysubs-regular-price-display" style="display: block; margin-top: 5px;">
                <strong><?php echo esc_html__('Regular Price:', 'arraysubs'); ?></strong>
                <span class="price-value"><?php echo $arraysubs_regular_price ? wp_kses_post(wc_price($arraysubs_regular_price)) : '<em>' . esc_html__('Not set', 'arraysubs') . '</em>'; ?></span>
            </span>
            <span id="arraysubs-sale-price-display" style="display: <?php echo $arraysubs_sale_price ? 'block' : 'none'; ?>; margin-top: 5px;">
                <strong><?php echo esc_html__('Sale Price:', 'arraysubs'); ?></strong>
                <span class="price-value"><?php echo $arraysubs_sale_price ? wp_kses_post(wc_price($arraysubs_sale_price)) : ''; ?></span>
                <span class="sale-schedule" style="display: <?php echo ($arraysubs_sale_price_dates_from || $arraysubs_sale_price_dates_to) ? 'inline' : 'none'; ?>; margin-left: 8px; font-size: 0.9em; color: #666;">
                    <?php
                    if ($arraysubs_sale_price_dates_from || $arraysubs_sale_price_dates_to) {
                        $arraysubs_from_text = $arraysubs_sale_price_dates_from ? date_i18n(get_option('date_format'), $arraysubs_sale_price_dates_from->getTimestamp()) : '';
                        $arraysubs_to_text = $arraysubs_sale_price_dates_to ? date_i18n(get_option('date_format'), $arraysubs_sale_price_dates_to->getTimestamp()) : '';

                        if ($arraysubs_from_text && $arraysubs_to_text) {
                            /* translators: 1: from date, 2: to date */
                            echo '<em>' . sprintf(esc_html__('(%1$s - %2$s)', 'arraysubs'), esc_html($arraysubs_from_text), esc_html($arraysubs_to_text)) . '</em>';
                        } elseif ($arraysubs_from_text) {
                            /* translators: %s: from date */
                            echo '<em>' . sprintf(esc_html__('(From %s)', 'arraysubs'), esc_html($arraysubs_from_text)) . '</em>';
                        } elseif ($arraysubs_to_text) {
                            /* translators: %s: to date */
                            echo '<em>' . sprintf(esc_html__('(Until %s)', 'arraysubs'), esc_html($arraysubs_to_text)) . '</em>';
                        }
                    }
                    ?>
                </span>
            </span>
        </p>
        <?php
        woocommerce_wp_select([
            'id'          => '_subscription_period',
            'label'       => __('Billing Period', 'arraysubs'),
            'options'     => [
                'day'      => __('Day', 'arraysubs'),
                'week'     => __('Week', 'arraysubs'),
                'month'    => __('Month', 'arraysubs'),
                'year'     => __('Year', 'arraysubs'),
                'lifetime' => __('Lifetime Deal', 'arraysubs'),
            ],
            'value'       => $arraysubs_subscription_period,
        ]);

        woocommerce_wp_text_input([
            'id'                => '_subscription_interval',
            'label'             => __('Billing Interval', 'arraysubs'),
            'description'       => __('Charge every X periods', 'arraysubs'),
            'type'              => 'number',
            'custom_attributes' => [
                'min'  => '1',
                'max'  => '12',
                'step' => '1',
            ],
            'value'             => $arraysubs_subscription_interval,
        ]);

        woocommerce_wp_text_input([
            'id'                => '_subscription_length',
            'label'             => __('Subscription Length', 'arraysubs'),
            'description'       => __('Number of billing cycles (0 = never expires)', 'arraysubs'),
            'type'              => 'number',
            'custom_attributes' => [
                'min'  => '0',
                'max'  => '365',
                'step' => '1',
            ],
            'value'             => $arraysubs_subscription_length,
        ]);
        ?>
    </div>

    <div class="options_group show_if_subscription" style="<?php echo $arraysubs_is_subscription !== 'yes' ? 'display:none;' : ''; ?>">
        <?php
        echo '<p class="form-field arraysubs-fixed-end-heading"><strong>' . esc_html__('Free Trial', 'arraysubs') . '</strong></p>';

        woocommerce_wp_text_input([
            'id'                => '_trial_length',
            'label'             => __('Trial Length', 'arraysubs'),
            'description'       => __('Number of trial periods (0 = no trial)', 'arraysubs'),
            'type'              => 'number',
            'custom_attributes' => [
                'min'  => '0',
                'step' => '1',
            ],
            'value'             => get_post_meta($arraysubs_post_id, '_trial_length', true) ?: 0,
        ]);

        woocommerce_wp_select([
            'id'          => '_trial_period',
            'label'       => __('Trial Period', 'arraysubs'),
            'options'     => [
                'day'   => __('Day', 'arraysubs'),
                'week'  => __('Week', 'arraysubs'),
                'month' => __('Month', 'arraysubs'),
                'year'  => __('Year', 'arraysubs'),
            ],
            'value'       => get_post_meta($arraysubs_post_id, '_trial_period', true) ?: 'day',
        ]);
        ?>
    </div>

    <div class="options_group show_if_subscription" style="<?php echo $arraysubs_is_subscription !== 'yes' ? 'display:none;' : ''; ?>">
        <?php
        echo '<p class="form-field arraysubs-fixed-end-heading"><strong>' . esc_html__('Sign-up Fee', 'arraysubs') . '</strong></p>';

        woocommerce_wp_text_input([
            'id'                => '_signup_fee',
            'label'             => __('Sign-up Fee', 'arraysubs') . ' (' . get_woocommerce_currency_symbol() . ')',
            'description'       => __('One-time fee charged at signup (independent of trial)', 'arraysubs'),
            'type'              => 'number',
            'custom_attributes' => [
                'step' => '0.01',
                'min'  => '0',
            ],
            'value'             => get_post_meta($arraysubs_post_id, '_signup_fee', true),
        ]);
        ?>
    </div>

    <div class="options_group show_if_subscription" style="<?php echo $arraysubs_is_subscription !== 'yes' ? 'display:none;' : ''; ?>">
        <?php
        woocommerce_wp_checkbox([
            'id'          => '_enable_renewal_price',
            'label'       => __('Different Renewal Price', 'arraysubs'),
            'description' => __('Set a different price for subsequent renewals', 'arraysubs'),
            'desc_tip'    => true,
        ]);

        woocommerce_wp_text_input([
            'id'                => '_renewal_price',
            'label'             => __('Renewal Price', 'arraysubs') . ' (' . get_woocommerce_currency_symbol() . ')',
            'description'       => __('Price for renewals after specified billing cycles', 'arraysubs'),
            'type'              => 'number',
            'custom_attributes' => [
                'step' => '0.01',
                'min'  => '0',
            ],
            'value'             => $arraysubs_renewal_price,
            'wrapper_class'     => 'show_if_renewal_price' . ($arraysubs_enable_renewal_price !== 'yes' ? ' hidden' : ''),
        ]);

        woocommerce_wp_text_input([
            'id'                => '_renewal_price_after',
            'label'             => __('Apply Renewal Price After', 'arraysubs'),
            'description'       => __('Number of billing periods after which renewal price takes effect', 'arraysubs'),
            'type'              => 'number',
            'custom_attributes' => [
                'step' => '1',
                'min'  => '1',
            ],
            'value'             => $arraysubs_renewal_price_after,
            'wrapper_class'     => 'show_if_renewal_price' . ($arraysubs_enable_renewal_price !== 'yes' ? ' hidden' : ''),
            'desc_tip'          => true,
        ]);
        ?>
    </div>

    <?php
    /**
     * Allow pro features to inject subscription product fields before the shipping section.
     *
     * @param int $arraysubs_post_id Product ID.
     */
    do_action('arraysubs_subscription_product_fields_before_shipping', $arraysubs_post_id);

    // Shipping settings section - only for physical products when pro shipping module is active
    $arraysubs_has_shipping_module = function_exists('arraysubs_has_module') && arraysubs_has_module('SubscriptionShipping') === 1;
    ?>
    <?php if ($arraysubs_has_shipping_module) : ?>
        <?php
        $arraysubs_product_obj = wc_get_product($arraysubs_post_id);
        $arraysubs_is_physical = $arraysubs_product_obj && !$arraysubs_product_obj->is_virtual() && !$arraysubs_product_obj->is_downloadable();
        $arraysubs_shipping_type = get_post_meta($arraysubs_post_id, '_arraysubs_shipping_type', true) ?: 'recurring';
        $arraysubs_initial_shipping_override = get_post_meta($arraysubs_post_id, '_arraysubs_initial_shipping_override', true);
        $arraysubs_renewal_shipping_override = get_post_meta($arraysubs_post_id, '_arraysubs_renewal_shipping_override', true);
        ?>
        <div class="options_group show_if_subscription arraysubs-shipping-section" style="<?php echo ($arraysubs_is_subscription !== 'yes' || !$arraysubs_is_physical) ? 'display:none;' : ''; ?>">
            <?php
            echo '<p class="form-field arraysubs-fixed-end-heading"><strong>' . esc_html__('Subscription Shipping', 'arraysubs') . '</strong></p>';
            echo '<p class="form-field description" style="margin-left: 0; font-style: italic;">' . esc_html__('These settings only apply to physical products (not virtual or downloadable).', 'arraysubs') . '</p>';

            woocommerce_wp_select([
                'id'          => '_arraysubs_shipping_type',
                'label'       => __('Shipping Type', 'arraysubs'),
                'description' => __('Choose how shipping is charged for subscription renewals', 'arraysubs'),
                'desc_tip'    => true,
                'options'     => [
                    'recurring' => __('Recurring - Charge shipping on every renewal', 'arraysubs'),
                    'one-time'  => __('One-time - Charge shipping only on first order', 'arraysubs'),
                ],
                'value'       => $arraysubs_shipping_type,
            ]);

            woocommerce_wp_text_input([
                'id'                => '_arraysubs_initial_shipping_override',
                'label'             => __('Initial Shipping Override', 'arraysubs') . ' (' . get_woocommerce_currency_symbol() . ')',
                'description'       => __('Set to 0 to use WooCommerce shipping calculation', 'arraysubs'),
                'desc_tip'          => true,
                'type'              => 'number',
                'custom_attributes' => [
                    'step' => '0.01',
                    'min'  => '0',
                ],
                'value'             => $arraysubs_initial_shipping_override,
                'placeholder'       => __('0 = WooCommerce shipping', 'arraysubs'),
            ]);

            woocommerce_wp_text_input([
                'id'                => '_arraysubs_renewal_shipping_override',
                'label'             => __('Renewal Shipping Override', 'arraysubs') . ' (' . get_woocommerce_currency_symbol() . ')',
                'description'       => __('Set to 0 to use the same as initial shipping', 'arraysubs'),
                'desc_tip'          => true,
                'type'              => 'number',
                'custom_attributes' => [
                    'step' => '0.01',
                    'min'  => '0',
                ],
                'value'             => $arraysubs_renewal_shipping_override,
                'placeholder'       => __('0 = Same as initial shipping', 'arraysubs'),
                'wrapper_class'     => 'show_if_recurring_shipping' . ($arraysubs_shipping_type !== 'recurring' ? ' hidden' : ''),
            ]);
            ?>
        </div>
    <?php endif; ?>

</div>