@extends('layouts.app')
@section('title')Plans  @endsection
@section('content')

<section class="content top-head">
	<div class="row">
		<div class="col-md-12">
			@if (\Session::has('success'))
			    <div class="alert alert-success mt-1 tac">
			        <a class="close" data-dismiss="alert" aria-label="close">&times;</a>
			        <ul>
			            <li>{!! \Session::get('success') !!}</li>
			        </ul>
			    </div>
			@endif
			<div class="box box-primary border-no">
                <div class="box-header with-border blue-back">
                  <h3 class="box-title">{{ __('Create Plan') }}</h3>
                </div>
                <!-- /.box-header -->
                <!-- form start -->
                <div class="box-body">
					<form method="POST" action="{{ route('plans.store') }}">
						@csrf
								<div class="form-group">
				           <label for="plan_name">{{ __('Plan Name') }}</label>
				           <span class="val-in">*</span>
				           <input id="plan_name" type="text" class="form-control{{ $errors->has('plan_name') ? ' is-invalid' : '' }}" name="plan_name" placeholder="Enter Plan Name" autofocus>
				           @if ($errors->has('plan_name'))
				           <span class="invalid-feedback" role="alert">
				            <strong>{{ $errors->first('plan_name') }}</strong>
				          </span>
				          @endif
				        </div>
				        <div class="form-group">
				           <label for="plan_type">{{ __('Plan Type') }}</label>
				           <span class="val-in">*</span>
				           <select class="form-control" name="plan_type" id="plan_type">
				           	<option value="all" selected>All</option>
				           	<option value="agent">Agent</option>
				           </select>
				           @if ($errors->has('plan_type'))
				           <span class="invalid-feedback" role="alert">
				            <strong>{{ $errors->first('plan_type') }}</strong>
				          </span>
				          @endif
				        </div>
				        <div class="form-group agent_listing" style="display: none;">
				           <label for="agent_id">{{ __('Agents') }}</label>
				           <span class="val-in">*</span>
				           <select class="form-control" name="agent_id" id="agent_id">
				           	<option value="" selected disabled>Please select agent</option>
				           	@foreach($agent_manager as $agent)
				           		<option value="{{$agent->id}}">{{$agent->name.' '.$agent->last_name}}</option>
				           	@endforeach
				           </select>
				           @if ($errors->has('agent'))
				           <span class="invalid-feedback" role="alert">
				            <strong>{{ $errors->first('agent') }}</strong>
				          </span>
				          @endif
				        </div>
				        <div class="form-group">
				           <label for="plan_amount">{{ __('Amount') }}</label>
				           <span class="val-in">*</span>
				           <input id="plan_amount" type="number" class="form-control{{ $errors->has('plan_amount') ? ' is-invalid' : '' }}" name="plan_amount" placeholder="Enter Plan Amount" autofocus>
				           @if ($errors->has('plan_amount'))
				           <span class="invalid-feedback" role="alert">
				            <strong>{{ $errors->first('plan_amount') }}</strong>
				          </span>
				          @endif
				        </div>
				        <div class="form-group">
				           <label for="plan_currency">{{ __('Currency') }}</label>
				           <span class="val-in">*</span>
				           <!-- <input id="plan_currency" type="text" class="form-control{{ $errors->has('plan_currency') ? ' is-invalid' : '' }}" name="plan_currency" placeholder="Enter Plan Name" autofocus> -->
				           <select id="plan_currency" name="plan_currency" class="form-control{{ $errors->has('plan_currency') ? ' is-invalid' : '' }}">
				           	<option selected disabled value="">Please choose currency</option>
				           	<option value="usd">USD</option>
				           </select>
				           @if ($errors->has('plan_currency'))
				           <span class="invalid-feedback" role="alert">
				            <strong>{{ $errors->first('plan_currency') }}</strong>
				          </span>
				          @endif
				        </div>
				        <div class="form-group">
				           <label for="interval_count">{{ __('Interval Count') }}</label>
				           <span class="val-in">*</span>
				           <input id="interval_count" type="number" class="form-control{{ $errors->has('interval_count') ? ' is-invalid' : '' }}" name="interval_count" placeholder="Enter Plan Interval Count" autofocus>
				           @if ($errors->has('interval_count'))
				           <span class="invalid-feedback" role="alert">
				            <strong>{{ $errors->first('interval_count') }}</strong>
				          </span>
				          @endif
				        </div>
						<div class="form-group">
				           <label for="billing_period">{{ __('Billing Period') }}</label>
				           <span class="val-in">*</span>
				           <select name="billing_period" id="billing_period" class="form-control{{ $errors->has('billing_period') ? ' is-invalid' : '' }}">
				           	<option disabled selected value=""> Choose billing Method</option>
				           	<option value="week">Weekly</option>
				           	<option value="month">Monthly</option>
				           	<option value="year">Yearly</option>
				           </select>
				           @if ($errors->has('billing_period'))
				           <span class="invalid-feedback" role="alert">
				            <strong>{{ $errors->first('billing_period') }}</strong>
				          </span>
				          @endif
				        </div>
				        <div class="form-group">
				           <label for="description">{{ __('Description') }}</label>
				           <textarea class="form-control" cols="5" rows="5" name="description"></textarea>
				           @if ($errors->has('description'))
				           <span class="invalid-feedback" role="alert">
				            <strong>{{ $errors->first('description') }}</strong>
				          </span>
				          @endif
				        </div>
				        <button type="submit" class="btn btn-primary">Save</button>
					</form>
				</div>
			</div>				
		</div>		
	</div>
</section>

<section class="content top-head">
	<div class="row">
		<div class="col-md-12">
			<div class="box box-primary border-no">
                <div class="box-header with-border blue-back">
                  <h3 class="box-title">{{ __('Plan List') }}</h3>
                </div>
                <!-- /.box-header -->
                <!-- form start -->
                <div class="box-body">
                	<div class="table-responsive">
                		<table class="table table-bordered">
                			<thead>
                				<tr>
                					<th>Plan Name</th>
                					<th>Amount</th>
                					<th>Currency</th>
                					<th>Type</th>
                					<th>Agent</th>
                					<th>Interval</th>
                					<th>Description</th>
                				</tr>
                			</thead>
                			<tbody>
                				@if(isset($plans))
                				@foreach($plans as $plan)
                				<tr>
                					<td>{{$plan->name ?? ''}}</td>
                					<td>{{ isset($plan->price) ? '$'.number_format($plan->price / 100,2) : '' }}</td>
                					<td>{{ isset($plan->currency) ? strtoupper($plan->currency) : '' }}</td>
                					<td>{{ isset($plan->plan_type) && $plan->plan_type == 'agent' ? 'Agent' : 'All' }}</td>
                					<td>
                						@if(isset($plan->agent_id))
													    @foreach($agents as $agent)
													        @if($agent->id == $plan->agent_id)
													            {{ $agent->name.' '.$agent->last_name  }}
													        @endif
													    @endforeach
														@endif
													</td>
                					<td>{{ isset($plan->billing_method) ? strtoupper($plan->billing_method.'ly') : ''}}</td>
                					<td>{{ isset($plan->description) ? strtoupper($plan->description) : '' }}</td>
                				</tr>
                				@endforeach
                				@endif
                			</tbody>
                		</table>
                	</div>
				</div>
			</div>				
		</div>		
	</div>
</section>
<script type="text/javascript">
	$(document).on('change','#plan_type', function () {
		var val = $(this).val();
		if(val == 'agent') {
			$('.agent_listing').show();
		} else {
			$('.agent_listing').hide();
		}
	})
</script>
@endsection