<?php

namespace App\Http\Controllers\SaleSetting;

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

   public function updateCostomer(Request $request)
{
    $data = $request->all();
    $id = $data['id'];

    

    // Find the existing customer
    $customer = people::find($id);

    if (!$customer) {
        return response()->json([
            'message' => 'Customer not found.',
        ], 404);
    }

    // Update basic fields
        $customer->type = "Supplier";
        $customer->name = $data['name'];
        $customer->address = $data['address'];
        $customer->contact_no = $data['contact'];
        $customer->description = $data['description'];


        if (isset($data['company'])) {
            $customer->company = $data['company'];
        }
    // Handle image upload
    if ($request->hasFile('image')) {
        $image = $request->file('image');
        $imageName = time() . rand(1, 9) . '.' . $image->getClientOriginalExtension();
        $image->move('assets/Costomer', $imageName);

        // Optionally delete the old image (if stored)
        // if (file_exists(public_path($customer->image))) {
        //     unlink(public_path($customer->image));
        // }

        $customer->image = 'assets/Costomer/' . $imageName;
    }

    $customer->save();

    return response()->json([
        'message' => 'Customer updated successfully!',
        'data' => $customer,
    ], 200);
}



     public function storeCostomer(Request $request)
    {
        $data = $request->all();
        $productCategory =  people::create([
            'type' => "Costomer",
            '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/Costomer', $imageName);

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

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


    
    public function deleteCostomer(Request $request)
{
    // Decode JSON body
    $data = $request->all();
    $id = $data['id'] ?? null;

    if (!$id) {
        return response()->json(['message' => 'ID is required.'], 400);
    }

    $customer = people::find($id);

    if (!$customer) {
        return response()->json(['message' => 'Customer not found.'], 404);
    }

    // Optionally delete the image file if exists
    // if ($customer->image && file_exists(public_path($customer->image))) {
    //     unlink(public_path($customer->image));
    // }

    $customer->delete();

    return response()->json(['message' => 'Customer deleted successfully.'], 200);
}


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

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