<?php

/*
 * *************************************************
 * Copyright (C) 2020 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 Doorprize
 *
 * @author andhy
 */
class Doorprize extends MY_Controller {

    public function __construct() {
        parent::__construct();
//        Ci::set_layout_file('main2');
    }

    public function test() {
        $absen_list = Absen_model::get_dbo()->get()->pluck('id')->toArray();
        $user_list = User_model::get_dbo('users')
                ->select('users.*')
                ->leftJoin('user_roles', 'users.id', '=', 'user_roles.user_id')
                ->leftJoin('roles', 'user_roles.role_id', '=', 'roles.id')
                ->where('roles.id', 3)->get();
        $batch = [];
        foreach ($absen_list as $absen) {
            foreach ($user_list as $user) {
                $batch[] = [
                    'user_id' => 4,
                    'visitor_id' => $user->id,
                    'absen_id' => $absen
                ];
            }
        }
        if ($batch) {
            Visitor_absen_model::insert($batch);
        }
    }

    public function index($location = null, $slug = null) {
        Ci::set_layout_file('main_doorprize');
        $doorprize = Doorprize_model::get_dbo()->where('slug', $slug)->first();
//        $absen_list = Absen_model::get_dbo()->get()->pluck('id')->toArray();
        if (!$location || !$slug || !$doorprize) {
            Ci::view('doorprize/error');
        } else {
            $is_img = false;
            $is_video = false;
            $ext = null;
            if ($doorprize->gambar) {
                $ext = pathinfo($doorprize->gambar, PATHINFO_EXTENSION);
                $is_img = in_array(strtolower($ext), ['png', 'jpg', 'jpeg', 'gif', 'webp', 'tiff']);
                $is_video = in_array(strtolower($ext), ['mp4', 'mov', 'wmv', 'avi', 'flv', 'mkv', 'mpeg']);
            }
//            $user_list = User_model::get_dbo('users')
//                ->select('users.*')
//                ->leftJoin('user_profile', 'users.id', '=', 'user_profile.user_id')
//                ->leftJoin('user_roles', 'users.id', '=', 'user_roles.user_id')
//                ->leftJoin('roles', 'user_roles.role_id', '=', 'roles.id')
//                ->where('roles.login_type', User_model::LOGIN_TYPE_PUBLIC)
//                ->where('user_roles.role_id', 3)
//                ->whereRaw("UPPER(user_profile.location) LIKE '%" . strtoupper($location) . "%'");
//            $absen_count = count($absen_list);
//            if ($absen_count > 0 && $user_list->count() > 0) {
//                foreach ($user_list as $user) {
//                    $user_absen_count = User_absen_model::get_dbo()
//                        ->where('user_id', $user->id)
//                        ->whereIn('absen_id', $absen_list)
//                        ->count();
//                    if ($user_absen_count == $absen_count) {
//                        $user->doorprize = User_model::DOORPRIZE_YES;
//                        $user->save();
//                    }
//                }
//            }
            if ($doorprize->total > 1) {
                $view = 'doorprize/index_multi';
            } else {
                $view = __METHOD__;
            }
            Ci::view($view, [
                'doorprize' => $doorprize,
                'hide_banner' => true,
                'location' => $location,
                'is_img' => $is_img,
                'is_video' => $is_video,
                'ext' => $ext
            ]);
        }
    }

    public function view() {
        $query = User_doorprize_model::get_dbo('user_doorprize')
            ->select('user_doorprize.*', 'user_profile.name AS nama_anggota', 'user_profile.optic AS nama_optic', 'doorprize.nama AS nama_doorprize')
            ->join('user_profile', 'user_doorprize.user_id', '=', 'user_profile.user_id', 'left')
            ->join('doorprize', 'user_doorprize.doorprize_id', '=', 'doorprize.id', 'left')
            ->where('user_doorprize.doorprize_id', 1);
        Ci::view(__METHOD__, ['data' => $query->get(), 'hide_banner' => true]);
    }

