@extends('layout.base')

@section('content')
<div class="row page-heading">
	<h3>Agregar Proyecto</h3>
</div>
<br />
<form method="post" class="form-horizontal" action="{{url('', ['p' => 'proyectos', 'a' => 'agregar'])}}">
<div class="form-group">
	<div class="col-md-2">Nombre</div>
	<div class="col-md-5"><input type="text" name="descripcion" class="form-control" /></div>
</div>
<div class="form-group">
	<div class="col-md-2">Inmobiliaria</div>
	<div class="col-md-4"><select name="inmobiliaria" class="form-control">
	@foreach ($inmobiliarias as $inmobiliaria)
		<option value="{{$inmobiliaria->rut}}"
		@if ($inmobiliaria->rut == $rut)
			selected="selected"
		@endif
		>{{$inmobiliaria->abreviacion}}</option>
	@endforeach
	</select></div>
</div>
<div class="form-group">
	<div class="col-md-2">Direcci&oacute;n</div>
	<div class="col-md-4"><input type="text" name="calle" class="form-control" /></div>
	<div class="col-md-1"><input type="text" name="numero" class="form-control" /></div>
	<div class="col-md-3"><input type="text" name="extra" class="form-control" /></div>
</div>
<div class="form-group">
	<div class="col-md-offset-2 col-md-4"><select name="region" class="form-control">
	@foreach ($regiones as $region)
		<option value="{{$region->id}}"
		@if ($region->numeral == 'RM')
			selected="selected"
		@endif
		>{{$region->descripcion}}</option>
	@endforeach
	</select></div>
	<div class="col-md-4"><select name="comuna" class="form-control"></select></div>
</div>
<div class="form-group">
	<div class="col-md-2">Fecha de Inicio</div>
	@include('form.fecha')
</div>
<div class="form-group">
	<div class="col-md-offset-2 col-md-3"><input type="submit" value="Agregar" class="form-control" /></div>
</div>
</form>
@endsection

@push('scripts')
<script type="text/javascript">
$(document).ready(function() {
	$("select[name='region']").change(function(e) {
		changeRegion($(this).val());
	});
	changeRegion($("select[name='region']").val());

	$.post('{!!nUrl('ajax', 'calles', ['ajax' => true])!!}', function(data) {
		$("input[name='calle']").typeahead({"source": data});
	}, 'json');
	$("input[name='extra']").typeahead({"source": ['Casa', 'Departamento', 'Oficina', 'Villa']});
});
function changeRegion(region) {
	var jq_comunas = $("select[name='comuna']");
	var region = $("select[name='region']").val();
	jq_comunas.html('');
	return $.post('{!!url('', ['p' => 'ajax', 'a' => 'comunas', 'ajax' => true])!!}', {"region": region}, function(data) {
		info = $.parseJSON(data);
		$.each(info, function(i, e) {
			jq_comunas.append(
					$('<option></option>').attr('value', e.id).html(e.descripcion)
			);
		});
	});
}
</script>
@endpush
