<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}


/**
 * Class Wiaas_Admin_Product_Pricing
 */
class Wiaas_Admin_Product_Pricing {

	public static function init() {
		add_action( 'woocommerce_product_options_pricing', array( __CLASS__, 'product_pricing' ) );

		add_action( 'woocommerce_process_product_meta', array( __CLASS__, 'process_meta_box' ), 1, 2 );
	}

	/**
	 * Renders wiaas pricing options for product
	 */
	public static function product_pricing() {

		global $post;

		$product  = wc_get_product( $post->ID );

		$product_pricing = Wiaas_Product_Pricing::get_product_price($product);

		include 'views/html-product-pricing.php';
	}

	/**
	 * Saves posted product pricing settings
	 * @param $post_id
	 * @param $post
	 */
	public function process_meta_box( $post_id, $post ) {

		$product = wc_get_product( $post_id );
		if ($product->get_type() === 'simple') {
			$is_recurring = $_POST['_wiaas_recurring_price'] === 'yes';
			$pay_period = isset($_POST['_wiaas_recurring_pay_period']) ? $_POST['_wiaas_recurring_pay_period'] : 0;

			Wiaas_Product_Pricing::set_product_price($product, $product->get_price(), $is_recurring, $pay_period);
		}
	}
}

Wiaas_Admin_Product_Pricing::init();