<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\LogInformation;
use App\MailTracker;
use App\Models\RAF\RAFLoanDocuments;
use DB;
use Log;
use Mail;
use Storage;
use View;
use Carbon\Carbon;


class HelperController extends Controller {
	public static function insertLogInfo($information_id) {
		$logInfo = LogInformation::where('application_id', $information_id)->first();

		if (@count($logInfo) == 0) {
			$information = DB::table('information')
				->where('id', $information_id)
				->first();

			return $logId = DB::table('log_information')
				->insertGetId([
					'application_id' => $information->id,
					'product_id' => $information->product_id,
					'title' => $information->title,
					'fname' => $information->fname,
					'lname' => $information->lname,
					'full_name' => $information->fname . " " . $information->lname,
					'dob' => $information->dob,
					'emp_status' => $information->emp_status,
					'occupation' => $information->occupation,
					'employer_name' => $information->employer_name,
					'building' => $information->building,
					'premiseName' => $information->premiseName,
					'address1' => $information->address1,
					'address2' => $information->address2,
					'address3' => $information->address3,
					'address4' => $information->address4,
					'postcode' => $information->postcode,
					'formattedPO' => $information->formattedPO,
					'postaltown' => $information->postaltown,
					'county' => $information->county,
					'email' => $information->email,
					'telephone' => $information->telephone,
					'mobile' => $information->mobile,
					'marketingOptOut' => $information->marketingOptOut,
					'under_writing_q1' => $information->under_writing_q1,
					'under_writing_ans' => $information->under_writing_ans,
					'under_writing_accept' => $information->under_writing_accept,
					'insurance_startdate' => $information->insurance_startdate,
					'transfer' => $information->transfer,
					'current_provider' => $information->current_provider,
					'current_policy_date' => $information->current_policy_date,
					'typeofcover' => $information->typeofcover,
					'loan_to_pay' => $information->loan_to_pay,
					'monthlycommitment' => $information->monthlycommitment,
					'monthlygrossincome' => $information->monthlygrossincome,
					'mortgagesalary' => $information->mortgagesalary,
					'mortgage' => $information->mortgage,
					'salary' => $information->salary,
					'age' => $information->age,
					'monthlybenefit' => $information->monthlybenefit,
					'monthlycost' => $information->monthlycost,
					'cover' => $information->cover,
					'cover_term' => $information->cover_term,
					'accept' => $information->accept,
					'accountname' => $information->accountname,
					'accountname2' => $information->accountname2,
					'accountnumber' => $information->accountnumber,
					'sortcode' => $information->sortcode,
					'iban' => $information->iban,
					'bank' => $information->bank,
					'bankbic' => $information->bankbic,
					'branch' => $information->branch,
					'branchbic' => $information->branchbic,
					'branch_address' => $information->branch_address,
					'town' => $information->town,
					'bank_postcode' => $information->bank_postcode,
					'bank_phone' => $information->bank_phone,
					'directdebits' => $information->directdebits,
					'preferreddate' => $information->preferreddate,
					'affid' => $information->affid,
					'ip' => $information->ip,
					'date_added' => $information->date_added,
					'time_added' => $information->time_added,
					'guid' => $information->guid,
					'status' => $information->status,
					'comment' => $information->comment,
					'mail_send' => $information->mail_send,
					'mms_senddate' => $information->mms_senddate,
					'mms_sendmonth' => $information->mms_sendmonth,
					'subscription' => $information->subscription,
					'formtype' => $information->formtype,
					'typeofprotection' => $information->typeofprotection,
					'excessperiod' => $information->excessperiod,
					'insurer' => $information->insurer,
					'smoker' => $information->smoker,
					'life_cover' => $information->life_cover,
					'life_cover_term' => $information->life_cover_term,
					'life_cover_type' => $information->life_cover_type,
					'life_cover_premium_type' => $information->life_cover_premium_type,
					'CIC' => $information->CIC,
					'lead_from' => $information->lead_from,
					'lead_source_field' => $information->lead_source_field,
					'benefit_period' => $information->benefit_period,
					'duration' => $information->duration,
					'duration_year' => $information->duration_year,
					'duration_month' => $information->duration_month,
					'contractor_deirectdebitref' => $information->contractor_deirectdebitref,
					'contractor_id' => $information->contractor_id,
					'policy_number' => $information->policy_number,
					'policy_number_full' => $information->policy_number_full,
					'mta_updated' => $information->mta_updated,
					'renew_updated' => '0',
					'insurance_enddate' => $information->insurance_enddate,
					'version' => 0,
					'edit_updated' => $information->edit_updated,
					'mta_date' => $information->mta_date,
					'payment_type' => $information->payment_type,
					'cancel_period_code' => $information->cancel_period_code,
				]);
		} else {
			return $logInfo->id;
		}
	}

