<?php

namespace WPAdverts\Stripe;

class Admin {
    
    /**
     * Path to this plugin files
     *
     * @var string
     */
    protected $_path = null;
    
    /**
     * Displays wp-admin / Classifieds / Options / Stripe panel
     * 
     * @since 1.0
     * @return void
     */
    public function page_options() {
        
        wp_enqueue_style( 'adverts-admin' );
    
        $flash = \Adverts_Flash::instance();
        $error = array();

        $options = get_option ( "wpadverts_stripe_config", array() );

        if( $options === null || empty( $options ) ) {
            $options = adverts_config( "stripe.ALL" );
        }

        include dirname( ADVERTS_PATH ) . '/wpadverts/addons/core/includes/admin-pages.php';

        $scheme = apply_filters( "adverts_form_scheme", $this->get_options_form_scheme(), array() );
        $form = new \Adverts_Form( $scheme );
        $form->bind( $options );

        $button_text = __("Update Options", "adverts");

        if(isset($_POST) && !empty($_POST)) {
            $form->bind( $_POST );
            $valid = $form->validate();

            if($valid) {   
                $values = $form->get_values();
                
                if( $form->get_value( 'send_receipts' ) == "1" ) {
                    $values['send_receipts'] = '1';
                } else {
                    $values['send_receipts'] = '0';
                }
                
                if( $form->get_value( 'live_confirm' ) == "1" ) {
                    $values['live_confirm'] = '1';
                } else {
                    $values['live_confirm'] = '0';
                }
                
                update_option("wpadverts_stripe_config", $values);
                $flash->add_info( __("Settings updated.", "adverts") );
            } else {
                $flash->add_error( __("There are errors in your form.", "adverts") );
            }
        }

        include Core::get_instance()->get_basedir() . '/admin/options.php';
    }
    
    /**
     * Returns options page form scheme
     * 
     * @since 1.0
     * @return array    Form scheme
     */
    public function get_options_form_scheme() {
        return array(
            "name" => "stripe_options",
            "action" => "",
            "field" => array(
                array(
                    "name"      => "_adverts_action",
                    "type"      => "adverts_field_hidden",
                    "order"     => 0,
                    "label"     => ""
                ),
                array(
                    "name"      => "_api_keys",
                    "type"      => "adverts_field_header",
                    "order"     => 9,
                    "label"     => __( 'API Keys', 'wpadverts-stripe' )
                ),
                array(
                    "name"      => "publishable_key",
                    "type"      => "adverts_field_text",
                    "label"     => __("Publishable Key", "wpadverts-stripe"),
                    "order"     => 10,
                    "meta"      => array(),
                    "attr"      => array(
                        "placeholder" => "pk_ ..."
                    )
                ),
                array(
                    "name"      => "secret_key",
                    "type"      => "adverts_field_text",
                    "label"     => __("Secret Key", "wpadverts-stripe"),
                    "order"     => 10,
                    "meta"      => array(),
                    "attr"      => array(
                        "placeholder" => "sk_ ..."
                    )
                ),
                array(
                    "name"      => "_signin_secrets",
                    "type"      => "adverts_field_header",
                    "order"     => 20,
                    "label"     => __( 'Payments Confirmation', 'wpadverts-stripe' )
                ),
                array(
                    "name"      => "_endpoint_url",
                    "type"      => "adverts_field_label",
                    "label"     => __("Endpoint URL", "wpadverts-stripe"),
                    "order"     => 21,
                    "content"     => '<code>'.admin_url( 'admin-ajax.php?action=wpadverts-stripe-webhook' ).'</code>',
                    "meta"      => array(),
                ),
                array(
                    "name"      => "payment_intent__succeeded",
                    "type"      => "adverts_field_text",
                    "label"     => __("Endpoint URL - Signing secret", "wpadverts-stripe"),
                    "order"     => 22,
                    "meta"      => array(),
                    "attr"      => array(
                        "placeholder" => "whsec_ ..."
                    ),
                    "hint" => __( "The Signing Secret is required to have the payment confirmations working correctly. If you do not know where to get it from check <a href=\"https://wpadverts.com/doc/where-do-i-find-my-signing-secret/\" target='_blank'>this article</a>.", "wpadverts-stripe")
                ),
                array(
                    "name"      => "live_confirm",
                    "type"      => "adverts_field_checkbox",
                    "label"     => __("Live Confirm", "wpadverts-stripe"),
                    "order"     => 23,
                    "meta"      => array(),
                    "attr"      => array(),
                    "options"   => array(
                        array( 
                            "value" => 1,
                            "text" => __( "Attempt to confirm Payment Intent with Stripe before checking payment status.", "wpadverts-stripe" )
                        )
                        
                    )
                ),
                array(
                    "name"      => "_options",
                    "type"      => "adverts_field_header",
                    "order"     => 30,
                    "label"     => __( 'Options', 'wpadverts-stripe' )
                ),
                array(
                    "name"      => "custom_title",
                    "type"      => "adverts_field_text",
                    "label"     => __("Payment Method Name", "wpadverts-stripe"),
                    "hint"      => __("This name will be visible to users in the checkout.", "wpadverts-stripe"),
                    "order"     => 30,
                    "meta"      => array(),
                ),
                array(
                    "name"      => "statement_descriptor",
                    "type"      => "adverts_field_text",
                    "label"     => __("Payment Description", "wpadverts-stripe"),
                    "hint"      => __("Extra information about a charge. This will appear on your customer’s credit card statement.", "wpadverts-stripe"),
                    "order"     => 30,
                    "meta"      => array(),
                ),
                array(
                    "name"      => "send_receipts",
                    "type"      => "adverts_field_checkbox",
                    "label"     => __("Receipts", "wpadverts-stripe"),
                    "order"     => 30,
                    "options"   => array(
                        array( "value" => 1, "text" => __( "I want Stripe to automatically email receipts to customers.", "wpadverts-stripe") )
                    ),
                    "meta"      => array(),
                ),
            )
        );
    }
}
