<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class History extends Model {

  /**
   * The database table used by the model.
   *
   * @var string
   */
  protected $table = 'history';

  /**
   * The attributes that are mass assignable.
   *
   * @var array
   */
  protected $fillable = ['user_id','view_id','type','status','created_at','updated_at'];
        
        protected function Addview($profile_id,$data_id,$type)
        {
               if(trim($profile_id)!="" && trim($data_id)!=""):
                   
                        $result = $this->where('user_id','=',$profile_id)
                              ->where ('view_id','=',$data_id)
                              ->where ('type','=',$type)     
                              ->get();
               
                    if($result->count()==0):
                        $result =$this->create([
                                               'user_id' => trim($profile_id),
                                               'view_id' => trim($data_id),
                                               'type' => trim($type),
                                             ]); 
                        return 'Data Success Fully Inserted';

                     else:
                        return 'Data Already Exists';
                     endif;
               
               endif;
        
        }
        protected function getViews_videos($profile_id)
        {
            return $result= $this->select('history.id as id','assets_data.thumb_img as thumb_img','assets_data.path as path','history.type as type','history.user_id as user_id','history.view_id as view_id','assets.name as assets_name')
                                ->leftJoin('assets', 'assets.id', '=', 'history.view_id')
                                ->leftJoin('assets_data', 'assets_data.asset_id', '=', 'assets.id')
        ->where("history.user_id",'=',$profile_id)
                                ->where("history.type",'=','V')
                                ->orderBy('history.id', 'desc')
                                ->get();
            
        }
         protected function getViews_pictures($profile_id)
        {
            return $result= $this->select('history.id as id','assets_data.thumb_img as thumb_img','assets_data.path as path','history.type as type','history.user_id as user_id','history.view_id as view_id','assets.name as assets_name')
                                ->leftJoin('assets', 'assets.id', '=', 'history.view_id')
                                ->leftJoin('assets_data', 'assets_data.asset_id', '=', 'assets.id')
        ->where("history.user_id",'=',$profile_id)
                                ->where("history.type",'=','I')
                                ->orderBy('history.id', 'desc')
                                ->get();
            
        } 
        protected function getViews_resumes($profile_id)
        {
            return $result= $this->select('history.id as id','assets_data.thumb_img as thumb_img','assets_data.path as path','history.type as type','history.user_id as user_id','history.view_id as view_id','assets.name as assets_name')
                                ->leftJoin('assets', 'assets.id', '=', 'history.view_id')
                                ->leftJoin('assets_data', 'assets_data.asset_id', '=', 'assets.id')
        ->where("history.user_id",'=',$profile_id)
                                ->where("history.type",'=','R')
                                ->orderBy('history.id', 'desc')
                                ->get();
            
        } 
        protected function getViews_profile($profile_id)
        {
            return $result= $this->select('history.id as id','profiles.name as profile_name','profiles.id as profile_id','profiles.profile_image as profile_image')
                                ->leftJoin('profiles', 'profiles.id', '=', 'history.view_id')
                                ->where("history.user_id",'=',$profile_id)
                                ->where("history.type",'=','P')
                                ->orderBy('history.id', 'desc')
                                ->get();
            
        }
          protected function getViewsVideos($take,$skip=0,$sea_key='')
    {
         return $result = $this->select('assets.profile_id','assets.id','assets.name','profiles.name as pro_name','assets_data.path','assets.processed','assets_data.thumb_img','assets.access')
                ->leftJoin('assets', 'assets.id','=','history.view_id')
                ->leftJoin('assets_data', 'assets_data.asset_id', '=', 'assets.id')
                ->leftJoin('profiles', 'profiles.id', '=', 'assets.profile_id')              
                //->where('profile_id','=',$profile_id)
                ->where ('history.type','=','V')
                ->where ('profiles.profile_type','=','T')
                ->orderBy('assets.created_at','desc')
                ->skip($skip)
                ->take($take)
                ->get();
    }
       protected function getViewsVideoscnt($sea_key='')
    {
          $result = $this->select('assets.profile_id','assets.id','assets.name','profiles.name as pro_name','assets_data.path','assets.processed','assets_data.thumb_img','assets.access')
               ->leftJoin('assets', 'assets.id','=','history.view_id')
                ->leftJoin('assets_data', 'assets_data.asset_id', '=', 'assets.id')
                ->leftJoin('profiles', 'profiles.id', '=', 'assets.profile_id')              
                //->where('profile_id','=',$profile_id)
                ->where ('history.type','=','V')
                ->where ('profiles.profile_type','=','T')
                ->orderBy('assets.created_at','desc')
                ->get();
                return  $result->count();
    }
     
}