	public function insertLogInfoStatus($information_id,$status) {
		$logInfo = LogInformation::where('application_id', $information_id)->first();

		$information = DB::table('information')
				->where('id', $information_id)
				->first();

			return $logId = DB::table('log_information')
				->insertGetId([
					'application_id' => $information->id,
					'product_id' => $information->product_id,
					'title' => $information->title,
					'fname' => $information->fname,
					'lname' => $information->lname,
					'full_name' => $information->fname . " " . $information->lname,
					'dob' => $information->dob,
					'emp_status' => $information->emp_status,
					'occupation' => $information->occupation,
					'employer_name' => $information->employer_name,
					'building' => $information->building,
					'premiseName' => $information->premiseName,
					'address1' => $information->address1,
					'address2' => $information->address2,
					'address3' => $information->address3,
					'address4' => $information->address4,
					'postcode' => $information->postcode,
					'formattedPO' => $information->formattedPO,
					'postaltown' => $information->postaltown,
					'county' => $information->county,
					'email' => $information->email,
					'telephone' => $information->telephone,
					'mobile' => $information->mobile,
					'marketingOptOut' => $information->marketingOptOut,
					'under_writing_q1' => $information->under_writing_q1,
					'under_writing_ans' => $information->under_writing_ans,
					'under_writing_accept' => $information->under_writing_accept,
					'insurance_startdate' => $information->insurance_startdate,
					'transfer' => $information->transfer,
					'current_provider' => $information->current_provider,
					'current_policy_date' => $information->current_policy_date,
					'typeofcover' => $information->typeofcover,
					'loan_to_pay' => $information->loan_to_pay,
					'monthlycommitment' => $information->monthlycommitment,
					'monthlygrossincome' => $information->monthlygrossincome,
					'mortgagesalary' => $information->mortgagesalary,
					'mortgage' => $information->mortgage,
					'salary' => $information->salary,
					'age' => $information->age,
					'monthlybenefit' => $information->monthlybenefit,
					'monthlycost' => $information->monthlycost,
					'cover' => $information->cover,
					'cover_term' => $information->cover_term,
					'accept' => $information->accept,
					'accountname' => $information->accountname,
					'accountname2' => $information->accountname2,
					'accountnumber' => $information->accountnumber,
					'sortcode' => $information->sortcode,
					'iban' => $information->iban,
					'bank' => $information->bank,
					'bankbic' => $information->bankbic,
					'branch' => $information->branch,
					'branchbic' => $information->branchbic,
					'branch_address' => $information->branch_address,
					'town' => $information->town,
					'bank_postcode' => $information->bank_postcode,
					'bank_phone' => $information->bank_phone,
					'directdebits' => $information->directdebits,
					'preferreddate' => $information->preferreddate,
					'affid' => $information->affid,
					'ip' => $information->ip,
					'date_added' => $information->date_added,
					'time_added' => $information->time_added,
					'guid' => $information->guid,
					'status' => $information->status,
					'comment' => $information->comment,
					'mail_send' => $information->mail_send,
					'mms_senddate' => $information->mms_senddate,
					'mms_sendmonth' => $information->mms_sendmonth,
					'subscription' => $information->subscription,
					'formtype' => $information->formtype,
					'typeofprotection' => $information->typeofprotection,
					'excessperiod' => $information->excessperiod,
					'insurer' => $information->insurer,
					'smoker' => $information->smoker,
					'life_cover' => $information->life_cover,
					'life_cover_term' => $information->life_cover_term,
					'life_cover_type' => $information->life_cover_type,
					'life_cover_premium_type' => $information->life_cover_premium_type,
					'CIC' => $information->CIC,
					'lead_from' => $information->lead_from,
					'lead_source_field' => $information->lead_source_field,
					'benefit_period' => $information->benefit_period,
					'duration' => $information->duration,
					'duration_year' => $information->duration_year,
					'duration_month' => $information->duration_month,
					'contractor_deirectdebitref' => $information->contractor_deirectdebitref,
					'contractor_id' => $information->contractor_id,
					'policy_number' => $information->policy_number,
					'policy_number_full' => $information->policy_number_full,
					'mta_updated' => $information->mta_updated,
					'renew_updated' => '0',
					'insurance_enddate' => $information->insurance_enddate,
					'version' => 0,
					'edit_updated' => $information->edit_updated,
					'mta_date' => $information->mta_date,
					'payment_type' => $information->payment_type,
					'cancel_period_code' => $information->cancel_period_code,
					'action'=>$status,
					'active_loan_id'=>$information->active_loan_id,
				]);
		
	}
	public function sendEmailWithpolicyDocuemnts($logId, $request) {
		$logInfo = DB::table('log_information')
			->where('log_information.id', $logId)
			->join('products', 'log_information.product_id', '=', 'products.id')
			->first();

		$raf_documents = LogInformation::find($logId);

		if (!empty($logInfo->schedule_pdf)) {
			$schedule_pdf_name_file = $logInfo->schedule_pdf;
			$isExists = Storage::disk('s3')->has($schedule_pdf_name_file);
			if (!$isExists) {
				$policy_document_file = null;
				$email_data['application_schedule_pdf_name_file'] = null;
			} else {
			//	$email_data['application_schedule_pdf_name_file'] = $schedule_pdf_name_file;
				$email_data['application_schedule_pdf_name_file'] = $schedule_pdf_name_file;
			}
		} else {
			$email_data['application_schedule_pdf_name_file'] = null;
		}

		//policy wording file
		$policy_document_file = null;
		if (!empty($logInfo->policy_wordings_doc)) {
			
			if($logInfo->product_id=="29" && Carbon::parse($logInfo->date_added)->greaterThan('2021-07-09'))
			{
				//echo "in this";
				$policy_document_file = 'static_policy_documents/' . $logInfo->policy_wordings_doc;

				//exit;
				
			}
			if($logInfo->product_id=="29" && Carbon::parse($logInfo->date_added)->greaterThan('2021-03-14'))
			{
				//echo "in this";
				$policy_document_file = 'static_policy_documents/Income_Bee_150_AS_Policy_Wording.pdf';

				//exit;
				
			}

			else if($logInfo->product_id=="29") {
				$policy_document_file = 'static_policy_documents/Income_Bee_150_Policy_Wording_pdf';
			}

			else {
				$policy_document_file = 'static_policy_documents/' . $logInfo->policy_wordings_doc;
			}


			$isExists = Storage::disk('s3')->has($policy_document_file);
			if (!$isExists) {
				$policy_document_file = null;
				$email_data['policy_document_file'] = null;
			} else {
				$email_data['policy_document_file'] = $policy_document_file;
			}
		} else {
			$email_data['policy_document_file'] = null;
		}

		//Relevant  IPID file()
		$ipid_file = null;
		if (!empty($logInfo->keyfacts_doc)) {
			
			
			$ipid_file = 'static_policy_documents/' . $logInfo->keyfacts_doc;



			if($logInfo->product_id=="29" && Carbon::parse($logInfo->date_added)->greaterThan('2021-07-09'))
			{
				//echo "in this";
				$ipid_file = 'static_policy_documents/' . $logInfo->keyfacts_doc;

				//exit;
				
			}
			
			else if($logInfo->product_id=="29") {
				$ipid_file = 'static_policy_documents/Income_Bee_150_AS_IPID.pdf';
			}


			$isExists = Storage::disk('s3')->has($ipid_file);
			if (!$isExists) {
				$ipid_file = null;
				$email_data['ipid_file'] = null;
			} else {
				$email_data['ipid_file'] = $ipid_file;
			}
		} else {
			$email_data['ipid_file'] = $ipid_file;
		}

		// Best_Insurance_Terms_Of_Buisiness
		$best_insurance_terms_of_buisiness_file = 'static_policy_documents/Best_Insurance_Terms_Of_Buisiness.pdf';

		$isExists = Storage::disk('s3')->has($best_insurance_terms_of_buisiness_file);
		if (!$isExists) {
			$best_insurance_terms_of_buisiness_file = null;
			$email_data['best_insurance_terms_of_buisiness_file'] = null;
		} else {
			$email_data['best_insurance_terms_of_buisiness_file'] = $best_insurance_terms_of_buisiness_file;
		}

		// what_next_file

		if($logInfo->product_id==29)
		$faq_file_name = "Frequently_Asked_Questions_";
		else
		$faq_file_name = "Frequently_Asked_Questions.pdf";

		$faq_file = 'static_policy_documents/' . $faq_file_name;

		$isExists = Storage::disk('s3')->has($faq_file);
		if (!$isExists) {
			$faq_file = null;
			$email_data['faq_file'] = null;
		} else {
			$email_data['faq_file'] = $faq_file;
		}

		$email_data['cc'] = $request->cc_emails;
		$email_data['bcc'] = $request->bcc_emails;
		$email_data['subject'] = "Important Information from Best Insurance: Your protection policy documentation";
		$email_data['from'] = "advisor@bestinsurance.co.uk";
		$email_data['email'] = $logInfo->email;
		$email_data['product_id']=$logInfo->product_id;
		if (env('SEND_EMAIL_ENV') == 'test') {
			$email_data['email'] = env('SEND_EMAIL_ID');
		}
		$email_data['first_name'] = $logInfo->fname;
		$email_data['last_name'] = $logInfo->lname;
		$email_data['title'] = $logInfo->title;
		$edd['mt_lead_id'] = $logInfo->application_id;
		$edd['to_email'] = $email_data['email'];
		$edd['mt_cc_mails'] = $email_data['cc'];
		$edd['mt_bcc_mails'] = $email_data['bcc'];
		$edd['mt_from_mail'] = $email_data['from'];
		$edd['mt_sent_status'] = false;

		$view = View::make('admin.mail.mail-schedule', ['first_name' => $email_data['first_name'], 'last_name' => $email_data['last_name'], 'title' => $email_data['title'],'product_id'=>$email_data['product_id']]);
		$mt_contents = $view->render();
		$edd['mt_contents'] = $mt_contents;
		$edd['mt_type'] = 1;

		// send mail
		// mail-schedule

		$raf_documents = RAFLoanDocuments::where('log_info_id', $logId)
			->where('action_type', 'new')
			->whereNotIn('Alias', ['Reinstatement Letter', 'Credit Agreement Reminder Letter', 'Action Needed Letter'])
			->whereNotNull('s3_url')
			->get()->groupBy('Alias');

		try {
			$mailSuccess = Mail::send(['html' => 'admin.mail.mail-schedule'], $email_data, function ($message) use ($email_data, $raf_documents, $logInfo) {
				$message->from($email_data['from']);
				$message->to($email_data['email'])
					->subject($email_data['subject']);

				if (!empty($email_data['cc'])) {
					$message->cc($email_data['cc']);
				}
				if (!empty($email_data['bcc'])) {
					$message->bcc($email_data['bcc']);
				}
				$message->bcc('archive@bestinsurance.co.uk');
				// checking if any merging docs available in s3 for the same policy, if found will attach the merge docs
				//dd($logInfo);
				if (!empty($logInfo->bundle_policy_documents) && !empty($logInfo->bundle_direct_debit_documents) && !empty($logInfo->bundle_terms_and_conditions_documents)) {

					$policy_document_file = $logInfo->bundle_policy_documents;
					$direct_debit_documents = $logInfo->bundle_direct_debit_documents;
					$bundle_terms_and_conditions_documents = $logInfo->bundle_terms_and_conditions_documents;

					//$message->attach(str_replace(" ", "+", trim($policy_document_file)));
					//$message->attach(str_replace(" ", "+", trim($direct_debit_documents)));
					//$message->attach(str_replace(" ", "+", trim($bundle_terms_and_conditions_documents)));

					$message->attachData(Storage::disk('s3')->get($policy_document_file),'Policy_Documents.pdf');
					$message->attachData(Storage::disk('s3')->get($direct_debit_documents),'Direct_Debit.pdf');
					$message->attachData(Storage::disk('s3')->get($bundle_terms_and_conditions_documents),'Terms_Conditions.pdf');



				} else {

					if (!empty($email_data['application_schedule_pdf_name_file'])) {
						$message->attachData(Storage::disk('s3')->get($email_data['application_schedule_pdf_name_file']),'Policy_Schedule.pdf');
					}
					if (!empty($email_data['policy_document_file'])) {

						$message->attachData(Storage::disk('s3')->get($email_data['policy_document_file']),'Policy_Wordings.pdf');
						//$message->attach(str_replace(" ", "+", trim($email_data['policy_document_file'])));
					}
					if (!empty($email_data['ipid_file'])) {
						$message->attachData(Storage::disk('s3')->get($email_data['ipid_file']),'IPID.pdf');
					//	$message->attach(str_replace(" ", "+", trim($email_data['ipid_file'])));
					}
					if (!empty($email_data['best_insurance_terms_of_buisiness_file'])) {
						$message->attachData(Storage::disk('s3')->get($email_data['best_insurance_terms_of_buisiness_file']),'Terms_and_Conditions.pdf');
						//$message->attach(str_replace(" ", "+", trim($email_data['best_insurance_terms_of_buisiness_file'])));
					}
					if (!empty($email_data['faq_file'])) {
						//$message->attach(str_replace(" ", "+", trim($email_data['faq_file'])));
						$message->attachData(Storage::disk('s3')->get($email_data['faq_file']),'FAQ.pdf');
					}

					foreach ($raf_documents as $key => $raf_document) {
						$message->attach(env('S3_BUCKET_URL') . $raf_document->s3_url);
					}
				}

			});

			$edd['mt_sent_status'] = true;
			// Insert into mail tracker
			$this->insertMailTracker($edd);
			return true;
		} catch (Exception $e) {
			$this->insertMailTracker($edd);
			return false;
		}
	}

