<?php

namespace App\Http\Controllers\SaleSetting;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\ToDoList;
use App\Models\people;
class supplierController extends Controller
{
    public function supplierRregister()
    {   
        $ToDoList = ToDoList::get();
        return view('Section.SaleSettings.supplierRegister',compact('ToDoList'));
      
    }

    public function storeSupplier(Request $request)
    {
        $data = $request->all();
        $productCategory =  people::create([
            'company' => $data['company'],
            'type' => "Supplier",
            'name' => $data['name'],
            'address' => $data['address'],
            'contact_no' => $data['contact'],
            'description' => $data['description'],

        ]);
          // Handle the image upload
    if ($request->hasFile('image')) {
        $image = $request->file('image');
        $imageName = time() . rand(1, 9) . '.' . $image->getClientOriginalExtension();
        $image->move('assets/Suppliers', $imageName);

        // Save the image details (if associated with another model or field in `People`)
        $productCategory->image = 'assets/Suppliers/' . $imageName;
        $productCategory->save();
    }

    return response()->json([
        'message' => 'Supplier created successfully!',
        'data' => $productCategory,
    ], 201);
    }

    public function listSuppliers(Request $request)
    {
        $Suppliers = People::where('type', 'Supplier')->get();

        $response['data'] = $Suppliers;
        return response()->json($response);
    }
    
}


