<?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\Capsule\Manager as DB;

/**
 * Description of Roles
 *
 * @author Andhy Taslin <andhy@earworks.co.id>
 * @since 1.0
 */
class Roles extends MY_Controller {

    public $base = 'roles/index';

    public function __construct() {
        parent::__construct();
        $this->load->helper(['form', 'pagination']);
    }

    public function index($pages = 1) {
//        $this->granted_backoffice(User_model::ACCESS_ROLE_LIST);
        
        $query = Role_model::get_dbo();
        $query_pagination = query_pagination($query, [
            'per_page' => $this->get('show', 20),
            'pages' => $pages,
            'order' => $this->get('order', 'id'),
            'keyword' => $this->get('keyword'),
            'search_field' => ['name', 'description'],
            'uri_segment' => 3,
            'base_url' => base_url($this->base),
        ]);

        Ci::view(__METHOD__, [
            'query_pagination' => $query_pagination,
            'pagination' => create_pagination($query_pagination),
            'redir' => '?redir=' . urlencode(current_url() . url_query())
        ]);
    }

    public function create() {
//        $this->granted_backoffice(User_model::ACCESS_ROLE_CREATE);
        $model = new Role_model();

        $data = [
            'name' => $this->post('name'),
            'description' => $this->post('description'),
            'login_type' => $this->post('login_type'),
            'status' => $this->post('status')
        ];

        $validate = $this->validate_form(['name', 'login_type', 'status'], false);

        if ($validate === true) {
            DB::beginTransaction();

            $model->save_update($data);

            Role_permission_model::arrange($model->id, $this->post('permissions'));

            DB::commit();

            redirect($this->get_redir());
        } elseif ($validate[static::$field_status] === static::CODE_INVALID_PARAMETER) {
           $this->_show_alert($validate);
        }
        $permission_list = Permission_model::get_dbo()->get();

        Ci::view(__METHOD__, [
            'data' => $data,
            'redir' => $this->get_redir(true),
            'model' => $model,
            'permission_list' => $permission_list
        ]);
    }

    public function update($id = null) {
//        $this->granted_backoffice(User_model::ACCESS_ROLE_UPDATE);
        $model = $this->_find($id);
        $data = [
            'name' => $this->post('name', $model->name),
            'description' => $this->post('description', $model->description),
            'login_type' => $this->post('login_type', $model->login_type),
            'status' => $this->post('status', $model->status)
        ];

        $validate = $this->validate_form(['name'], false);

        if ($validate === true) {
            DB::beginTransaction();

            $model->save_update($data);

            Role_permission_model::arrange($model->id, $this->post('permissions'));

            DB::commit();

            redirect($this->get_redir());
        } elseif ($validate[static::$field_status] === static::CODE_INVALID_PARAMETER) {
            $this->_show_alert($validate);
        }
        $permission_list = Permission_model::get_dbo()->get();

        Ci::view(__METHOD__, [
            'data' => $data,
            'redir' => $this->get_redir(true),
            'model' => $model,
            'permission_list' => $permission_list
        ]);
    }

    public function delete($id = null) {
//        $this->granted_backoffice(User_model::ACCESS_ROLE_DELETE);
        DB::beginTransaction();
        $model = $this->_find($id);
        $model->delete();
        Role_permission_model::get_dbo()->where('role_id', $id)->delete();
        DB::commit();
        redirect($this->get_redir());
    }

    protected function _delete_all($ids) {
//        $this->granted_backoffice(User_model::ACCESS_ROLE_DELETE);
        DB::beginTransaction();
        Role_model::get_dbo()->whereIn('id', $ids)->delete();
        Role_permission_model::get_dbo()->whereIn('role_id', $ids)->delete();
        DB::commit();
        redirect($this->get_redir());
    }

    private function _find($id = null) {
        $model = Role_model::get_dbo()->where('id', $id)->first();
        if (!$model || !$id) {
            // show err
            redirect($this->base);
        }
        return $model;
    }

}
