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

class Employees extends CI_Controller {

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

        $this->load->database();
        $this->load->helper(['url', 'file']);

        date_default_timezone_set('Asia/Jakarta');
    }

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

        $data['departments'] = $this->db
            ->where('is_active',1)
            ->order_by('department_name','ASC')
            ->get('k_departments')
            ->result();

        $data['positions'] = $this->db
            ->where('is_active',1)
            ->order_by('position_name','ASC')
            ->get('k_positions')
            ->result();

        $data['branches'] = $this->db
            ->order_by('branch_name','ASC')
            ->get('k_branches')
            ->result();

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

    // =========================================================
    // GET ALL
    // =========================================================
    public function getAll()
    {
        try {

            $data = $this->db
                ->select("
                    e.*,

                    d.department_name,
                    p.position_name,
                    b.branch_name
                ")
                ->from('k_employees e')

                ->join(
                    'k_departments d',
                    'd.id = e.department_id',
                    'left'
                )

                ->join(
                    'k_positions p',
                    'p.id = e.position_id',
                    'left'
                )

                ->join(
                    'k_branches b',
                    'b.id = e.branch_id',
                    'left'
                )

                ->order_by('e.department_id','ASC')
                ->get()
                ->result();

            return $this->json([
                'status' => true,
                'data'   => $data
            ]);

        } catch (Exception $e) {

            return $this->json([
                'status'  => false,
                'message' => $e->getMessage(),
                'data'    => []
            ]);
        }
    }

    // =========================================================
    // DETAIL
    // =========================================================
    public function detail($id = null)
    {
        try {

            $data = $this->db
                ->select("
                    e.*,

                    d.department_name,
                    p.position_name,
                    b.branch_name
                ")
                ->from('k_employees e')

                ->join(
                    'k_departments d',
                    'd.id = e.department_id',
                    'left'
                )

                ->join(
                    'k_positions p',
                    'p.id = e.position_id',
                    'left'
                )

                ->join(
                    'k_branches b',
                    'b.id = e.branch_id',
                    'left'
                )

                ->where('e.id',$id)
                ->get()
                ->row();

            if (!$data) {

                return $this->json([
                    'status'  => false,
                    'message' => 'Data tidak ditemukan'
                ]);
            }

            return $this->json([
                'status' => true,
                'data'   => $data
            ]);

        } catch (Exception $e) {

            return $this->json([
                'status'  => false,
                'message' => $e->getMessage()
            ]);
        }
    }

    // =========================================================
    // STORE
    // =========================================================
    public function store()
    {
        try {

            if (empty(trim($this->input->post('full_name')))) {

                return $this->json([
                    'status' => false,
                    'message'=> 'Nama wajib diisi'
                ]);
            }

            if (empty($this->input->post('join_date'))) {

                return $this->json([
                    'status' => false,
                    'message'=> 'Tanggal masuk wajib diisi'
                ]);
            }

            $employee_code = $this->generateEmployeeCode();

            $photo = $this->uploadPhoto();

            $data = $this->prepareData();

            $data['employee_code'] = $employee_code;
            $data['photo']         = $photo;

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

            return $this->json([
                'status'  => true,
                'message' => 'Berhasil simpan data'
            ]);

        } catch (Exception $e) {

            return $this->json([
                'status'  => false,
                'message' => $e->getMessage()
            ]);
        }
    }

    // =========================================================
    // UPDATE
    // =========================================================
    public function update($id = null)
    {
        try {

            $employee = $this->db
                ->get_where('k_employees',['id'=>$id])
                ->row();

            if (!$employee) {

                return $this->json([
                    'status'  => false,
                    'message' => 'Data tidak ditemukan'
                ]);
            }

            $data = $this->prepareData();

            if (!empty($_FILES['photo_file']['name'])) {

                $photo = $this->uploadPhoto();

                if (
                    !empty($employee->photo)
                    &&
                    file_exists(FCPATH . $employee->photo)
                ) {
                    unlink(FCPATH . $employee->photo);
                }

                $data['photo'] = $photo;
            }

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

            return $this->json([
                'status'  => true,
                'message' => 'Berhasil update data'
            ]);

        } catch (Exception $e) {

            return $this->json([
                'status'  => false,
                'message' => $e->getMessage()
            ]);
        }
    }

    // =========================================================
    // DELETE
    // =========================================================
    public function delete($id = null)
    {
        try {

            $employee = $this->db
                ->get_where('k_employees',['id'=>$id])
                ->row();

            if (!$employee) {

                return $this->json([
                    'status'  => false,
                    'message' => 'Data tidak ditemukan'
                ]);
            }

            if (
                !empty($employee->photo)
                &&
                file_exists(FCPATH . $employee->photo)
            ) {
                unlink(FCPATH . $employee->photo);
            }

            $this->db->delete('k_employees',[
                'id'=>$id
            ]);

            return $this->json([
                'status'  => true,
                'message' => 'Berhasil hapus data'
            ]);

        } catch (Exception $e) {

            return $this->json([
                'status'  => false,
                'message' => $e->getMessage()
            ]);
        }
    }

    // =========================================================
    // GENERATE CODE
    // =========================================================
    private function generateEmployeeCode()
    {
        $last = $this->db
            ->order_by('id','DESC')
            ->get('k_employees')
            ->row();

        $number = $last ? $last->id + 1 : 1;

        return 'RJN-' . str_pad($number,5,'0',STR_PAD_LEFT);
    }

    // =========================================================
    // UPLOAD PHOTO
    // =========================================================
    private function uploadPhoto()
    {
        if (empty($_FILES['photo_file']['name'])) {
            return null;
        }

        $path = FCPATH . 'uploads/karyawan/';

        if (!is_dir($path)) {
            mkdir($path,0777,true);
        }

        $config['upload_path']   = './uploads/karyawan/';
        $config['allowed_types'] = 'jpg|jpeg|png|webp';
        $config['max_size']      = 2048;
        $config['encrypt_name']  = true;

        $this->load->library('upload',$config);

        if (!$this->upload->do_upload('photo_file')) {

            throw new Exception(
                strip_tags(
                    $this->upload->display_errors()
                )
            );
        }

        $upload = $this->upload->data();

        return 'uploads/karyawan/' . $upload['file_name'];
    }

    // =========================================================
    // PREPARE DATA
    // =========================================================
    private function prepareData()
    {
        return [

            'nik' => trim($this->input->post('nik')),

            'full_name' => trim(
                $this->input->post('full_name')
            ),

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

            'birth_place' => trim(
                $this->input->post('birth_place')
            ),

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

            'phone' => trim(
                $this->input->post('phone')
            ),

            'email' => trim(
                $this->input->post('email')
            ),

            'address' => trim(
                $this->input->post('address')
            ),

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

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

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

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

            'religion' => trim(
                $this->input->post('religion')
            ),

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

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

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

            'basic_salary' => $this->input->post('basic_salary') ?: 0,

            'bank_name' => trim(
                $this->input->post('bank_name')
            ),

            'bank_account_number' => trim(
                $this->input->post('bank_account_number')
            ),

            'bank_account_name' => trim(
                $this->input->post('bank_account_name')
            ),

            'bpjs_kesehatan_number' => trim(
                $this->input->post('bpjs_kesehatan_number')
            ),

            'bpjs_ketenagakerjaan_number' => trim(
                $this->input->post('bpjs_ketenagakerjaan_number')
            ),

            'fingerprint_user_id' => trim(
                $this->input->post('fingerprint_user_id')
            ),

            'is_active' => $this->input->post('is_active') ?: 1
        ];
    }

    // =========================================================
    // JSON
    // =========================================================
    private function json($data)
    {
        return $this->output
            ->set_content_type('application/json')
            ->set_output(json_encode($data));
    }
}