<?php

namespace App\Http\Controllers;

use App\Models\AccountList;
use App\Models\Announcement;
use App\Models\AttendanceEmployee;
use App\Models\Employee;
use App\Models\Event;
use App\Models\LandingPageSection;
use App\Models\Meeting;
use App\Models\Job;
use App\Models\Order;
use App\Models\Payees;
use App\Models\Payer;
use App\Models\Plan;
use App\Models\Ticket;
use App\Models\User;
use App\Models\Utility;
use Illuminate\Support\Facades\Auth;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index()
    {

        if (!Auth::check()) {
            return redirect('login');
        }


        $user = Auth::user();
        if ($user->type == 'employee') {
            $emp = Employee::where('user_id', '=', $user->id)->first();

            $announcements = Announcement::orderBy('announcements.id', 'desc')->take(5)->leftjoin('announcement_employees', 'announcements.id', '=', 'announcement_employees.announcement_id')->where('announcement_employees.employee_id', '=', $emp->id)->orWhere(
                function ($q) {
                    $q->where('announcements.department_id', 0)->where('announcements.employee_id', 0);
                }
            )->get();

            $employees = Employee::get();
            $meetings = Meeting::orderBy('meetings.id', 'desc')->take(5)->leftjoin('meeting_employees', 'meetings.id', '=', 'meeting_employees.meeting_id')->where('meeting_employees.employee_id', '=', $emp->id)->orWhere(
                function ($q) {
                    $q->where('meetings.department_id', 0)->where('meetings.employee_id', 0);
                }
            )->get();
            $events = Event::select('events.*', 'events.id as event_id', 'event_employees.*')->leftjoin('event_employees', 'events.id', '=', 'event_employees.event_id')->where('event_employees.employee_id', '=', $emp->id)->orWhere(
                function ($q) {
                    $q->where('events.department_id', 0)->where('events.employee_id', 0);
                }
            )->get();
            $arrEvents = [];

            foreach ($events as $event) {

                $arr['id'] = $event['event_id'];
                $arr['title'] = $event['title'];
                $arr['start'] = $event['start_date'];
                $arr['end'] = $event['end_date'];
                $arr['className'] = $event['color'];
                // $arr['borderColor']     = "#fff";
                $arr['url'] = (!empty($event['event_id'])) ? route('eventsshow', $event['event_id']) : '0';
                //  $arr['url']                = (!empty($event['event_id'])) ? route('eventsshow', $event['event_id']) : '0';

                // $arr['textColor']       = "white";
                $arrEvents[] = $arr;
            }

            $date = date("Y-m-d");
            $time = date("H:i:s");
            $employeeAttendance = AttendanceEmployee::orderBy('id', 'desc')->where('employee_id', '=', !empty(\Auth::user()->employee) ? \Auth::user()->employee->id : 0)->where('date', '=', $date)->first();

            $officeTime['startTime'] = Utility::getValByName('company_start_time');
            $officeTime['endTime'] = Utility::getValByName('company_end_time');

            return view('dashboard.dashboard', compact('arrEvents', 'announcements', 'employees', 'meetings', 'employeeAttendance', 'officeTime'));
        } else if ($user->type == 'super admin') {
            $user = \Auth::user();
            $user['total_user'] = $user->countCompany();
            $user['total_paid_user'] = $user->countPaidCompany();
            $user['total_orders'] = Order::total_orders();
            $user['total_orders_price'] = Order::total_orders_price();
            $user['total_plan'] = Plan::total_plan();
            $user['most_purchese_plan'] = (!empty(Plan::most_purchese_plan()) ? Plan::most_purchese_plan()->name : '');

            $chartData = $this->getOrderChart(['duration' => 'week']);

            return view('dashboard.super_admin', compact('user', 'chartData'));
        } else {

            $events = Event::where('created_by', '=', \Auth::user()->creatorId())->get();
            $arrEvents = [];

            foreach ($events as $event) {
                $arr['id'] = $event['id'];
                $arr['title'] = $event['title'];
                $arr['start'] = $event['start_date'];
                $arr['end'] = $event['end_date'];
                $arr['className'] = $event['color'];
                // $arr['borderColor']     = "#fff";
                // $arr['textColor']       = "white";
                $arr['url'] = route('event.edit', $event['id']);

                $arrEvents[] = $arr;
            }

            $announcements = Announcement::orderBy('announcements.id', 'desc')->take(5)->where('created_by', '=', \Auth::user()->creatorId())->get();

            $employees = User::where('type', '=', 'employee')->where('created_by', '=', \Auth::user()->creatorId())->get();
            $countEmployee = count($employees);

            $user = User::where('type', '!=', 'employee')->where('created_by', '=', \Auth::user()->creatorId())->get();
            $countUser = count($user);
            $countTicket = Ticket::where('created_by', '=', \Auth::user()->creatorId())->count();
            $countOpenTicket = Ticket::where('status', '=', 'open')->where('created_by', '=', \Auth::user()->creatorId())->count();
            $countCloseTicket = Ticket::where('status', '=', 'close')->where('created_by', '=', \Auth::user()->creatorId())->count();

            $currentDate = date('Y-m-d');

            // $employees     = User::where('type', '=', 'employee')->where('created_by', '=', \Auth::user()->creatorId())->get();
            // $countEmployee = count($employees);
            $notClockIn = AttendanceEmployee::where('date', '=', $currentDate)->get()->pluck('employee_id');

            $notClockIns = Employee::where('created_by', '=', \Auth::user()->creatorId())->whereNotIn('id', $notClockIn)->get();

            $accountBalance = AccountList::where('created_by', '=', \Auth::user()->creatorId())->sum('initial_balance');
            $activeJob = Job::where('status', 'active')->where('created_by', '=', \Auth::user()->creatorId())->count();
            $inActiveJOb = Job::where('status', 'in_active')->where('created_by', '=', \Auth::user()->creatorId())->count();

            $totalPayee = Payees::where('created_by', '=', \Auth::user()->creatorId())->count();
            $totalPayer = Payer::where('created_by', '=', \Auth::user()->creatorId())->count();

            $meetings = Meeting::where('created_by', '=', \Auth::user()->creatorId())->limit(8)->get();

            $users = User::find(\Auth::user()->creatorId());
            // $plan = Plan::find($users->plan);
            $storage_limit = 1000;
            $plan = Plan::first();


            return view('dashboard.dashboard', compact('arrEvents', 'announcements', 'employees', 'activeJob', 'inActiveJOb', 'meetings', 'countEmployee', 'countUser', 'countTicket', 'countOpenTicket', 'countCloseTicket', 'notClockIns', 'accountBalance', 'totalPayee', 'totalPayer', 'users', 'plan', 'storage_limit'));
        }

    }

    public function getOrderChart($arrParam)
    {
        $arrDuration = [];
        if ($arrParam['duration']) {
            if ($arrParam['duration'] == 'week') {
                $previous_week = strtotime("-2 week +1 day");
                for ($i = 0; $i < 14; $i++) {
                    $arrDuration[date('Y-m-d', $previous_week)] = date('d-M', $previous_week);
                    $previous_week = strtotime(date('Y-m-d', $previous_week) . " +1 day");
                }
            }
        }

        $arrTask = [];
        $arrTask['label'] = [];
        $arrTask['data'] = [];
        foreach ($arrDuration as $date => $label) {

            $data = Order::select(\DB::raw('count(*) as total'))->whereDate('created_at', '=', $date)->first();
            $arrTask['label'][] = $label;
            $arrTask['data'][] = $data->total;
        }

        return $arrTask;
    }
}