	public function sendEmailWithEditpolicyDocuemnts($logId, $request) {
		$logInfo = DB::table('log_information')
			->where('log_information.id', $logId)
			->join('products', 'log_information.product_id', '=', 'products.id')
			->first();

		$raf_documents = LogInformation::find($logId);

		if (!empty($logInfo->schedule_pdf)) {
			$schedule_pdf_name_file = $logInfo->schedule_pdf;
			$isExists = Storage::disk('s3')->has($schedule_pdf_name_file);
			if (!$isExists) {
				$policy_document_file = null;
				$email_data['application_schedule_pdf_name_file'] = null;
			} else {
				$email_data['application_schedule_pdf_name_file'] = $schedule_pdf_name_file;
			}
		} else {
			$email_data['application_schedule_pdf_name_file'] = null;
		}

		//policy wording file
		$policy_document_file = null;
		if (!empty($logInfo->policy_wordings_doc)) {
		//	$policy_document_file = 'static_policy_documents/' . $logInfo->policy_wordings_doc;


		if($logInfo->product_id=="29" && Carbon::parse($logInfo->date_added)->greaterThan('2021-07-09'))
		{
			//echo "in this";
			$policy_document_file = 'static_policy_documents/' . $logInfo->policy_wordings_doc;

			//exit;
			
		}
		if($logInfo->product_id=="29" && Carbon::parse($logInfo->date_added)->greaterThan('2021-03-14'))
		{
			//echo "in this";
			$policy_document_file = 'static_policy_documents/Income_Bee_150_AS_Policy_Wording.pdf';

			//exit;
			
		}

		else if($logInfo->product_id=="29") {
			$policy_document_file = 'static_policy_documents/Income_Bee_150_Policy_Wording_pdf';
		}

		else {
			$policy_document_file = 'static_policy_documents/' . $logInfo->policy_wordings_doc;
		}

			$isExists = Storage::disk('s3')->has($policy_document_file);
			if (!$isExists) {
				$policy_document_file = null;
				$email_data['policy_document_file'] = null;
			} else {
				$email_data['policy_document_file'] = $policy_document_file;
			}
		} else {
			$email_data['policy_document_file'] = null;
		}

		//Relevant  IPID file()
		$ipid_file = null;
		if (!empty($logInfo->keyfacts_doc)) {
			$ipid_file = 'static_policy_documents/' . $logInfo->keyfacts_doc;



			if($logInfo->product_id=="29" && Carbon::parse($logInfo->date_added)->greaterThan('2021-07-09'))
			{
				//echo "in this";
				$ipid_file = 'static_policy_documents/' . $logInfo->keyfacts_doc;

				//exit;
				
			}
			
			else if($logInfo->product_id=="29") {
				$ipid_file = 'static_policy_documents/Income_Bee_150_AS_IPID.pdf';
			}



			$isExists = Storage::disk('s3')->has($ipid_file);
			if (!$isExists) {
				$ipid_file = null;
				$email_data['ipid_file'] = null;
			} else {
				$email_data['ipid_file'] = $ipid_file;
			}
		} else {
			$email_data['ipid_file'] = $ipid_file;
		}

		// Best_Insurance_Terms_Of_Buisiness
		$best_insurance_terms_of_buisiness_file = 'static_policy_documents/Best_Insurance_Terms_Of_Buisiness.pdf';

		$isExists = Storage::disk('s3')->has($best_insurance_terms_of_buisiness_file);
		if (!$isExists) {
			$best_insurance_terms_of_buisiness_file = null;
			$email_data['best_insurance_terms_of_buisiness_file'] = null;
		} else {
			$email_data['best_insurance_terms_of_buisiness_file'] = $best_insurance_terms_of_buisiness_file;
		}

		if($logInfo->product_id==29)
		$faq_file_name = "Frequently_Asked_Questions_.pdf";
		else
		$faq_file_name = "Frequently_Asked_Questions.pdf";

		$faq_file = 'static_policy_documents/' . $faq_file_name;

		$isExists = Storage::disk('s3')->has($faq_file);
		if (!$isExists) {
			$what_next_file = null;
			$email_data['faq_file'] = null;
		} else {
			$email_data['faq_file'] = $faq_file;
		}

		$email_data['cc'] = $request->cc_emails;
		$email_data['bcc'] = $request->bcc_emails;
		$email_data['subject'] = "Your protection policy schedule and documents";
		$email_data['from'] = "advisor@bestinsurance.co.uk";
		$email_data['email'] = $logInfo->email;
		$email_data['product_id']=$logInfo->product_id;
		if (env('SEND_EMAIL_ENV') == 'test') {
			$email_data['email'] = env('SEND_EMAIL_ID');
		}
		$email_data['first_name'] = $logInfo->fname;
		$email_data['last_name'] = $logInfo->lname;
		$email_data['title'] = $logInfo->title;

		$edd['mt_lead_id'] = $logInfo->application_id;
		$edd['mt_cc_mails'] = $email_data['cc'];
		$edd['mt_bcc_mails'] = $email_data['bcc'];
		$edd['mt_from_mail'] = $email_data['from'];
		$edd['to_email'] = $email_data['email'];
		$edd['mt_sent_status'] = false;

		$view = View::make('admin.mail.mail-schedule', ['first_name' => $email_data['first_name'], 'last_name' => $email_data['last_name'], 'title' => $email_data['title'],'product_id'=>$email_data['product_id']]);
		$mt_contents = $view->render();
		$edd['mt_contents'] = $mt_contents;
		$edd['mt_type'] = 1;

		// send mail
		// mail-schedule

		$raf_documents = RAFLoanDocuments::where('log_info_id', $logId)
			->where('action_type', 'edit')
			->whereNotIn('Alias', ['Reinstatement Letter', 'Credit Agreement Reminder Letter', 'Action Needed Letter'])
			->whereNotNull('s3_url')
			->get();

		try {
			$mailSuccess = Mail::send(['html' => 'admin.mail.mail-schedule'], $email_data, function ($message) use ($email_data, $raf_documents) {
				$message->from($email_data['from']);
				$message->to($email_data['email'])
					->subject($email_data['subject']);

				if (!empty($email_data['cc'])) {
					$message->cc($email_data['cc']);
				}
				if (!empty($email_data['bcc'])) {
					$message->bcc($email_data['bcc']);
				}
				$message->bcc('archive@bestinsurance.co.uk');
				if (!empty($email_data['application_schedule_pdf_name_file'])) {
					//	$message->attach(str_replace(" ", "+", trim($email_data['application_schedule_pdf_name_file'])));
	
						$message->attachData(Storage::disk('s3')->get($email_data['application_schedule_pdf_name_file']),'Policy_Schedule.pdf');
					}
					if (!empty($email_data['policy_document_file'])) {
						//$message->attach(str_replace(" ", "+", trim($email_data['policy_document_file'])));
						$message->attachData(Storage::disk('s3')->get($email_data['policy_document_file']),'Policy_Wordings.pdf');
					}
					if (!empty($email_data['ipid_file'])) {
						//$message->attach(str_replace(" ", "+", trim($email_data['ipid_file'])));
						$message->attachData(Storage::disk('s3')->get($email_data['ipid_file']),'IPID.pdf');
					}
					if (!empty($email_data['best_insurance_terms_of_buisiness_file'])) {
					//	$message->attach(str_replace(" ", "+", trim($email_data['best_insurance_terms_of_buisiness_file'])));
	
						$message->attachData(Storage::disk('s3')->get($email_data['best_insurance_terms_of_buisiness_file']),'Terms_and_Conditions.pdf');
					}
					if (!empty($email_data['faq_file'])) {
					//	$message->attach(str_replace(" ", "+", trim($email_data['faq_file'])));
						$message->attachData(Storage::disk('s3')->get($email_data['faq_file']),'FAQ.pdf');
					}
	
					foreach ($raf_documents as $key => $raf_document) {
						//$message->attach(env('S3_BUCKET_URL') . $raf_document->s3_url);
						$message->attachData(Storage::disk('s3')->get($raf_document->s3_url),$raf_document->Alias.'.pdf');
	
					}
			});

			$edd['mt_sent_status'] = true;
			// Insert into mail tracker
			$this->insertMailTracker($edd);
			return true;
		} catch (Exception $e) {
			$this->insertMailTracker($edd);
			return false;
		}
	}

