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

class Shifts extends CI_Controller {

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

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

    // ==========================================
    // PAGE
    // ==========================================
    public function index()
    {
        $data = [
            "active_menu" => "shift"
        ];

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

    // =====================================================
    // DATATABLE
    // =====================================================
    public function get_data()
    {
        $list = $this->db
            ->order_by('id','DESC')
            ->get('k_shifts')
            ->result();

        $data = [];
        $no   = 1;

        foreach($list as $r){

            $night = $r->is_night_shift == 1
                ? '<span class="badge bg-dark">Night Shift</span>'
                : '<span class="badge bg-success">Normal</span>';

            $data[] = [
                $no++,
                $r->shift_code,
                $r->shift_name,
                $r->checkin_time,
                $r->checkout_time,
                $r->late_tolerance_minutes . ' Menit',
                $r->work_hours . ' Jam',
                $night,
                '
                    <button class="btn btn-warning btn-sm btn-edit"
                            data-id="'.$r->id.'">
                        Edit
                    </button>

                    <button class="btn btn-danger btn-sm btn-delete"
                            data-id="'.$r->id.'">
                        Delete
                    </button>
                '
            ];
        }

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

    // =====================================================
    // DETAIL
    // =====================================================
    public function detail($id)
    {
        $data = $this->db
            ->get_where('k_shifts',['id'=>$id])
            ->row();

        echo json_encode($data);
    }

    // =====================================================
    // SAVE
    // =====================================================
    public function save()
    {
        $data = [
            'shift_code'              => $this->input->post('shift_code'),
            'shift_name'              => $this->input->post('shift_name'),
            'checkin_time'            => $this->input->post('checkin_time'),
            'checkout_time'           => $this->input->post('checkout_time'),
            'late_tolerance_minutes'  => $this->input->post('late_tolerance_minutes'),
            'work_hours'              => $this->input->post('work_hours'),
            'is_night_shift'          => $this->input->post('is_night_shift')
        ];

        $this->db->insert('k_shifts',$data);

        echo json_encode([
            'status' => true,
            'message'=> 'Shift berhasil ditambahkan'
        ]);
    }

    // =====================================================
    // UPDATE
    // =====================================================
    public function update()
    {
        $id = $this->input->post('id');

        $data = [
            'shift_code'              => $this->input->post('shift_code'),
            'shift_name'              => $this->input->post('shift_name'),
            'checkin_time'            => $this->input->post('checkin_time'),
            'checkout_time'           => $this->input->post('checkout_time'),
            'late_tolerance_minutes'  => $this->input->post('late_tolerance_minutes'),
            'work_hours'              => $this->input->post('work_hours'),
            'is_night_shift'          => $this->input->post('is_night_shift')
        ];

        $this->db->where('id',$id);
        $this->db->update('k_shifts',$data);

        echo json_encode([
            'status' => true,
            'message'=> 'Shift berhasil diupdate'
        ]);
    }

    // =====================================================
    // DELETE
    // =====================================================
    public function delete($id)
    {
        $this->db->delete('k_shifts',['id'=>$id]);

        echo json_encode([
            'status' => true,
            'message'=> 'Shift berhasil dihapus'
        ]);
    }
}