<?php

namespace App\Http\Controllers;

use App\irsx;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;

class IrsxController extends Controller
{

  public function __construct()
    {
        $this->middleware(['auth', 'pass']);
//        ->except(['create', 'store'])
    }
    
    public function index(){

      // $date1 = Carbon::now()->subYear()->format('Y');
      $date1 = Carbon::now()->format('Y');
      // print($date1);
      $date = (!empty($_GET["date"])) ? ($_GET["date"]) : ($date1);
      $sex = (!empty($_GET["gender"])) ? ($_GET["gender"]) : 'all';
      
      $data = irsx::distinct()->orderBy('year', 'desc')->get(['year']);
      if(!empty($_GET["gender"]) OR !empty($_GET["date"])){

        if(($sex != 'all') AND ($date != 'all')){
          $result = DB::select("SELECT country, year, sex, pathogen, age, specimen, antibiotic,
        SUM(value) FILTER (WHERE status='R') AS Resistance,
        SUM(value) FILTER (WHERE status='I') AS Intermidiate,
        SUM(value) FILTER (WHERE status='S') AS Susceptable,
        SUM(value) FILTER (WHERE status='X') AS None,
        SUM(value) FILTER (WHERE status='NS') AS ns
      FROM ris
      where year = '$date' and sex = '$sex'
      GROUP BY country, year, sex, pathogen, age, specimen, antibiotic");
          }

        elseif(($sex == 'all') AND ($date != 'all')){
          $result = DB::select("SELECT country, year, sex, pathogen, age, specimen, antibiotic,
          SUM(value) FILTER (WHERE status='R') AS Resistance,
          SUM(value) FILTER (WHERE status='I') AS Intermidiate,
          SUM(value) FILTER (WHERE status='S') AS Susceptable,
          SUM(value) FILTER (WHERE status='X') AS None,
          SUM(value) FILTER (WHERE status='NS') AS ns
        FROM ris
        where year = '$date'
        GROUP BY country, year, sex, pathogen, age, specimen, antibiotic");
          }

          elseif(($sex != 'all') AND ($date == 'all')){
            $result = DB::select("SELECT country, year, sex, pathogen, age, specimen, antibiotic,
          SUM(value) FILTER (WHERE status='R') AS Resistance,
          SUM(value) FILTER (WHERE status='I') AS Intermidiate,
          SUM(value) FILTER (WHERE status='S') AS Susceptable,
          SUM(value) FILTER (WHERE status='X') AS None,
          SUM(value) FILTER (WHERE status='NS') AS ns
        FROM ris
        where sex = '$sex'
        GROUP BY country, year, sex, pathogen, age, specimen, antibiotic");
          }
          else{
            $result = DB::select("SELECT country, year, sex, pathogen, age, specimen, antibiotic,
          SUM(value) FILTER (WHERE status='R') AS Resistance,
          SUM(value) FILTER (WHERE status='I') AS Intermidiate,
          SUM(value) FILTER (WHERE status='S') AS Susceptable,
          SUM(value) FILTER (WHERE status='X') AS None,
          SUM(value) FILTER (WHERE status='NS') AS ns
        FROM ris
        GROUP BY country, year, sex, pathogen, age, specimen, antibiotic");
          }
  } else{
    $result = DB::select("SELECT country, year, sex, pathogen, age, specimen, antibiotic,
          SUM(value) FILTER (WHERE status='R') AS Resistance,
          SUM(value) FILTER (WHERE status='I') AS Intermidiate,
          SUM(value) FILTER (WHERE status='S') AS Susceptable,
          SUM(value) FILTER (WHERE status='X') AS None,
          SUM(value) FILTER (WHERE status='NS') AS ns
        FROM ris
        where year = '$date'
        GROUP BY country, year, sex, pathogen, age, specimen, antibiotic");
  }
        
        

    // return $result;
    return view('pages.risx')
    ->with('risx', $result)
    ->with('data', $data)
    ->with('date', $date)
    ->with('sex', $sex);

    }
}
