<?php

namespace App\Http\Controllers\Admin;

use App\Customer;
use App\CustomerDocument;
use App\Document;
use Carbon\Carbon;
use Illuminate\Foundation\Auth\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Mail;
use Mockery\Tests\React_WritableStreamInterface;

class AdminController extends Controller
{
    public function store(Request $request)
    {
        //
        $name = $request['name'];
        $email = $request['email'];
        $phone = $request['phone'];
        $pass = $request['password'];
        $repass = $request['re-password'];
        if ($pass == $repass) {
            $save_register = new User();
            $save_register->name = $name;
            $save_register->email = $email;
            $save_register->phone = $phone;
            $save_register->password = bcrypt($pass);
            $save_register->role = '1';
            $save_register->status = '1';
            $save_register->save();
            return redirect('/')->withSuccess('Register Successfully');
        } else {
            return redirect()->back()->withErrors('Password Mismatch, Try Again!');
        }


    }


    public function sendFeedbackMail(Request $request)
    {
        $name = $request->name;
        $email = $request->email;
        $description = $request->description;
        $data = array(
            'name' => $request->name,
            'email' => $request->email,
            'description' => $request->description,
        );

        Mail::send('feedback', $data, function ($message) use ($data) {
            $message->from($data['email']);
            $message->to('support@chiseltechnologies.com')->subject('Message From ' . $data['name']);
        });
        return redirect()->back()->withSuccess('Your Message send Successfully');
    }

    public function login(Request $request)
    {
        $user_id = $request['email'];
        $pass = $request['password'];
        $admin_check = $request['admin_check'];
        if ($admin_check == 1) {
            if (Auth::attempt(array('email' => $user_id, 'password' => $pass, 'status' => '1', 'role' => $admin_check)) ||
                Auth::attempt(array('name' => $user_id, 'password' => $pass, 'status' => '1', 'role' => $admin_check)) ||
                Auth::attempt(array('phone' => $user_id, 'password' => $pass, 'status' => '1', 'role' => $admin_check))
            ) {
                return redirect('/dashboard')->withSuccess('Welcome to Admin');
            } else {
                return redirect()->back()->withErrors('Invalid Username Password');
            }
        } else {
            return redirect()->back()->withErrors('Invalid Role');
        }


    }


    public function dashboardView(Request $request)
    {
        $totel_documents = Document::all()->count();
        $totel_customers = Customer::all()->count();
        $totel_today_coc = Customer::whereDate('created_at', '=', date('Y-m-d'))->count();
        $get_documents = CustomerDocument::orderBy('id', 'DESC')->limit('6')->get();

        $get_documents_months = CustomerDocument::select('created_at')->get()->groupBy(function ($date) {
            return Carbon::parse($date->created_at)->format('m');
        });


        return view('dashboard')->with(['totel_docs' => $totel_documents, 'tot_customer' => $totel_customers, 'tot_today' => $totel_today_coc
            , 'get_documents' => $get_documents]);
    }

    public function dashboardDocView($id)
    {
        $show_documents = CustomerDocument::where('id', $id)->get();
        return view('customers.document_view')->with(['documents' => $show_documents]);
    }

}
