<?php

namespace App\Controllers\Admin;

use App\Controllers\BaseController;
use App\Models\SettingModel;
use App\Models\UploadModel;
use App\Models\UserModel;
use App\Models\EmailModel;
use App\Models\AuthModel;

class User extends BaseController
{
    public function view_all_users()
    {
        $setting = new SettingModel();
        $user = new UserModel();
        $data['app_setting'] = $setting->get_application_setting();
        $data['all_users'] = $user->get_all_stock_point();
        echo view('admin/include/header', $data);
        echo view('admin/user_management/view_all_users');
        echo view('admin/include/footer');
    }

    public function role_permission()
    {
        $setting = new SettingModel();
        $user = new UserModel();
        $data['app_setting'] = $setting->get_application_setting();
        $data['role_permission'] = $user->get_role_permission_for_role_table();
        $data['tables'] = $user->get_info();
        echo view('admin/include/header', $data);
        echo view('admin/user_management/role_permission');
        echo view('admin/include/footer');
    }

    public function add_new_role()
    {
        $user = new UserModel();
        if($user->check_role_name_status($this->request->getVar('role_name')))
        {
            session()->set('error', "Role Name Already Exits");
            return redirect()->route('admin/user/role_permission');
        }
        else
        {
            $data['role_name']          = $this->request->getVar('role_name');
            $data['blog']               = $this->request->getVar('blog')== 1 ? 1 : 0;
            $data['report']             = $this->request->getVar('report')== 1 ? 1 : 0;
            $data['contact_message']    = $this->request->getVar('contact_message')== 1 ? 1 : 0;
            $data['role_mgmt']          = $this->request->getVar('role_mgmt')== 1 ? 1 : 0;
            $data['emp_mgmt']           = $this->request->getVar('emp_mgmt')== 1 ? 1 : 0;
            $data['main_page_setting']  = $this->request->getVar('main_page_setting')== 1 ? 1 : 0;
            $data['member']             = $this->request->getVar('member')== 1 ? 1 : 0;
            $data['admission']          = $this->request->getVar('admission')== 1 ? 1 : 0;
            $data['employee']           = $this->request->getVar('employee')== 1 ? 1 : 0;
            $data['delete']             = $this->request->getVar('delete')== 1 ? 1 : 0;
            $data['edit']               = $this->request->getVar('edit')== 1 ? 1 : 0;
            $data['is_emp']             = 1;
            if($user->add_new_role_permission($data))
            {
                session()->set('success', "Role Successfully Saved");
                return redirect()->route('admin/user/role_permission');
            }
            else
            {
                session()->set('error', "Role Saved Failed");
                return redirect()->route('admin/user/role_permission');
            }
        }
    }

    public function edit_permission($id)
    {
        $setting = new SettingModel();
        $user = new UserModel();
        $data['app_setting'] = $setting->get_application_setting();
        $data['permission'] = $user->get_role_permission_detaild_by_id($id);
        $data['tables'] = $user->get_info();
        echo view('admin/include/header', $data);
        echo view('admin/user_management/edit_permission');
        echo view('admin/include/footer');
    }

    public function update_role_permission($id)
    {
        $user = new UserModel();

        $data['role_name']          = $this->request->getVar('role_name');
        $data['blog']               = $this->request->getVar('blog')== 1 ? 1 : 0;
        $data['report']             = $this->request->getVar('report')== 1 ? 1 : 0;
        $data['contact_message']    = $this->request->getVar('contact_message')== 1 ? 1 : 0;
        $data['role_mgmt']          = $this->request->getVar('role_mgmt')== 1 ? 1 : 0;
        $data['emp_mgmt']           = $this->request->getVar('emp_mgmt')== 1 ? 1 : 0;
        $data['main_page_setting']  = $this->request->getVar('main_page_setting')== 1 ? 1 : 0;
        $data['member']             = $this->request->getVar('member')== 1 ? 1 : 0;
        $data['admission']          = $this->request->getVar('admission')== 1 ? 1 : 0;
        $data['employee']           = $this->request->getVar('employee')== 1 ? 1 : 0;
        $data['delete']             = $this->request->getVar('delete')== 1 ? 1 : 0;
        $data['edit']               = $this->request->getVar('edit')== 1 ? 1 : 0;

        if($user->update_role_permission($data, $id))
        {
            session()->set('success', "Role Successfully Saved");
            return redirect()->route('admin/user/role_permission');
        }
        else
        {
            session()->set('error', "Role Saved Failed");
            return redirect()->route('admin/user/role_permission');
        }
    }

    public function delete_role_permission($id)
    {
        $user = new UserModel();
        if($user->delete_role_permission($id))
        {
            session()->set('success', "Role Successfully Deleted");
            return redirect()->route('admin/user/role_permission');
        }
        else
        {
            session()->set('error', "Role Deleted Failed");
            return redirect()->route('admin/user/role_permission');
        }
    }

    public function add_new_user()
    {
        $setting = new SettingModel();
        $user = new UserModel();
        $data['app_setting'] = $setting->get_application_setting();
        $data['code'] = $user->get_store_last_code();
        echo view('admin/include/header', $data);
        echo view('admin/user_management/add_new_user');
        echo view('admin/include/footer');
    }

    public function send_email($id)
    {
        $setting = new SettingModel();
        $user = new UserModel();
        $data['app_setting'] = $setting->get_application_setting();
        $data['code'] = $user->get_user_email_by_user_id($id);
        echo view('admin/include/header', $data);
        echo view('admin/user_management/add_new_user');
        echo view('admin/include/footer');
    }

    

    

    

    
}
