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

    public $base = 'permissions/index';

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

    public function index($pages = 1) {
        $this->granted_backoffice(User_model::ACCESS_PERMISSION_LIST);
        
        $query = Permission_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('permissions/index'),
        ]);

        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_PERMISSION_CREATE);
        $data = [
            'group' => $this->post('group'),
            'name' => $this->post('name'),
            'description' => $this->post('description'),
            'status' => $this->post('status')
        ];

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

        if ($validate === true) {
            $model = new Permission_model();
            $model->save_update($data);
            redirect($this->get_redir());
        } elseif ($validate[static::$field_status] === static::CODE_INVALID_PARAMETER) {
           $this->_show_alert($validate);
        }
        Ci::view(__METHOD__, ['data' => $data, 'redir' => $this->get_redir(true)]);
    }

    public function update($id = null) {
        $this->granted_backoffice(User_model::ACCESS_PERMISSION_UPDATE);
        $model = $this->_find($id);

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

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

        if ($validate === true) {
            $model->save_update($data);
            redirect($this->get_redir());
        } elseif ($validate[static::$field_status] === static::CODE_INVALID_PARAMETER) {
            $this->_show_alert($validate);
        }

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

    public function delete($id = null) {
        $this->granted_backoffice(User_model::ACCESS_PERMISSION_DELETE);
        
        $model = $this->_find($id);
        $model->delete();
        redirect($this->get_redir());
    }

    protected function _delete_all($ids) {
        $this->granted_backoffice(User_model::ACCESS_PERMISSION_DELETE);
        
        Permission_model::get_dbo()->whereIn('id', $ids)->delete();
        redirect($this->get_redir());
    }

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

}
