<?php

/*
 * *************************************************
 * Copyright (C) 2021 Earworks.                    *
 * Codeigniter Project Template Modification       *
 * Please contact our email for more information : *
 * contact@earworks.co.id                          *
 * *************************************************
 */

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

/**
 * Description of Visitor_absen
 *
 * @author Andhy Taslin <andhy@earworks.co.id>
 * @since 1.0
 */
class Visitor_absen extends MY_Controller {

    public $base = 'visitor-absen/index/';

    public function __construct() {
        parent::__construct();
        $this->load->helper(['form', 'pagination', 'slug']);
    }

    public function index($absen_id = null, $pages = 1) {

        $absen = $this->_find_absen($absen_id);

        $query = Visitor_absen_model::get_dbo('visitor_absen')
                ->select('visitor_absen.*','user_profile.name as nama_user', 'visitor_profile.name as nama_visitor')
                ->leftJoin('users', 'visitor_absen.user_id', '=', 'users.id')
                ->leftJoin('user_profile', 'users.id', '=', 'user_profile.user_id')
                ->leftJoin('users as visitors', 'visitor_absen.visitor_id', '=', 'visitors.id')
                ->leftJoin('user_profile as visitor_profile', 'visitors.id', '=', 'visitor_profile.user_id')
                ->where('visitor_absen.absen_id', $absen->id);

        $query_pagination = query_pagination($query, [
            'per_page' => $this->get('show', 20),
            'pages' => $pages,
            'order' => $this->get('order', '-visitor_absen.created_at'),
            'keyword' => $this->get('keyword'),
            'search_field' => ['user_profile.name', 'visitor_profile.name'],
            'uri_segment' => 4,
            'base_url' => $this->base . $absen->id,
        ]);

        Ci::view(__METHOD__, [
            'absen' => $absen,
            'query_pagination' => $query_pagination,
            'pagination' => create_pagination($query_pagination),
            'redir' => '?redir=' . urlencode(current_url() . url_query())
        ]);
    }

    public function delete($absen_id = null, $user_id = null, $visitor_id = null) {
        $this->granted_backoffice(User_model::ACCESS_ABSEN_DELETE);
        $model = $this->_find($absen_id, $user_id, $visitor_id);
        // delete absen
        $model->delete();
        // result
        $this->session->set_flashdata('success', 'Hapus data berhasil');
        redirect($this->get_redir());
    }

    protected function _delete_all($ids) {
        $this->granted_backoffice(User_model::ACCESS_ABSEN_DELETE);
        Capsule::beginTransaction();
        // delete user absen
        User_absen_model::get_dbo()->whereIn('absen_id', $ids)->delete();
        // delete absen
        Absen_model::get_dbo()->whereIn('id', $ids)->delete();
        // commit
        Capsule::commit();
        // result
        $this->session->set_flashdata('success', 'Hapus data berhasil, Nama: ' . $model->nama);
        redirect($this->get_redir());
    }

    private function _find_absen($id = null) {
        $model = Absen_model::get_dbo()->where('id', $id)->first();
        if (!$model || !$id) {
            $this->session->set_flashdata('danger', 'Data tidak ditemukan');
            redirect($this->base);
        }
        return $model;
    }

    private function _find($absen_id = null, $user_id = null, $visitor_id = null) {
        $model = Visitor_absen_model::get_dbo()->whereKey($absen_id, $user_id, $visitor_id)->first();
        if (!$model || !$absen_id || !$user_id || !$visitor_id) {
            $this->session->set_flashdata('danger', 'Data tidak ditemukan');
            redirect($this->base);
        }
        return $model;
    }

}
