<?php

namespace App\Http\Controllers;

use App\Models\Booking;
use App\Services\HTMLHelper;
use Carbon\Carbon;
use Composer\DependencyResolver\Request;
use DataTables;
use App\User;
use Input;

class DataTableController extends Controller
{
    public function datatable()
    {
        return view('datatable');
    }

    /*public function getLeads()
    {

        $query = User::where('deleted_at', '=', null);

        return \DataTables::eloquent($query)
            ->addColumn('first_name', function (User $user) {
                return $user->account ? $user->account->first_name : '';
            })
            ->addColumn('last_name', function (User $user) {
                return $user->account ? $user->account->last_name : '';
            })
            ->addColumn('action', function (User $user) {
                return '<a href="' . route('admin_lead_edit', [$user->id]) . '" class="btn icon-btn btn-sm btn-primary"><span class="far fa-edit"></span></a>';
            })
            ->addColumn('confirmed', function (User $user) {
                return $user->confirmed ? '<span class="badge badge-pill badge-success"><i class="far fa-check"></i></span>' : '<span class="badge badge-pill badge-danger"><i class="far fa-times"></i></span>';
            })
            ->addColumn('active', function (User $user) {
                return $user->active ? ' <span class="badge badge-pill badge-success"><i class="far fa-check"></i></span>' : '<span class="badge badge-pill badge-danger"><i class="far fa-times"></i></span>';
            })
            ->orderColumn('confirmed', 'confirmed $1')
            ->orderColumn('active', 'active $1')
            ->rawColumns(['action', 'confirmed', 'active'])
            ->make(true);
    }*/

    public function getUsers()
    {
        //confirmation_code_remider is delete 2
        $query = User::where('deleted_at', '=', null);

        return \DataTables::eloquent($query)
            ->addColumn('action_edit', function (User $user) {
                return '<a href="' . route('admin_user_edit', [$user->id]) . '" class="btn icon-btn btn-sm btn-primary"><span class="far fa-edit"></span></a>';
            })
            ->addColumn('admin', function (User $user) {
                return '<a href="#" data-toggle="modal" data-target="#modals-default" data-id="'.$user->id.'" data-email="'.$user->email.'" data-admin="'.$user->admin.'" data-active="'.$user->active.'" data-confirmed="'.$user->confirmed.'">'.HTMLHelper::getRoleLabel($user->admin).'</a>';
            })
            ->addColumn('confirmed', function (User $user) {
                return $user->confirmed ? '<span class="badge badge-pill badge-success"><i class="far fa-check"></i></span>' : '<span class="badge badge-pill badge-danger"><i class="far fa-times"></i></span>';
            })
            ->addColumn('active', function (User $user) {
                return $user->active ? ' <span class="badge badge-pill badge-success"><i class="far fa-check"></i></span>' : '<span class="badge badge-pill badge-danger"><i class="far fa-times"></i></span>';
            })
            ->addColumn('action_delete', function (User $user) {
                return '<a href="' . route('admin_user_delete', [$user->id]) . '" class="btn icon-btn btn-sm btn-danger" onclick="return confirm(\''.__('Really delete entry?').'\');"><span class="far fa-trash"></span></a>';
            })
            ->orderColumn('confirmed', 'confirmed $1')
            ->orderColumn('active', 'active $1')
            ->orderColumn('admin', 'active $1')
            ->rawColumns(['action_edit', 'admin', 'confirmed', 'active', 'action_delete'])
            ->make(true);
    }

    public function getBookings()
    {
        //confirmation_code_remider is delete 2
        $query = Booking::query();


        return \DataTables::eloquent($query)
            ->addColumn('action_edit', function (Booking $booking) {
                return '<a href="' . route('booking_detail', [$booking->id]) . '" class="btn icon-btn btn-sm btn-primary"><span class="far fa-edit"></span></a>';
            })
            ->addColumn('id', function (Booking $booking) {
                return '<a data-order="'.$booking->id.'" href="' . route('booking_detail', [$booking->id]) . '" data-id="'.$booking->id.'">'.$booking->id.'</a>';
            })
            ->orderColumn('id', 'id $1')
            ->rawColumns(['action_edit', 'id'])

            ->make(true);
    }

