<?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
}

// 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;
?>

<div id="arraysubs_product_data" class="panel woocommerce_options_panel">
    <div class="arraysubs-variation-fields arraysubs-simple-fields">
        <div class="show_if_subscription" style="<?php echo $arraysubs_is_subscription !== 'yes' ? 'display:none;' : ''; ?>">
            <!-- Recurring Price Info -->
            <div class="arraysubs-variation-section">
                <div class="arraysubs-variation-price-info">
                    <span class="arraysubs-section-description">
                        <?php echo esc_html__('The subscription will use the product\'s regular price and sale price (if set) for recurring billing.', 'arraysubs'); ?>
                    </span>
                    <div class="arraysubs-price-display">
                        <span id="arraysubs-regular-price-display" class="arraysubs-variation-regular-price-display">
                            <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" class="arraysubs-variation-sale-price-display" style="display: <?php echo $arraysubs_sale_price ? 'inline-flex' : 'none'; ?>;">
                            <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'; ?>;">
                                <?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>
                    </div>
                </div>
            </div>

            <!-- Billing Schedule -->
            <div class="arraysubs-variation-section">
                <div class="arraysubs-variation-row">
                    <div class="arraysubs-variation-col arraysubs-variation-col-half">
                        <label for="_subscription_period"><?php esc_html_e('Billing Period', 'arraysubs'); ?></label>
                        <select id="_subscription_period" name="_subscription_period">
                            <option value="day" <?php selected($arraysubs_subscription_period, 'day'); ?>><?php esc_html_e('Day', 'arraysubs'); ?></option>
                            <option value="week" <?php selected($arraysubs_subscription_period, 'week'); ?>><?php esc_html_e('Week', 'arraysubs'); ?></option>
                            <option value="month" <?php selected($arraysubs_subscription_period, 'month'); ?>><?php esc_html_e('Month', 'arraysubs'); ?></option>
                            <option value="year" <?php selected($arraysubs_subscription_period, 'year'); ?>><?php esc_html_e('Year', 'arraysubs'); ?></option>
                            <option value="lifetime" <?php selected($arraysubs_subscription_period, 'lifetime'); ?>><?php esc_html_e('Lifetime Deal', 'arraysubs'); ?></option>
                        </select>
                    </div>
                    <div class="arraysubs-variation-col arraysubs-variation-col-half">
                        <div class="arraysubs-variation-col-split">
                            <div class="arraysubs-variation-col-split-item">
                                <label for="_subscription_interval"><?php esc_html_e('Billing Interval', 'arraysubs'); ?></label>
                                <input type="number"
                                    id="_subscription_interval"
                                    min="1"
                                    max="12"
                                    step="1"
                                    name="_subscription_interval"
                                    value="<?php echo esc_attr($arraysubs_subscription_interval); ?>" />
                                <span class="description"><?php esc_html_e('Charge every X periods', 'arraysubs'); ?></span>
                            </div>
                            <div class="arraysubs-variation-col-split-item">
                                <label for="_subscription_length"><?php esc_html_e('Subscription Length', 'arraysubs'); ?></label>
                                <input type="number"
                                    id="_subscription_length"
                                    min="0"
                                    max="365"
                                    step="1"
                                    name="_subscription_length"
                                    value="<?php echo esc_attr($arraysubs_subscription_length); ?>" />
                                <span class="description"><?php esc_html_e('Number of billing cycles (0 = never expires)', 'arraysubs'); ?></span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Trial Settings -->
            <div class="arraysubs-variation-section">
                <div class="arraysubs-section-title"><?php esc_html_e('Trial Settings', 'arraysubs'); ?></div>
                <div class="arraysubs-variation-row">
                    <div class="arraysubs-variation-col arraysubs-variation-col-half">
                        <label for="_trial_length"><?php esc_html_e('Trial Length', 'arraysubs'); ?></label>
                        <input type="number"
                            id="_trial_length"
                            min="0"
                            step="1"
                            name="_trial_length"
                            value="<?php echo esc_attr(get_post_meta($arraysubs_post_id, '_trial_length', true) ?: 0); ?>" />
                        <span class="description"><?php esc_html_e('Number of trial periods (0 = no trial)', 'arraysubs'); ?></span>
                    </div>
                    <div class="arraysubs-variation-col arraysubs-variation-col-half">
                        <label for="_trial_period"><?php esc_html_e('Trial Period', 'arraysubs'); ?></label>
                        <select id="_trial_period" name="_trial_period">
                            <option value="day" <?php selected(get_post_meta($arraysubs_post_id, '_trial_period', true) ?: 'day', 'day'); ?>><?php esc_html_e('Day', 'arraysubs'); ?></option>
                            <option value="week" <?php selected(get_post_meta($arraysubs_post_id, '_trial_period', true) ?: 'day', 'week'); ?>><?php esc_html_e('Week', 'arraysubs'); ?></option>
                            <option value="month" <?php selected(get_post_meta($arraysubs_post_id, '_trial_period', true) ?: 'day', 'month'); ?>><?php esc_html_e('Month', 'arraysubs'); ?></option>
                            <option value="year" <?php selected(get_post_meta($arraysubs_post_id, '_trial_period', true) ?: 'day', 'year'); ?>><?php esc_html_e('Year', 'arraysubs'); ?></option>
                        </select>
                    </div>
                </div>
            </div>

            <!-- Sign-up Fee -->
            <div class="arraysubs-variation-section">
                <div class="arraysubs-section-title"><?php esc_html_e('Sign-up Fee', 'arraysubs'); ?></div>
                <div class="arraysubs-variation-row">
                    <div class="arraysubs-variation-col arraysubs-variation-col-half">
                        <label for="_signup_fee">
                            <?php esc_html_e('Sign-up Fee', 'arraysubs'); ?>
                            <?php echo '(' . esc_html(get_woocommerce_currency_symbol()) . ')'; ?>
                        </label>
                        <input type="number"
                            id="_signup_fee"
                            step="0.01"
                            min="0"
                            name="_signup_fee"
                            value="<?php echo esc_attr(get_post_meta($arraysubs_post_id, '_signup_fee', true)); ?>"
                            class="wc_input_price" />
                        <span class="description"><?php esc_html_e('One-time fee charged at signup (independent of trial)', 'arraysubs'); ?></span>
                    </div>
                    <div class="arraysubs-variation-col arraysubs-variation-col-half">
                        <!-- Empty column for layout consistency -->
                    </div>
                </div>
            </div>

            <!-- Different Renewal Price -->
            <div class="arraysubs-variation-section">
                <div class="arraysubs-section-title">
                    <label class="arraysubs-checkbox-label">
                        <input type="checkbox"
                            id="_enable_renewal_price"
                            name="_enable_renewal_price"
                            value="yes"
                            class="checkbox"
                            <?php checked($arraysubs_enable_renewal_price, 'yes'); ?> />
                        <?php esc_html_e('Different Renewal Price', 'arraysubs'); ?>
                    </label>
                </div>
                <div class="show_if_renewal_price<?php echo $arraysubs_enable_renewal_price !== 'yes' ? ' hidden' : ''; ?>">
                    <div class="arraysubs-variation-row">
                        <div class="arraysubs-variation-col arraysubs-variation-col-half">
                            <label for="_renewal_price">
                                <?php esc_html_e('Renewal Price', 'arraysubs'); ?>
                                <?php echo '(' . esc_html(get_woocommerce_currency_symbol()) . ')'; ?>
                            </label>
                            <input type="number"
                                id="_renewal_price"
                                step="0.01"
                                min="0"
                                name="_renewal_price"
                                value="<?php echo esc_attr($arraysubs_renewal_price); ?>"
                                class="wc_input_price" />
                            <span class="description"><?php esc_html_e('Price for renewals after specified billing cycles', 'arraysubs'); ?></span>
                        </div>
                        <div class="arraysubs-variation-col arraysubs-variation-col-half">
                            <label for="_renewal_price_after"><?php esc_html_e('Apply Renewal Price After', 'arraysubs'); ?></label>
                            <input type="number"
                                id="_renewal_price_after"
                                step="1"
                                min="1"
                                name="_renewal_price_after"
                                value="<?php echo esc_attr($arraysubs_renewal_price_after); ?>" />
                            <span class="description"><?php esc_html_e('Number of billing periods after which renewal price takes effect', 'arraysubs'); ?></span>
                        </div>
                    </div>
                </div>
            </div>
        </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_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);
            ?>
            <!-- Subscription Shipping -->
            <div class="arraysubs-variation-section show_if_subscription arraysubs-shipping-section" style="<?php echo ($arraysubs_is_subscription !== 'yes' || !$arraysubs_is_physical) ? 'display:none;' : ''; ?>">
                <div class="arraysubs-section-title"><?php esc_html_e('Subscription Shipping', 'arraysubs'); ?></div>
                <span class="arraysubs-section-description">
                    <?php esc_html_e('These settings only apply to physical products (not virtual or downloadable).', 'arraysubs'); ?>
                </span>
                <div class="arraysubs-variation-row">
                    <div class="arraysubs-variation-col arraysubs-variation-col-full">
                        <label for="_arraysubs_shipping_type"><?php esc_html_e('Shipping Type', 'arraysubs'); ?></label>
                        <select id="_arraysubs_shipping_type" name="_arraysubs_shipping_type" class="arraysubs-shipping-type-select">
                            <option value="recurring" <?php selected($arraysubs_shipping_type, 'recurring'); ?>>
                                <?php esc_html_e('Recurring - Charge shipping on every renewal', 'arraysubs'); ?>
                            </option>
                            <option value="one-time" <?php selected($arraysubs_shipping_type, 'one-time'); ?>>
                                <?php esc_html_e('One-time - Charge shipping only on first order', 'arraysubs'); ?>
                            </option>
                        </select>
                        <span class="description"><?php esc_html_e('Choose how shipping is charged for subscription renewals', 'arraysubs'); ?></span>
                    </div>
                </div>
                <div class="arraysubs-variation-row" style="margin-top: 10px;">
                    <div class="arraysubs-variation-col arraysubs-variation-col-half">
                        <label for="_arraysubs_initial_shipping_override">
                            <?php esc_html_e('Initial Shipping Override', 'arraysubs'); ?>
                            <?php echo '(' . esc_html(get_woocommerce_currency_symbol()) . ')'; ?>
                        </label>
                        <input type="number"
                            id="_arraysubs_initial_shipping_override"
                            step="0.01"
                            min="0"
                            name="_arraysubs_initial_shipping_override"
                            value="<?php echo esc_attr($arraysubs_initial_shipping_override); ?>"
                            placeholder="<?php esc_attr_e('0 = WooCommerce shipping', 'arraysubs'); ?>" />
                        <span class="description"><?php esc_html_e('Set to 0 to use WooCommerce shipping calculation', 'arraysubs'); ?></span>
                    </div>
                    <div class="arraysubs-variation-col arraysubs-variation-col-half show_if_recurring_shipping<?php echo $arraysubs_shipping_type !== 'recurring' ? ' hidden' : ''; ?>">
                        <label for="_arraysubs_renewal_shipping_override">
                            <?php esc_html_e('Renewal Shipping Override', 'arraysubs'); ?>
                            <?php echo '(' . esc_html(get_woocommerce_currency_symbol()) . ')'; ?>
                        </label>
                        <input type="number"
                            id="_arraysubs_renewal_shipping_override"
                            step="0.01"
                            min="0"
                            name="_arraysubs_renewal_shipping_override"
                            value="<?php echo esc_attr($arraysubs_renewal_shipping_override); ?>"
                            placeholder="<?php esc_attr_e('0 = Same as initial shipping', 'arraysubs'); ?>" />
                        <span class="description"><?php esc_html_e('Set to 0 to use the same as initial shipping', 'arraysubs'); ?></span>
                    </div>
                </div>
            </div>
        <?php endif; ?>
    </div>
</div>
