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

class Holidays extends CI_Controller {

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

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

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

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

    // =========================================
    // GET DATA
    // =========================================
    public function get_data()
    {
        $data = $this->db
            ->order_by('holiday_date','DESC')
            ->get('k_holidays')
            ->result();

        $rows = [];

        $no = 1;

        foreach($data as $d){

            $rows[] = [
                $no++,
                date('d M Y', strtotime($d->holiday_date)),
                $d->holiday_name,
                $d->is_national == 1
                    ? '<span class="badge bg-success">Nasional</span>'
                    : '<span class="badge bg-secondary">Perusahaan</span>',
                '
                    <button class="btn btn-warning btn-sm btn-edit"
                            data-id="'.$d->id.'">
                        Edit
                    </button>

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

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

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

        echo json_encode($data);
    }

    // =========================================
    // SAVE
    // =========================================
    public function save()
    {
        $holiday_date = $this->input->post('holiday_date');
        $holiday_name = $this->input->post('holiday_name');
        $is_national  = $this->input->post('is_national');

        $cek = $this->db
            ->get_where('k_holidays',[
                'holiday_date' => $holiday_date
            ])
            ->row();

        if($cek){

            echo json_encode([
                'status' => false,
                'message' => 'Tanggal holiday sudah ada'
            ]);

            return;
        }

        $insert = [
            'holiday_date' => $holiday_date,
            'holiday_name' => $holiday_name,
            'is_national'  => $is_national
        ];

        $this->db->insert('k_holidays',$insert);

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

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

        $cek = $this->db
            ->where('holiday_date',$holiday_date)
            ->where('id !=',$id)
            ->get('k_holidays')
            ->row();

        if($cek){

            echo json_encode([
                'status' => false,
                'message' => 'Tanggal holiday sudah ada'
            ]);

            return;
        }

        $update = [
            'holiday_date' => $holiday_date,
            'holiday_name' => $holiday_name,
            'is_national'  => $is_national
        ];

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

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

    // =========================================
    // DELETE
    // =========================================
    public function delete($id)
    {
        $this->db
            ->where('id',$id)
            ->delete('k_holidays');

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

}