<?php

namespace App\Exports;

use App\Models\User;
use App\Models\HRMS\EmployeeAttendance;
use App\Models\School;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\FromCollection;
use Illuminate\Support\Facades\Session;

ini_set('gd.jpeg_ignore_warning', 1);
ini_set('memory_limit', '3048M');
class AttendanceExport implements FromCollection, WithHeadings
{

    protected $admin_id;
    protected $school_id, $department_id, $branch_id,$start_date,$end_date;
    public function __construct()
    {
            // fetch session and use it in entire class with constructor
            if (session()->get('loggedIn')['user_type'] == 'admin') {
                $this->admin_id = session()->get('loggedIn')['id'];
                $this->school_id = School::where("admin_id",$this->admin_id)->pluck("id")->first();
            } else {
                $this->admin_id = session()->get('loggedIn')['admin_id'];
                $this->school_id = School::where("admin_id",$this->admin_id)->pluck("id")->first();
            }
         
            
    }
    public function collection()
	{
        
        $this->department_id = isset($_GET["department"]) ? $_GET["department"] : null;
        $this->branch_id = isset($_GET["branch_id"]) ? $_GET["branch_id"] : null;
        $this->start_date = isset($_GET["start_date"]) ? $_GET["start_date"] : null;
        $this->end_date = isset($_GET["end_date"]) ? $_GET["end_date"] : null;
        
        //return $admin_id;
		$query = EmployeeAttendance::where("employee_attendances.admin_id","=",$this->admin_id)
        ->leftJoin('employees','employees.user_unique_id','=','employee_attendances.user_unique_id')
        ->leftJoin('employee_departments','employee_departments.id','=','employees.department')
        ->leftJoin('schools','schools.id','=','employees.branch_id');
        
        if($this->start_date && $this->end_date)
        {
            $query->whereDate("employee_attendances.date",">=",$this->start_date);
            $query->whereDate("employee_attendances.date","<=",$this->end_date);
        }
        if($this->department_id)
        {
            $query->where('employees.department',$this->department_id);
        }
        if($this->branch_id)
        {
            $query->where('employees.branch_id',$this->branch_id);
        }
        
        
        
		
		
		return $query->select(  
                "schools.school",
                "employees.employee_id",
                "employees.name as employee_name",
                "employee_departments.name as department_name",
                "employees.designation",
                "employee_attendances.date",
                "employee_attendances.status",
                "employee_attendances.in_time",
                "employee_attendances.out_time",
                "employee_attendances.late",
                "employee_attendances.overtime"
            )->orderBy('employee_attendances.created_at', 'ASC')->get();
           
        
	}

	public function headings(): array
	{
		return [
            "Branch Name",
            "Employee Id",
            "Employee Name",
            "Department",
            "Designation",
            "Date",
            "Status",
            "In Time",
            "Out Time",
            "Late",
            "Overtime"
		];
	}
}
