<?php

namespace App\Controllers\Admin;

use App\Controllers\BaseController;
use App\Models\SettingModel;
use App\Models\UserModel;
use App\Models\UploadModel;
use App\Models\EmailModel;
use Dompdf\Dompdf;
use Dompdf\Options;

class Member extends BaseController
{

    public function view_all_member()
    {
        $setting = new SettingModel();
        $user = new UserModel();
        $data['app_setting'] = $setting->get_application_setting();
        if(user()->user_role == 'Admin')
        {
            $data['all_members'] = $user->view_all_member();
        }
        else
        {
            $data['all_members'] = $user->view_all_member_for_own(user()->user_id);
        }
        

        echo view('admin/include/header', $data);
        echo view('admin/member/view_all_member');
        echo view('admin/include/footer');
    }

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

    public function add_member()
    {
        $user = new UserModel();
        $upload = new UploadModel();
        $email = new EmailModel();
        $setting = new SettingModel();
        $app_setting = $setting->get_application_setting();

        $workingHours = array(
            'Monday'    => array(
                'start' => isset($_POST['mon-start']) ? $_POST['mon-start'] : ''
            ),
            'Tuesday'   => array(
                'start' => isset($_POST['tue-start']) ? $_POST['tue-start'] : ''
            ),
            'Wednesday'   => array(
                'start' => isset($_POST['wed-start']) ? $_POST['wed-start'] : ''
            ),
            'Thursday'   => array(
                'start' => isset($_POST['thu-start']) ? $_POST['thu-start'] : ''
            ),
            'Friday'   => array(
                'start' => isset($_POST['fri-start']) ? $_POST['fri-start'] : ''
            ),
            'Saturday'   => array(
                'start' => isset($_POST['sat-start']) ? $_POST['sat-start'] : ''
            ),
            'Sunday'   => array(
                'start' => isset($_POST['sun-start']) ? $_POST['sun-start'] : ''
            ),
        );

        $data['user_name']          = $this->request->getVar('user_name');
        $data['user_designation']   = $this->request->getVar('user_designation');
        $data['user_mobile']        = $this->request->getVar('user_mobile');
        $data['user_email']         = $this->request->getVar('user_email');
        $data['user_dob']           = $this->request->getVar('user_dob');
        $data['user_education']     = $this->request->getVar('user_education');
        $data['user_employment']    = $this->request->getVar('user_employment');
        $data['user_pin_code']      = $this->request->getVar('user_pin_code');
        $data['user_address']       = $this->request->getVar('user_address');
        $data['user_post']          = $this->request->getVar('user_post');
        $data['user_dist']          = $this->request->getVar('user_dist');
        $data['user_state']         = $this->request->getVar('user_state');
        $data['user_availability']  = json_encode($workingHours);
        $data['user_added_by']      = user()->user_id;
        $data['created_at']         = date('Y-m-d');
        $data['user_role']          = 'Member';
        $data['user_password']      = hash("sha256", $this->request->getVar('user_mobile'));
        $data['user_rand_code']     = rand();
        $data['user_is_teacher']        = $this->request->getVar('user_is_teacher') == 1 ? 1 : 0;
        $data['user_other_organisation'] = $this->request->getVar('user_other_organisation');
        $data['user_employment_nature'] = $this->request->getVar('user_employment_nature');
        $data['user_empoyed_company_name'] = $this->request->getVar('user_empoyed_company_name');
        $data['user_work_nature'] = $this->request->getVar('user_work_nature');

        $data['user_gurdian_status'] = $this->request->getVar('user_gurdian_status');

        $data['father_name'] = $this->request->getVar('father_name');
        $data['mother_name'] = $this->request->getVar('mother_name');
        $data['gurdian_name'] = $this->request->getVar('gurdian_name');
        $data['orphange_name'] = $this->request->getVar('orphange_name');


        if($this->request->getFile('user_image')->isValid() && !$this->request->getFile('user_image')->hasMoved())
        {
            $data['user_image'] = $upload->upload_image($this->request->getFile('user_image'));
        }

        if($user->add_member($data))
        {
            if($this->request->getVar('user_email'))
            {
                helper('pdf_helper');

                if($this->request->getVar('user_gurdian_status') == 'Father')
                {
                    $data['image_path'] = 'back/id_card/father.jpg';
                }
                elseif($this->request->getVar('user_gurdian_status') == 'Father & Mother')
                {
                    $data['image_path'] = 'back/id_card/father_mother.jpg';
                }
                elseif($this->request->getVar('user_gurdian_status') == 'Mother')
                {
                    $data['image_path'] = 'back/id_card/mother.jpg';
                }
                elseif($this->request->getVar('user_gurdian_status') == 'Gurdian')
                {
                    $data['image_path'] = 'back/id_card/gurdian.jpg';
                }
                else
                {
                    $data['image_path'] = 'back/id_card/orphange.jpg';
                }

                $html = view('id_card', $data);

                generate_pdf($html, 'id_card_'.$data['user_mobile']);


                $subject = 'Welcome to '.$app_setting->application_name;
                $email_id = $this->request->getVar('user_email');
                $email_data['app_name'] = $app_setting->application_name;
                $email_data['password'] = $this->request->getVar('user_mobile');
                $email_data['mobile'] = $this->request->getVar('user_mobile');
                $msg = view('email/member_registration_email', $email_data);
                $email->send_email($email_id, $subject, $msg, $mailType = 'html', $title = '', $this->request->getVar('user_mobile'));
            }

            session()->set('success', "Member Successfully Added");
            return redirect()->to('admin/member/view_all_member');

        }
        else
        {
            session()->set('error', "Member Added Failed");
            return redirect()->to('admin/member/view_all_member');
        }
    }

