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

    protected $table = "user_absen";
    public $timestamps = true;
    public $incrementing = false;
    public $primaryKey = ['absen_id', 'user_id'];

    public static function arrange($parent_id, $child_ids = []) {
        static::get_dbo()->where('absen_id', $parent_id)->delete();
        $insert = [];
        foreach ($child_ids as $id) {
            $insert[] = [
                'absen_id' => $parent_id,
                'user_id' => $id,
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s'),
                'created_by' => null,
                'updated_by' => null
            ];
        }
        return static::insert($insert);
    }

    public function scopeWhere_key($query, $absen_id = null, $user_id = null) {
        return $query->where('absen_id', $absen_id)->where('user_id', $user_id);
    }

    public function absen() {
        return $this->belongsTo(Absen_model::class, 'absen_id');
    }

    public function user() {
        return $this->belongsTo(User_model::class, 'user_id');
    }

    public function do_absen_visitor($visitor, $user, $data = []) {
        $absen = $this->absen;
        if (!$absen || !$visitor) {
            return ['status' => 2, 'msg' => lang('absen_data_not_exists'), 'data' => []];
        }
        $syarat_and = @unserialize($absen->syarat);
        $result = [];
        $requested = [];
        if ($syarat_and) {
            foreach ($syarat_and as $res1) {
                $syarat_or = @explode('|', $res1);
                $syarat_or_count = Visitor_absen_model::get_dbo('visitor_absen')
                        ->leftJoin('absen', 'visitor_absen.absen_id', '=', 'absen.id')
                        ->where('visitor_id', $visitor->id)
                        ->whereIn('absen.slug', $syarat_or)
                        ->count();
                if ($syarat_or_count >= 1) {
                    $result[] = 'yes';
                } else {
                    $result[] = 'no';
                    $requested_absen = Absen_model::get_dbo()->whereIn('slug', $syarat_or)->get();
                    if (($requested_counter = $requested_absen->count()) > 0) {
                        $requested_text = null;

                        foreach ($requested_absen as $k => $req_absen) {
                            $requested_text .= $req_absen->nama;
                            if ($k != $requested_counter - 1) {
                                $requested_text .= ' / ';
                            }
                        }
                        $requested[] = $requested_text;
                    }
                }
            }
        }
        if (in_array('no', $result)) {
            return ['status' => 2, 'msg' => lang('absen_mandatory_failed'), 'data' => $requested];
        }
        $exists = Visitor_absen_model::get_dbo()
                ->where('absen_id', $this->absen_id)
                ->where('visitor_id', $visitor->id)
                ->count();
        if ($exists > 0) {
            return ['status' => 2, 'msg' => lang('already_absen'), 'data' => []];
        }
        $visitor_absen = new Visitor_absen_model();
        $visitor_absen->absen_id = $this->absen_id;
        $visitor_absen->visitor_id = $visitor->id;
        $visitor_absen->user_id = $user['user']['id'];
        $visitor_absen->souvenir = isset($data['souvenir']) && !empty($data['souvenir']) ? $data['souvenir'] : '0';
        $visitor_absen->created_by = $user['user']['id'];
        $visitor_absen->updated_by = $user['user']['id'];
        if ($visitor_absen->save()) {
            return ['status' => 1, 'msg' => lang('absen_success'), 'data' => []];
        } else {
            return ['status' => 2, 'msg' => lang('absen_failed'), 'data' => []];
        }
        /*
         * UPDATE `users` SET `password` = '$2y$10$L1BZ0N.Xlxyhn9A63lYntuj9nvYvJz94cef2B0vZM1KXsn4miX0Wu', `password_default` = 'sg2022', `deleted_at` = NULL WHERE `users`.`id` = 2; UPDATE `users` SET `password` = '$2y$10$L1BZ0N.Xlxyhn9A63lYntuj9nvYvJz94cef2B0vZM1KXsn4miX0Wu', `password_default` = 'sg2022', `deleted_at` = NULL WHERE `users`.`id` = 3; UPDATE `users` SET `password` = '$2y$10$L1BZ0N.Xlxyhn9A63lYntuj9nvYvJz94cef2B0vZM1KXsn4miX0Wu', `password_default` = 'sg2022', `deleted_at` = NULL WHERE `users`.`id` = 4; UPDATE `users` SET `password` = '$2y$10$L1BZ0N.Xlxyhn9A63lYntuj9nvYvJz94cef2B0vZM1KXsn4miX0Wu', `password_default` = 'sg2022', `deleted_at` = NULL WHERE `users`.`id` = 5; UPDATE `users` SET `password` = '$2y$10$L1BZ0N.Xlxyhn9A63lYntuj9nvYvJz94cef2B0vZM1KXsn4miX0Wu', `password_default` = 'sg2022', `deleted_at` = NULL WHERE `users`.`id` = 6; UPDATE `users` SET `password` = '$2y$10$L1BZ0N.Xlxyhn9A63lYntuj9nvYvJz94cef2B0vZM1KXsn4miX0Wu', `password_default` = '2022', `deleted_at` = NULL WHERE `users`.`id` = 7; UPDATE `users` SET `password` = '$2y$10$L1BZ0N.Xlxyhn9A63lYntuj9nvYvJz94cef2B0vZM1KXsn4miX0Wu', `password_default` = '2022', `deleted_at` = NULL WHERE `users`.`id` = 8; UPDATE `users` SET `password` = '$2y$10$L1BZ0N.Xlxyhn9A63lYntuj9nvYvJz94cef2B0vZM1KXsn4miX0Wu', `password_default` = '2022', `deleted_at` = NULL WHERE `users`.`id` = 9;
         */
    }

}
