<?php

namespace App\Http\Controllers\Admin;

use App\Landing;
use App\LandingDescription;
use App\Http\Requests\LandingRequest;
use Exception;
use Illuminate\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Redirector;
use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;

class LandingController extends Controller
{

    /** Redirect to this path after each operation success*/
    private $redirectSuccessPath = '/admin/landings';
    /** View folder */
    private $viewDirectory = 'admin.landings.';

    public function __construct()
    {
        $this->redirectSuccessPath = getCurrentLocale().$this->redirectSuccessPath;
    }
    /**
     * Display a listing of the resource.
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $title= __('landings.head');
        $request= $request->toArray();
        return view($this->viewDirectory.'index',compact('title'));
    }

    /**
     * return data of the landings.
     *
     * @param Request $request
     * @return View
     */
    public function grid(Request $request) :View
    {
        $query = Landing::join('landing_descriptions AS landingDesc', 'landings.id', 'landingDesc.landing_id')
            ->where('landingDesc.language_id', currentLanguage()->id)
            ->select(['landingDesc.banner_description','landings.*']);

        if ($request->date_from) {
            $query->whereDate('landings.created_at', '>=', $request->date_from);
        }

        if ($request->date_to) {
            $query->whereDate('landings.created_at', '<=', $request->date_to);
        }


        if ( !is_null($request->status) && $request->status == 0) {
            $query->onlyTrashed();
        }

        $landings = $query->cursor();

        return view($this->viewDirectory.'grid',compact('landings'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param Landing $landing
     * @return View
     */
    public function edit(Landing $landing) :View
    {
        $action= route('landings.update', $landing->id);
        $head = metaFields('landings', 'edit', getCurrentLocale()) ?? __('landings.edit');

        $query = LandingDescription::where('landing_id', $landing->id)
            ->join('languages', 'languages.id', 'landing_descriptions.language_id')
            ->select(['landing_descriptions.*', 'languages.local']);

        $landingDescription= $query->get();

        foreach ($landingDescription as $row) {
            $landing[$row->local]= $row;
        }

        $local = getCurrentLocale();

        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

    /**
     * Update the specified resource in storage.
     *
     * @param LandingRequest $request
     * @param Landing $landing
     * @return RedirectResponse|Redirector
     */
    public function update(LandingRequest $request, Landing $landing)
    {
        $landing->touch();

        // Delete old description
        LandingDescription::where('landing_id', $landing->id)->delete();

        // Insert new description
        $this->saveData( $request, $landing->id );

        return redirect($this->redirectSuccessPath)->with('message', __('dashboard.saveDone'));
    }

    /**
     * Handle Save form data
     *
     * @param LandingRequest $request
     * @param int $landing_id
     * @return void
     */

    private function saveData(LandingRequest $request, int $landing_id ):void
    {
        $requestData= $request->all();

        foreach (languages() as $lang) {
            $data=[
                'landing_id'=> $landing_id,
                'language_id'=> $lang->id,
                'banner_description'=> $requestData['banner_description_'.$lang->local],
                'sec_2_description'=> $requestData['sec_2_description_'.$lang->local],
                'sec_3_box_1_description'=> $requestData['sec_3_box_1_description_'.$lang->local],
                'sec_3_box_2_description'=> $requestData['sec_3_box_2_description_'.$lang->local],
                'sec_3_box_3_description'=> $requestData['sec_3_box_3_description_'.$lang->local],
                'sec_3_box_4_description'=> $requestData['sec_3_box_4_description_'.$lang->local],
                'sec_3_title'=> $requestData['sec_3_title_'.$lang->local],
                'sec_3_description'=> $requestData['sec_3_description_'.$lang->local],
                'sec_4_title'=> $requestData['sec_4_title_'.$lang->local],
                'sec_4_description'=> $requestData['sec_4_description_'.$lang->local],
                'sec_4_box_1_description'=> $requestData['sec_4_box_1_description_'.$lang->local],
                'sec_4_box_2_description'=> $requestData['sec_4_box_2_description_'.$lang->local],
                'sec_4_box_3_description'=> $requestData['sec_4_box_3_description_'.$lang->local],
                'sec_4_box_4_description'=> $requestData['sec_4_box_4_description_'.$lang->local],
                'sec_5_description'=> $requestData['sec_5_description_'.$lang->local],
            ];
            LandingDescription::create($data);
        }

    }
}
