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


$id = isset($customer_id) ? 'extras_customer_'.$customer_id : 'extras_default';

?>

<table id="<?php esc_attr_e($id, 'wiaas') ?>" style="margin-top: 20px;" class="widefat wiaas-configured-prices">

	<thead>
	<tr>
		<td>
			<?php
			if (isset($customer_id)) {
				echo '<h4>'. esc_html(wiaas_get_organization_name($customer_id)). '</h4>';
			}
			?>
        </td>
		<td><?php esc_html_e('Minimal sell price', 'wiaas') ?></td>
		<td><?php esc_html_e('Extra commision', 'wiaas') ?></td>
		<td><?php esc_html_e('Default price', 'wiaas') ?></td>
		<td><?php esc_html_e('Visible?', 'wiaas') ?>
			<?php
				if (isset($customer_id)) {
					?>
						<i
							data-customer_id="<?php esc_attr_e($customer_id, 'wiaas') ?>"
							class="dashicons dashicons-dismiss wiaas_remove_cl_extras"
							></i>
					<?php
				}
			?>
		</td>
	</tr>
	</thead>
	<tbody>
	<?php
	foreach ($configured_prices as $type => $configured_price) {

		$extra_type = isset($customer_id) ? $type.'_customer_'.$customer_id : $type.'_default';

		?>
		<input
			type="hidden"
			name="cl_extras[<?php esc_attr_e($extra_type, 'wiaas')?>][type]"
			value="<?php esc_attr_e($type, 'wiaas')?>"
		>

		<?php
		if (isset($customer_id)) {
			?>
			<input
				type="hidden"
				name="cl_extras[<?php esc_attr_e($extra_type, 'wiaas')?>][customer]"
				value="<?php esc_attr_e($customer_id, 'wiaas')?>"
			>
			<?php
		}
		?>

		<tr id="<?php esc_attr_e($extra_type, 'wias')?>">
			<td>
				<strong><?php esc_html_e($configured_price['payment_type']) ?></strong>
			</td>
			<td class="form-group">
				<div class="form-control">
					<label>Fixed:</label>
					<input type="text" value="<?php esc_attr_e($configured_price['minimal_fixed_price'], 'wiaas') ?>" readonly>
				</div>
				<div class="form-control">
					<label>Monthly:</label>
					<input type="text" value="<?php esc_attr_e($configured_price['minimal_recurrent_price'], 'wiaas') ?>" readonly>
					<?php
					if($configured_price['package_pay_period'] > 0) {
						echo  sprintf( '<span>(%s for services)</span>', esc_attr( $configured_price['minimal_services_price'] ) );
					}
					?>

				</div>
			</td>
			<td class="form-group">
				<div class="form-control">
					<label>+</label>
					<input
                        class="wiaas-cl-extra-input"
                        data-target="wiaas_cl_fixed_extra_<?php esc_attr_e($extra_type, 'wiaas') ?>"
                        data-type="fixed"
						name="cl_extras[<?php esc_attr_e($extra_type, 'wiaas')?>][fixed]"
						value="<?php esc_attr_e($cl_extras[$extra_type]['fixed'], 'wiaas') ?>"
						type="text"
					>
				</div>
				<?php
				if($configured_price['package_pay_period'] > 0) {
					?>
					<div class="form-control">
						<label>+</label>
						<input
                            class="wiaas-cl-extra-input"
                            data-target="wiaas_cl_recurrent_extra_<?php esc_attr_e($extra_type, 'wiaas') ?>"
                            data-type="recurrent"
							name="cl_extras[<?php esc_attr_e($extra_type, 'wiaas')?>][recurrent]"
							value="<?php esc_attr_e($cl_extras[$extra_type]['recurrent'], 'wiaas') ?>"
							type="text"
						>
						<label><?php esc_html_e('(EPR)', 'wiaas') ?></label>
					</div>
					<?php
				}
				?>
				<div class="form-control">
					<label>+</label>
					<input
                        class="wiaas-cl-extra-input"
                        data-target="wiaas_cl_monthly_extra_<?php esc_attr_e($extra_type, 'wiaas') ?>"
                        data-type="services"
						name="cl_extras[<?php esc_attr_e($extra_type, 'wiaas')?>][services]"
						value="<?php esc_attr_e($cl_extras[$extra_type]['services'], 'wiaas') ?>"
						type="text"
					>
                    <label><?php esc_html_e('(ESR)', 'wiaas') ?></label>
				</div>
			</td>
			<td>
				<div> =
                    <strong
                            id="wiaas_cl_fixed_extra_<?php esc_attr_e($extra_type, 'wiaas') ?>"
                            data-base="<?php esc_attr_e($configured_price['minimal_fixed_price'], 'wiaas') ?>"
                    >
                        <?php esc_html_e($configured_price['minimal_fixed_price'] + $cl_extras[$extra_type]['fixed'], 'wiaas') ?>
                    </strong>
                </div>
				<div> =
                    <strong
                            id="wiaas_cl_monthly_extra_<?php esc_attr_e($extra_type, 'wiaas') ?>"
                            data-base="<?php esc_attr_e($configured_price['minimal_recurrent_price'], 'wiaas') ?>"
                            data-recurrent="<?php esc_attr_e($cl_extras[$extra_type]['recurrent'], 'wiaas') ?>"
                            data-services="<?php esc_attr_e($cl_extras[$extra_type]['services'], 'wiaas') ?>"
                    >
	                    <?php
                        esc_html_e($configured_price['minimal_recurrent_price'] +
                                   $cl_extras[$extra_type]['recurrent'] +
                                   $cl_extras[$extra_type]['services'], 'wiaas')
                        ?>
                </div>
			</td>
			<td class="form-group">
				<input
					name="cl_extras[<?php esc_attr_e($extra_type, 'wiaas')?>][visible]"
					<?php checked($cl_extras[$extra_type]['visible'], true, true) ?>
					type="checkbox"
				>
			</td>
		</tr>
		<?php
	}
	?>
	</tbody>
</table>
