<?php

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

use DB;
use Carbon\Carbon;
use Image;

class VersionController 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!");
                $arr[$i]['date']    = $ex02[0];
                $arr[$i]['name']    = $ex03[0];
                $arr[$i]['text']    = fread($myfile,filesize($file));
                fclose($myfile);
                $i++;
            }
        }
        $view['files'] = array_reverse($arr);
        return view('endoindex.version.index',$view);
    }
    public function show($id){
        return view('endoindex.home.show');

    }
    public function store(Request $r){
        if(isset($r->event)){
            $event = $r->event;
            $this->$event($r);
            if($event=="update_git") return redirect("git");
        }
    }


    public function update_git($r){
        // dd($r);

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

    public function create(Request $r){
        return view('endoindex.version.create');
    }
}