    public function pdf()
    {
        $data['image_path'] = 'back/id_card/father.jpg';
        helper('pdf_helper');
        $html = view('id_card', $data);
        generate_pdf($html, 'sample_pdf');
    }

    public function edit_memeber($code)
    {
        if (!check_user_permission('edit')) {
            session()->set('error', "Permission Denied");
            return redirect()->back();
        }
        $setting = new SettingModel();
        $user = new UserModel();
        $data['app_setting'] = $setting->get_application_setting();
        $data['role_permission'] = $user->get_all_designation_role_permission();
        $data['member'] = $user->get_user_by_code($code);
        echo view('admin/include/header', $data);
        echo view('admin/member/edit_member');
        echo view('admin/include/footer');
    }

    public function update_member($code)
    {
        if (!check_user_permission('edit')) {
            session()->set('error', "Permission Denied");
            return redirect()->back();
        }
        $user = new UserModel();
        $upload = new UploadModel();

        $workingHours = array(
            'Monday'    => array(
                'start' => isset($_POST['mon-start']) ? $_POST['mon-start'] : ''
            ),
            'Tuesday'   => array(
                'start' => isset($_POST['tue-start']) ? $_POST['tue-start'] : ''
            ),
            'Wednesday'   => array(
                'start' => isset($_POST['wed-start']) ? $_POST['wed-start'] : ''
            ),
            'Thursday'   => array(
                'start' => isset($_POST['thu-start']) ? $_POST['thu-start'] : ''
            ),
            'Friday'   => array(
                'start' => isset($_POST['fri-start']) ? $_POST['fri-start'] : ''
            ),
            'Saturday'   => array(
                'start' => isset($_POST['sat-start']) ? $_POST['sat-start'] : ''
            ),
            'Sunday'   => array(
                'start' => isset($_POST['sun-start']) ? $_POST['sun-start'] : ''
            ),
        );

        $data['user_name']          = $this->request->getVar('user_name');
        $data['user_designation']   = $this->request->getVar('user_designation');
        $data['user_mobile']        = $this->request->getVar('user_mobile');
        $data['user_email']         = $this->request->getVar('user_email');
        $data['user_dob']           = $this->request->getVar('user_dob');
        $data['user_education']     = $this->request->getVar('user_education');
        $data['user_employment']    = $this->request->getVar('user_employment');
        $data['user_pin_code']      = $this->request->getVar('user_pin_code');
        $data['user_address']       = $this->request->getVar('user_address');
        $data['user_post']          = $this->request->getVar('user_post');
        $data['user_dist']          = $this->request->getVar('user_dist');
        $data['user_state']         = $this->request->getVar('user_state');
        $data['user_address']       = $this->request->getVar('user_address');
        $data['user_gender']        = $this->request->getVar('user_gender');
        $data['user_availability']  = json_encode($workingHours);
        $data['user_is_teacher']    = $this->request->getVar('user_is_teacher') == 1 ? 1 : 0;
        $data['user_other_organisation'] = $this->request->getVar('user_other_organisation');
        $data['user_employment_nature'] = $this->request->getVar('user_employment_nature');
        $data['user_empoyed_company_name'] = $this->request->getVar('user_empoyed_company_name');
        $data['user_work_nature'] = $this->request->getVar('user_work_nature');
        
        $image = $user->get_user_by_code($code);
        if($this->request->getFile('user_image')->isValid() && !$this->request->getFile('user_image')->hasMoved())
        {
            if (file_exists(substr($image->user_image, 1))) {
                unlink(substr($image->user_image, 1));
            }
            $data['user_image'] = $upload->upload_image($this->request->getFile('user_image'));
        }

        if($user->update_member($data, $code))
        {
            session()->set('success', "Member Successfully Updated");
            return redirect()->to('admin/member/view_all_member');
        }
        else
        {
            session()->set('error', "Member Updated Failed");
            return redirect()->to('admin/member/view_all_member');
        }
    }

