<?php

namespace App\Http\Controllers;

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

class BusinessSourceController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $sources = BusinessSource::orderBy('name', 'asc')->get();
        return  view('setup-business-leeds', compact('sources'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\Response
     */
    public function create()
    {
        return view('libs/add-business-source');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $name = $request['source-name'];
        $status = $request['status'];

        $available_names = BusinessSource::where('name', $name)->first();

        if($available_names){
            return 'exist';
        }else{
            $init = new BusinessSource;

            $init->name = $name;
            $init->status = $status;
            if($init->save()){
                return 'success';
            }else{
                return 'error';
            }
        }

    }

    /**
     * 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(int $id)
    {

        $init =  BusinessSource::find($id);
        if($init->status == 1){
            $new_status = 0;
        }else{
            $new_status = 1;
        }
        $init->status = $new_status;
        if($init->update()){
            return 'success';
        }else{
            return 'error';
        }
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

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