	public function sendEmailWithMTADocuemnts($logId, $request) {
		$logInfo = DB::table('log_information')
			->where('log_information.id', $logId)
			->join('products', 'log_information.product_id', '=', 'products.id')
			->first();

		// generated MTA
		if (!empty($logInfo->mta_pdf)) {
			$isExists = Storage::disk('s3')->has($logInfo->mta_pdf);
			if ($isExists) {
				$email_data['gererated_mta_file'] = $logInfo->mta_pdf;
			} else {
				$email_data['gererated_mta_file'] = null;
			}
		} else {
			$email_data['gererated_mta_file'] = null;
		}

		//policy wording file
		if (!empty($logInfo->policy_wordings_doc)) {
			//$policy_wordings_doc = 'static_policy_documents/' . $logInfo->policy_wordings_doc;


			if($logInfo->product_id=="29" && Carbon::parse($logInfo->date_added)->greaterThan('2021-07-09'))
			{
				//echo "in this";
				$policy_document_file = 'static_policy_documents/' . $logInfo->policy_wordings_doc;

				//exit;
				
			}
			if($logInfo->product_id=="29" && Carbon::parse($logInfo->date_added)->greaterThan('2021-03-14'))
			{
				//echo "in this";
				$policy_document_file = 'static_policy_documents/Income_Bee_150_AS_Policy_Wording.pdf';

				//exit;
				
			}

			else if($logInfo->product_id=="29") {
				$policy_document_file = 'static_policy_documents/Income_Bee_150_Policy_Wording_pdf';
			}

			else {
				$policy_document_file = 'static_policy_documents/' . $logInfo->policy_wordings_doc;
			}
			


			$isExists = Storage::disk('s3')->has($policy_document_file);
			if ($isExists) {
				$email_data['policy_document_file'] = $policy_document_file;
			} else {
				$email_data['policy_document_file'] = null;
			}
		} else {
			$email_data['policy_document_file'] = null;
		}

		//Relevant  IPID file()
		if (!empty($logInfo->keyfacts_doc)) {
			$ipid_file = 'static_policy_documents/' . $logInfo->keyfacts_doc;



			if($logInfo->product_id=="29" && Carbon::parse($logInfo->date_added)->greaterThan('2021-07-09'))
			{
				//echo "in this";
				$ipid_file = 'static_policy_documents/' . $logInfo->keyfacts_doc;

				//exit;
				
			}
			
			else if($logInfo->product_id=="29") {
				$ipid_file = 'static_policy_documents/Income_Bee_150_AS_IPID.pdf';
			}



			$isExists = Storage::disk('s3')->has($ipid_file);
			if ($isExists) {
				$email_data['ipid_file'] = $ipid_file;
			} else {
				$email_data['ipid_file'] = null;
			}
		} else {
			$email_data['ipid_file'] = null;
		}

		// Best_Insurance_Terms_Of_Buisiness
		$best_insurance_terms_of_buisiness_file = 'static_policy_documents/Best_Insurance_Terms_Of_Buisiness.pdf';
		$isExists = Storage::disk('s3')->has($best_insurance_terms_of_buisiness_file);
		if ($isExists) {
			$email_data['best_insurance_terms_of_buisiness_file'] = $best_insurance_terms_of_buisiness_file;
		} else {
			$email_data['best_insurance_terms_of_buisiness_file'] = null;
		}
		$email_data['product_id']=$logInfo->product_id;
		if($logInfo->product_id==29)
		$faq_file_name = "Frequently_Asked_Questions_";
		else
		$faq_file_name = "Frequently_Asked_Questions.pdf";

		$faq_file = 'static_policy_documents/' . $faq_file_name;

		$isExists = Storage::disk('s3')->has($faq_file);
		if ($isExists) {
			$email_data['faq_file'] = $faq_file;
		} else {
			$email_data['faq_file'] = null;
		}

		$email_data['cc'] = $request->cc_emails;
		$email_data['bcc'] = $request->bcc_emails;
		$email_data['subject'] = "Your protection policy schedule and documents";
		$email_data['from'] = "advisor@bestinsurance.co.uk";
		$email_data['email'] = $logInfo->email;
		if (env('SEND_EMAIL_ENV') == 'test') {
			$email_data['email'] = env('SEND_EMAIL_ID');
		}
		$email_data['first_name'] = $logInfo->fname;
		$email_data['last_name'] = $logInfo->lname;
		$email_data['title'] = $logInfo->title;

		$edd['mt_lead_id'] = $logInfo->application_id;
		$edd['mt_cc_mails'] = $email_data['cc'];
		$edd['mt_bcc_mails'] = $email_data['bcc'];
		$edd['mt_from_mail'] = $email_data['from'];
		$edd['to_email'] = $email_data['email'];
		$edd['mt_sent_status'] = false;

		$view = View::make('admin.mail.mail-schedule', ['first_name' => $email_data['first_name'], 'last_name' => $email_data['last_name'], 'title' => $email_data['title'],'product_id'=>$email_data['product_id']]);
		$mt_contents = $view->render();
		$edd['mt_contents'] = $mt_contents;
		$edd['mt_type'] = 2; //mta

		// send mail
		// mail-schedule

		$raf_documents = RAFLoanDocuments::where('log_info_id', $logId)
			->where('action_type', 'mta')
			->whereNotIn('Alias', ['Reinstatement Letter', 'Credit Agreement Reminder Letter', 'Action Needed Letter'])
			->whereNotNull('s3_url')
			->get();

		try {
			$mailSuccess = Mail::send(['html' => 'admin.mail.mail-schedule'], $email_data, function ($message) use ($email_data, $raf_documents) {
				$message->from($email_data['from']);
				$message->to($email_data['email'])
					->subject($email_data['subject']);

				if (!empty($email_data['cc'])) {
					$message->cc($email_data['cc']);
				}

				if (!empty($email_data['bcc'])) {
					$message->bcc($email_data['bcc']);
				}
				$message->bcc('archive@bestinsurance.co.uk');
				if (!empty($email_data['gererated_mta_file'])) {
					//	$message->attach(str_replace(" ", "+", trim($email_data['gererated_mta_file'])));
						$message->attachData(Storage::disk('s3')->get($email_data['gererated_mta_file']),'MTA_Schedule.pdf');
					}
	
					if (!empty($email_data['policy_document_file'])) {
					//	$message->attach(str_replace(" ", "+", trim($email_data['policy_document_file'])));
						$message->attachData(Storage::disk('s3')->get($email_data['policy_document_file']),'Policy_Wordings.pdf');
					}
	
					if (!empty($email_data['ipid_file'])) {
						//$message->attach(str_replace(" ", "+", trim($email_data['ipid_file'])));
						$message->attachData(Storage::disk('s3')->get($email_data['ipid_file']),'IPID.pdf');
					}
	
					if (!empty($email_data['best_insurance_terms_of_buisiness_file'])) {
					//	$message->attach(str_replace(" ", "+", trim($email_data['best_insurance_terms_of_buisiness_file'])));
						$message->attachData(Storage::disk('s3')->get($email_data['best_insurance_terms_of_buisiness_file']),'Terms_and_Conditions.pdf');
					}
	
					if($email_data['product_id']!="31" && $email_data['product_id']!="30")
					{
						if (!empty($email_data['faq_file'])) {
							//$message->attach(str_replace(" ", "+", trim($email_data['faq_file'])));
							$message->attachData(Storage::disk('s3')->get($email_data['faq_file']),'FAQ.pdf');
						}
					}
					foreach ($raf_documents as $key => $raf_document) {
	
					//	$message->attach(env('S3_BUCKET_URL') . $raf_document->s3_url);
						$message->attachData(Storage::disk('s3')->get($raf_document->s3_url),$raf_document->Alias.'.pdf');
					}
			});

			$edd['mt_sent_status'] = true;
			// Insert into mail tracker
			$this->insertMailTracker($edd);
			return true;
		} catch (Exception $e) {
			$this->insertMailTracker($edd);
			return false;
		}
	}

