<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\BusinessUnit;
use Validator;
use Response;
use View;

class BusinessunitController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
  
    public function index()
    {
        $businessunits = BusinessUnit::orderBy('id', 'desc')->get();

        return view('Config.BusinessunitConfig.index', ['businessunits' => $businessunits]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'business_unit_code' => 'required|min:2|max:32',
            'business_unit' => 'required|min:2|max:32|regex:/^[a-z ,.\'-]+$/i',
            'country' => 'required|min:2|max:35|regex:/^[a-z ,.\'-]+$/i',
            'city' => 'required|min:2|max:35|regex:/^[a-z ,.\'-]+$/i',
            'address_1' => 'required|min:2|max:35',
            'business_unit_head' => 'required|min:2|max:32|regex:/^[a-z ,.\'-]+$/i',
            
        ]);
            $businessunits = new BusinessUnit();
            $businessunits->business_unit = $request->business_unit;
            $businessunits->business_unit_code = $request->business_unit_code;
            $businessunits->country = $request->country;
            $businessunits->city = $request->city;
            $businessunits->address_1 = $request->address_1;
            $businessunits->business_unit_head = $request->business_unit_head;
            $businessunits->save();
            return response()->json($businessunits);
        
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $businessunits = BusinessUnit::findOrFail($id);

        return response()->json($businessunits);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $businessunits = BusinessUnit::findOrFail($id);

        return response()->json($businessunits);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $request->validate([
            'business_unit_code' => 'required|min:2|max:32',
            'business_unit' => 'required|min:2|max:32|regex:/^[a-z ,.\'-]+$/i',
            'country' => 'required|min:2|max:35|regex:/^[a-z ,.\'-]+$/i',
            'city' => 'required|min:2|max:35|regex:/^[a-z ,.\'-]+$/i',
            'address_1' => 'required|min:2|max:35',
            'business_unit_head' => 'required|min:2|max:32|regex:/^[a-z ,.\'-]+$/i',
            
        ]);
       
            $businessunits = BusinessUnit::findOrFail($id);
            $businessunits->business_unit = $request->business_unit;
            $businessunits->business_unit_code = $request->business_unit_code;
            $businessunits->country = $request->country;
            $businessunits->city = $request->city;
            $businessunits->address_1 = $request->address_1;
            $businessunits->business_unit_head = $request->business_unit_head;
            $businessunits->save();
            return response()->json($businessunits);
        
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $businessunits = BusinessUnit::findOrFail($id);
        $businessunits->delete();

        return response()->json($businessunits);
    }
}
