<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Download;
use Input;
use File;

class DownloadController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getIndex()
    {
        return view('Admin.Download');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {       


        $this->validate($request, [
            'file' => 'required',
            'title'=>'required',
          
        ]);
         $save= new Download;
      
        $save->title_nepali=$request->title_nepali;
        $save->title=$request->title;
        $file = Input::file('file');
        $filename =  time().'_'.$file->getClientOriginalName();
        $file->move(base_path().'/public/img/Download', $filename);
        $save->file_name=$filename;
    
          $save->token=AdminlogController::getToken('Download');
         $save->save();

         return redirect('admin_download')->with('info', 'Datas Saved Successfully!');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {       


        $replace = Download::find($id);
if(Input::hasFile('file'))
{
$Image = public_path("/img/Download/{$replace->file_name}"); // get previous image from folder
if (File::exists($Image)) { // unlink or remove previous image from folder
unlink($Image);
}
$file = Input::file('file');

$filename =  time().'_'.$file->getClientOriginalName();


$file->move(base_path().'/public/img/Download/', $filename);



         $data=array(


       
        'title_nepali'=>$request->title_nepali,
        'title'=>$request->title,
        'file_name'=>$filename,


        );

}else{
    $data=array(


       
        'title_nepali'=>$request->title_nepali,
        'title'=>$request->title,
       


        );
}
         

         Download::where('id',$id)->update($data);
           return redirect('admin_download')->with('info', 'Datas Updated Successfully!');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
