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

class Leaverequests extends CI_Controller {

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

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

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

        $data['employees'] = $this->db
            ->where('is_active',1)
            ->order_by('full_name','ASC')
            ->get('k_employees')
            ->result();

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

    // ==========================================
    // DATATABLE
    // ==========================================
    public function get_data()
    {
        $this->db->select('
            lr.*,
            e.full_name,
            e.fingerprint_user_id
        ');
    
        $this->db->from('k_leave_requests lr');
    
        $this->db->join(
            'k_employees e',
            'e.id=lr.employee_id',
            'left'
        );
    
        $this->db->order_by('lr.id','DESC');
    
        $list = $this->db->get()->result();
    
        $data = [];
        $no   = 1;
    
        foreach($list as $r){
    
            // =====================================
            // LEAVE TYPE
            // =====================================
            $leave_type = '-';
    
            if($r->leave_type == 'annual'){
                $leave_type = 'Cuti Tahunan';
            }
    
            if($r->leave_type == 'sick'){
                $leave_type = 'Sakit';
            }
    
            if($r->leave_type == 'permit'){
                $leave_type = 'Izin';
            }
    
            if($r->leave_type == 'maternity'){
                $leave_type = 'Cuti Melahirkan';
            }
    
            if($r->leave_type == 'unpaid'){
                $leave_type = 'Cuti Tidak Dibayar';
            }
    
            // =====================================
            // STATUS
            // =====================================
            if($r->approval_status == 'approved'){
    
                $status = '
                    <span class="badge bg-success">
                        Disetujui
                    </span>
                ';
    
            } elseif($r->approval_status == 'rejected'){
    
                $status = '
                    <span class="badge bg-danger">
                        Ditolak
                    </span>
                ';
    
            } else {
    
                $status = '
                    <span class="badge bg-warning text-dark">
                        Pending
                    </span>
                ';
            }
    
            $data[] = [
                $no++,
                $r->fingerprint_user_id,
                $r->full_name,
                $leave_type,
                $r->start_date,
                $r->end_date,
                $r->total_days . ' Hari',
                $status,
                '
                    <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_leave_requests',
                ['id'=>$id]
            )
            ->row();

        echo json_encode($data);
    }

    // ==========================================
    // SAVE
    // ==========================================
    public function save()
    {
        try{

            $start_date = $this->input->post('start_date');
            $end_date   = $this->input->post('end_date');

            $total_days = (
                strtotime($end_date)
                -
                strtotime($start_date)
            ) / 86400 + 1;

            $data = [

                'employee_id' => $this->input->post('employee_id'),

                'leave_type' => $this->input->post('leave_type'),

                'start_date' => $start_date,

                'end_date' => $end_date,

                'total_days' => $total_days,

                'reason' => $this->input->post('reason'),

                'approval_status' => $this->input->post('approval_status')
            ];

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

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

        } catch(Exception $e){

            echo json_encode([
                'status' => false,
                'message'=> $e->getMessage()
            ]);
        }
    }

    // ==========================================
    // UPDATE
    // ==========================================
    public function update()
    {
        try{

            $id = $this->input->post('id');

            $start_date = $this->input->post('start_date');
            $end_date   = $this->input->post('end_date');

            $total_days = (
                strtotime($end_date)
                -
                strtotime($start_date)
            ) / 86400 + 1;

            $data = [

                'employee_id' => $this->input->post('employee_id'),

                'leave_type' => $this->input->post('leave_type'),

                'start_date' => $start_date,

                'end_date' => $end_date,

                'total_days' => $total_days,

                'reason' => $this->input->post('reason'),

                'approval_status' => $this->input->post('approval_status')
            ];

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

            $this->db->update(
                'k_leave_requests',
                $data
            );

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

        } catch(Exception $e){

            echo json_encode([
                'status' => false,
                'message'=> $e->getMessage()
            ]);
        }
    }

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

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