<!-- $pdo=Product::class -->
@use(App\Models\Group)
@use(App\Models\Invoice)
@use(App\Models\Product)
@use(App\Models\ProviderOauth)

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

		<form method="POST">
			@csrf

			<div class="row">
				<!-- Product ID -->
				<div class="col-12 col-sm-9 col-md-4 col-xl-3">
					<x-leenooks::form.text id="translate.name_short" name="translate[name_short]" icon="fa-atom" label="Product ID" old="translate.name_short" :value="$pdo->pid"/>
				</div>

				<!-- Product Name -->
				<div class="col-12 col-sm-9 col-md-8 col-xl-9">
					<x-leenooks::form.text id="translate.name_detail" name="translate[name_detail]" icon="fa-atom" label="Product Name" old="translate.name_detail" :value="$pdo->name"/>
				</div>
			</div>

			<div class="row">
				<div class="col-12">
					<x-leenooks::form.textarea id="translate.description" name="translate[description]" label="Product Description" placeholder="Full Description..." old="translate.description" :value="$pdo->description"/>
				</div>
			</div>

			<div class="row">
				<div class="col-12 col-md-4">
					<div class="row">
						<!-- Active -->
						<div class="col-6">
							<x-leenooks::form.toggle name="active" label="Active" :value="$pdo->active"/>
						</div>
					</div>

					<div class="row">
						<!-- Product Type -->
						<div class="col-12">
							<x-leenooks::form.select name="model" icon="fa-list" label="Product" choose="true" groupby="active" :value="get_class($pdo->type)" :options="Product::availableTypes()->map(fn($item)=>['id'=>$item,'value'=>$item])"/>
						</div>
					</div>

					<div class="row">
						<!-- Supplied Product -->
						<div class="col-12" id="supplied_product">
							<x-leenooks::form.select name="model_id" icon="fa-shopping-cart" label="Supplied Product"/>
						</div>
					</div>

					<div class="row">
						<!-- Accounting -->
						<div class="col-12">
							<span class="h5">Accounting</span>
							<hr>

							@production
								<!-- @todo When returning with a bad value old() is not selecting the previous value, may need to have the full html here instead of a component -->
								@foreach (ProviderOauth::providers() as $apo)
									<x-leenooks::form.select :id="sprintf('acc_%d',$apo->id)" :name="sprintf('accounting[%d]',$apo->id)" icon="fa-calculator" :label="ucfirst($apo->name)" :choose="true" :value="$pdo->provider_ref($apo)" old="accounting" :options="$pdo->accounting($apo->name)"/>
								@endforeach
							@endproduction
						</div>
					</div>
				</div>

				<div class="col-12 offset-md-3 col-md-5">
					<span class="h5">Pricing</span><small> Ex Taxes</small>
					<hr>
					@error('pricing')
						<x-leenooks::errors/>
					@enderror

					<ul class="nav nav-pills w-100 pl-0 pt-2 pb-2">
						@foreach($g=Group::pricing()->active()->get() as $go)
							<li class="nav-item"><a class="nav-link @if(! $loop->index)active @endif" href="#pg_{{ $go->id }}" data-toggle="tab">{{ $go->name }}</a></li>
						@endforeach
					</ul>

					<div class="tab-content">
						@foreach($g as $go)
							<div class="tab-pane fade @if(! $loop->index)show active @endif" id="pg_{{ $go->id }}">

								@foreach(Invoice::billing_periods as $bp=>$detail)
									<div class="row">
										<div class="col-6">
											<div class="form-group">
												<label for="{{ $x=sprintf('base_%d_%d',$bp,$go->id) }}">{{ $detail['name'] }} Reoccurring</label>
												<div class="input-group has-validation">
													<div class="input-group-prepend">
														<span class="input-group-text"><i class="fa-fw fas fa-calculator"></i></span>
													</div>
													<input type="text" class="form-control text-right @error($x) is-invalid @enderror" id="{{ $x }}" name="{{ sprintf('pricing[%d][%d][base]',$bp,$go->id) }}" {{ ($ca=$pdo->charge_available($bp,$go)) ? 'value' : 'placeholder' }}="{{ $c=$pdo->charge($bp,$go,'base') }}" @if(is_null($c) || ! $ca) disabled @endif>
													<div class="input-group-append">
														<div class="input-group-text">
															<input type="checkbox" name="{{ sprintf('pricing[%d][show]',$bp) }}" @if($c && $ca)checked @endif>
														</div>
													</div>
													<span class="invalid-feedback">
														@error($x)
															{{ $message }}
														@enderror
													</span>
												</div>
											</div>
										</div>

										<div class="col-6">
											<div class="form-group">
												<label for="{{ $x=sprintf('setup_%d_%d',$bp,$go->id) }}">{{ $detail['name'] }} Setup</label>
												<div class="input-group has-validation">
													<div class="input-group-prepend">
														<span class="input-group-text"><i class="fa-fw fas fa-cog"></i></span>
													</div>
													<input type="text" class="form-control text-right @error($x) is-invalid @enderror" id="{{ $x }}" name="{{ sprintf('pricing[%d][%d][setup]',$bp,$go->id) }}" {{ $ca ? 'value' : 'placeholder' }}="{{ $c=$pdo->charge($bp,$go,'setup') }}" @if(is_null($c) || ! $ca) disabled @endif>
													<span class="invalid-feedback">
														@error($x)
															{{ $message }}
														@enderror
													</span>
												</div>
											</div>
										</div>
									</div>
								@endforeach
							</div>
						@endforeach
					</div>
				</div>
			</div>

			<div class="row">
				<div class="col">
					<x-leenooks::button.reset/>
					<x-leenooks::button.submit class="float-right">Save</x-leenooks::button.submit>
				</div>
			</div>
		</form>
	</div>
</div>

@section('page-scripts')
	<script type="text/javascript">
		// Get a list of supplier items matching this type to populate model_id
		function supplied_products(type,destination,selected) {
			destination.prop('disabled',true);

			$.ajax({
				type: 'GET',
				dataType: 'json',
				data: {type: type},
				cache: false,
				url: '{{ url('a/supplied_products') }}',
				timeout: 2000,
				error: function(x) {
					// @todo add a spinner
					alert('Failed to submit');
				},
				success: function(data) {
					destination
						.empty()
						.append($('<option>'))
						.append(data.map(function(item,key) {
							return new Option(item.name,item.id,selected && selected==item.id,selected && selected==item.id);
						}))
						.prop('disabled',false);
				}
			});
		}

		$(document).ready(function() {
			$('#model').on('change',function(item) {
				if ($(this).val()) {
					$('#supplied_product').show();
					supplied_products($(this).val(),$('#model_id'));

				} else {
					$('#supplied_product').hide();
				}
			});

			// After we render the page, hide the supplied_product if this product has no model.
			// We do this here, because adding d-none to the div results in the select2 input not presenting correctly
			if (! $('#model').val())
				$('#supplied_product').hide();
			else
				supplied_products($('#model').val(),$('#model_id'),{{ old('model_id',$pdo->model_id) }});

			// @todo when there is a value in 1 of the two boxes in the row, the toggle incorrectly disables one and enables the other
			$('input[type=checkbox]').on('click',function(item) {
				var input = $(this).parent().parent().parent().find('input[type="text"]');
				input.prop('disabled',(i,v)=>!v);

				// Find the setup input and toggle it
				input = $('#'+input.attr('id').replace('base','setup')+'');
				input.prop('disabled',(i,v)=>!v);
			});
		});
	</script>
@append