<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Roles extends Admin_Controller {

    private $perm_category = array();

    function __construct() {
        parent::__construct();
        $this->load->config('mailsms');
        $this->perm_category = $this->config->item('perm_category');
    }

    function index() {
        if (!$this->rbac->hasPrivilege('backup', 'can_view')) {
            access_denied();
        }

        $data['title'] = 'Add Role';
        $this->session->set_userdata('top_menu', 'System Settings');
        $this->session->set_userdata('sub_menu', 'admin/roles');

        $this->form_validation->set_rules(
                'name', 'Name', array(
            'required',
            array('check_exists', array($this->role_model, 'valid_check_exists'))
                )
        );
        if ($this->form_validation->run() == FALSE) {
            $listroute = $this->role_model->get();
            $data['listroute'] = $listroute;
            $this->load->view('layout/header');
            $this->load->view('admin/roles/create', $data);
            $this->load->view('layout/footer');
        } else {
            $data = array(
                'name' => $this->input->post('name')
            );
            $this->role_model->add($data);
            $this->session->set_flashdata('msg', '<div class="alert alert-success text-left">Role added successfully</div>');
            redirect('admin/roles');
        }
    }

    // function permission($id) {
    //     $data['title'] = 'Add Role';
    //     $data['id'] = $id;
    //     $role = $this->role_model->get($id);
    //     $data['role'] = $role;
    //     $permission_model = $this->rolepermission_model->getPermissionWithSelectedByRole($id);
    //     $data['permissions'] = $permission_model;
    //     if ($this->input->server('REQUEST_METHOD') == "POST") {
    //         $role_id = $this->input->post('role_id');
    //         $prev_array = $this->input->post('prev_array');
    //         $module_perm = $this->input->post('module_perm');
    //         if (!isset($prev_array)) {
    //             $prev_array = array();
    //         }
    //         if (!isset($module_perm)) {
    //             $module_perm = array();
    //         }
    //         $delete_array = array_diff($prev_array, $module_perm);
    //         $insert_diff = array_diff($module_perm, $prev_array);
    //         $insert_array = array();
    //         if (!empty($insert_diff)) {
    //             foreach ($insert_diff as $key => $value) {
    //                 $insert_array[] = array(
    //                     'role_id' => $role_id,
    //                     'permission_id' => $value
    //                 );
    //             }
    //         }
    //         $this->rolepermission_model->getInsertBatch($insert_array, $role_id, $delete_array);
    //         $this->session->set_flashdata('msg', '<div class="alert alert-success text-left">Permission updated successfully</div>');
    //         redirect('admin/roles/index');
    //     }
    //     $this->load->view('layout/header');
    //     $this->load->view('admin/roles/allotmodule', $data);
    //     $this->load->view('layout/footer');
    // }

    function permission($id) {
        $data['title'] = 'Add Role';
        $data['id'] = $id;
        $role = $this->role_model->get($id);

        $data['role'] = $role;
        $role_permission = $this->role_model->find($role['id']);


        $data['role_permission'] = $role_permission;

        if ($this->input->server('REQUEST_METHOD') == "POST") {

            $per_cat_post = $this->input->post('per_cat');
            $role_id = $this->input->post('role_id');
            $to_be_insert = array();
            $to_be_update = array();
            $to_be_delete = array();
            foreach ($per_cat_post as $per_cat_post_key => $per_cat_post_value) {
                $insert_data = array();
                $ar = array();
                foreach ($this->perm_category as $per_key => $per_value) {
                    $chk_val = $this->input->post($per_value . "-perm_" . $per_cat_post_value);

                    if (isset($chk_val)) {
                        $insert_data[$per_value] = 1;
                    } else {
                        $ar[$per_value] = 0;
                    }
                }

                $prev_id = $this->input->post('roles_permissions_id_' . $per_cat_post_value);
                if ($prev_id != 0) {

                    if (!empty($insert_data)) {
                        $insert_data['id'] = $prev_id;
                        $to_be_update[] = array_merge($ar, $insert_data);
                    } else {
                        $to_be_delete[] = $prev_id;
                    }
                } elseif (!empty($insert_data)) {
                    $insert_data['role_id'] = $role_id;
                    $insert_data['perm_cat_id'] = $per_cat_post_value;
                    $to_be_insert[] = array_merge($ar, $insert_data);
                }
            }
            // echo "<pre/>";
            // // print_r($to_be_insert);
            // // print_r($to_be_update);
            // print_r($to_be_delete);
            // exit();
            $this->role_model->getInsertBatch($role_id, $to_be_insert, $to_be_update, $to_be_delete);
            redirect('admin/roles/permission/' . $id);
        }

        $this->load->view('layout/header');
        $this->load->view('admin/roles/allotmodule', $data);
        $this->load->view('layout/footer');
    }

    function edit($id) {
        $data['title'] = 'Edit Role';
        $data['id'] = $id;
        $editrole = $this->role_model->get($id);
        $data['editrole'] = $editrole;
        $data['name'] = $editrole["name"];

        $this->form_validation->set_rules(
                'name', 'Name', array(
            'required',
            array('check_exists', array($this->role_model, 'valid_check_exists'))
                )
        );
        if ($this->form_validation->run() == FALSE) {
            $listroute = $this->role_model->get();
            $data['listroute'] = $listroute;
            $this->load->view('layout/header');
            $this->load->view('admin/roles/edit', $data);
            $this->load->view('layout/footer');
        } else {
            $data = array(
                'id' => $id,
                'name' => $this->input->post('name')
            );
            $this->role_model->add($data);
            $this->session->set_flashdata('msg', '<div class="alert alert-success text-left">Role updated successfully</div>');
            redirect('admin/roles/index');
        }
    }

    function delete($id) {
        $data['title'] = 'Fees Master List';
        $this->role_model->remove($id);
        redirect('admin/roles/index');
    }

}

?>