package Dojo::Controller::Home;
use Mojo::Base 'Mojolicious::Controller';
use Mojo::Template;
use Dojo::Support qw{ log dmph merge_hash load_module get_names};
use Dojo::Conf;

sub tst{
  my $c=shift;
  #  $c->stash( merge_hash( 
  #  (load_module("home/tst"))[0],
  #  (load_module("home/contact"))[0]
  #));
  # $c->stash(apend => ["home/contact"]);
   $c->stash( (load_module("home/tst"))[0] );
  $c->stash(layout=> "defaultContact");
  my $x =  [get_names("public/home/cal/img/")];
  log("@$x");
  $c->stash( ilist => $x);
  $c->dbv->tst;

}

# User ========================================================================
  sub home {
    my $c = shift;
    $c->stash((load_module("home/home"))[0]);
    $c->stash( $c->dbv->mod) ;
  }

  sub pang {
    my $c = shift;
    my ($v,$w)=load_module("home/pang");
    $c->stash($v);
    $c->stash( $c->dbv->md_to_hash(@{$w->{'md'}}) );
  }

  sub cal {
    my $c = shift;
    $c->stash((load_module("home/cal"))[0]);
    my $data=$c->dbv->calendar_events;
    my $block=$c->dbv->calendar_monthblock;
    my %hdata;
    map{ push @{ $hdata{$_->{bid}} },$_; }(@$data);
    $c->stash( r=>\%hdata, b=>$block);
  }

  sub event{
    my $c = shift;
    $c->stash((load_module("home/event"))[0]);
    $c->stash(layout=> "defaultContact");
    $c->stash($c->dbv->event($c->param("id")));
  }

  sub contact{
    my $c = shift;
    if ($c->param("mup")){
      $c->flash(mname => $c->param("mname"));
      $c->redirect_to('contact2');
    }else{ 
      $c->stash((load_module("home/contact"))[0]);
    }
  }

  sub contact2{
    my $c = shift;
    $c->redirect_to("home") unless $c->flash('mname');
    $c->stash( mname=>$c->flash('mname'));
    $c->stash((load_module("home/contact2"))[0]);
  }

  sub store{
    my $c = shift;
    $c->stash((load_module("home/store"))[0]);
    $c->stash( r=>$c->dbv->store);
    $c->stash(layout=> "defaultContact");
  }

  sub tv{
    my $c = shift;
    $c->stash( merge_hash( 
      (load_module("home/tv"))[0],
      (load_module("home/tv/trans"))[0]
    ));
    $c->stash( videos=>$c->dbv->tv_videos, table=>$c->dbv->tv_series);
    $c->stash(layout=> "defaultContact");
  }

  sub podcast{
    my $c = shift;
    $c->stash((load_module("home/podcast"))[0]);
    $c->stash(layout=> "defaultContact");
    $c->stash( t=>$c->dbv->podcast_txt, pod=>$c->dbv->podcast_infoHash);
  }

  sub radio{
    my $c=shift;
    $c->stash((load_module("home/radio"))[0]);
    $c->stash(layout=> "defaultContact");
    $c->stash($c->dbv->mod);
    $c->stash($c->dbv->conf_radio);
    $c->stash(nick=>$c->session("nick"));
  }
  
  sub candy{
    my $c=shift;
    $c->stash( css=>["/ext/candy/libs.min.css","/home/candy/default.css"]);
    $c->stash( js=>["/home/candy/loader.js","/ext/candy/libs.min.js","/ext/candy/candy.min.js"]);

    $c->stash(layout=> "clean");
    $c->stash($c->dbv->conf_chat);
    $c->stash(nick=>$c->session("nick"));
  }

