<?php

namespace App\Controllers\Admin;

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

class Email extends BaseController
{
    
    public function send_email($id, $i_d)
    {
        $setting = new SettingModel();
        $user = new UserModel();
        $data['app_setting'] = $setting->get_application_setting();
        $data['email'] = $user->get_user_email_by_user_id($id);
        $data['i_d'] = $i_d;
        echo view('admin/include/header', $data);
        echo view('admin/email/send_email');
        echo view('admin/include/footer');
    }

    function send_new_email()
    {
        $email = new EmailModel();
        $setting = new SettingModel();
        $subject = $this->request->getVar('email_subject');
        $email_data['title'] = $this->request->getVar('email_subject');
        $email_data['content'] = $this->request->getVar('email_content');
        $email_data['app_setting'] = $setting->get_application_setting();
        $msg = view('email/send_email_template', $email_data);
        $email_id = $this->request->getVar('email_id');
        $data = $email->send_email($email_id, $subject, $msg, $mailType = 'html', $subject);
        if($this->request->getVar('email_i_d') == 1)
        {
            if($data == 2)
            {
                session()->set('success', "Email Successfully Send");
                return redirect()->to('admin/report/all_volunteer_report');
            }
            else
            {
                session()->set('error', 'Email Sending Failed. Please Check Server');
                return redirect()->to('admin/report/all_volunteer_report');
            }
        }
        else
        {
            if($data == 2)
            {
                session()->set('success', "Email Successfully Send");
                return redirect()->to('admin/report/all_subscribers_report');
            }
            else
            {
                session()->set('error', 'Email Sending Failed. Please Check Server');
                return redirect()->to('admin/report/all_subscribers_report');
            }
        }
    }

    function sending_email()
    {
        $setting = new SettingModel();
        $user = new UserModel();
        $data['app_setting'] = $setting->get_application_setting();
        echo view('admin/include/header', $data);
        echo view('admin/email/send_email_all');
        echo view('admin/include/footer');
    }

    function send_all_email()
    {
        $setting = new SettingModel();
        $email = new EmailModel();
        if($this->request->getVar('send_post_to_subscribes') == 1 && $this->request->getVar('send_post_to_volunteer') == 1)
        {
            $all_subscribers = $setting->get_all_subscribers_and_volunteer();
        }
        elseif($this->request->getVar('send_post_to_subscribes') == 1)
        {
            $all_subscribers = $setting->get_all_subscribers_for_email();
        }
        elseif($this->request->getVar('send_post_to_volunteer') == 1)
        {
            $all_subscribers = $setting->get_all_volunteer_for_email();
        }
        else
        {
            session()->set('error', 'Please Select Volunteer or Subscribes');
            return redirect()->to('admin/email/sending_email');
        }
        
                
        $subject = $this->request->getVar('email_subject');
        $email_data['content'] = $this->request->getVar('email_content');
        $email_data['title'] = $this->request->getVar('email_subject');
        $email_data['app_setting'] = $setting->get_application_setting();
        $msg = view('email/send_email_template', $email_data);
        foreach ($all_subscribers as $subscribers) 
        {
            $email_id = $subscribers->user_email;
            $email->send_email($email_id, $subject, $msg, $mailType = 'html', $subject);
        }

        session()->set('success', "Email Successfully Send");
        return redirect()->to('admin/email/sending_email');
    }

    

    

    
}
