<?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 Dashboard
 *
 * @author Andhy Taslin <andhy@earworks.co.id>
 * @since 1.0
 */
class Dashboard extends MY_Controller {

    public function post_absen_list() {
        $this->_init_api(false);
//        $tasks = Tasks_model::get_dbo()->get();
        $absens = Absen_model::get_dbo()->get();
        return $this->_success(null, [
//                'posts' => $tasks->toArray(),
                'absens' => $absens->toArray(),
        ]);
    }

    public function index() {
        $this->_init_api(true);
        $user_absen = User_absen_model::get_dbo()->where('user_id', $this->user['user']['id'])->first();
        $this->user['absen'] = $user_absen ? $user_absen->absen->toArray() : [];
        $this->_success(null, $this->user);
    }

    public function profile() {
        $this->_init_api(true);
        $user_absen = User_absen_model::get_dbo()->where('user_id', $this->user['user']['id'])->first();
        $this->user['absen'] = $user_absen ? $user_absen->absen->toArray() : [];
        $this->_success(null, $this->user);
    }

    public function visitor_scan() {
        $this->_init_api(true);
        $data = [
            'code' => $this->post('code'),
        ];
        $validate = $this->validate_form(['code'], true);
        if ($validate === true) {
            $user = User_model::get_dbo()->where('identity', $data['code'])->first();
            if (!$user) {
                return $this->_error('User not found');
            } else {
                $return_user = [
                    'id' => $user->id,
                    'identity' => $user->identity,
                    ] + $user->profile->toArray();
                return $this->_success('User scanned', $return_user);
            }
        } else {
            return $validate; // invalid request [post or get] or invalid validation form
        }
    }

    public function visitor_absen() {
        $this->_init_api(true);
        $user_absen = User_absen_model::get_dbo()->where('user_id', $this->user['user']['id'])->first();
        if (!$user_absen) {
            return $this->_error('Does not have absen data');
        }
        $data = [
            'visitor_id' => $this->post('visitor_id')
        ];
        $validate = $this->validate_form(['visitor_id'], true);
        if ($validate === true) {
            $scan_visitor = User_model::get_dbo()->where('id', $data['visitor_id'])->first();
            if (!$scan_visitor) {
                return $this->_error('Visitor not found');
            }
            $result = $user_absen->do_absen_visitor($scan_visitor, $this->user);
            if ($result['status'] == 1) {
                $this->_success($result['msg']);
            } else {
                $error = $result['data'] ? $result['msg'] . "\n- " . implode("\n- ", $result['data']) : $result['msg'];
                return $this->_error($error);
            }
        } else {
            return $validate; // invalid request [post or get] or invalid validation form
        }
    }

    public function absen_list($pages = 1) {
        Ci::helper('pagination');
        $this->_init_api(true);
        $user_absen = User_absen_model::get_dbo()->where('user_id', $this->user['user']['id'])->first();
        if (!$user_absen) {
            redirect('dashboard/not-have-absen');
        }
        $query = Visitor_absen_model::get_dbo('visitor_absen')
            ->select('visitor_absen.*', 'user_profile.name as nama_user', 'visitor_profile.name as nama_visitor', 'visitor_profile.optic as nama_optic')
            ->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', 1);
            ->where('visitor_absen.absen_id', $user_absen->absen_id);

        $query_pagination = query_pagination($query, [
            'per_page' => 200,
            'pages' => $pages,
            'order' => $this->get('order', '-visitor_absen.created_at'),
            'keyword' => $this->get('keyword'),
            'search_field' => ['visitor_profile.name', 'visitor_profile.optic'],
            'uri_segment' => 4,
            'base_url' => 'dashboard/absen-list',
        ]);
        $pagination = create_pagination($query_pagination);
        return $this->_success('get absen success', [
                'data' => $query_pagination['data']->toArray(),
                'links' => $pagination->create_api_links()
        ]);
    }

}
