<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Leads;
use App\Models\InvoiceService;
use App\Models\PaymentGateway;
use App\Models\Currency;
use App\Models\Settings;
use Illuminate\Support\Str;

class LeadsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(){
        $leads = Leads::orderBy('id', 'desc')->paginate(10);
        return view('backend.leads.index')->with('leads',$leads);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(){
        return view('backend.leads.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request){
        $this->validate($request,[
            'name'=>'required',
            'email'=>'required',
            'phone'=>'required',
        ]);
        $data = $request->all();
        // dd($data);
        $lead = new Leads();
        $lead->name = $data['name'];
        $lead->email = $data['email'];
        $lead->phone = $data['phone'];
        $lead->ip = $data['ip'];
        $lead->city = $data['city'];
        $lead->region = $data['region'];
        $lead->country = $data['country'];
        $lead->save();
        $lead_id = $lead->id;
        if($lead_id){
            $lead_mail = Leads::find($lead_id);
            if($lead_mail){
                $details = [
                    'name' => $lead_mail->name,
                    'email' => $lead_mail->email,
                    'phone' => $lead_mail->phone,
                    'ip' => $lead_mail->ip,
                    'city' => $lead_mail->city,
                    'region' => $lead_mail->region,
                    'country' => $lead_mail->country,
                ];

                try {
                    \Mail::to('sales@amzpublicationshub.com')->send(new \App\Mail\LeadMail($details));
                } catch (\Throwable $th) {
                    //throw $th;
                }
            }
            request()->session()->flash('success','Lead successfully added');
        }
        else{
            request()->session()->flash('error','Error occurred, Please try again!');
        }
        return redirect()->route('leads.index');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id){
        $lead = Leads::with('leadData')->findOrFail($id);
        return view('backend.leads.show')->with('lead', $lead);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id){
       $lead = Leads::findOrFail($id);
       return view("backend.leads.edit")->with("lead", $lead);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request,[
            'name' => 'required',
            'email'=> 'required',
            'phone'=> 'required',
        ]);
        $lead = Leads::findOrFail($id);
        $data= $request->all();
        $lead->name = $data['name'];
        $lead->email = $data['email'];
        $lead->phone = $data['phone'];
        if($lead->save()){
            request()->session()->flash('success','Lead successfully updated');
        }
        else{
            request()->session()->flash('error','Error occurred, Please try again!');
        }
        return redirect()->route('leads.index');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $lead = Leads::findOrFail($id);
        $status = $lead->delete();
        if($status){
            request()->session()->flash('success','Lead successfully deleted');
        }
        else{
            request()->session()->flash('error','Error while deleting Lead');
        }
        return redirect()->route('leads.index');
    }

    public function deleteAllTest(Request $request){
        $lead = new Leads();
        $deleteWordsArray = explode(',', $request->delete_words);
        $deleteWordsArray = array_map('trim', $deleteWordsArray);
        $status = $lead->deleteTestLeads($deleteWordsArray);
        if($status){
            return redirect()->back()->with('success', 'All Test Leads Deleted');
        }else{
            return redirect()->back()->with('error', 'Unable to delete Test Leads');
        }
    }

}
