<?php

use app\libraries\Userstamp;

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
/* Author: Jorge Torres
 * Description: Login model class
 */

class System_admin_tenant_model extends CI_Model
{
    protected $table = 'mas_admin_tenant';

    public function __construct()
    {
        parent::__construct();
    }

    public function listing()
    {
        return $this->db->from($this->table)
            ->join('mas_tenant', 'mas_admin_tenant.tenant_no = mas_tenant.code')
            ->select('mas_admin_tenant.username, mas_tenant.code, mas_tenant.name')
            ->get()
            ->result_array();
    }

    public function listing_username($username)
    {
        return $this->db->from($this->table)
            ->join('mas_tenant', 'mas_admin_tenant.tenant_no = mas_tenant.code')
            ->where('mas_admin_tenant.username', $username)
            ->select('mas_admin_tenant.username, mas_tenant.code, mas_tenant.name')
            ->get()
            ->result_array();
    }

    public function save($params, $mode, $id = 0)
    {
        if ($mode === 'c') {
            $this->create($params);
        } else {
            $this->update($params, $id);
        }
        return $this->db->insert_id();
    }

    public function create($params)
    {
        Userstamp::$timestamp = true;
        $this->db->insert($this->table, Userstamp::create($params));
    }

    public function update($params, $id)
    {
        Userstamp::$timestamp = true;
        $this->db->update($this->table, Userstamp::create($params), ['id' => $id]);
    }

    public function create_in_employee_tenant_siteadmin($params)
    {
        $this->load->model('Tenant_model');
        $tenants = $this->Tenant_model->listing();

        foreach ($tenants as $tenant) {
            $this->create([
                'username' => $params['username'],
                'tenant_no' => $tenant['code'],
            ]);
        }

        return true;
    }

    public function create_in_employee_tenant($params)
    {
        return $this->create([
            'username' => $params['username'],
            'tenant_no' => $params['tenant_no'],
        ]);
    }

    public function update_in_employee_tenant_as_siteadmin($params)
    {
        $this->load->model('Tenant_model');
        $this->db
            ->where('username', $params['username'])
            ->update($this->table, $params);

        $tenants = $this->Tenant_model->listing();

        foreach ($tenants as $tenant) {
            if ($tenant['code'] == $params['tenant_no']) {
                continue;
            }

            $this->create([
                'username' => $params['username'],
                'tenant_no' => $tenant['code'],
            ]);
        }

        return true;
    }

    public function update_in_employee_tenant_as_non_siteadmin($params)
    {
        $this->load->model('Tenant_model');
        $this->db
            ->where('username', $params['username'])
            ->update($this->table, $params);

        $tenants = $this->Tenant_model->listing();

        foreach ($tenants as $tenant) {
            if ($tenant['code'] == $params['tenant_no']) {
                continue;
            }

            $this->db->where('tenant_no', $tenant['code'])->delete($this->table);
        }
    }

    public function delete()
    {
    }
}
