<?php
/*
@copyright
Fleet Manager v7.2
Copyright (C) 2017-2025 Hyvikk Solutions <https://hyvikk.com/> All rights reserved.
Design and developed by Hyvikk Solutions <https://hyvikk.com/>
*/
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Model\Organization;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class OrganizationController extends Controller {

    // Show all organizations
    public function index() {
        $data = Organization::orderBy('id', 'desc')->get();
        return view('organization.show', compact('data'));
    }

    // Show create form
    public function create() {
        return view('organization.create');
    }

    // Store organization
    public function store(Request $request) {
        $validation = Validator::make($request->all(), [
            'organization_name' => 'required|string|max:255',
            'email' => 'required|email|max:255',
            'phone' => 'required|numeric|digits_between:7,10|unique:organization,phone',
            'address' => 'required|string|max:500',
            'city' => 'required|string|max:100',
            'state' => 'required|string|max:100',
            'country' => 'required|string|max:100',
            'zipcode' => 'required|digits_between:1,20',

        ]);

        if ($validation->fails()) {
            return back()->withErrors($validation)->withInput();
        }

        $data = new Organization();
        $data->organization_name = $request->organization_name;
        $data->email = $request->email;
        $data->phone = $request->phone;
        $data->address = $request->address;
        $data->city = $request->city;
        $data->state = $request->state;
        $data->country = $request->country;
        $data->zipcode = $request->zipcode;

        $data->save();

        return redirect()->route('organization.index')->with('success', 'Organization created successfully.');
    }

    // Show edit form
    public function edit($id) {
        $data = Organization::findOrFail($id);
        return view('organization.edit', compact('data'));
    }

    // Update organization
    public function update(Request $request, $id) {
        $validation = Validator::make($request->all(), [
            'organization_name' => 'required|string|max:255',
            'email' => 'required|email|max:255',
            'phone' => 'required|numeric|digits_between:7,10|unique:organization,phone,' . $id,
            'address' => 'required|string|max:500',
            'city' => 'required|string|max:100',
            'state' => 'required|string|max:100',
            'country' => 'required|string|max:100',
            'zipcode' => 'required|digits_between:1,20',

        ]);

        if ($validation->fails()) {
            return back()->withErrors($validation)->withInput();
        }

        $data = Organization::findOrFail($id);
        $data->organization_name = $request->organization_name;
        $data->email = $request->email;
        $data->phone = $request->phone;
        $data->address = $request->address;
        $data->city = $request->city;
        $data->state = $request->state;
        $data->country = $request->country;
        $data->zipcode = $request->zipcode;

        $data->save();

        return redirect()->route('organization.index')->with('success', 'Organization updated successfully.');
    }

    // Delete single organization
    public function destroy(Request $request) {
        $data = Organization::findOrFail($request->id);
        $data->delete();

        return redirect()->back()->with('success', 'Organization deleted successfully.');
    }

    // Bulk delete organizations
    public function multiple_delete(Request $request) {
        $data = Organization::whereIn('id', $request->ids)->get();
        foreach ($data as $d) {
            $d->delete();
        }

        return redirect()->back()->with('success', 'Selected organizations deleted successfully.');
    }
}
