<?php

namespace App\Controllers\Admin;

use App\Controllers\BaseController;
use App\Models\SettingModel;
use App\Models\UserModel;

class Employee extends BaseController
{

    function view_all_employee()
    {
        $setting = new SettingModel();
        $user = new UserModel();
        $data['app_setting'] = $setting->get_application_setting();
        if(user()->user_role == 'Admin')
        {
            $data['all_users'] = $user->get_all_employee();
        }
        else
        {
            $data['all_users'] = $user->get_all_employee_by_added_by(user()->user_id);
        }
        echo view('admin/include/header', $data);
        echo view('admin/employee/view_all_users');
        echo view('admin/include/footer');
    }

    function add_new_employee()
    {
        $setting = new SettingModel();
        $user = new UserModel();
        $data['app_setting'] = $setting->get_application_setting();
        $data['all_role'] = $user->get_all_employee_role_for_admin();
        $data['all_designation'] = $user->get_all_designation_role_permission();
        echo view('admin/include/header', $data);
        echo view('admin/employee/add_new_user');
        echo view('admin/include/footer');
    }

    function new_employee_add()
    {
        $user = new UserModel();
        $setting = new SettingModel();
        $password = $this->request->getVar('user_mobile');
        $app_setting = $setting->get_application_setting();
        $data['user_name'] = $this->request->getVar('user_name');
        $data['user_email'] = $this->request->getVar('user_email');
        $data['user_mobile'] = $this->request->getVar('user_mobile');
        $data['user_role'] = $this->request->getVar('user_role');
        if($this->request->getVar('user_role') == 'Teacher')
        {
            $data['user_is_teacher'] = 1;
        }
        $data['user_designation'] = $this->request->getVar('user_designation');
        $data['user_pin_code']  = $this->request->getVar('user_pin_code');
        $data['user_address'] = $this->request->getVar('user_address');
        $data['user_state'] = $this->request->getVar('user_work_state');
        $data['user_dist'] = $this->request->getVar('user_work_dist');
        $data['user_post'] = $this->request->getVar('user_work_taulka');
        $data['user_password'] = hash("sha256", $this->request->getVar('user_mobile'));
        $data['created_at'] = date('Y-m-d');
        $data['is_employee']    = 1;
        $data['user_rand_code'] = rand();
        $data['user_paid_status'] = $this->request->getVar('user_paid_status');
        $data['user_rental_amount'] = $this->request->getVar('user_rental_amount');

        // $data['user_aadhar_card'] = $this->request->getVar('user_aadhar_card');
        // $data['user_pan_card'] = $this->request->getVar('user_pan_card');
        // $data['user_bank_acc'] = $this->request->getVar('user_bank_acc');
        // $data['user_bank_ifsc'] = $this->request->getVar('user_bank_ifsc');
        // $data['user_bank_name'] = $this->request->getVar('user_bank_name');
        // $data['user_bank_branch'] = $this->request->getVar('user_bank_branch');
        // $data['user_status'] = "Active";
        
        
        if($user->add_new_user($data))
        {
            session()->set('success', "Employee Successfully Saved");
            return redirect()->back();
        }
        else
        {
            session()->set('error', "Employee Saved Failed");
            return redirect()->back();
        }
    }

    function update_employee($id)
    {
        $setting = new SettingModel();
        $user = new UserModel();
        $data['app_setting'] = $setting->get_application_setting();
        $data['all_role'] = $user->get_all_employee_role_for_admin();
        $data['user'] = $user->get_user_by_id($id);
        $data['all_designation'] = $user->get_all_designation_role_permission();
        echo view('admin/include/header', $data);
        echo view('admin/employee/update_user');
        echo view('admin/include/footer');
    }

    function employee_update($id)
    {
        $user = new UserModel();
        $data['user_name'] = $this->request->getVar('user_name');
        $data['user_email'] = $this->request->getVar('user_email');
        $data['user_mobile'] = $this->request->getVar('user_mobile');
        $data['user_role'] = $this->request->getVar('user_role');
        $data['user_designation'] = $this->request->getVar('user_designation');
        $data['user_pin_code']  = $this->request->getVar('user_pin_code');
        $data['user_address'] = $this->request->getVar('user_address');
        $data['user_state'] = $this->request->getVar('user_work_state');
        $data['user_dist'] = $this->request->getVar('user_work_dist');
        $data['user_post'] = $this->request->getVar('user_work_taulka');
        $data['user_paid_status'] = $this->request->getVar('user_paid_status');
        $data['user_rental_amount'] = $this->request->getVar('user_rental_amount');
        if($this->request->getVar('user_role') == 'Teacher')
        {
            $data['user_is_teacher'] = 1;
        }
        if($user->update_user($data, $id))
        {
            session()->set('success', "Employee Successfully Saved");
            return redirect()->back();
        }
        else
        {
            session()->set('error', "Employee Saved Failed");
            return redirect()->back();
        }
    }
    
    function delete_employee($id)
    {
        $user = new UserModel();

        $user_img = $user->get_user_by_id($id);
        if(file_exists(substr($user_img->user_image, 1)))
        {
            unlink(substr($user_img->user_image, 1));
        }
        if($user->delete_user($id))
        {
            session()->set('success', "Employee Successfully Deleted");
            return redirect()->route('admin/employee/all_employee');
        }
        else
        {
            session()->set('error', "Employee Deleted Failed");
            return redirect()->route('admin/employee/all_employee');
        }
    }
}
