<!-- $o=Service::class -->
@use(App\Models\Invoice)
@use(App\Models\Product)

<div class="row">
	<div class="col-12">
		<h4>Update Service Details <x-leenooks::button.success class="float-right"/></h4>
		<hr>

		<form method="POST" action="{{ url('a/service/update',[$o->id]) }}">
			@csrf

			<div class="row">
				<!-- External Billing -->
				<div class="col-2">
					<x-leenooks::form.toggle id="external_billing" name="external_billing" label="External Billing" :value="$o->external_billing"/>
					<x-leenooks::form.toggle id="suspend_billing" name="suspend_billing" label="Suspend Billing" :value="$o->suspend_billing"/>
				</div>

				<div class="col-1"></div>

				<div class="col-12 col-sm-9 col-md-6 col-xl-5">
					<x-leenooks::form.date id="invoice_next_at" name="invoice_next_at" icon="fa-calendar" label="Billing Start Date" :value="($o->getRawOriginal('invoice_next_at') ?: $o->getRawOriginal('connect_at'))"/>
				</div>

				<!-- Price -->
				<div class="col-12 col-sm-9 col-md-12 col-xl-3">
					<x-leenooks::form.text name="price" icon="fa-dollar-sign" label="Price" :value="$o->price"/>
				</div>
			</div>

			<div class="row">
				<div class="col-12 col-sm-3">
					<x-leenooks::form.text name="supplierid" icon="fa-hashtag" label="Supplier ID" :value="$o->supplierid"/>
				</div>

				<div class="col-12 col-sm-9 col-md-6 col-xl-5">
					<x-leenooks::form.select id="recur_schedule" name="recur_schedule" icon="fa-redo" label="Renew Term" :value="$o->recur_schedule" :options="collect(Invoice::billing_periods)->map(fn($item,$key)=>['id'=>$key,'value'=>$item['name']])"/>
				</div>

				<!-- Cost -->
				<div class="col-12 col-sm-4 col-xl-3">
					<x-leenooks::form.text name="cost" icon="fa-dollar-sign" label="Cost (Monthly)" :value="$o->cost"/>
				</div>
			</div>

			<div class="row">
				<div class="col-12">
					<!-- PRODUCT -->
					<x-leenooks::form.select name="product_id" icon="fa-list" label="Product" :helper="$o->product->category_name"
						:value="$o->product_id"
						:options="Product::with(['type.supplied','translate'])
							->get()
							->filter(fn($item)=>($item->category_lc === $o->product->category_lc))
							->sortBy('name')
							->map(fn($item)=>[
								'id'=>$item->id,
								'value'=>sprintf('%s $%3.2f [%s/%3.2f]',
									$item->name,
									$o->account->taxed($item->base_charge())*Invoice::billing_change($item->billing_interval,Invoice::BILL_MONTHLY),
									$item->supplied->name,
									$o->account->taxed($item->base_cost)*Invoice::billing_change($item->billing_interval,Invoice::BILL_MONTHLY),
								)])" :required="true"/>
				</div>
			</div>
			<hr>

			@includeIf('theme.backend.adminlte.service.widget.'.$o->product->category_lc.'.update',['o'=>$o->type])

			<div class="row">
				<div class="col">
					@can('wholesaler')
						<x-leenooks::button.reset/>
						<x-leenooks::button.submit class="float-right">Save</x-leenooks::button.submit>
					@endcan
				</div>
			</div>
		</form>
	</div>
</div>

@section('page-scripts')
	<script type="text/javascript">
		function toggle_billing_external(item) {
			if (item.is(':checked')) {
				$('#suspend_billing').closest('.form-group').addClass('d-none');
				$('#invoice_next_at').closest('.form-group').parent().addClass('d-none');
				$('#price').closest('.form-group').parent().addClass('d-none');
				$('#recur_schedule').closest('.form-group').parent().addClass('d-none');

				item.closest('.form-group').addClass('mb-0');

			} else {
				$('#suspend_billing').closest('.form-group').removeClass('d-none');
				$('#invoice_next_at').closest('.form-group').parent().removeClass('d-none');
				$('#price').closest('.form-group').parent().removeClass('d-none');
				$('#recur_schedule').closest('.form-group').parent().removeClass('d-none');

				item.closest('.form-group').removeClass('mb-0');
			}
		}

		function toggle_billing_suspend(item) {
			if (item.is(':checked')) {
				$('#invoice_next_at').prop('readonly',true);
				$('#price').prop('readonly',true);

				recur_schedule_readonly(true);

			} else {
				$('#invoice_next_at').prop('readonly',false);
				$('#price').prop('readonly',false);

				recur_schedule_readonly(false);
			}
		}

		$(document).ready(function() {
			toggle_billing_external($('#external_billing'));
			toggle_billing_suspend($('#suspend_billing'));

			$('#external_billing').on('click',function(item) {
				toggle_billing_external($(this));
			});

			$('#suspend_billing').on('click',function(item) {
				toggle_billing_suspend($(this));
			});
		});
	</script>
@append