<?php

namespace App\Http\Controllers;

use Exception;
use Illuminate\Http\Request;
use App\Mail\loanMail;
use App\Mail\loanMailAr;

use illuminate\contract\Mailer;
use Illuminate\Support\Facades\Mail;
use Validator;


class LoanMailController extends Controller
{
    //
    public function index(Request $request){

        $validator = Validator::make($request->all(), [
            'student_name' => 'required',
            'student_email' => 'required',
            'loan_amount' => 'required',
            'application_number' => 'required',
            'web_version_language' => 'required',
        ]);


        if ($validator->fails()) {
            return response()->json(['error'=>$validator->errors()], 401);
        }
        $data=[
            'subject'=>'test subject',
            'body'=>'Hello this is test',
            'student_name'=>$request['student_name'],
            'student_email'=>$request['student_email'],
            'loan_amount'=>$request['loan_amount'],
            'application_number'=>$request['application_number'],
            'web_version_language'=>$request['web_version_language'],
        ];

        try {
            if($request['web_version_language'] == 'EN'){
                Mail::to($request['student_email'])->send(new loanMail($data));
            }
            if($request['web_version_language'] == 'AR'){
                Mail::to($request['student_email'])->send(new loanMailAr($data));

            }
                return response()->json(['Greate check your Email']);
        } catch(Exception $th){
            return response()->json(['sorry somthing went wrong']);

        }
    }
}
