<?php

/**
 * Class Wiaas_Admin_CM_Packages
 *
 */
class Wiaas_Admin_CL_Packages {

	/**
	 * Handles output of Commercial Lead packages page in admin panel.
	 *
	 * Shows list of packages available for sale and packages sold by current commercial lead.
	 * Enables adding package to commercial lead catalogue and setting package specific prices
	 * for current commercial lead.
	 *
	 */

	public static function init() {

		add_action( 'admin_enqueue_scripts', array(__CLASS__, 'enqueue_scripts'), 100 );

		add_action( 'admin_menu', array(__CLASS__, 'add_cl_packages_menu') );
	}

	public static  function enqueue_scripts() {
		$plugin_url = untrailingslashit( plugins_url( '/', WIAAS_FILE ) );

		wp_enqueue_style( 'wiaas_admin_menu', $plugin_url . '/assets/css/menu.css' );
	}

	public static function add_cl_packages_menu() {
		add_menu_page(
			__( 'Products', 'wiaas' ),
			__( 'Products', 'wiaas' ),
			'wiaas_manage_cl_products',
			'wiaas-cl-packages',
			array(__CLASS__, 'output_list'),
			null,
			'55.5' );

		add_submenu_page(
			null,
			__( 'Products', 'wiaas' ),
			null,
			'wiaas_manage_cl_products',
			'wiaas-cl-product',
			array(__CLASS__, 'output_package')
		);
	}

	public static function output_list() {

		$packages_list = new Wiaas_Admin_CL_Packages_List();

		$packages_list->prepare_items();

		require 'views/html-admin-cl-packages-page.php';
	}

	public static function output_package() {
		wp_enqueue_script( 'jquery-ui-tabs' );

		$package_id = absint( $_GET['id'] );

		$package = wc_get_product($package_id);

		if (!$package) {
			return;
		}

		if (! empty($_POST['wiaas_save_cl_extras_nonce']) && ! empty($_POST['cl_extras'])) {
			 if (wp_verify_nonce($_POST['wiaas_save_cl_extras_nonce'], 'wiaas_save_cl_extras')) {
				 self::_handle_cl_extras_update($package_id);
			 }
		}

		$bundled_items = $package->get_bundled_items();

		$bundled_items_per_category = array();
		foreach ($bundled_items as $bundled_item) {
			$category = Wiaas_Product_Category::get_category($bundled_item->product);
			if (isset($category)) {
				if ($category ==='hardware' || $category === 'software') {
					$category = 'products';
				}
				$bundled_items_per_category[$category] ?: array();
				$bundled_items_per_category[$category][] = $bundled_item;
			}
		}

		$configured_prices = Wiaas_Package_Pricing::get_package_prices($package);

		$cl_id = wiaas_get_current_user_organization_id();

        $cl_extras = Wiaas_Package_CL_Pricing::get_extras($cl_id, $package_id);

        $has_default_cl_extras = ! empty($cl_extras);

		// default values for catalogue package
		if (! $has_default_cl_extras) {
            $cl_extras = array();

			foreach ($configured_prices as $type => $configured_price) {
                $cl_extras[$type.'_default'] = array(
					'visible' => true,
					'fixed' => 0,
					'services' => 0,
					'recurrent' => 0
				);
			}
		}

		$package_cl_extra_types = array_keys($cl_extras);

		$customer_ids_with_extras = array();
		foreach ($package_cl_extra_types as $package_cl_extra_type) {
			$customer_id_with_extras = explode('_customer_', $package_cl_extra_type)[1];

			if (isset($customer_id_with_extras) && ($customer_id_with_extras = absint($customer_id_with_extras)) > 0) {
				$customer_ids_with_extras[] = $customer_id_with_extras;
			}
		}
		$customer_ids_with_extras = array_unique($customer_ids_with_extras);

		require 'views/html-cl-package-details.php';
	}




	// PRIVATE HANDLERS

	private static function _handle_cl_extras_update($package_id) {
		$cl_extras = wp_unslash($_POST['cl_extras']);

		$pay_types = array_keys(Wiaas_Package_Pricing::get_available_pay_types());

		$posted_cl_extras = array();

		foreach ($cl_extras as $cl_extra) {

			$customer_id = isset($cl_extra['customer']) ? absint($cl_extra['customer']) : 0;
			$type = sanitize_key($cl_extra['type']);

			$fixed_extra = isset($cl_extra['fixed']) ? floatval($cl_extra['fixed']) : 0;
			$services_extra = isset($cl_extra['services']) ? floatval($cl_extra['services']) : 0;
			$recurrent_extra = isset($cl_extra['recurrent']) ? floatval($cl_extra['recurrent']) : 0;

			$extra_type = $customer_id > 0 ? $type.'_customer_'.$customer_id : $type.'_default';

			if (in_array($type, $pay_types)) {
				$posted_cl_extras[$extra_type] = array(
					'type' => $type,
					'visible' => $cl_extra['visible'] === 'on',
					'fixed' => $fixed_extra,
					'services' => $services_extra,
					'recurrent' => $recurrent_extra,
				);
			}
		}

        Wiaas_Package_CL_Pricing::set_extras(
			wiaas_get_current_user_organization_id(),
			$package_id,
			$posted_cl_extras
		);
	}
}

Wiaas_Admin_CL_Packages::init();