	// renew start
	public function sendEmailWithRenewDocuemnts($logId, $request) {
		$logInfo = DB::table('log_information')
			->where('log_information.id', $logId)
			->join('products', 'log_information.product_id', '=', 'products.id')
			->first();

		if (!empty($logInfo->renew_pdf)) {
			$isExists = Storage::disk('s3')->has($logInfo->renew_pdf);
			if ($isExists) {
				$email_data['gererated_renew_file'] = $logInfo->renew_pdf;
			} else {
				$email_data['gererated_renew_file'] = null;
			}
		} else {
			$email_data['gererated_renew_file'] = null;
		}

		//policy wording file
		if (!empty($logInfo->policy_wordings_doc)) {
			//$policy_wordings_doc = 'static_policy_documents/' . $logInfo->policy_wordings_doc;


			if($logInfo->product_id=="29" && Carbon::parse($logInfo->date_added)->greaterThan('2021-07-09'))
			{
				//echo "in this";
				$policy_document_file = 'static_policy_documents/' . $logInfo->policy_wordings_doc;

				//exit;
				
			}
			if($logInfo->product_id=="29" && Carbon::parse($logInfo->date_added)->greaterThan('2021-03-14'))
			{
				//echo "in this";
				$policy_document_file = 'static_policy_documents/Income_Bee_150_AS_Policy_Wording.pdf';

				//exit;
				
			}

			else if($logInfo->product_id=="29") {
				$policy_document_file = 'static_policy_documents/Income_Bee_150_Policy_Wording_pdf';
			}

			else {
				$policy_document_file = 'static_policy_documents/' . $logInfo->policy_wordings_doc;
			}

			$isExists = Storage::disk('s3')->get($policy_document_file);
			if ($isExists) {
				$email_data['policy_document_file'] = $policy_document_file;
			} else {
				$email_data['policy_document_file'] = null;
			}
		} else {
			$email_data['policy_document_file'] = null;
		}

		if($logInfo->product_id==29)
		$faq_file_name = "Frequently_Asked_Questions_";
		else
		$faq_file_name = "Frequently_Asked_Questions.pdf";

		$faq_file = 'static_policy_documents/' . $faq_file_name;

		$isExists = Storage::disk('s3')->has($faq_file);
		if ($isExists) {
			$email_data['faq_file'] = $faq_file;
		} else {
			$email_data['faq_file'] = null;
		}

		//Relevant  IPID file()
		if (!empty($logInfo->keyfacts_doc)) {
			$ipid_file = 'static_policy_documents/' . $logInfo->keyfacts_doc;



			if($logInfo->product_id=="29" && Carbon::parse($logInfo->date_added)->greaterThan('2021-07-09'))
			{
				//echo "in this";
				$ipid_file = 'static_policy_documents/' . $logInfo->keyfacts_doc;

				//exit;
				
			}
			
			else if($logInfo->product_id=="29") {
				$ipid_file = 'static_policy_documents/Income_Bee_150_AS_IPID.pdf';
			}


			$isExists = Storage::disk('s3')->has($ipid_file);
			if ($isExists) {
				$email_data['ipid_file'] = $ipid_file;
			} else {
				$email_data['ipid_file'] = null;
			}
		} else {
			$email_data['ipid_file'] = null;
		}

		// Best_Insurance_Terms_Of_Buisiness
		$best_insurance_terms_of_buisiness_file = 'static_policy_documents/Best_Insurance_Terms_Of_Buisiness.pdf';
		$isExists = Storage::disk('s3')->get($best_insurance_terms_of_buisiness_file);
		if ($isExists) {
			$email_data['best_insurance_terms_of_buisiness_file'] = $best_insurance_terms_of_buisiness_file;
		} else {
			$email_data['best_insurance_terms_of_buisiness_file'] = null;
		}

		$email_data['cc'] = $request->cc_emails;
		$email_data['bcc'] = $request->bcc_emails;
		$email_data['subject'] = "Important Information from Best Insurance: Your policy schedule documentation";
		$email_data['from'] = "advisor@bestinsurance.co.uk";
		$email_data['email'] = $logInfo->email;
		// $email_data['email']="srikanth.kudikala@quadone.com";
		$email_data['first_name'] = $logInfo->fname;
		$email_data['first_name'] = $logInfo->fname;
		$email_data['last_name'] = $logInfo->lname;
		$email_data['title'] = $logInfo->title;

		$edd['mt_lead_id'] = $logInfo->application_id;
		$edd['mt_cc_mails'] = $email_data['cc'];
		$edd['mt_bcc_mails'] = $email_data['bcc'];
		$edd['mt_from_mail'] = $email_data['from'];
		$edd['to_email'] = $email_data['email'];
		$edd['mt_sent_status'] = false;
		$view = View::make('admin.mail.mail-renew', ['first_name' => $email_data['first_name'], 'last_name' => $email_data['last_name'], 'title' => $email_data['title']]);
		// $view = View::make('admin.mail.mail-renew', ['first_name' => $email_data['first_name']]);
		$mt_contents = $view->render();
		$edd['mt_contents'] = $mt_contents;
		$edd['mt_type'] = 4; //renew

		$raf_documents = RAFLoanDocuments::where('log_info_id', $logId)
			->where('action_type', 'renew')
			->whereNotIn('Alias', ['Reinstatement Letter', 'Credit Agreement Reminder Letter', 'Action Needed Letter'])
			->whereNotNull('s3_url')
			->get()->groupBy('Alias');
		// send mail
		try {
			$mailSuccess = Mail::send(['html' => 'admin.mail.mail-renew'], $email_data, function ($message) use ($email_data, $raf_documents, $logInfo) {
				$message->from($email_data['from']);
				$message->to($email_data['email'])
					->subject($email_data['subject']);

				if (!empty($email_data['cc'])) {
					$message->cc($email_data['cc']);
				}

				if (!empty($email_data['bcc'])) {
					$message->bcc($email_data['bcc']);
				}
				$message->bcc('archive@bestinsurance.co.uk');
				if (!empty($logInfo->bundle_policy_documents) && !empty($logInfo->bundle_direct_debit_documents) && !empty($logInfo->bundle_terms_and_conditions_documents)) {

					$policy_document_file = $logInfo->bundle_policy_documents;
					$direct_debit_documents = $logInfo->bundle_direct_debit_documents;
					$bundle_terms_and_conditions_documents = $logInfo->bundle_terms_and_conditions_documents;

				//	$message->attach(str_replace(" ", "+", trim($policy_document_file)));
				//	$message->attach(str_replace(" ", "+", trim($direct_debit_documents)));
				//	$message->attach(str_replace(" ", "+", trim($bundle_terms_and_conditions_documents)));


					$message->attachData(Storage::disk('s3')->get($policy_document_file),'Policy_Document.pdf');
					$message->attachData(Storage::disk('s3')->get($direct_debit_documents),'Direct_Debit_Document.pdf');
					$message->attachData(Storage::disk('s3')->get($bundle_terms_and_conditions_documents),'Terms_and_Conditions.pdf');

				} else {

					if (!empty($email_data['gererated_renew_file'])) {
						//$message->attach(str_replace(" ", "+", trim($email_data['gererated_renew_file'])));
						$message->attachData(Storage::disk('s3')->get($email_data['gererated_renew_file']),'Renew_Schedule.pdf');
					}

					if (!empty($email_data['policy_document_file'])) {
						//$message->attach(str_replace(" ", "+", trim($email_data['policy_document_file'])));
						$message->attachData(Storage::disk('s3')->get($email_data['policy_document_file']),'Policy_Wordings.pdf');
					}

					if (!empty($email_data['ipid_file'])) {
					//	$message->attach(str_replace(" ", "+", trim($email_data['ipid_file'])));
						$message->attachData(Storage::disk('s3')->get($email_data['ipid_file']),'IPID.pdf');
					}

					if (!empty($email_data['best_insurance_terms_of_buisiness_file'])) {
						//$message->attach(str_replace(" ", "+", trim($email_data['best_insurance_terms_of_buisiness_file'])));
						$message->attachData(Storage::disk('s3')->get($email_data['best_insurance_terms_of_buisiness_file']),'Terms_and_Conditions.pdf');
					}

					if (!empty($email_data['faq_file'])) {
						//$message->attach(str_replace(" ", "+", trim($email_data['faq_file'])));
						$message->attachData(Storage::disk('s3')->get($email_data['faq_file']),'FAQ.pdf');
					}

					foreach ($raf_documents as $key => $raf_document) {
					//	$message->attach(env('S3_BUCKET_URL') . $raf_document->orderBy('primary_id', 'desc')->first()->s3_url);
						$message->attachData(Storage::disk('s3')->get($raf_document->s3_url),$raf_document->Alias.'.pdf');
					}
				}
			});

			$edd['mt_sent_status'] = true;
			// Insert into mail tracker
			$this->insertMailTracker($edd);
			return true;
		} catch (Exception $e) {
			$this->insertMailTracker($edd);
			return false;
		}
	}
	// renew end

