<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Classes\PHPMailer;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use App\Models\VisitData;
use Maatwebsite\Excel\Facades\Excel;
use App\Exports\ClientVisitExport;
use Illuminate\Support\Facades\File;

use App\Models\User;

class CronController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function dailyreport(Request $request)
    {
        

        $fileName = 'dailyreport' . time() . '.xlsx';
        // Export the file to the public path
        $file = Excel::store(new ClientVisitExport(), $fileName, 'export_public');
        if($file){
            echo $path = public_path()."<br>";
            echo $url = public_path().'/uploads/'.$fileName;
            //$url = asset('public/uploads/dailyreport1740643401.xlsx');
            $mailConfigData = DB::table('email_config')->first();
            $teamMemberlist = User::where('is_admin',true)->get();
            foreach ($teamMemberlist as $teamMember) {
                $data['from_email'] = $teamMember->sender_email;
                $data['password'] = '';
                $data['from_name'] = $teamMember->sender_name;
                $data['receiver_email'] = $teamMember->Email; //'ketul.ketul.patel@gmail.com';
                $data['subject'] = 'DAILY MIS REPORT';
                $data['body'] = '<p>Hi,</p><p>Please Find the attached Daily MIS Report</p>';
                $data['cc_email'] = $teamMember->cc_email;

                $emailConfig = DB::table('email_config')->first();
                //dd($emailConfig);
                $mail = new PHPMailer();

                $mail->IsSMTP(); // Set mailer to use SMTP
                $mail->Host = $emailConfig->host; // Specify main and backup server
                //$mail->Host = "Give IP Address"; // If the above does not work.
                $mail->Port = $emailConfig->smtp_port; // Set the SMTP port
                $mail->SMTPAuth = true; // Enable SMTP authentication
                $mail->Username = $emailConfig->username; // SMTP username
                $mail->Password = $emailConfig->password; // SMTP password
                $mail->SMTPSecure = $emailConfig->smtp_encryption; // Enable encryption, 'ssl' also accepted

                $mail->From = $data['from_email'];
                $mail->FromName = $data['from_name'];

                $mail->AddAddress($data['receiver_email']); // Add a recipient

                if($data['cc_email'] != null && $data['cc_email'] != ""){
                    $recipients = explode(',', $data['cc_email']);

                    foreach($recipients as $ccemail)
                    {
                       $mail->AddCC($ccemail, $data['from_name']);
                    }
                }

                $mail->IsHTML(true); // Set email format to HTML
                $sub = '=?UTF-8?B?'.base64_encode($data['subject']).'?=';
                $mail->Subject = $sub;
                $mail->Body = $data['body'];

                $mail->addAttachment($url,$fileName);
                $response = $mail->Send();
                echo $response;
            }
            
        }
            
    }

    
    

}