    public function delete_member($code)
    {
        if (!check_user_permission('delete')) {
            session()->set('error', "Permission Denied");
            return redirect()->back();
        }
        $user = new UserModel();
        $image = $user->get_user_by_code($code);
        if (file_exists(substr($image->user_image, 1))) {
                unlink(substr($image->user_image, 1));
            }
        if($user->delete_member($code))
        {
            session()->set('success', "Member Successfully Deleted");
            return redirect()->to('admin/member/view_all_member');
        }
        else
        {
            session()->set('error', "Member Deleted Failed");
            return redirect()->to('admin/member/view_all_member');
        }
    }

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

    public function add_new_designation()
    {
        $user = new UserModel();

        $data['role_name']          = $this->request->getVar('role_name');
        $data['blog']               = $this->request->getVar('blog')== 1 ? 1 : 0;
        $data['setting']            = $this->request->getVar('setting')== 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['venue']              = $this->request->getVar('venue')== 1 ? 1 : 0;
        $data['employee']           = $this->request->getVar('employee')== 1 ? 1 : 0;
        $data['sharnarthi']         = $this->request->getVar('sharnarthi')== 1 ? 1 : 0;
        $data['expense_management'] = $this->request->getVar('expense_management')== 1 ? 1 : 0;
        $data['delete']             = $this->request->getVar('delete')== 1 ? 1 : 0;
        $data['edit']               = $this->request->getVar('edit')== 1 ? 1 : 0;
        $data['is_designation']     = 1;

        if($user->add_new_role_permission($data))
        {
            session()->set('success', "Designation Successfully Saved");
            return redirect()->back();
        }
        else
        {
            session()->set('error', "Designation Saved Failed");
            return redirect()->back();
        }
    }

    public function edit_designation($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/designation/edit_designation');
        echo view('admin/include/footer');
    }

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

        $data['blog']               = $this->request->getVar('blog')== 1 ? 1 : 0;
        $data['setting']            = $this->request->getVar('setting')== 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['venue']              = $this->request->getVar('venue')== 1 ? 1 : 0;
        $data['employee']           = $this->request->getVar('employee')== 1 ? 1 : 0;
        $data['sharnarthi']         = $this->request->getVar('sharnarthi')== 1 ? 1 : 0;
        $data['expense_management'] = $this->request->getVar('expense_management')== 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', "Designation Successfully Saved");
            return redirect()->back();
        }
        else
        {
            session()->set('error', "Designation Saved Failed");
            return redirect()->back();
        }
    }

    public function delete_designation($id)
    {
        $user = new UserModel();
        if($user->delete_role_permission($id))
        {
            session()->set('success', "Designation Successfully Deleted");
            return redirect()->back();
        }
        else
        {
            session()->set('error', "Designation Deleted Failed");
            return redirect()->back();
        }
    }
   
}
