<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Models\ZapPage;
use App\Models\ZapPageFormButtons;
use App\Models\ZapPageFormFields;
use App\Http\Controllers\AppController;
//use Illuminate\Support\Facades\Schema;
//use Illuminate\Support\Facades\DB;

class CmsController extends AppController
{
    
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\View\View
     */
    public function index()
    {
       
        $zapPages = ZapPage::get();
        return view('admin.zap_pages.index', compact('zapPages'));
    }
    
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\View\View
     */
    public function validate(Request $request)
    {
        $zapPage = new ZapPage();
        $validate = Validator::make($request->all(), [
            'slug' => 'required|unique:zap_pages,slug',
            'title' => 'required',
        ]);
        if($validate->fails()){
            return response()->json([
                'status' => false,
                'message' => 'Validation errors in your request',
                'errors' => $validate->errors()
            ], 400);
        }
        return response()->json([
            'status' => true,
            'message' => 'sucess',
            'errors' => ''
        ], 200);
        //return $zapPage->validate($request);
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\View\View
     */
    public function create()
    {
        return view('admin.zap_pages.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     *
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function store(Request $request)
    {
        
        $requestData = $request->all();
        $filePath ='';
        //print_r($file); exit;
        if(isset($request->file()['image'])){
            $file = $request->file()['image'];
            $savedAs = $requestData['slug'] . '_'.$requestData['title'].'.'.$file->extension();
            $filePath = $file->storeAs('uploads', $savedAs, 'public');
        }
        $zapPageData = [
            'slug' => $requestData['slug'],
            'title' => $requestData['title'],
            'sub_title' => $requestData['sub_title'],
            'image' => $filePath,
            'content' =>  $requestData['content'],
        ];
        $zapPage = ZapPage::create($zapPageData);

        foreach($requestData['form-fields'] as $formFields){
            $formFieldsData = [
               'zap_page_id' => $zapPage['id'],
                'type' => $formFields['field_type'],
                'name' => $formFields['field_name'],
                'title' => $formFields['field_title'],
                'place_holder' => $formFields['place_holder'],    
            ];
            $zapPageForm = ZapPageFormFields::create($formFieldsData);
        }
        //exit;
        foreach($requestData['form-buttons'] as $buttonFields){
            $formButtonData = [
                'zap_page_id' => $zapPage['id'],
                'button_type' => $buttonFields['button_type'],
                'button_value' => $buttonFields['button_value'],
                'on_submit' => $buttonFields['button_submit']
            ];
            $zapPageButton = ZapPageFormButtons::create($formButtonData);
        }

        return redirect('admin/zap-pages')->with('flash_message', 'Zap Pages added!');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     *
     * @return \Illuminate\View\View
     */
    public function show($id)
    {
        $zapPage = ZapPage::findOrFail($id);
        $zapPageFormFields = ZapPageFormFields::where('zap_page_id', $id)->get();
        $zapPageFormButtons = ZapPageFormButtons::where('zap_page_id', $id)->get();
        return view('admin.zapPages.show', compact('zapPage', 'zapPageFormFields', 'zapPageFormButtons'));

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     *
     * @return \Illuminate\View\View
     */
    public function edit($id)
    {
        $zapPage = ZapPage::findOrFail($id);
        $zapPageFormFields = ZapPageFormFields::where('zap_page_id', $id)->get();
        $zapPageFormButtons = ZapPageFormButtons::where('zap_page_id', $id)->get();
        //print_r($zapPageFormFields); exit;
        return view('admin.zap_pages.edit', compact('zapPage', 'zapPageFormFields', 'zapPageFormButtons'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param  int  $id
     *
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function update(Request $request, $id)
    {
        
        $requestData = $request->all();
        
        $zapPage = ZapPage::findOrFail($id);
        
        $filePath ='';
        //print_r($file); exit;
        if(isset($request->file()['image'])){
            $file = $request->file()['image'];
            $savedAs = $requestData['slug'] . '_'.$requestData['title'].'.'.$file->extension();
            $filePath = $file->storeAs('uploads', $savedAs, 'public');
        }
        $zapPageData = [
            'slug' => $requestData['slug'],
            'title' => $requestData['title'],
            'sub_title' => $requestData['sub_title'],
            'image' => $filePath,
           // 'content' =>  $requestData['content'],
        ];
        $zapPage->update($zapPageData);
        $formFields = ZapPageFormFields::where('zap_page_id', $id)->get()->toArray();
        foreach($formFields as $formField){
            $this->destroyFormField($formField['id']);
        } 
        foreach($requestData['form-fields'] as $formFields){
            $formFieldsData = [
               'zap_page_id' => $zapPage['id'],
                'type' => $formFields['field_type'],
                'name' => $formFields['field_name'],
                'title' => $formFields['field_title'],
                'place_holder' => $formFields['place_holder'],    
            ];
            $zapPageForm = ZapPageFormFields::create($formFieldsData);
        }
        $formButtons = ZapPageFormButtons::where('zap_page_id', $id)->get()->toArray();
        foreach($formButtons as $formButton){
            $this->destroyFormButton($formButton['id']);
        }
        foreach($requestData['form-buttons'] as $buttonFields){
            $formButtonData = [
                'zap_page_id' => $zapPage['id'],
                'button_type' => $buttonFields['button_type'],
                'button_value' => $buttonFields['button_value'],
                'on_submit' => $buttonFields['button_submit']
            ];
            $zapPageButton = ZapPageFormButtons::create($formButtonData);
        }
        return redirect('admin/zap-pages')->with('flash_message', 'Zap Pages updated!');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     *
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function destroy($id)
    {
        ZapPage::destroy($id);
        $formFields = ZapPageFormFields::where('zap_page_id', $id)->get()->toArray();
        foreach($formFields as $formField){
            $this->destroyFormField($formField['id']);
        } 
        $formButtons = ZapPageFormButtons::where('zap_page_id', $id)->get()->toArray();
        foreach($formButtons as $formButton){
            $this->destroyFormButton($formButton['id']);
        }
        return redirect('admin/zap-pages')->with('flash_message', 'Zap Pages  deleted!');
    }
    public function destroyFormField($id){
        if(ZapPageFormFields::destroy($id)){
            return true;
        }
    }
    public function destroyFormButton($id){
        if(ZapPageFormButtons::destroy($id)){
            return true;
        }
    }
}
