<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Company;
use App\Models\EmailTemplate;
use App\Models\Forum;
use App\Models\Job;
use App\Models\Product;
use App\Models\Promotion;
use App\Models\Setting;
// use App\Models\Setting;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class AdminController extends Controller
{
    
    public function dashboard(){
        
        $users = User::where('role_id', 2)->count();
        /*$companies = Company::count();
        $products =  Product::count();   
        $jobs =  Job::count();   
        $forums =  Forum::count()*/;
        $companies = $products = $jobs = $forums = [];
        return view('admin.dashboard', compact(
            'users', 
            'companies',
            'products',
            'jobs',
            'forums',
        ));
    }

    public function login(Request $request) {
        if ($request->method() == 'POST'){
            $validator = Validator::make($request->all(), [
                'email' => 'required|email',
                'password' => 'required'
            ]);
            if ($validator->fails()){
                return redirect()->back()->withErrors($validator->errors())->withInput();
            }
            $user = User::where('email', $request->input('email'))->first();
            if ($user != null && $user->role_id == 1){
                if (Hash::check($request->input('password'), $user->password)) {
                    Auth::login($user);
                    return redirect(route('admin.dashboard'));
                }else{
                    return back()->withErrors(['password' => 'Invalid Email or Password']);
                }
            }else{
                return back()->withErrors(['password' => 'Invalid Email or Password']);
            }
        }
        return view('admin.login');
    }

    public function setting(Request $request) {
        $content = Setting::firstOrFail();
        if ($request->method() == 'POST') {

            $data = [
                'title',
                'phone',
                'email',
                'address',
                'logo',
                'about_us',
                'privacy_policy',
                'terms_and_conditions',
            ];
            foreach ($data as $val) {
                $content->$val = $request->input($val);
            }

            if ($request->has('logo')) {

                $dir      = "uploads/setting/";
                $file     = $request->file('logo');
                $fileName = time().'-setting.'.$file->getClientOriginalExtension();
                $file->move($dir, $fileName);
                $content->logo = $dir.$fileName;
            }
            $content->save();

            return redirect()->back()->with('success', 'Site Setting Updated Successfully');
        }

        return view('admin.setting', compact('content'));
    }

    public function changePassword()
    {
        return view('admin.change-password');
    }

    public function updateAdminPassword(Request $request)
    {
        $id = Auth::user()->id;

        $this->validate($request, [
            'current_password' => 'required',
            'password' => 'required|min:8|confirmed',
        ]);

        if (Hash::check($request->current_password, Auth::User()->password)) {
            $content = User::find($id);
            $content->password = Hash::make($request->password);
            $content->save();
            return redirect()->back()->with('success', 'Password Update Successfully.');
        }else{
            return back()->withErrors(['current_password' => 'Your current Password not recognized']);
        }

    }

    public function updateToken(Request $request){
        try{
            $request->user()->update(['fcm_token'=>$request->token]);
            return response()->json([
                'success'=>true
            ]);
        }catch(\Exception $e){
            report($e);
            return response()->json([
                'success'=>false
            ],500);
        }
    }

    public function emailTemplates(){
        return view('admin.email-templates');
    }

    public function viewEmailTemplates($id){
        $extra = "Thank you for choosing Trading FMCG. Use the following OTP to complete your Sign Up procedures.";
        $emailDesc = EmailTemplate::where('id', $id)->value('description');
        if($emailDesc != null){
            $extra = $emailDesc;
        }
        $name = Auth::user()->name;
        $to = Auth::user()->email;
        $text = '12345';
        return view('emails.send-otp', compact('extra', 'name', 'to', 'text'));
    }

    public function editEmailTemplate(Request $request, $id){
        EmailTemplate::where('id', $id)->update([
            'description' => $request->description
        ]);

        return back()->with('success', 'Template updated successfully');
    }

    public function countries(){
        return view('admin.countries');
    }
}