	public function sendEmailWithCancelDocuemnts($logId, $request) {
		$logInfo = DB::table('log_information')
			->where('log_information.id', $logId)
			->join('products', 'log_information.product_id', '=', 'products.id')
			->first();

		/*$cancel_filename = public_path().$logInfo->cancel_pdf;
        $cancel_filename = $this->sanitizePath($cancel_filename);*/
		$cancel_filename = $logInfo->cancel_pdf;

		$isExists = Storage::disk('s3')->has($logInfo->cancel_pdf);
		if ($isExists) {
			$email_data['cancel_filename'] = $cancel_filename;
		} else {
			$email_data['cancel_filename'] = null;
		}

		$email_data['cc'] = $request->cc_emails;
		$email_data['bcc'] = $request->bcc_emails;
		$email_data['subject'] = "Important Information from Best Insurance: Your protection policy cancelation documentation";
		$email_data['from'] = "advisor@bestinsurance.co.uk";
		$email_data['email'] = $logInfo->email;
		if (env('SEND_EMAIL_ENV') == 'test') {
			$email_data['email'] = env('SEND_EMAIL_ID');
		}
		$email_data['first_name'] = $logInfo->fname;
		$email_data['last_name'] = $logInfo->lname;
		$email_data['title'] = $logInfo->title;

		$edd['mt_lead_id'] = $logInfo->application_id;
		$edd['mt_cc_mails'] = $email_data['cc'];
		$edd['mt_bcc_mails'] = $email_data['bcc'];
		$edd['mt_from_mail'] = $email_data['from'];
		$edd['to_email'] = $email_data['email'];
		$edd['mt_sent_status'] = false;

		$view = View::make('admin.mail.mail-cancel', ['first_name' => $email_data['first_name'], 'last_name' => $email_data['last_name'], 'title' => $email_data['title']]);

		$mt_contents = $view->render();
		$edd['mt_contents'] = $mt_contents;
		$edd['mt_type'] = 3; //cancel

		// send mail
		// mail-schedule

		$raf_documents = RAFLoanDocuments::where('log_info_id', $logId)
			->where('action_type', 'cancel')
			->whereNotIn('Alias', ['Reinstatement Letter', 'Credit Agreement Reminder Letter', 'Action Needed Letter'])
			->whereIn('Alias', ['Cancellation Letter'])
			->whereNotNull('s3_url')
		//	->get();
			->orderBy('primary_id', 'desc')
			->first();
			//dd($raf_documents);

		try {
			$mailSuccess = Mail::send(['html' => 'admin.mail.mail-cancel'], $email_data, function ($message) use ($email_data, $raf_documents) {
				$message->from($email_data['from']);
				$message->to($email_data['email'])
					->subject($email_data['subject']);
				if (!empty($email_data['cc'])) {
					$message->cc($email_data['cc']);
				}

				if (!empty($email_data['bcc'])) {
					$message->bcc($email_data['bcc']);
				}
				$message->bcc('archive@bestinsurance.co.uk');
				if (!empty($email_data['cancel_filename'])) {
					//	$message->attach(str_replace(" ", "+", trim($email_data['cancel_filename'])));
						$message->attachData(Storage::disk('s3')->get($email_data['cancel_filename']),'Cancel.pdf');
					}
				//	foreach ($raf_documents as $key => $raf_document) {
						//$message->attach(env('S3_BUCKET_URL') . $raf_document->s3_url);
						if (!empty($raf_documents))
						{
						$message->attachData(Storage::disk('s3')->get($raf_documents->s3_url),$raf_documents->Alias.'.pdf');
						}
				//	}
			});

			$edd['mt_sent_status'] = true;
			// Insert into mail tracker
			$this->insertMailTracker($edd);
			return true;
		} catch (Exception $e) {
			$this->insertMailTracker($edd);
			return false;
		}
	}


