<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Helpers\Helper;
use App\Models\CommonArt;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Hash;
use App\Models\Employee;
use App\Models\Lead;
use Session;
use Illuminate\Support\Facades\Auth;
use App\Traits\Sms;


class FrontPageController extends Controller
{
    use Sms;
    
    // user information page
    public function employee_info()
    {
        $employee = Employee::where('id', auth()->guard('employee')->user()->id)->first();
        
        return view('front.employee_info', ['data' => $employee]);
        
    }
    // user update information
    public function employee_update(Request $request, $id) 
    {
       $validator = Validator::make($request->all(), [
            // 'email' => 'required|email|unique:employees,email,'.$id,
            // 'whatsapp_number' => 'required|numeric|regex:/^([0-9\s\-\+\(\)]*)$/|min:10',
            
            'employee_id' => 'required|unique:employees,employee_id,'.$id,
            'password' => 'confirmed'
        ]);
        // dd($validator->fails());
        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator)->withInput();
        }
        $input = $request->input();
        unset($input['_token']);
        $employee = Employee::find($id);
        $employeeArr = array(
            // 'email' => (isset($input['email'])?$input['email']:NULL),
            // 'whatsapp_number' => (isset($input['whatsapp_number'])?$input['whatsapp_number']:NULL),
            'password' => (isset($input['password'])?$input['password']:NULL),
            // 'temp_password' => (isset($input['password'])?$input['password']:NULL),
        );
        Employee::where('id', $id)->update($employeeArr);
        return redirect()->route('create-customer')->withSuccess(array('success' =>  "Profile Updated"));
        
    }
    public function create_customer()
    {
        $emp_id = auth()->guard('employee')->user()['id'];
        $emp_leads = Lead::where(['employee_id' => $emp_id])->get();
        return view('front.create_entry', ['data' => $emp_leads]);
        
    }
    public function steps(Request $request)
    {
        $emp_id = auth()->guard('employee')->user()->id;
        $where = ['employee_id' => $emp_id];
        $emp_leads = Lead::where($where)->get();
        return view('front.steps', ['data' => $emp_leads]);
        
    }
    
    public function store_customer(Request $request)
    {
       //dd($request->customer);
       $emp_id = auth()->guard('employee')->user()['id'];
       $validator = Validator::make($request->all(), [
            // 'customer' => 'required|array|distinct',
            // "customer.*"  => "required|string|distinct|min:3",
            'customer' => [
                'required',
                'array',
                function ($attribute, $value, $fail) use($emp_id) {
                    // Check if any of the names exist in the leads table
                    $existingNames = Lead::where(['employee_id' => $emp_id])->whereIn('customer', $value)->pluck('customer')->toArray();
                    // dd($existingNames);
                    // if (count($existingNames) !== count(array_unique($existingNames))) {
                    if (count($existingNames) > 0) {
                        $fail('Some customer names are not unique.');
                    }
                },
            ],
        ]);
       if ($validator->fails()) {
            return redirect()->back()->withErrors($validator)->withInput();
        }

        $input = $request->input();
        unset($input['_token']);
        
        $emp_leads = Lead::where(['employee_id' => $emp_id])->get();
        if(count($emp_leads) >= 50){
            return redirect()->back()->withErrors(array('error' =>  "You are exceed leads limits"));
        }else{
            if($request->has('customer')){
                foreach($request->customer as $k=>$v){
                    Lead::create(array("employee_id"=> $emp_id, "customer" =>$v));
                }
                return redirect()->route('thank-you')->withErrors(array('success' =>  "Customer Submitted Successfully"));
            }
            
        }
    }
    public function thank_you()
    {
        $emp_id = auth()->guard('employee')->user()['id'];
        $emp_leads = Lead::where(['employee_id' => $emp_id])->get();
        //dd($emp_leads);
        return view('front.thank-you', ['data' => $emp_leads]);
    }
    public function download_customer(Request $request)
    {
        $emp_id = auth()->guard('employee')->user()['id'];
        $where = array(['employee_id', '=', $emp_id]);
        $search = "";
        if($request->has("search") && !empty($request->search)){
            $search = $request->search;
            $where[] = ["customer","LIKE", "%".$request->search."%"];
        }
        $emp_leads = Lead::where($where)->paginate(10);
        //dd($emp_leads);
        return view('front.download', ['data' => $emp_leads, "search" => $search]);
        
    }
    public function commonArts(Request $request)
    {
        $where = array(['status', '=', 1]);
        $search = "";
        if($request->has("search") && !empty($request->search)){
            $search = $request->search;
            $where[] = ["name","LIKE", "%".$request->search."%"];
        }
        $emp_leads = CommonArt::where($where)->orderBy('name', 'asc')->paginate(10);
        //dd($emp_leads);
        return view('front.commonArt', ['data' => $emp_leads, "search" => $search]);
        
    }

    
}
