<?php

namespace App\Http\Controllers\Manager;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;
use App\Models\Department;
use App\Models\Employee;
use Illuminate\Support\Facades\Storage;
use App\Models\Setting;


class ManagerEmployeeController extends Controller
{
    public function index()
    {
        $employees = Employee::all();
        $departments = Department::all();
        $settings = Setting::first();
        return view('manager.employee.index', compact('employees' , 'settings' , 'departments'));
    }

    public function addpage(){
        $departments = Department::all();
        return view('manager/employee/add' , compact('departments'));
    }


    public function store(Request $request)
    {
        // Validate the incoming request data
        $request->validate([
            'name' => 'required|string|max:255',
            'email' => 'required|email|unique:employees,email',
            'phone' => 'required|string|max:255',
            'department' => 'required|string|max:255',
            'address' => 'required|string|max:255',
            'img' => 'nullable|image|max:2048',
            'bank_name' => 'nullable|string',
            'account_no' => 'nullable|string',
            'branch_name' => 'nullable|string',
            'routing_no' => 'nullable|string'
        ]);

        // Create a new employee instance
        $employee = new Employee();
        $employee->name = $request->name;
        $employee->email = $request->email;
        $employee->phone = $request->phone;
        $employee->department = $request->department; // Corrected column name
        $employee->address = $request->address;
        $employee->bank_name = $request->bank_name;
        $employee->account_no = $request->account_no;
        $employee->branch_name = $request->branch_name;
        $employee->routing_no = $request->routing_no;

        
       if ($request->hasFile('img')) {
            $image = $request->file('img');
            $imageName = time() . '.' . $image->getClientOriginalExtension();
            $image->move(public_path('storage/img'), $imageName); // Store the image in public/storage/img directory
            $employee->img = $imageName;
        }

        $employee->bank_name = $request->filled('bank_name') ? $request->bank_name : '';
        $employee->account_no = $request->filled('account_no') ? $request->account_no : '';
        $employee->branch_name = $request->filled('branch_name') ? $request->branch_name : '';
        $employee->routing_no = $request->filled('routing_no') ? $request->routing_no : '';
        
        // Save the employee to the database
        $employee->save();

        // Redirect back with success message
        return redirect()->route('manager.employee.all')->with('success', 'Employee added successfully');
    }


    public function show($id)
    {
        $employee = Employee::findOrFail($id);
        return response()->json($employee);
    }


    public function edit($id)
    {
        $employee = Employee::findOrFail($id);
        $departments = Department::all();
        return view('manager/employee/edit', compact('employee', 'departments'));
    }

    public function update(Request $request, $id)
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'email' => 'required|email|max:255',
            'phone' => 'required|string|max:15',
            'department' => 'required|string|max:255',
            'address' => 'required|string|max:255',
            'img' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
            'bank_name' => 'nullable|string',
            'account_no' => 'nullable|string',
            'branch_name' => 'nullable|string',
            'routing_no' => 'nullable|string'
        ]);

        $employee = Employee::findOrFail($id);
        $employee->name = $request->name;
        $employee->email = $request->email;
        $employee->phone = $request->phone;
        $employee->department = $request->department;
        $employee->address = $request->address;
        $employee->bank_name = $request->bank_name;
        $employee->account_no = $request->account_no;
        $employee->branch_name = $request->branch_name;
        $employee->routing_no = $request->routing_no;

      if ($request->hasFile('img')) {
            $image = $request->file('img');
            $imageName = time() . '.' . $image->getClientOriginalExtension();
            $image->move(public_path('storage/img'), $imageName); // Store the image in public/storage/img directory
            $employee->img = $imageName;
        }

         // Check for optional fields and set to null if not present
        $employee->bank_name = $request->filled('bank_name') ? $request->bank_name : '';
        $employee->account_no = $request->filled('account_no') ? $request->account_no : '';
        $employee->branch_name = $request->filled('branch_name') ? $request->branch_name : '';
        $employee->routing_no = $request->filled('routing_no') ? $request->routing_no : '';

        $employee->save();

        return redirect()->route('manager.employee.all')->with('success', 'Employee updated successfully');
    }


    public function destroy(Employee $employee)
    {
        $employee->delete();
        return redirect()->route('manager.employee.all')->with('success', 'Employee deleted successfully.');
    }

}
