<?php

namespace AhmadFatoni\ApiGenerator\Controllers\API;

use Cms\Classes\Controller;
use BackendMenu;

use Illuminate\Http\Request;
use AhmadFatoni\ApiGenerator\Helpers\Helpers;
use Illuminate\Support\Facades\Validator;
use tps\Airport\Models\Content;
use tps\Airport\Models\Menu;
use Config;


class sectionController extends Controller
{
    protected $Content;

    protected $helpers;

    public function __construct(Content $Content, Helpers $helpers)
    {
        parent::__construct();
        $this->Content    = $Content;
        $this->helpers          = $helpers;
    }

    protected function validateForm($data, $rules)
    {
        return Validator::make($data, $rules);
    }

    public function index(Request $request)
    {
        $path = 'http://216.250.8.104:8081' . Config::get('cms.storage.media.path');

        $dataVal = $request->all();

        $rules = [
            'page' => 'required',
            //'locale' => 'required'
        ];

        $validator = $this->validateForm($dataVal, $rules);
        if ($validator->fails()) {
            return $this->helpers->apiArrayResponseBuilder(400, 'fail', $validator->errors());
        }

        $data = Content::where('menu_slug', input('page'))->select('id', 'note', 'menu_slug', 'data')->get();

        if (count($data) != 0) {

            $data[0]->image_path = $path;
            $contents = array();
            $dataq = json_decode($data);

            for ($i = 0; $i < count($data[0]->data); $i++) {
                if ($dataq[0]->data[$i]->section_id != null && $dataq[0]->data[$i]->flight_type == 0) {
                    $modified = $dataq[0]->data[$i]->section::where('id', $dataq[0]->data[$i]->section_id)->first();
                    
                    $modified->translateContext($dataVal["locale"] ?? 'ru');
                    
                    $modified->makeHidden(['created_at', 'updated_at', 'deleted_at']);
                    $contents = array_merge($contents, array($modified));
                    // dump($testq);
                } 
                else if($dataq[0]->data[$i]->flight_type == 1 && $dataq[0]->data[$i]->section_id == null){
                    $modified = $dataq[0]->data[$i]->section::where('type_flight', input("flight_type"))->first();
                    
                    $modified->translateContext($dataVal["locale"] ?? 'ru');
                    $contents = array_merge($contents, array($modified));
                }
                else{
                   
                    $modified = $dataq[0]->data[$i]->section::select('id', 'title', 'slug', 'excerpt', 'published_at', 'type')
                                                   ->orderBy('published_at', 'desc')
                                                   ->limit($dataq[0]->data[$i]->limit)
                                                   ->with(['featured_images'])
                                                   ->with('views')
                                                   ->get();
                    
                     
                       if($modified){
                            $modified->each(function ($item, $key) use($dataVal) {
                                $item->translateContext($dataVal["locale"] ?? 'ru');
                            });
                        }
                    
                     $post_section = array(
                            "type"=> "post",
                            "posts"=> $modified
                        );
                    $contents = array_merge($contents, array($post_section));
                }
            }
            $data[0]->section_contents = $contents;

            $data->makeHidden(['data']);

            // dd($data[0]->data);
            return $this->helpers->apiArrayResponseBuilder(200, 'success', $data);
        } else {
            return $this->helpers->apiArrayResponseBuilder(404, 'not found data', []);
        }
    }
    
    
    
    public function getMenu(Request $request)
    {
        $dataVal = $request->all();
        
        $data = Menu::select('id', 'name', 'sub_menu', 'order', 'menu_slug', 'status')
                      ->orderBy('order', 'asc')
                      ->get();

        if (count($data) != 0) {
           
           
                $data->each(function ($item, $key) use($dataVal) {
                    $item->translateContext($dataVal["locale"] ?? 'ru');
                });
            
            
            return $this->helpers->apiArrayResponseBuilder(200, 'success', $data);
            
        } else {
            return $this->helpers->apiArrayResponseBuilder(404, 'not found data', []);
        }
    }
}
