<?php

namespace App\Http\Livewire\Pages\FormatFileMaster;

use Livewire\Component;
use App\Models\ConfFormatFile;
use Livewire\WithFileUploads;

class FileCreate extends Component
{
    use WithFileUploads;

    public $name;
    public $file;


    protected $rules = [
        'name' => 'required|string|max:255',
    ];

    public function updatedFile()
{
    $this->validate([
        'file' => 'required|file|mimes:txt|max:10240', // 10MB max file size
    ]);

}
    public function save()
    {
        $this->validate();

        if ($this->file) {
            $filePath = $this->file->store('runtime/format', 'public');
        }
        $date = date("Ymd");
        $timestamp = date("His");


        $fileFormat = new ConfFormatFile;
        $fileFormat->name = $this->name;
        $fileFormat->file = $filePath;
        $fileFormat->date = $date;
        $fileFormat->timestamp = $timestamp;
        $fileFormat->save();

        $this->reset(['name', 'file']);
        return redirect()->route('format-file-master.index')->with('message', 'Format File saved successfully.');
    }
    public function render()
    {
        return view('livewire.pages.format-file-master.file-create');
    }
}