#==============================================================================

  
# Admin =======================================================================
  sub admin_home{
    my $c=shift;
    $c->dbv->umod($c->param('link'),$c->param('update'))  
      if (($c->param("hup") // " " )=~/Cambiar/);
    $c->stash((load_module("home/admin/home"))[0]);
    $c->stash(template=>"home/admin/home");
    $c->stash(layout=>"admin");
    $c->stash( $c->dbv->mod);
  }
  
  sub admin_radio{
    my $c=shift;
    $c->dbv->umod($c->param('link'),'rmod')  
      if (($c->param("hup") // " " )=~/Cambiar/);
    $c->stash($c->dbv->mod);
    $c->stash($c->dbv->conf_radio);
    $c->stash( merge_hash(   
      (load_module("home/admin/radio"))[0],
      (load_module("home/admin/candySwitch"))[0],
    ));
    $c->stash(apend => ["home/admin/candySwitch"]);
    $c->stash(template=>"home/admin/radio");
    $c->stash(layout=>"admin");
  }
  
  sub admin_mensajes{
    my $c=shift;
    $c->stash((load_module("home/admin/mensajes"))[0]);
    $c->stash( h=>$c->dbv->msg_heads);
    $c->stash(template=>"home/admin/mensajes");
    $c->stash(layout=>"admin");
  } 
    
  sub admin_eventos{
    my $c=shift;
    if ( (my $t=$c->param('type') // "") =~/[ecp]/){
      my $id=$c->param('id');
      my $req=$c->param('req');
      if ( $req eq "Eliminar"){
        $c->dbv->event_del($id)  if ($t eq 'e');
        $c->dbv->course_del($id) if ($t eq 'c');
        $c->dbv->place_del($id)  if ($t eq 'p');
      } 
      elsif( $req eq "Aceptar"){
        if($t eq 'e'){
          my @list=map{$c->param($_)}qw/eplace yini mini dini yend mend dend cost promo pen ecourse eimg een id/;
          $c->dbv->event_up(@list) if ($id ==0);
          $c->dbv->event_ch(@list) if ($id >0);
        }
        if($t eq 'c'){
          my @list=map{$c->param($_)}qw/cname csubjects cservices cen id/;
          $c->dbv->course_up(@list) if ($id ==0);
          $c->dbv->course_ch(@list) if ($id >0);
        }
        if($t eq 'p'){
          my @list=map{$c->param($_)}qw/pname paddr pobs pto pst pco plat plng pen id/;
          $c->dbv->place_up(@list) if ($id ==0);
          $c->dbv->place_ch(@list) if ($id >0);
        }
    

      }

      $c->redirect_to("/admin/eventos");
    }
    


    $c->stash( 
      courses=>$c->dbv->courses,
      places=>$c->dbv->places,
      months=>$c->dbv->months, 
      events=>$c->dbv->events, 
      img=>$c->dbv->eimgList,
    );
    $c->stash((load_module("home/admin/event"))[0]);
    $c->stash(template=>"home/admin/event");
    $c->stash(layout=>"admin");
  }

  sub admin_json{
    my $c=shift;
    my $id = $c->param('id');
    my $req = $c->param('dreq');
    my $json = "304";
    $json = $c->dbv->msg($id)    if $req=~/mensaje/;
    $json = $c->dbv->course($id) if $req=~/course/;
    $json = $c->dbv->place($id)  if $req=~/place/;
    $json = $c->dbv->eventa($id)  if $req=~/event/;
    $c->render(json=>$json);
  }


    #  if (($c->param("id") // " " ) eq "0") {
    #    $c->dbv->eup if (($c->param("eup") // " " ) =~/Aceptar/);
    #    $c->dbv->cup if (($c->param("cup") // " " ) =~/Aceptar/);
    #    $c->dbv->pup if (($c->param("pup") // " " ) =~/Aceptar/);
    #  } 
    #  else{
    #    if    (($c->param("cup") // " " )=~/Aceptar/){ $c->dbv->cch; }
    #    elsif (($c->param("eup") // " " )=~/Aceptar/){ $c->dbv->ech; }
    #    elsif (($c->param("pup") // " " )=~/Aceptar/){ $c->dbv->pch; }
    #    elsif (($c->param("edel") // " " )=~/Eliminar/){ $c->dbv->edel; }
    #    elsif (($c->param("cdel") // " " )=~/Eliminar/){ $c->dbv->cdel; }
    #    elsif (($c->param("pdel") // " " )=~/Eliminar/){ $c->dbv->pdel; }
    #}};
    #}
    #}
#==============================================================================

1;
