<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Attendancemonitoring extends CI_Controller {

    public function __construct()
    {
        parent::__construct();

        $this->load->database();
    }

    public function index()
    {
        $data = [
            "active_menu" => "attendance_monitoring"
        ];

        $this->load->view('partials/header', $data);
        $this->load->view('employees/attendance_monitoring');
        $this->load->view('partials/footer');
    }

    public function get_data()
    {
        $date = $this->input->get('date');

        if(!$date){
            $date = date('Y-m-d');
        }

        $data = $this->db->query("
            SELECT
                a.*,
                e.employee_code,
                e.full_name,
                s.shift_name
            FROM k_attendances a
            LEFT JOIN k_employees e ON e.id = a.employee_id
            LEFT JOIN k_shifts s ON s.id = a.shift_id
            WHERE a.attendance_date = '$date'
            ORDER BY a.checkin_time DESC
        ")->result();

        $rows = [];
        $no = 1;

        foreach($data as $d){

            $rows[] = [
                $no++,
                $d->employee_code,
                $d->full_name,
                $d->shift_name,
                $d->checkin_time,
                $d->checkout_time,
                $d->attendance_status
            ];
        }

        echo json_encode([
            'data' => $rows
        ]);
    }
}