@extends('layout.base')

@section('content')
<h2>Contabilidad</h2>
<form action="#" class="form-horizontal">
  <div class="form-group">
    <div class="col-md-1">Mes</div>
    <div class="col-md-3"><select name="mes" class="form-control">
      <option value="">Todo</option>
<?php $f = Carbon\Carbon::today(); ?>
      @for ($y = date('Y'); $y >= date('Y') - 5; $y --)
        @for ($m = 12; $m > 0; $m --)
          @if ($y == date('Y') and $m >= date('m'))
            @continue
          @endif
          <option value="{{$y}}-{{$m}}" @if ($m == $f->month-1 and $y == $f->year) selected="selected" @endif>{{ucwords(strftime('%B - %Y', mktime(0, 0, 0, $m, 1, $y)))}}</option>
        @endfor
      @endfor
    </select></div>
  </div>
</form>
<div id="proyectos">
@include('informes.proyectos', ['a' => 'contabilidad'])
</div>
@endsection

@push('scripts')
<script type="text/javascript">
function changeUrl(url, field, change) {
  if (url.indexOf(field) > -1) {
    var nurl = [];
    var info = url.split('&');
    $.each(info, function(k, v) {
      if (v.indexOf(field) == -1) {
        nurl.push(v);
      } else {
        if (change != '') {
          nurl.push(field + '=' + change);
        }
      }
    });
    nurl = nurl.join('&');
    return nurl;
  } else {
    if (change != '') {
      nurl = url + '&' + field + '=' + change;
      return nurl;
    }
  }
  return url;
}
function updateUrls(select, link_id) {
  var f = $("select[name='" + select + "']").val();
  var links = $('#' + link_id + ' a');
  links.each(function(k, v) {
    url = $(v).attr('href');
    nurl = changeUrl(url, 'fecha', f);
    $(v).attr('href', nurl);
  });
}
$(document).ready(function() {
  $("select[name='mes']").change(function(e) {
    updateUrls('mes', 'proyectos');
  });
  updateUrls('mes', 'proyectos');
});
</script>
@endpush
