<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Auth;
use App\Models\systemAdministration\info;
use App\Models\systemAdministration\language;
use Illuminate\Http\Request;
use Validator;

class InfoController extends Controller
{

    /**
     * Create a new controller instance.
     *
     * @return void
     */

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $infos = Info::orderBy("id", "desc")->paginate(1);
        return view("back-end.info.index", compact("infos"));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $languages = language::all();
        return view("back-end.info.create", compact('languages'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = Validator::make($request->all(), [
            "website_title" => "required|max:255",
            "phone1" => "required",
            "phone2" => "required",
            "email" => "required",
            "address" => "required",
            "facebook_url" => "required",
            "twitter_url" => "required",
            "youtube_url" => "required"
        ])->validate();
        $info = new Info();
        $info->user_id = Auth::id();
        $info->website_title = $request->website_title;
        $info->phone1 = $request->phone1;
        $info->phone2 = $request->phone2;
        $info->email = $request->email;
        $info->local_address = $request->address;
        $info->about = $request->about;
        $info->vission = $request->vission;
        $info->mission = $request->mission;
        $info->facebook_url = $request->facebook_url;
        $info->twitter_url = $request->twitter_url;
        $info->youtube_url = $request->youtube_url;
        $info->lingding_url = $request->lingding_url;
        $info->whats_app = $request->whats_app;
        $info->tiktok_url = $request->tiktok_url;
        $info->instegram_url = $request->instegram_url;
        $info->language_id = $request->language_id;
        if (request()->hasFile('image')) {
            $date = date('Y-M');
            $filename = 'infos-' . time() . '-' . request()->file('image')->getClientOriginalName();
            $path = request()->file('image')->storeAs('infos/' . $date, $filename, 'public');
        } else {
            $path = "image/noimage.jpg";
        }
        $info->logo = $path;
        $is_saved = $info->save();
        if ($is_saved) {
            session()->flash("msg", "Success!");
            return redirect()->route('info.index');
        }
    }

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

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Post  $post
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $info = Info::find($id);
        $languages = language::all();
        return view("back-end.info.edit", compact("info", "languages"));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Post  $post
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $info = Info::find($id);
        $info->user_id = Auth::id();
        $info->website_title = $request->website_title;
        $info->phone1 = $request->phone1;
        $info->phone2 = $request->phone2;
        $info->email = $request->email;
        $info->local_address = $request->address;
        $info->about = $request->about;
        $info->vission = $request->vission;
        $info->mission = $request->mission;
        $info->facebook_url = $request->facebook_url;
        $info->twitter_url = $request->twitter_url;
        $info->youtube_url = $request->youtube_url;
        $info->lingding_url = $request->lingding_url;
        $info->whats_app = $request->whats_app;
        $info->tiktok_url = $request->tiktok_url;
        $info->instegram_url = $request->instegram_url;
        $info->language_id = $request->language_id;
        if (request()->hasFile('image')) {
            $date = date('Y-M');
            $filename = 'infos-' . time() . '-' . request()->file('image')->getClientOriginalName();
            $path = request()->file('image')->storeAs('infos/' . $date, $filename, 'public');
        } else {
            $path = $info->logo;
        }
        $info->logo = $path;

        $is_saved = $info->save();
        if ($is_saved) {
            session()->flash("msg", "Success!");
            return redirect()->route('info.index');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Post  $post
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $info = Info::find($id);
        $is_saved = $info->delete();
        if ($is_saved) {
            session()->flash("msg", "Success!");
            return redirect()->route('info.index');
        }
    }
}