	public function sendEmailWithRejectDocuemnts($logId, $request) {
		$logInfo = DB::table('log_information')
			->where('log_information.id', $logId)
			->join('products', 'log_information.product_id', '=', 'products.id')
			->first();

		
		$email_data['cc'] = $request->cc_emails;
		$email_data['bcc'] = $request->bcc_emails;
		$email_data['subject'] = "Your Income Protection Application by Best Insurance";
		$email_data['from'] = "advisor@bestinsurance.co.uk";
		$email_data['email'] = $logInfo->email;
		if (env('SEND_EMAIL_ENV') == 'test') {
			$email_data['email'] = env('SEND_EMAIL_ID');
		}
		$email_data['first_name'] = $logInfo->fname;
		$email_data['last_name'] = $logInfo->lname;
		$email_data['title'] = $logInfo->title;

		$edd['mt_lead_id'] = $logInfo->application_id;
		$edd['mt_cc_mails'] = $email_data['cc'];
		$edd['mt_bcc_mails'] = $email_data['bcc'];
		$edd['mt_from_mail'] = $email_data['from'];
		$edd['to_email'] = $email_data['email'];
		$edd['mt_sent_status'] = false;

		$view = View::make('admin.mail.mail-reject', ['first_name' => $email_data['first_name'], 'last_name' => $email_data['last_name'], 'title' => $email_data['title']]);

		$mt_contents = $view->render();
		$edd['mt_contents'] = $mt_contents;
		$edd['mt_type'] = 3; //cancel

		// send mail
		// mail-schedule

		
		try {
			$mailSuccess = Mail::send(['html' => 'admin.mail.mail-reject'], $email_data, function ($message) use ($email_data) {
				$message->from($email_data['from']);
				$message->to($email_data['email'])
					->subject($email_data['subject']);
				if (!empty($email_data['cc'])) {
					$message->cc($email_data['cc']);
				}

				if (!empty($email_data['bcc'])) {
					$message->bcc($email_data['bcc']);
				}
				$message->bcc('archive@bestinsurance.co.uk');
			});

			$edd['mt_sent_status'] = true;
			// Insert into mail tracker
			$this->insertMailTracker($edd);
			return true;
		} catch (Exception $e) {
			$this->insertMailTracker($edd);
			return false;
		}
	}

