<?php

namespace App\Http\Controllers\EndoINDEX;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

use Illuminate\Support\Facades\DB;

class AboutProjectController extends Controller
{
    public function __construct(Request $r){checklogin();}

    public function index(){
        $files = glob("D:\laragon\htdocs\\endoindex\\version\\*.txt"); // get all file names
        $arr = array();
        $i=0;
        foreach ($files as $file) {

            if (is_file($file)) {
                $ex01       = explode("\\",$file);
                $ex02       = explode("_",end($ex01));
                $ex03       = explode(".",end($ex02));
                $myfile     = fopen($file, "r") or die("Unable to open file!");
                $json       = jsonDecode(fread($myfile,filesize($file)));
                $arr[$i]= (array) $json;
                fclose($myfile);
                $i++;
            }
        }
        $view['files'] = array_reverse($arr);
        // dd($view);
        return view('endoindex.aboutProject/index' ,$view);
    }


    public function show($id){


        return view('endoindex.aboutProject/index');

    }

    public function store(Request $r){

        if(isset($r->event)){
            $event = $r->event;

            $this->$event($r);
            if($event == "create_about") return redirect("about");
        }
    }

    public function create(){


        return view('endoindex.aboutProject/create');

    }


    public function create_about($r){


        $date       = date("ymd");
        // $version       = $r->version;
        $filename   = "D:\laragon\htdocs\\endoindex\\version\\$date"."_".".txt";
        $check      = file_exists($filename);
        ($check) ? $mode="a": $mode="w";
        $mode="w";
        $myfile = fopen($filename, $mode) or die("Unable to open file!");

        // $getabout = file_get_contents($filename);


        $arr = array();
        $arr['version'] = $r->version;
        $arr['date'] = $r->date;
        $arr['about'] = $r->about;

        $json = jsonEncode($arr);
        // $myfile = '';
        // dd($json);
        fwrite($myfile, $json);
        fclose($myfile);

    }


}
