<!-- $o = Product::class -->
<div class="row">
	<div class="col-12">
		<h3>Product Details @include('adminlte::widget.success_button')</h3>
		<hr>

		<form class="g-0 needs-validation" method="POST" enctype="multipart/form-data" role="form">
			@csrf

			<div class="row">
				<!-- Product ID -->
				<div class="col-12 col-sm-9 col-md-4 col-xl-3">
					@include('adminlte::widget.form_text',[
						'label'=>'Product ID',
						'icon'=>'fas fa-atom',
						'id'=>'translate.name_short',
						'old'=>'translate.name_short',
						'name'=>'translate[name_short]',
						'value'=>$o->pid,
					])
				</div>

				<!-- Product Name -->
				<div class="col-12 col-sm-9 col-md-8 col-xl-9">
					@include('adminlte::widget.form_text',[
						'label'=>'Product Name',
						'icon'=>'fas fa-atom',
						'id'=>'translate.name_detail',
						'old'=>'translate.name_detail',
						'name'=>'translate[name_detail]',
						'value'=>$o->name,
					])
				</div>
			</div>

			<div class="row">
				<div class="col-12">
					<div class="form-group @error('description') is-invalid @enderror">
						<!-- Product Description -->
						<label for="description_full" class="col-form-label">Product Description</label>
						<div class="input-group">
							<div class="w-100">
								<textarea class="textarea" id="description" name="translate[description]" placeholder="Full Description">{!! old('description',$o->description) ?? '' !!}</textarea>
								<span class="invalid-feedback" role="alert">
									@error('description')
										{{ $message }}
									@enderror
								</span>
							</div>
						</div>
					</div>
				</div>
			</div>

			<div class="row">
				<div class="col-12 col-md-4">
					<div class="row">
						<!-- Active -->
						<div class="col-6">
							@include('adminlte::widget.form_toggle',[
								'label'=>'Active',
								'id'=>'active',
								'old'=>'active',
								'name'=>'active',
								'value'=>$o->active ?? '',
							])
						</div>
					</div>

					<div class="row">
						<!-- Product Type -->
						<div class="col-12">
							@include('adminlte::widget.form_select',[
								'label'=>'Product Type',
								'icon'=>'fas fa-list',
								'id'=>'model',
								'old'=>'model',
								'name'=>'model',
								'options'=>\App\Models\Product::availableTypes()->transform(function($item) { return ['id'=>$item,'value'=>$item]; }),
								'value'=>get_class($o->type),
							])
						</div>
					</div>

					<div class="row">
						<!-- Supplied Product -->
						<div class="col-12" id="supplier_product">
							<div class="form-group">
								<label for="model_id">Supplied Product</label>
								<div class="input-group has-validation">
									<div class="input-group-prepend">
										<span class="input-group-text"><i class="fa-fw fas fa-shopping-cart"></i></span>
									</div>
									<select class="form-control @error('model_id') is-invalid @enderror" id="model_id" name="model_id"></select>
									<span class="invalid-feedback" role="alert">
										@error('model_id')
											{{ $message }}
										@enderror
									</span>
								</div>
							</div>
						</div>
					</div>

					<div class="row">
						<!-- Accounting -->
						<div class="col-12">
							<span class="h5">Accounting</span>
							<hr>
							@foreach (\App\Models\ProviderOauth::providers() as $apo)
								<div class="form-group">
									<label for="{{ $x=sprintf('acc_%d',$apo->id) }}">{{ ucfirst($apo->name) }}</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>
										<select class="form-control @error($x) is-invalid @enderror select" id="{{ $x }}" name="{{ sprintf('accounting[%d]',$apo->id) }}">
											<option></option>
											@foreach ($o->accounting($apo->name) as $v)
												<option value="{{ $v['id'] }}" @if($o->provider_ref($apo) === (string)$v['id'])selected @endif>{{ $v['value'] }}</option>
											@endforeach
										</select>
										<span class="invalid-feedback" role="alert">
											@error($x)
												{{ $message }}
											@enderror
										</span>
									</div>
								</div>
							@endforeach
						</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')
						@include('adminlte::widget.errors')
					@enderror

					<ul class="nav nav-pills w-100 pl-0 pt-2 pb-2">
						@foreach(\App\Models\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(\App\Models\Group::pricing()->active()->get() as $go)
							<div class="tab-pane fade @if(! $loop->index)show active @endif" id="pg_{{ $go->id }}" role="tabpanel">

								@foreach(\App\Models\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=$o->charge_available($bp,$go)) ? 'value' : 'placeholder' }}="{{ $c=$o->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" role="alert">
														@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=$o->charge($bp,$go,'setup') }}" @if(is_null($c) || ! $ca) disabled @endif>
													<span class="invalid-feedback" role="alert">
														@error($x)
															{{ $message }}
														@enderror
													</span>
												</div>
											</div>
										</div>
									</div>
								@endforeach
							</div>
						@endforeach
					</div>
				</div>
			</div>

			<div class="row">
				<!-- Buttons -->
				<div class="col-12">
					<a href="{{ url('/home') }}" class="btn btn-danger">Cancel</a>
					@can('wholesaler')
						<button type="submit" name="submit" class="btn btn-success mr-0 float-right">@if ($o->exists)Save @else Add @endif</button>
					@endcan
				</div>
			</div>
		</form>
	</div>
</div>

@section('page-scripts')
	@css(select2)
	@css(simplemde)
	@js(select2,autofocus)
	@js(simplemde)

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

			$.ajax({
				type: 'GET',
				dataType: 'json',
				data: {type: type},
				cache: false,
				url: '{{ url('api/a/supplier_products') }}',
				timeout: 2000,
				error: function(x) {
					// @todo add a spinner
					//spinner.toggleClass('d-none').toggleClass('fa-spin');
					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() {
			new SimpleMDE({ element: $('.textarea')[0], forceSync: true });

			$('#model').on('change',function(item) {
				if ($(this).val()) {
					$('#supplier_product').show();
					supplier_products($(this).val(),$('#model_id'));

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

			$('#model_id').select2();
			$('.select').select2();

			// After we render the page, hide the supplier_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())
				$('#supplier_product').hide();
			else
				supplier_products($('#model').val(),$('#model_id'),{{ old('model_id',$o->model_id) }});

			$('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