    public function ajax_view($doorprize_id = null) {
        $doorprize = Doorprize_model::get_dbo()->where('id', $doorprize_id)->first();
        if ($doorprize) {
            $query = User_doorprize_model::get_dbo('user_doorprize')
                ->select('user_profile.card_name', 'user_profile.name', 'user_profile.optic')
                ->join('user_profile', 'user_doorprize.user_id', '=', 'user_profile.user_id', 'left')
                ->join('doorprize', 'user_doorprize.doorprize_id', '=', 'doorprize.id', 'left')
                ->where('user_doorprize.doorprize_id', $doorprize->id);
//            Ci::view(__METHOD__, ['data' => $query->get(), 'hide_banner' => true]);
            exit(json_encode([
                'status' => 1,
                'data' => $query->get()->toArray(),
                'doorprize' => $doorprize->toArray()
            ]));
        } else {
            exit(json_encode([
                'status' => 2,
                'msg' => 'Doorprize not found'
            ]));
        }
    }

    public function index_ipad() {
        Ci::view(__METHOD__);
    }

    /**
     * 
     * @return JSON 
     *      [user_id] => 2
      [name] => User
      [email] => finch_user@earworks.co.id
      [phone] => 081385189758
      [address] => Senayan Trade Center
      [city] => Jakarta Selatan
      [gender] =>
      [division] =>
      [department] =>
      [title] =>
      [tshirt] =>
      [sleeve] =>
      [point1] =>
      [point2] =>
     */
    public function get_list($location = null) {
        $doorprize_query = User_doorprize_model::get_dbo()->groupBy('user_doorprize.user_id')->get();
        $user_exists = [];
        $optic_exists = [];
        if ($doorprize_query->count() > 0) {
            $user_exists = $doorprize_query->pluck('user_id')->toArray();
            foreach ($doorprize_query as $doorprize_user) {
                if (isset($doorprize_user->user) && !empty($doorprize_user->user)) {
                    $optic_exists[] = $doorprize_user->user->profile->optic;
                }
            }
        }
        $absen_model = Absen_model::get_dbo()
//            ->where('location', strtolower($location))
            ->first();
        if (!$absen_model) {
            exit(json_encode(['status' => 2, 'msg' => 'absen not found']));
        }
// add skipped user id here
// 
// draw user
        $absen = Visitor_absen_model::get_dbo('visitor_absen')
            ->select('user_profile.user_id', 'user_profile.name', 'user_profile.optic', 'user_profile.card_name')
            ->leftJoin('user_profile', 'visitor_absen.visitor_id', '=', 'user_profile.user_id')
            ->leftJoin('users', 'visitor_absen.visitor_id', '=', 'users.id')
            ->leftJoin('user_roles', 'visitor_absen.visitor_id', '=', 'user_roles.user_id')
//            ->leftJoin('roles', 'user_roles.role_id', '=', 'roles.id')
//            ->where('roles.login_type', User_model::LOGIN_TYPE_PUBLIC)
            ->where('user_roles.role_id', 3)
            ->where('users.doorprize', User_model::DOORPRIZE_YES)
            ->where('visitor_absen.absen_id', $absen_model->id);
// undi by user
//        if ($user_exists) {
//            $absen->whereNotIn('visitor_absen.visitor_id', $user_exists); // skip user special condition
//        }
// undi by optic
        if ($optic_exists) {
            $absen->whereNotIn('user_profile.optic', $optic_exists); // skip user special condition
        }
        $absen = $absen->groupBy('visitor_absen.visitor_id')
                ->orderByRaw('RAND()')
                ->get()->toArray();

        exit(json_encode([
            'status' => 1,
            'data' => $absen
        ]));
    }

    public function draw() {
        $register_id = $this->post('registerId');
        $doorprize_id = $this->post('doorprizeId');
        if (is_array($register_id) && !empty($register_id)) {
            foreach ($register_id as $uid) {
                $exists = User_doorprize_model::get_dbo()->where('user_id', $uid)->count();
                if ($exists > 0) {
                    continue;
                }
                $model = new User_doorprize_model();
                $model->doorprize_id = $doorprize_id;
                $model->user_id = $uid;
                $model->save();
            }
        }

        exit(json_encode([
            'status' => 1,
//            'data' => $absen
        ]));
    }

}
