<?php

namespace App\Http\Controllers\backend;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Alert;
use App\ContactDetail;

class ContactDetailController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request, ContactDetail $model)
    {
        //getting the details
        $contacts = $model::latest()->limit(1)->get();
        return view('backend.contact_detail',compact(['contacts']));
    }

    /**
     * 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, ContactDetail $model)
    {
        //getting the values
        $data_contact = [
            'email' => $request->email,
            'fax' => $request->fax,
            'toll_free' => $request->toll_free,
            'telephone' => $request->telephone,
            'phone1' => $request->phone1,
            'phone2' => $request->phone2,
            'address' => $request->address,
        ];

        $data = $model::create($data_contact);

        if($data){
            return response()->json(["success"=>"Contact Details Created Successfully."]);
        }
        else{
            return response()->json(["errors"=>"Unable To Create Contact Details."]);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, ContactDetail $model)
    {
        //getting the values
        $id = $request->contact;

        $data_update = [
            'email' => $request->email,
            'fax' => $request->fax,
            'toll_free' => $request->toll_free,
            'telephone' => $request->telephone,
            'phone1' => $request->phone1,
            'phone2' => $request->phone2,
            'address' => $request->address,
        ];

        $data = $model::whereId($id)->update($data_update);

        if($data){
            return response()->json(["success"=>"Contact Details updated Successfully."]);
        }
        else{
            return response()->json(["errors"=>"Unable to update Contact Details."]);
        }

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
