<?php

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

/**
 * Class Wiaas_Admin_Package_Types
 */
class Wiaas_Admin_Package_Types {

	public static function init() {

		add_action( 'woocommerce_product_options_general_product_data', array( __CLASS__, 'package_types_data' ) );

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

	/**
	 * Renders wiaas package types options
	 */
	public static function package_types_data() {

		global $post;

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

		include 'views/html-package-types.php';
	}

	/**
	 * Saves posted wiaas package type data
	 * @param $package_id
	 */
	public static function process_meta_box($package_id) {

		$package = wc_get_product($package_id);

		if ($package->get_type() === 'bundle') {
			// Save wiaas package type
			Wiaas_Package_Type::set_package_type($package_id, $_POST['wiaas_package_type']);
		} else {
			Wiaas_Package_Type::set_package_type($package_id, null);
		}
	}
}

Wiaas_Admin_Package_Types::init();