<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Token;
use App\FileData;
use App\ConnectModel;
use DB;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Validator;
use Input;
use File;
use Intervention\Image\Facades\Image;

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


      public function getPopup()
    {
        return view('Admin.PopUp');
    }



      public function store(Request $request)
    {
        
          


        


                $file = Input::file('image');

                $rules = array('file' => 'required'); //'required|mimes:png,gif,jpeg,txt,pdf,doc'
                $validator = Validator::make(array('file' => $file), $rules);
                if ($validator->passes()) {


                    $location = public_path('img/PopUp/Popup.jpg');
                    Image::make($file)->encode('jpg', 75)->resize(761, 519)->save($location);

              
                }
            




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

    /**

     *
     * @return \Illuminate\Http\Response
     */

    public static function getToken($name) {

        $token = new Token;
        $token->token_name = $name;

        $token->save();

        return $token->id;

    }

    public static function IsSelected($check, $tobecheck) {
        if ($check == $tobecheck) {
            echo "selected";
        }
    }

       public static function getimagefromtoken($token){

    $data=DB::table('file_datas')->where('token','=',$token)->get();

    return $data;

  }


     public static function FileSaver(Request $request,$folder,$height,$width,$update=false){
         $i = 0;
        $files = Input::file('image');

        
            if(is_array($files)){
                $file_count = count($files);
                foreach ($files as $file) {

                 $rules = array('file' => 'required'); //'required|mimes:png,gif,jpeg,txt,pdf,doc'
                  $validator = Validator::make(array('file'=> $file), $rules);
                     if($validator->passes()){
                         $filename = DB::table('tokens')->max('tokenid').$i .'.jpg';


                     $location = public_path('img/'.$folder.'/' . $filename);
                     Image::make($file)->encode('jpg', 75)->resize($height, $width)->save($location);


                     $save= new FileData;

                        $save->token=DB::table('tokens')->max('tokenid');
                        $save->file_name=$filename;
                        $save->token_name=$folder;


        $save->save();

             }
                     $i ++;


             }

             return $file_count;
         }
         else{
           $rules = array('files' => 'required'); //'required|mimes:png,gif,jpeg,txt,pdf,doc'
        $validator = Validator::make(array('files' => $files), $rules);
        if ($validator->passes()) {
            $filename = DB::table('tokens')->max('tokenid') . '.jpg';

          $location = public_path('img/'.$folder.'/' . $filename);
           Image::make($files)->encode('jpg', 75)->resize($height, $width)->save($location);

            $save = new FileData;

         $save->token=DB::table('tokens')->max('tokenid');
         $save->file_name=$filename;
         $save->token_name=$folder;


        $save->save();

        }

       

         }




    }


    public static function FileUpdater(Request $request,$folder,$height,$width,$lastNo,$id){

     $files = Input::file('image');
     if(is_array($files)){
            if(Input::hasfile('image')){

         // counting of uploaded images
            $file_count = count($files);
  // start count how many uploaded
            $i = $lastNo;


               foreach ($files as $file) {
                $rules = array('file' => 'required'); //'required|mimes:png,gif,jpeg,txt,pdf,doc'
                 $validator = Validator::make(array('file'=> $file), $rules);
                    if($validator->passes()){
                        $filename =  $id.($i) .'.jpg';

                         $location = public_path('img/'.$folder.'/' . $filename);
           Image::make($file)->encode('jpg', 75)->resize($height, $width)->save($location);

                               }
                    $i ++;
                          $save = new FileData;

         $save->token=$id;
         $save->file_name=$filename;
         $save->token_name=$folder;


        $save->save();
            }


        }
     }else{
              if (Input::hasFile('image')) {
            $Image = public_path('img/'.$folder.'/' . $id); // get previous image from folder
            if (File::exists($Image)) {
                // unlink or remove previous image from folder
                unlink($Image);
            }
            $file = Input::file('image');
            $rules = array('file' => 'required'); //'required|mimes:png,gif,jpeg,txt,pdf,doc'
            $validator = Validator::make(array('file' => $file), $rules);
            if ($validator->passes()) {
                $filename =  $id . '.jpg';

                $location = public_path('img/'.$folder.'/' . $filename);
                Image::make($file)->encode('jpg', 75)->resize($height, $width)->save($location);
            }

        }
        }


    }


    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
     public static function table_generator($tablename,$selectcol,$route,$foldername)

    {
        $col=array('token');
        $select= array_merge($selectcol,$col);

          $done= DB::table($tablename)->orderBy($tablename.'.id', 'asc')->join('tokens','tokenid','=','token')->select(DB::raw(implode(",",$select)))->paginate(10);


          echo $done;
$token=null;
$i=1;
        foreach ($done as $key) {
        echo '<tr>';
echo '<td>'.($i++).'</td>';
$j='';
            foreach ($key as $value=>$index) {

if($value!='is_Show' &&$value!='token'){
        echo '<td>'.$index.'</td>';

            }
elseif ($value=='is_Show') {
$j=$index;

}
else{


?>
    <td><a href='<?php echo "delete/".$route."/".$index."/".$tablename."/".$foldername;?>'  class="btn btn-danger delbtn" >Delete</a>

  <a href='<?php echo "edit/".$route."/".$index."/".$tablename;?>' class="btn btn-success" id="edit" >Edit</a></td>
  <?php

}

        }
        echo '</tr>';




}



    }


    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($route,$id,$tablename)
    {
      $single= ConnectModel::editdata($id,$tablename)->first();


   return redirect($route)->with('avisd',$single);


    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
     public function delete($route,$id,$tablename,$foldername){


    DB::table('tokens')->where('tokenid', '=', $id)->delete();


    DB::table($tablename)->where('token', '=', $id)->delete();
    $take=DB::table('file_datas')->where('token', '=', $id)->get();

    foreach ($take as $key => $value) {
       $file= $value->file_name;
       $filename = public_path().'/img/'.$foldername.'/'.$file;
      File::delete($filename);
    }

   DB::table('file_datas')->where('token', '=', $id)->delete();
    return redirect($route)->with('Data Deleted Successfully');
}

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

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