<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\customers;

class customersController extends Controller
{
    public function index(){
        $customers = customers::orderBy("id","desc")->get();
        return response()->json($customers);
    }
    public function store(Request $request)
    {
        $request->validate([
        'routes_id'=> 'required',
        'customerName' => 'required',
        'customerAddress' => 'required',
        'customerNIC' => 'required',
        'customerTelephone' => 'required',
        'customerMobile'=> 'required',
        'customerEmail' => 'required',
        'creaditLimit' => 'required',
        'is_vat_customer' => 'required',
        ]);
        $customer = customers::create([
            'routes_id' => $request->routes_id,
            'customerName' => $request->customerName,
            'customerAddress'=> $request->customerAddress,
            'customerNIC'=> $request->customerNIC,
            'customerTelephone'=> $request->customerTelephone,
            'customerMobile'=> $request->customerMobile,
            'customerEmail'=> $request->customerEmail,
            'creaditLimit'=> $request->creaditLimit,
            'is_vat_customer'=> $request->is_vat_customer,
        ]);

        return response()->json($customer);

    }

    public function update(Request $request, $id){
        $customer = customers::find($id);
        $customer->routes_id = $request->routes_id;
        $customer->customerName = $request->customerName;
        $customer->customerAddress = $request->customerAddress;
        $customer->customerNIC = $request->customerNIC;
        $customer->customerTelephone = $request->customerTelephone;
        $customer->customerMobile = $request->customerMobile;
        $customer->customerEmail = $request->customerEmail;
        $customer->creaditLimit = $request->creaditLimit;
        $customer->is_vat_customer = $request->is_vat_customer;
        $customer->save();
        return response()->json($customer);

    }
}
