<?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');
use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Database\Eloquent\Builder;
/**
 * Description of MY_Eloquent
 *
 * @author Andhy Taslin <andhy@earworks.co.id>
 * @since 1.0
 */
class MY_Model extends Eloquent {

    public static $relation_list = [];
    public static $CI;

    // status
    const STATUS_ACTIVE = 1;
    const STATUS_BANNED = 2;
    // active;
    const ACTIVE = 1;
    const INACTIVE = 0;

    public static function status_label($selected = null) {
        $lib = [
            self::STATUS_ACTIVE => lang('ACTIVE'),
            self::STATUS_BANNED => lang('BANNED'),
        ];
        return array_key_exists($selected, $lib) ? $lib[$selected] : $lib;
    }

    public static function active_label($selected = null) {
        $lib = [
            self::STATUS_ACTIVE => lang('ACTIVE'),
            self::STATUS_BANNED => lang('BANNED'),
        ];
        return array_key_exists($selected, $lib) ? $lib[$selected] : $lib;
    }

    public function __construct(array $attributes = array()) {
        parent::__construct($attributes);
        static::$CI = & get_instance();
    }

    public function get_display() {
        if (isset($this->name)) {
            return $this->name;
        } else {
            foreach ($this->attributes as $key => $value) {
                if ($key != "id") {
                    return $value;
                }
            }

            return null;
        }
    }

    public static function random_string($length = 64, $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') {
        $characters = $pool;
        $charactersLength = strlen($characters);
        $randomString = '';
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[rand(0, $charactersLength - 1)];
        }
        return $randomString;
    }

    public static function relation_list() {
        $all_relation_list = array();

        foreach (static::$relation_list as $key => $value) {
            $all_relation_list[] = $key;
            if (!class_exists($value))
                static::$CI->load->model($value);
            if (count($value::relation_list()) > 0) {
                foreach ($value::relation_list() as $child_key => $child_value) {
                    $all_relation_list[] = $key . "." . $child_value;
                }
            }
        }
        return $all_relation_list;
    }

    /**
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public static function get_dbo($base_table = null) {
        $active_field = ($base_table) ? $base_table . '.active' : 'active';
        return static::with(self::relation_list())->where($active_field, true);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public static function get_dbo_inactive($base_table = null) {
        $active_field = ($base_table) ? $base_table . '.active' : 'active';
        return static::with(self::relation_list())->where("active", false);
    }

    public function save_update($data) {
        $this->fillable(array_keys($data));
        $this->fill($data);
        return $this->save();
    }

    /**
     * Set the keys for a save update query.
     *
     * @param  \Illuminate\Database\Eloquent\Builder  $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    protected function setKeysForSaveQuery(Builder $query) {
        $keys = $this->getKeyName();
        if (!is_array($keys)) {
            return parent::setKeysForSaveQuery($query);
        }

        foreach ($keys as $keyName) {
            $query->where($keyName, '=', $this->getKeyForSaveQuery($keyName));
        }

        return $query;
    }

    /**
     * Get the primary key value for a save query.
     *
     * @param mixed $keyName
     * @return mixed
     */
    protected function getKeyForSaveQuery($keyName = null) {
        if (is_null($keyName)) {
            $keyName = $this->getKeyName();
        }

        if (isset($this->original[$keyName])) {
            return $this->original[$keyName];
        }

        return $this->getAttribute($keyName);
    }

//    public function set_visible_all() {
//        $this->makeVisible($this->hidden);
//        return $this;
//    }
}