    public function getRequests()
    {

        $query = Booking::where('lead_id', '!=', NULL);

        if(Input::get('full_firstname_search') != ""){
            $query->where('participant_firstname', 'LIKE', '%'.Input::get('full_firstname_search').'%');
        }

        if(Input::get('full_lastname_search') != ""){
            $query->where('participant_name', 'LIKE', '%'.Input::get('full_lastname_search').'%');
        }

        //   $query->where('end_date', '<=', $now);

        if(Input::get('travel_option_search')){
            $now = Carbon::now();


            switch (Input::get('travel_option_search')){
                case 'before_2':
                    $query->whereBetween('start_date', [Carbon::now()->modify('-2 month'), $now]);

                    break;
                case 'brefore_1':
                    $query->whereBetween('start_date', [Carbon::now()->modify('-1 month'), $now]);

                    break;
                case 'on_site':
                    $query->where('start_date', '<=', $now);
                    $query->where('end_date', '>=', $now);
                    break;
                case 'after_1':
                    $query->whereBetween('start_date', [$now, Carbon::now()->modify('+1 month')]);

                    break;
                case 'after_2':
                    $query->whereBetween('start_date', [$now, Carbon::now()->modify('+2 month')]);

                    break;
            }
        }else{
            $start = null;
            $end = null;
            if(Input::get('arrival_start_date') != ""){
                $arrStart = explode(".", Input::get('arrival_start_date'));
                if(count($arrStart) == 3){
                    $start = Carbon::create($arrStart[2], $arrStart[1], $arrStart[0], 0, 0, 0);
                }
            }
            if(Input::get('arrival_end_date') != ""){
                $arrEnd = explode(".", Input::get('arrival_end_date'));
                if(count($arrEnd) == 3){
                    $end = Carbon::create($arrEnd[2], $arrEnd[1], $arrEnd[0], 23, 59, 59);
                }
            }
            if($start && $end){
                $query->whereBetween('start_date', [$start, $end]);
            }
            if($start && !$end){
                $query->where('start_date', '>=', $start);
            }
            if(!$start && $end){
                $query->where('start_date', '<=', $end);
            }

            $start = null;
            $end = null;
            if(Input::get('departure_start_date') != ""){
                $arrStart = explode(".", Input::get('departure_start_date'));
                if(count($arrStart) == 3){
                    $start = Carbon::create($arrStart[2], $arrStart[1], $arrStart[0], 0, 0, 0);
                }
            }
            if(Input::get('departure_end_date') != ""){
                $arrEnd = explode(".", Input::get('departure_end_date'));
                if(count($arrEnd) == 3){
                    $end = Carbon::create($arrEnd[2], $arrEnd[1], $arrEnd[0], 23, 59, 59);
                }
            }
            if($start && $end){
                $query->whereBetween('end_date', [$start, $end]);
            }
            if($start && !$end){
                $query->where('end_date', '>=', $start);
            }
            if(!$start && $end){
                $query->where('end_date', '<=', $end);
            }
        }



        if(Input::get('sort_travel_country_id') != ""){
            $query->where('travel_country_id', '=', Input::get('sort_travel_country_id'));
        }
        if(Input::get('sort_travelagenda_id') != ""){
            $query->where('travelagenda_id', '=', Input::get('sort_travelagenda_id'));
        }

        if(Input::get('sort_sf_guard_user_id') != ""){
            $query->where('sf_guard_user_id', '=', Input::get('sort_sf_guard_user_id'));
        }

        if(Input::get('sort_travel_documents') != ""){
            $query->where('travel_documents', '=', Input::get('sort_travel_documents'));
        }


        if(Input::get('full_lead_id_search') != ""){
            $query->where('lead_id', 'LIKE',  '%'.Input::get('full_lead_id_search'). '%');
        }
        if(Input::get('full_booking_id_search') != ""){
            $query->where('id', 'LIKE', '%'.Input::get('full_booking_id_search').'%');
        }

        //confirmation_code_remider is delete 2
      //

        return \DataTables::eloquent($query)
            ->addColumn('action_edit', function (Booking $booking) {
                return ''; //'<a href="' . route('booking_detail', [$booking->id]) . '" class="btn icon-btn btn-sm btn-primary"><span class="far fa-edit"></span></a>';
            })
            ->addColumn('lead_id', function (Booking $booking) {
                return '<a data-order="'.$booking->lead_id.'" href="'.make_old_url('leads/'.$booking->lead_id.'/edit').'" data-id="'.$booking->lead_id.'">'.$booking->lead_id.'</a>';
            })
            ->addColumn('participant_firstname', function (Booking $booking) {
                return '<a data-order="'.$booking->participant_firstname.'" href="'.make_old_url('customer/'.$booking->customer_id.'/edit').'" data-id="'.$booking->customer_id.'">'.$booking->participant_firstname.'</a>';
            })
            ->addColumn('participant_name', function (Booking $booking) {
                return '<a data-order="'.$booking->participant_name.'" href="'.make_old_url('customer/'.$booking->customer_id.'/edit').'" data-id="'.$booking->customer_id.'">'.$booking->participant_name.'</a>';
            })
            ->addColumn('id', function (Booking $booking) {
                return '<a data-order="'.$booking->id.'" href="'.make_old_url('booking/'.$booking->id.'/edit').'" data-id="'.$booking->id.'">'.$booking->id.'</a>';
            })
            ->addColumn('travel_country_id', function (Booking $booking) {
                return '<span data-order="'.($booking->travel_country_id ? $booking->travel_country_id : 0).'">'.($booking->travel_country_id ? $booking->travel_country->name : "-").'</span>';
            })
            ->addColumn('travelagenda_id', function (Booking $booking) {
                return '<span data-order="'.($booking->travelagenda_id ? $booking->travelagenda_id : 0).'">'.($booking->travelagenda_id ? $booking->travel_agenda->name : "-").'</span>';
            })
            ->addColumn('start_date', function (Booking $booking) {
                return $booking->getStartDateFormat();
            })
            ->addColumn('end_date', function (Booking $booking) {
                return $booking->getEndDateFormat();
            })
            ->addColumn('travel_documents', function (Booking $booking) {
                return $booking->travel_documents ? ' <span data-order="1" class="badge badge-pill badge-success"><i class="far fa-check"></i></span>' : '<span data-order="0"  class="badge badge-pill badge-danger"><i class="far fa-times"></i></span>';
            })
            ->addColumn('sf_guard_user_id', function (Booking $booking) {
                return '<span data-order="'.($booking->sf_guard_user_id ? $booking->sf_guard_user_id : 0).'">'.($booking->sf_guard_user_id? $booking->sf_guard_user->first_name." ".$booking->sf_guard_user->last_name : "-").'</span>';
            })
            ->addColumn('lead.status_id', function (Booking $booking) {
                return '<span data-order="'.($booking->lead->status_id ? $booking->lead->status_id : 0).'">'.($booking->lead->status_id ? $booking->lead->status->name : "-").'</span>';
            })
          /*  ->filterColumn('travel_country_id', function($query, $keyword) {

                if($keyword != "") {
                    $query->whereRaw("travel_country_id = ?", $keyword);
                }

            })
            ->filterColumn('travelagenda_id', function($query, $keyword) {
                if($keyword != ""){
                    $query->whereRaw("travelagenda_id = ?", $keyword);

                }
            })
          */
            ->orderColumn('lead_id', 'lead_id $1')
            ->orderColumn('participant_firstname', 'participant_firstname $1')
            ->orderColumn('participant_name', 'participant_name $1')
            ->orderColumn('id', 'id $1')
            ->orderColumn('travel_country_id', 'travel_country_id $1')
            ->orderColumn('travelagenda_id', 'travelagenda_id $1')
            ->orderColumn('sf_guard_user_id', 'sf_guard_user_id $1')
            ->orderColumn('start_date', 'start_date $1')
            ->orderColumn('end_date', 'end_date $1')
            ->orderColumn('travel_documents', 'travel_documents $1')
            ->rawColumns(['action_edit', 'lead_id', 'participant_firstname', 'participant_name', 'travel_country_id', 'travelagenda_id', 'sf_guard_user_id', 'lead.status_id', 'id', 'travel_documents'])
            ->make(true);
    }





}



/**/