<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use App\Models\Syllabus;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class SyllabusController extends Controller
{
    protected $base_route = 'backend.syllabus';
    protected $view_path = 'backend.syllabus';
    protected $image_path = 'images/syllabus';
    protected $panel =  'Syllabus';

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $action = 'List';
        $data['rows'] = Syllabus::all();
        return view($this->loadDataToView($this->view_path . '.index'), compact('action','data'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $action = 'Create';
        return view($this->loadDataToView($this->view_path . '.create'), compact('action'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
        'title' => 'required',
        'file_downloads' => 'required',
        ]);

        if($request->hasFile('file_downloads')){
            $file = $request->file('file_downloads');
            $fileName = time() . '_' . $file->getClientOriginalName();
            $file->move(public_path($this->image_path . '/'), $fileName);
            $request->request->add(['file' => $fileName]);
        }
        $request->request->add(['created_by' => Auth::user()->id]);
        $data['row'] = Syllabus::create($request->all());
        if($data['row']){
            $request->session()->flash('success_message', $this->panel . ' Created Successfully');
        } else {
            $request->session()->flash('error_message', $this->panel . ' Can not create');
        }
        return redirect()->route($this->base_route . '.create');


    }

     /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $action = 'Show';
        $data['row'] = Syllabus::find($id);
        return view($this->loadDataToView($this->view_path . '.show'), compact('action','data'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $action = 'Edit';
        $data['row'] = Syllabus::find($id);
        return view($this->loadDataToView($this->view_path . '.edit'), compact('action','data'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $data['row'] = Syllabus::find($id);
        $oldFile = $data['row']->file;
        if($request->hasFile('file_downloads')){
            $file = $request->file('file_downloads');
            $fileName = time() . '_' . $file->getClientOriginalName();
            $file->move(public_path($this->image_path . '/'), $fileName);
            $request->request->add(['file' => $fileName]);
            if($oldFile) {
                if (file_exists(public_path($this->image_path . '/' . $data['row']->file))) {
                    unlink(public_path($this->image_path . '/' . $oldFile));
                }
            }
        }
        $request->request->add(['updated_by' => Auth::user()->id]);
        if($data['row']->update($request->all())){
            $request->session()->flash('success_message', $this->panel . ' Updated Successfully');
        } else {
            $request->session()->flash('error_message', $this->panel . ' Can not update');
        }
        return redirect()->route($this->base_route . '.index');

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request,$id)
    {
        $data['row'] = Syllabus::find($id);
        $oldImage = $data['row']->file;
        if($data['row']->delete()){
            if($oldImage) {
                if (file_exists(public_path($this->image_path . '/' . $data['row']->file))) {
                    unlink(public_path($this->image_path . '/' . $oldImage));
                }
            }
            $request->session()->flash('success_message', $this->panel . ' Deleted Successfully');
        } else {
            $request->session()->flash('error_message', $this->panel . ' Can not delete');
        }
        return redirect()->route($this->base_route . '.index');
    }
}