	public function insertMailTracker($data) {
		$mailtracker = new MailTracker;
		$mailtracker->mt_lead_id = $data['mt_lead_id'];
		$mailtracker->mt_cc_mails = $data['mt_cc_mails'];
		$mailtracker->mt_bcc_mails = $data['mt_bcc_mails'];
		$mailtracker->mt_from_mail = $data['mt_from_mail'];
		$mailtracker->mt_sent_status = $data['mt_sent_status'];
		$mailtracker->mt_contents = $data['mt_contents'];
		$mailtracker->mt_type = $data['mt_type'];
		$mailtracker->to_email = $data['to_email'];
		$mailtracker->mt_created_u_id = \Auth::user()->id;
		$mailtracker->save();
	}

	public static function insertMailTrackerReview($data) {
		$mailtracker = new MailTracker;
		$mailtracker->mt_lead_id = $data['mt_lead_id'];
		$mailtracker->mt_cc_mails = $data['mt_cc_mails'];
		$mailtracker->mt_bcc_mails = $data['mt_bcc_mails'];
		$mailtracker->mt_from_mail = $data['mt_from_mail'];
		$mailtracker->mt_sent_status = $data['mt_sent_status'];
		$mailtracker->mt_contents = $data['mt_contents'];
		$mailtracker->mt_type = $data['mt_type'];
		$mailtracker->to_email = $data['to_email'];
		$mailtracker->mt_created_u_id = \Auth::user()->id;
		$mailtracker->save();
	}
	public function sanitizePath($path) {
		$directory_sep = null;
		if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
			$directory_sep = '\\';
			return str_replace($directory_sep, '/', $path);
		} else {
			$directory_sep = "\\";
			return str_replace($directory_sep, '//', $path);
		}
	}

	public function sendmail() {
		$email_data['what_next_file'] =
			"policy_documents\BDM000000049\mta_02042019043820.pdf";
		$email_data['what_next_file'] = $this->sanitizePath($email_data['what_next_file']);
		$email_data['from'] = "advisor@bestinsurance.co.uk";
		if (env('SEND_EMAIL_ENV') == 'test') {
			$email_data['email'] = env('SEND_EMAIL_ID');
		}
		$email_data['first_name'] = "srikanth";
		$email_data['subject'] = "Test";
		$mailSuccess = Mail::send(['test' => 'hhhh'], $email_data, function ($message) use ($email_data) {
			$message->from($email_data['from']);
			$message->to($email_data['email'])
				->subject($email_data['subject']);

			if (!empty($email_data['what_next_file'])) {
				$message->attach($email_data['what_next_file']);
			}
		});
		return "sent";
	}

	public function download($path) {
		Log::info($path);

		$fs = \Storage::disk('s3');
		$stream = $fs->readStream($path);
		return \Response::stream(function () use ($stream) {
			fpassthru($stream);
		}, 200, [
			"Content-Type" => $fs->getMimetype($path),
			"Content-Length" => $fs->getSize($path),
			"Content-disposition" => "attachment; filename=\"" . basename($path) . "\"",
		]);
	}

}
