<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
?>

<style>
    table tr:not(.wiaas-package-price) td:not(:first-child) {
        text-align: center;
        vertical-align: middle; !important;
    }

</style>

<div id="wiaas_package_pricing_editor" class="wrap">

    <h1>
        <?php
        esc_html_e($package->get_title(), 'wiaas');

        $country = Wiaas_Countries::get_package_country($package);

        if (! empty($country)) {

            esc_html_e(' (sold in ' . $country['name'] . ')', 'wiaas');
        }
        ?>
    </h1>

    <br class="clear" />

    <div id="col-container">
        <div id="post-body" class="metabox-holder columns-2">

            <div id="col-right">

                <div class="col-wrap">

                    <div class="inside">

                        <table class="widefat">

		                    <tbody>
		                    <?php

		                    $products_total_cost = 0;
		                    $one_time_services_cost = 0;
		                    $recurring_services_cost = 0;
		                    $installation_cost = 0;

		                    foreach ($bundled_items_per_category as $cat => $items) {

			                    ?>
                                <tr style="background: #F5F5F5;">
                                    <td colspan="4">
                                        <strong style="text-transform: uppercase;">
						                    <?php esc_html_e($cat . ' (' . count($items) . ')', 'wiaas') ?>
                                        </strong>
                                    </td>
                                </tr>

			                    <?php
			                    foreach ($items as $item) {
				                    $product = $item->product;

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

				                    $total_cost = Wiaas_Pricing::get_product_total_cost($product);

				                    ?>
                                    <tr>
                                        <td>
                                            <span>
							                    <?php esc_html_e($item->get_quantity(), 'wiaas') ?>
                                                x
							                    <?php esc_html_e($product->get_title(), 'wiaas') ?>
                                            </span>
                                        </td>

                                        <td>
						                    <?php
						                    esc_html_e(
							                    $total_cost * $item->get_quantity(),
							                    'wiaas')
						                    ?>
                                        </td>

                                        <td>
						                    <?php
						                    esc_html_e(
							                    $total_cost . ' per unit',
							                    'wiaas');
						                    ?>

                                        </td>

                                        <td>
						                    <?php
						                    if ($product_price['is_recurring']) {
							                    esc_html_e(
								                    $product_price['price'] . ' / month for ' . $product_price['pay_period'] . ' months',
								                    'wiaas');

						                    } else {

							                    echo '-';
						                    }
						                    ?>
                                        </td>

                                    </tr>
				                    <?php
			                    }
			                    ?>

                                <tr class="wiaas-package-price">
                                    <td style="border-top: 1px solid #e5e5e5;"></td>
                                    <td colspan="3" style="border-top: 1px solid #e5e5e5;">
                                        <strong>
						                    <?php

                                                if ($cat === 'product') {

	                                                $products_total_cost = wiaas_get_package_hardware_procurement_cost($package) +
                                                                  wiaas_get_package_software_procurement_cost($package);

                                                    esc_html_e('Total: ' . $products_total_cost, 'wiaas');
                                                }

                                                if ($cat === 'service') {

                                                    $one_time_services_cost = wiaas_get_package_one_time_services_procurement_cost($package);

                                                    $recurring_services_cost = wiaas_get_package_recurring_services_procurement_cost($package);

	                                                esc_html_e('One time services: ' . $one_time_services_cost, 'wiaas');

	                                                echo '<br>';

	                                                esc_html_e('Recurring services: ' . $recurring_services_cost . ' / month ', 'wiaas');
                                                }

                                                if ($cat === 'installation') {

                                                    $installation_cost = wiaas_get_package_installation_procurement_cost($package);

	                                                esc_html_e('Installation: ' . $installation_cost, 'wiaas');
                                                }
                                            ?>
                                        </strong>
                                    </td>
                                </tr>

			                    <?php
		                    }
		                    ?>
                            </tbody>

                            <tfoot>
                            <tr class="wiaas-package-price">
                                <td>
                                    <h4>TOTAL COST :</h4>
                                </td>
                                <td colspan="3">
                                    <strong>Fixed: <?php esc_html_e($products_total_cost + $one_time_services_cost + $installation_cost)?></strong>
                                    <br>
                                    <strong>Recurring: <?php esc_html_e($recurring_services_cost)?> / month</strong>
                                </td>
                            </tr>
                            </tfoot>

                        </table>

                    </div>
                </div>
            </div>

            <div id="col-left">

                <div class="col-wrap">
                    <div class="form-wrap">

                        <h2><?php esc_html_e('Set package prices', 'wiaas') ?></h2>

                        <form action="" method="post">

                            <input type="hidden" name="page" value="wiaas-package_price_editor">
                            <input type="hidden" name="id" value="<?php esc_attr_e($package->get_id(), 'wiaas') ?>">

                            <div class="form-field">
                                <label><?php esc_html_e('Max cost margin:', 'wiaas') ?></label>
                                <input
                                        id="wiaas_pricing_rules_max_cost_margin"
                                        name="wiaas_max_cost_margin"
                                        type="number"
                                        min="0"
                                        value="<?php esc_attr_e($max_cost_margin, 'wiaas') ?>"
                                        data-total="<?php esc_attr_e(Wiaas_Pricing::get_package_total_cost($package), 'wiaas') ?>"
                                >

                                <p
                                        id="wiaas_pricing_rules_max_cost_margin_error"
                                        <?php if(Wiaas_Package_Status::get_package_status($package->get_id()) !== Wiaas_Package_Status::INVALID_MARGIN) echo 'style="display: none;"' ?>
                                        class="wiaas-package-error"
                                >The total costs are greater than the package max margin!</p>

                            </div>

                            <div class="form-field">
                                <label><?php esc_html_e('Commision ( % ):', 'wiaas') ?></label>
                                <input
                                        id="wiaas_pricing_rules_commision"
                                        name="wiaas_pricing_rules_commision"
                                        type="number"
                                        value="<?php esc_attr_e($commission, 'wiaas') ?>"
                                        min="0" max="100"
                                >
                                <p id="wiaas_pricing_rules_commission_details">
                                    <?php  esc_html_e('Commercial lead: ' . (100 - $commission) .'%', 'wiaas') ?>
                                </p>
                            </div>

                            <div class="form-field">

                                <div class="wrap">

                                    <select id="wiaas_pay_types" name="wiaas-pay-type" class="pricing_rule_mode">
                                        <option selected value="0" disabled> Select payment type ...</option>

                                        <?php
	                                    $available_pay_types =  Wiaas_Package_Pricing::get_available_pay_types();

	                                    foreach ($available_pay_types as $name => $pay_type) {
		                                    $is_added = ! empty( $pricing_rules[$name])
		                                    ?>
                                            <option
                                                    value="<?php esc_attr_e($name, 'wiaas') ?>"
                                                    id="wiaas_pay_type_<?php esc_attr_e($name, 'wiaas') ?>"
			                                    <?php disabled($is_added, true, true) ?>
                                            >
			                                    <?php esc_html_e( $pay_type['title'], 'wiaas' ); ?>
                                            </option>
		                                    <?php
	                                    }
	                                    ?>

                                    </select>

                                    <button
                                            title="<?php _e( 'Add pricing type.', 'wiaas' ); ?>"
                                            id="wiaas-add-pricing"
                                            type="button"
                                            class="button">
		                                <?php _e( 'Add Pricing Type', 'wiaas' ); ?>
                                    </button>

                                </div>
                            </div>

                            <div id="wiaas_package_pricing_rules">

	                            <?php
	                                require 'html-package-pricing-rules-list.php';
	                            ?>

                            </div>

	                        <?php wp_nonce_field( 'wiaas_save_package_prices', 'wiaas_save_package_prices_nonce' ); ?>

                            <input type="submit" value="Save" class="button button-primary button-large">
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
