<?php

namespace App\Http\Controllers\Admin;

use App\Models\User;
use App\Models\School;
use App\Models\Classes;
use App\Models\Section;
use App\Helpers\Frontend;
use App\Models\Attendance;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;

class TeacherAttendanceController extends Controller
{
    protected $admin_id = '';
    protected $school_id = '';
    public function __construct()
    {
        $this->middleware(function ($request, $next) {
            // fetch session and use it in entire class with constructor
            $this->school_id = session()->get('school')['id'];
            $this->admin_id = session()->get('loggedIn')['id'];
            return $next($request);
        });
    }
    // View attendance
    public function index(Request $request)
    {
        $data['mainSuperAdminMenu'] = 'teacher_attendance';
        $data['superadminSubMenu'] = 'teacher_attendance';
        $data['classes'] = Classes::where(['admin_id' => $this->admin_id, 'school_id' => $this->school_id])->get();
        $schoolCodes = Frontend::get_schools();
        $data['schools'] = School::Active($schoolCodes)->get();
        // Students
        $data['students'] = NULL;
        $query = User::with('attendance');
        // Gender
        if (!empty($request->gender)) {
            $query->where('user_details.gender', $request->gender);
        }
        // Enrollment number
        if (!empty($request->roll_no)) {
            $query->where('users.name', 'LIKE', '%' . $request->roll_no . '%');
            $query->where('users.user_unique_id', 'LIKE', '%' . $request->roll_no . '%');
        }
        $query->Join('user_details', 'user_details.user_id', '=', 'users.id');
        $query->where('users.status', '1');
        $query->where('users.school_id', $this->school_id);
        $query->where('users.admin_id', $this->admin_id);
        $query->orderBy('user_details.id', 'DESC');
        $data['students'] = $query->get(['users.id', 'users.profile_pics', 'users.user_unique_id', 'user_details.id as admission_no', 'users.name as fullname', 'user_details.gender', 'users.status', 'users.created_at']);
        // dd($data);
        return view('teacher_attendance.list', $data);
    }
    public function mark_attendance(Request $request)
    {
        $rules = [
            'student_ids' => 'required|array',
            'status' => 'required',
            'date' => 'required'
        ];
        $validator = Validator::make($request->all(), $rules);
        if ($validator->fails()) {
            return response()->json($validator->errors(), '422');
        }
        foreach ($request->student_ids as $user_id) {
            $user = User::find($user_id);
            $matchThese = ['user_id' => $user->id, 'date' => $request->date];
            $attData = array(
                'user_id' => $user->id,
                'status' => $request->status,
                'date' => $request->date,
            );
            Attendance::updateOrCreate($matchThese, $attData);
            // Send SMS When User Absent
            if ($request->status == 'absent') {
                $parent = $user->guardian()->first();
                $school = $user->school()->first();
                if (!empty($parent)) {
                    $phone = $user->guardian()->first()->phone;
                    // dd($request->date);
                    Frontend::send_absent_sms($phone, $school->school, $request->date);
                }
            }
        }
        // dd('helo');
        return response()->json(['status' => true, 'message' => 'Bulk attendance marked successfully!'], '200');
    }
    public function single_mark_attendance(Request $request)
    {
        $rules = [
            'user_id' => 'required',
            'status' => 'required',
            'date' => 'required'
        ];
        $validator = Validator::make($request->all(), $rules);
        if ($validator->fails()) {
            return response()->json($validator->errors(), '422');
        }
        $query = User::where('users.id', $request->user_id);
        $user = $query->first('users.*');
        $matchThese = ['user_id' => $request->user_id, 'date' => $request->date];
        $attData = array(
            'user_id' => $request->user_id,
            'status' => $request->status,
            'date' => $request->date,
        );
        Attendance::updateOrCreate($matchThese, $attData);
        // Send SMS When User Absent
        // if ($request->status == 'absent') {
        // 	$parent = $user->guardian()->first();
        // 	$school = $user->school()->first();
        // 	if (!empty($parent)) {
        // 		$phone = $user->guardian()->first()->phone;
        // 		Frontend::send_absent_sms($phone, $school->school, $request->date);
        // 	}
        // }
        return response()->json(['status' => true, 'message' => $user->name . ' Bulk attendance marked successfully!'], '200');
    }
    public function attendance_sheet(Request $request)
    {
        // dd($request->all());
        // dd(Session::get('school')->id);
        $data['mainSuperAdminMenu'] = 'teacher_attendance';
        $data['superadminSubMenu'] = 'teacher_attendance_report';
        $data['school'] = School::where(['id' => $this->school_id, 'admin_id' => $this->admin_id])->first();
        // Students
        $data['className'] = NULL;
        $data['sectionName'] = NULL;
        $data['students'] = NULL;
        $query = User::with('attendance');
        // Gender
        if (!empty($request->gender)) {
            $query->where('user_details.gender', $request->gender);
        }
        // Enrollment number
        if (!empty($request->roll_no)) {
            $query->where('users.name', 'LIKE', '%' . $request->roll_no . '%');
        }
        $query->Join('user_details', 'user_details.user_id', '=', 'users.id');
        $query->where('users.status', '1');
        $query->where('users.school_id', $this->school_id);
        $query->where('users.admin_id', $this->admin_id);
        $query->orderBy('user_details.id', 'DESC');
        $data['students'] = $query->get(['users.id', 'users.profile_pics', 'users.user_unique_id', 'user_details.id as admission_no', 'users.name as fullname', 'user_details.gender', 'users.status', 'users.created_at']);
        // dd($data['students']);
        return view('teacher_attendance.report', $data);
    }
}
