<?php

namespace App\Models\Panel\User;

use App\Models\Panel\UserModel;

class StaffModel extends UserModel
{

    public function findId($id)
    {
        $builder = $this->db->table($this->table);
        $builder->select('users.*,GROUP_CONCAT( roles.name) role,mk.id kelas_id,mk.nama nama_kelas,mk.kode kode_kelas');
        $builder->join('users_roles ur', 'ur.user_id=users.id', 'left');
        $builder->join('m_kelas mk', 'mk.id=users.kelas_id', 'left');
        $builder->join('roles', 'roles.id=ur.role_id', 'left');
        $builder->where(['users.id' => $id, 'users.deleted_at' => null]);
        $result = $builder->get()->getRowArray();
        if (!$result) return null;
        $user = new \App\Entities\Panel\User();
        $result['role'] = explode(',', $result['role'] ?? '');
        $user->fill($result);
        if ($user->hasRole('admin')) return null;
        return  $user;
    }
    public function findUsername($username)
    {
        $builder = $this->db->table($this->table);
        $builder->select('users.*,GROUP_CONCAT( roles.name) role,mk.id kelas_id,mk.nama nama_kelas,mk.kode kode_kelas');
        $builder->join('users_roles ur', 'ur.user_id=users.id', 'left');
        $builder->join('m_kelas mk', 'mk.id=users.kelas_id', 'left');
        $builder->join('roles', 'roles.id=ur.role_id', 'left');
        $builder->where(['users.username' => $username, 'users.deleted_at' => null]);
        $result = $builder->get()->getRowArray();
        if (!$result) return null;
        $user = new \App\Entities\Panel\User();
        $result['role'] = explode(',', $result['role'] ?? '');
        $user->fill($result);
        if ($user->hasRole('admin')) return null;
        return  $user;
    }

    public function deleteId($id)
    {
        $this->db->query("DELETE users FROM users LEFT JOIN users_roles ur ON ur.user_id=users.id LEFT JOIN roles ON roles.id=ur.role_id WHERE users.id=?", [$id]);
        return $this->db->affectedRows() > 0;
    }

    public function getAll($opt = [])
    {
        $bu = base_url(config('app')->imgPath . "/users");
        $builder = $this->db->table($this->table);
        $builder->select("users.id,CONCAT('$bu/',users.cover) cover,CONCAT('$bu/',users.photo) photo,users.nomor_induk,users.username,users.fullname,users.status,GROUP_CONCAT(roles.name) role,mk.id kelas_id,mk.nama nama_kelas,mk.kode kode_kelas");
        $builder->join('users_roles ur', 'ur.user_id=users.id', 'left');
        $builder->join('m_kelas mk', 'mk.id=users.kelas_id', 'left');
        $builder->join('roles', 'roles.id=ur.role_id', 'left');
        $builder->where(['users.deleted_at' => null]);
        if (isset($opt['role'])) {
            switch (true) {
                case is_array($opt['role']):
                    if (count($opt['role']) < 1) break;
                    if (is_integer($opt['role'][0])) $builder->whereIn('roles.id', $opt['role']);
                    else $builder->whereIn('roles.name', $opt['role']);
                    break;
                case is_string($opt['role']):
                    $builder->where('roles.name', $opt['role']);
                    break;
                case is_integer($opt['role']):
                    $builder->where('roles.id', $opt['role']);
                    break;
            }
        } else {
            $builder->where('roles.id !=', 1);
        }
        $builder->groupBy('users.id');
        $users = [];
        foreach ($builder->get()->getResultArray() as $user) {
            $u = new \App\Entities\Panel\User();
            $user['role'] = explode(',', $user['role'] ?? '');
            $u->fill($user);
            if ($u->hasRole('admin')) continue;
            $users[] = $u;
        }
        return $users;
    }
}
