<?php

defined( 'ABSPATH' ) or exit;

$is_custom_url = !array_key_exists( $offer->get_image_url( 'edit' ), pwlpar_default_logos() );

?>
<form id="pwlpara-offer-edit-form" method="post" data-action="save_offer">
    <input type="hidden" name="offer_id" value="<?php echo esc_attr( $offer->get_id() ); ?>">
    <div style="display: flex;">
        <div>
            <fieldset>
                <div class="pwlpara-fieldset">
                    <div class="pwlpara-form-field">
                        <label>
                            <input type="checkbox" id="pwlpara-offer-edit-active" name="active" <?php checked( $offer->get_status( 'edit' ) == 'publish' ); ?>>
                            <?php esc_html_e( 'Offer is active', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?>
                            <?php echo wp_kses_post( wc_help_tip( esc_html__( 'This Offer will appear on the Rewards dashboard to be claimed by the customer when it is active and the Begin Date and End Date restrictions have been met.', 'pw-loyalty-points-and-rewards-for-woocommerce' ) ) ); ?>
                        </label>
                    </div>

                    <div class="pwlpara-form-field">
                        <label>
                            <?php esc_html_e( 'Name', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?><br />
                            <input type="text" id="pwlpara-offer-edit-title" name="title" style="width: 250px;" value="<?php echo esc_attr( $offer->get_title( 'edit' ) ); ?>" required>
                        </label>
                    </div>

                    <div class="pwlpara-form-field">
                        <label>
                            <?php esc_html_e( 'Description', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?>
                            <?php echo wp_kses_post( wc_help_tip( esc_html__( 'Shown to the customer on the Rewards dashboard. HTML is allowed.', 'pw-loyalty-points-and-rewards-for-woocommerce' ) ) ); ?><br />
                            <textarea id="pwlpara-offer-edit-description" class="pwlpara-offer-edit-textarea" name="description" rows="4"><?php echo esc_html( $offer->get_description( 'edit' ) ); ?></textarea>
                        </label>
                    </div>

                    <div class="pwlpara-form-field">
                        <label>
                            <?php esc_html_e( 'Points', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?>
                            <?php echo wp_kses_post( wc_help_tip( esc_html__( 'Number of rewards points required to claim this Offer. Set to zero or leave blank if this reward is free.', 'pw-loyalty-points-and-rewards-for-woocommerce' ) ) ); ?><br />
                            <input type="number" id="pwlpara-offer-edit-points" name="points" class="short" min="0" value="<?php echo esc_attr( $offer->get_points( 'edit' ) ); ?>">
                        </label>
                    </div>
                </div>
            </fieldset>
        </div>
        <div id="pwlpara-offer-edit-preview">
            <?php
                pwlpar_offer( array(
                    'offer' => $offer,
                    'balance' => PHP_INT_MAX,
                ) );
            ?>
            <div class="pwlpara-preview-label">
                <?php esc_html_e( 'This is only a preview. The site Theme might override the styles on the front end website.', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?>
            </div>
        </div>
    </div>

    <div class="pwlpara-pro-feature">
        <?php
            pwlpara_pro_feature_notice( 'offer-edit', '50px', '250px', '', false );
        ?>
        <fieldset>
            <div class="pwlpara-form-field">
                <label>
                    <?php esc_html_e( 'Category', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?>
                    <br />
                    <select id="pwlpara-offer-edit-category-id">
                        <option value="0"><?php esc_html_e( 'None', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?></option>
                    </select>
                </label>
            </div>

            <div class="pwlpara-form-field">
                <label>
                    <?php esc_html_e( 'Image', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?><br />
                    <select>
                        <?php
                            foreach ( pwlpar_default_logos() as $url => $name ) {
                                ?>
                                <option value="<?php echo esc_attr( $url ); ?>" <?php selected( $url, $offer->get_image_url( 'edit' ) ); ?>><?php echo esc_html( $name ); ?></option>
                                <?php
                            }
                        ?>
                    </select>
                </label>
            </div>

            <div class="pwlpara-form-field">
                <label>
                    <?php esc_html_e( 'Terms and conditions', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?>
                    <?php echo wp_kses_post( wc_help_tip( esc_html__( 'Shown to the customer on the Rewards dashboard when they click the button to view the terms. HTML is allowed.', 'pw-loyalty-points-and-rewards-for-woocommerce' ) ) ); ?><br />
                    <textarea id="pwlpara-offer-edit-terms" class="pwlpara-offer-edit-textarea" name="terms" rows="4"></textarea>
                </label>
            </div>

            <div class="pwlpara-form-field">
                <label>
                    <?php esc_html_e( 'Begin date', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?>
                    <?php echo wp_kses_post( wc_help_tip( esc_html__( 'Optional. This Offer will not appear on the Rewards dashboard until this date has been reached.', 'pw-loyalty-points-and-rewards-for-woocommerce' ) ) ); ?><br />
                    <input type="text" id="pwlpara-offer-edit-begin-date" value="" placeholder="YYYY-MM-DD">
                    <input type="time" id="pwlpara-offer-edit-begin-time" value="">
                    <a href="#" class="pwlpara-offer-edit-date-reset-button button"><?php esc_html_e( 'Clear', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?></a>
                </label>
            </div>

            <div class="pwlpara-form-field">
                <label>
                    <?php esc_html_e( 'End date', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?>
                    <?php echo wp_kses_post( wc_help_tip( esc_html__( 'Optional. This Offer will not appear on the Rewards dashboard once this date has passed.', 'pw-loyalty-points-and-rewards-for-woocommerce' ) ) ); ?><br />
                    <input type="text" value="" placeholder="YYYY-MM-DD">
                    <input type="time" value="">
                    <a href="#" class="button"><?php esc_html_e( 'Clear', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?></a>
                </label>
            </div>
        </fieldset>
    </div>

    <fieldset>
        <div class="pwlpara-fieldset">
            <div class="pwlpara-fieldset-title" style="margin-top: 32px;">
                <?php esc_html_e( 'Reward coupon type', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?>
            </div>

            <div class="pwlpara-form-field">
                <div class="pwlpara-offer-edit-coupon-types">
                    <?php foreach( pwlpar_get_offer_coupon_types() as $key => $label ) : ?>
                        <label class="pwlpara-offer-edit-coupon-type <?php echo $offer->get_coupon_type( 'edit' ) == $key ? 'pwlpara-offer-edit-coupon-type-active' : ''; ?>" data-coupon-type="<?php echo esc_attr( $key ); ?>">
                            <input type="radio" class="pwlpara-offer-edit-coupon-type-radio-button" name="coupon_type" value="<?php echo esc_attr( $key ); ?>" <?php checked( $offer->get_coupon_type( 'edit' ), $key ); ?>>
                            <span class="pwlpara-offer-edit-coupon-type-title"><?php echo esc_html( $label ); ?></span>
                            <div class="pwlpara-offer-edit-coupon-type-text"><?php echo esc_html( pwlpar_get_offer_coupon_type_description( $key ) ); ?></div>
                        </label>
                    <?php endforeach; ?>
                </div>
            </div>

            <div id="pwlpara-offer-coupon-settings">
                <div>
                    <div class="pwlpara-form-field">
                        <label>
                            <?php esc_html_e( 'Coupon amount', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?>
                            <span>(<?php echo wp_kses_post( get_woocommerce_currency_symbol() ); ?>)</span>
                            <br />
                            <input type="text" id="pwlpara-offer-edit-coupon-amount" name="coupon_amount" class="short wc_input_price" value="<?php echo esc_attr( $offer->get_coupon_amount( 'edit' ) ); ?>">
                        </label>
                    </div>
                </div>
                <div>
                    <div class="pwlpara-form-field">
                        <label>
                            <input type="checkbox" id="pwlpara-offer-edit-free-shipping" name="free_shipping" <?php checked( $offer->get_free_shipping() ); ?>>
                            <?php esc_html_e( 'Allow free shipping', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?>
                        </label>
                    </div>

                    <div class="pwlpara-form-field">
                        <label>
                            <input type="checkbox" id="pwlpara-offer-edit-individual-use" name="individual_use" <?php checked( $offer->get_individual_use() ); ?>>
                            <?php esc_html_e( 'Individual use only', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?>
                        </label>
                    </div>

                    <div class="pwlpara-form-field">
                        <label>
                            <input type="checkbox" id="pwlpara-offer-edit-exclude-sale-items" name="exclude_sale_items" <?php checked( $offer->get_exclude_sale_items() ); ?>>
                            <?php esc_html_e( 'Exclude sale items', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?>
                        </label>
                    </div>

                    <div class="pwlpara-form-field">
                        <label>
                            <?php esc_html_e( 'Products', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?><br />
                            <select class="wc-product-search" multiple="multiple" style="width: 50%;" name="product_ids[]" data-placeholder="<?php echo esc_attr( 'Search for a product&hellip;', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?>" data-action="woocommerce_json_search_products_and_variations">
                                <?php
                                    $product_ids = $offer->get_product_ids();

                                    foreach ( $product_ids as $product_id ) {
                                        $product = wc_get_product( $product_id );
                                        if ( is_object( $product ) ) {
                                            echo '<option value="' . esc_attr( $product_id ) . '"' . selected( true, true, false ) . '>' . esc_html( wp_strip_all_tags( $product->get_formatted_name() ) ) . '</option>';
                                        }
                                    }
                                ?>
                            </select>
                            <?php echo wp_kses_post( wc_help_tip( esc_html__( 'Products that the coupon will be applied to, or that need to be in the cart in order for the "Fixed cart discount" to be applied.', 'pw-loyalty-points-and-rewards-for-woocommerce' ) ) ); ?>
                        </label>
                    </div>

                    <div class="pwlpara-form-field">
                        <label>
                            <?php esc_html_e( 'Exclude products', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?><br />
                            <select class="wc-product-search" multiple="multiple" style="width: 50%;" name="excluded_product_ids[]" data-placeholder="<?php echo esc_attr( 'Search for a product&hellip;', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?>" data-action="woocommerce_json_search_products_and_variations">
                                <?php
                                    $product_ids = $offer->get_excluded_product_ids();

                                    foreach ( $product_ids as $product_id ) {
                                        $product = wc_get_product( $product_id );
                                        if ( is_object( $product ) ) {
                                            echo '<option value="' . esc_attr( $product_id ) . '"' . selected( true, true, false ) . '>' . esc_html( wp_strip_all_tags( $product->get_formatted_name() ) ) . '</option>';
                                        }
                                    }
                                ?>
                            </select>
                            <?php echo wp_kses_post( wc_help_tip( esc_html__( 'Products that the coupon will not be applied to, or that cannot be in the cart in order for the "Fixed cart discount" to be applied.', 'pw-loyalty-points-and-rewards-for-woocommerce' ) ) ); ?>
                        </label>
                    </div>

                    <div class="pwlpara-form-field">
                        <label>
                            <?php esc_html_e( 'Categories', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?><br />
                            <select name="category_ids[]" style="width: 50%;"  class="wc-enhanced-select" multiple="multiple" data-placeholder="<?php echo esc_attr( 'Any category', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?>">
                                <?php
                                    $category_ids = $offer->get_product_categories();
                                    $categories   = get_terms( 'product_cat' );

                                    if ( $categories ) {
                                        foreach ( $categories as $cat ) {
                                            echo '<option value="' . esc_attr( $cat->term_id ) . '"' . esc_html( wc_selected( $cat->term_id, $category_ids ) ) . '>' . esc_html( $cat->name ) . '</option>';
                                        }
                                    }
                                ?>
                            </select>
                            <?php echo wp_kses_post( wc_help_tip( esc_html__( 'Product categories that the coupon will be applied to, or that need to be in the cart in order for the "Fixed cart discount" to be applied.', 'pw-loyalty-points-and-rewards-for-woocommerce' ) ) ); ?>
                        </label>
                    </div>

                    <div class="pwlpara-form-field">
                        <label>
                            <?php esc_html_e( 'Exclude categories', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?><br />
                            <select name="excluded_category_ids[]" style="width: 50%;"  class="wc-enhanced-select" multiple="multiple" data-placeholder="<?php echo esc_attr( 'Any category', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?>">
                                <?php
                                    $category_ids = $offer->get_excluded_product_categories();
                                    $categories   = get_terms( 'product_cat' );

                                    if ( $categories ) {
                                        foreach ( $categories as $cat ) {
                                            echo '<option value="' . esc_attr( $cat->term_id ) . '"' . esc_html( wc_selected( $cat->term_id, $category_ids ) ) . '>' . esc_html( $cat->name ) . '</option>';
                                        }
                                    }
                                ?>
                            </select>
                            <?php echo wp_kses_post( wc_help_tip( esc_html__( 'Product categories that the coupon will be applied to, or that need to be in the cart in order for the "Fixed cart discount" to be applied.', 'pw-loyalty-points-and-rewards-for-woocommerce' ) ) ); ?>
                        </label>
                    </div>
                </div>

                <div class="pwlpara-pro-feature">
                    <?php
                        pwlpara_pro_feature_notice( 'offer-edit-coupon', '25px', '250px', '', false );
                    ?>
                    <div class="pwlpara-form-field">
                        <label>
                            <?php esc_html_e( 'Coupon will expire X days after being claimed', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?>
                            <?php echo wp_kses_post( wc_help_tip( esc_html__( 'Automatically creates an expiration date on the Reward for X days after it is claimed. Leave blank and there will not be an expiration date on the Reward.', 'pw-loyalty-points-and-rewards-for-woocommerce' ) ) ); ?><br />
                            <input type="number" id="pwlpara-offer-edit-expires-in-days" name="expires_in_days" class="short" min="0" value="365">
                        </label>
                    </div>

                    <div class="pwlpara-form-field">
                        <label>
                            <?php esc_html_e( 'Limit per customer', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?>
                            <?php echo wp_kses_post( wc_help_tip( esc_html__( 'How many times this offer can be redeemed by an individual rewards member.', 'pw-loyalty-points-and-rewards-for-woocommerce' ) ) ); ?><br />
                            <input type="number" id="pwlpara-offer-edit-limit-per-customer" name="limit_per_customer" class="short" min="0" value="1">
                        </label>
                    </div>

                    <div class="pwlpara-form-field">
                        <label>
                            <input type="checkbox" id="pwlpara-offer-edit-limit-reset" name="limit_reset" checked>
                            <?php esc_html_e( 'Allow limit to reset', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?>
                            <?php echo wp_kses_post( wc_help_tip( esc_html__( 'Customers can claim this offer again after a designated waiting period, providing a wonderful incentive for them to come back to your shop!', 'pw-loyalty-points-and-rewards-for-woocommerce' ) ) ); ?>
                        </label>
                    </div>

                    <div id="pwlpara-offer-edit-limit-reset-container">
                        <div class="pwlpara-form-field">
                            <label>
                                <?php esc_html_e( 'Reset after', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?><br />
                                <input type="number" id="pwlpara-offer-edit-limit-reset-amount" name="limit_reset_amount" class="short" min="1" value="1">
                                    <select id="pwlpara-offer-edit-limit-reset-period" name="limit_reset_period">
                                        <option><?php esc_html_e( 'Days', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?></option>
                                </select>
                            </label>
                        </div>
                    </div>
                </div>
            </div>

            <div id="pwlpara-offer-pro-coupon-type" style="display: none; padding: 16px 32px;">
                <?php
                    printf(
                        /* translators: %s: link to Pro version */
                        esc_html__( 'This reward coupon type is only available in %s', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
                        '<br><strong><a href="https://www.pimwick.com/pw-loyalty-points-and-rewards-for-woocommerce/" target="_blank">PW Loyalty Points and Rewards for WooCommerce Pro</a></strong>.'
                    );
                ?>
                <div style="margin-top: 16px;">
                    <a href="https://www.pimwick.com/pw-loyalty-points-and-rewards-for-woocommerce/" class="pwlpara-pro-banner-link" target="_blank"><img src="<?php echo esc_attr( pwlpar_relative_url( '/assets/images/banner-772x250.jpg' ) ); ?>" alt="PW Loyalty Points and Rewards for WooCommerce Pro" class="pwlpara-pro-banner"></a>
                </div>
            </div>
        </div>
    </fieldset>

    <fieldset>
        <div class="pwlpara-fieldset">
            <div id="pwlpara-offer-edit-form-message"></div>
            <div id="pwlpara-offer-edit-form-buttons-container">
                <input type="submit" class="button button-primary" value="<?php esc_attr_e( 'Save offer', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?>">
                <a href="<?php echo esc_attr( pwlpara_admin_url( 'offers' ) ); ?>" class="pwlpara-cancel-button button button-secondary"><?php esc_html_e( 'Cancel', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?></a>
            </div>
        </div>
    </fieldset>
</form>
