<?php

namespace App\Models;

use DB;
use Illuminate\Database\Eloquent\Model;

class Shareable extends Model
{
    public $table      = 'shareable_link';
    public $timestamps = false;

    /*
     * Date: 30-08-2021
     * Author: Imran Khan
     * Parameter:param,join
     * Description:  this function will return the Shareable List for admin portal
     */

    public static function GetAdminShareableList($param = null,$join = null)
    {  
        $shareable_link = DB::table('shareable_link as s')->selectRaw('(CASE 
                        WHEN s.from_date > UNIX_TIMESTAMP() && s.link_status="A" THEN "Awaiting"
                        WHEN s.from_date <= UNIX_TIMESTAMP() && s.to_date >= UNIX_TIMESTAMP() && s.link_status="A" && s.downloaded < s.nod_allow THEN "Active"
                        WHEN s.from_date <= UNIX_TIMESTAMP() && s.to_date >= UNIX_TIMESTAMP() && s.link_status="A" && s.downloaded >= s.nod_allow THEN "Expire"
                        WHEN s.from_date < UNIX_TIMESTAMP() && s.to_date < UNIX_TIMESTAMP() && s.link_status="A"  THEN "Expire"
                        WHEN s.link_status="D" THEN "Deleted"
                        END) as status, s.downloaded, s.shareable_link_id, s.doi, s.link, s.nod_allow, s.from_date, s.to_date, s.comments, s.link_status, s.created_on, s.created_by, s.updated_on, s. updated_by, u.name, u.email')

        ->join('users as u', 'u.id', '=', 's.user_id');

        if (!empty($param['user_name'])) {
            $shareable_link  = $shareable_link->where('u.name', 'like', '%' . $param['user_name'] . '%');
        }

        if (!empty($param['user_email'])) {
            $shareable_link  = $shareable_link->where('u.email', 'like', '%' . $param['user_email'] . '%');
        }

        if (!empty($param['doi'])) {
            $shareable_link = $shareable_link->where('s.doi', '=', $param['doi']);
        }

        if(!empty($param['fromdate']) && !empty($param['todate'])){
            $from_date = strtotime($param['fromdate']." 00:00:00");
            $to_date = strtotime($param['todate']." 23:59:59");            
            $shareable_link = $shareable_link->whereRaw("(s.updated_on >= '".$from_date."' and s.updated_on <= '".$to_date."')" );
        }

        $shareable_link = $shareable_link->orderBy('s.shareable_link_id', 'DESC')->groupBy('s.shareable_link_id');

        if (!empty($param['paginate'])) {

            $shareable_link = $shareable_link->paginate(10);
        }
        else
        {
             $shareable_link = $shareable_link->get();
        }

        return $shareable_link;
    }

        /*
     * Date: 02-09-2021
     * Author: Imran Khan
     * Parameter: data[]
     * Description:  this function will return the pubilsher data
     */
    public static function Add($data)
    {
        $id = DB::table('shareable_link')->insertGetId($data);
        return $id;
    }

    /*
     * Date: 03-09-2021
     * Author: Imran Khan
     * Parameter: data
     * Description:  this function will Add shareable details
     */

    public static function AddShareableDownload($data)
    {
       return DB::table('shareable_link_detail')->insertGetId($data);
    }

    /*
     * Date: 03-09-2020
     * Author: Imran Khan
     * Parameter: banner_id
     * Description:  this function will return the shareable link detail
     */
    public static function GetShareableById($id)
    {
        $shareable_link = DB::table('shareable_link as s')->selectRaw('s.shareable_link_id, s.doi, s.link, s.nod_allow, s.from_date, s.to_date, s.comments, s.downloaded, s.link_status, s.created_on, s.created_by, s.updated_on, s. updated_by, s.link_status, u.name, u.email, u.country')->join('users as u', 'u.id', '=', 's.user_id')->where('shareable_link_id', '=', $id)->first();
        return $shareable_link;
    }

    /*
     * Date: Date: 03-09-2021
     * Author: Imran Khan
     * Parameter: data , id
     * Description:  this function will update the shareable link table
     */
    public static function UpdateShareableLink($data, $id)
    {
        DB::table('shareable_link')->where('shareable_link_id', $id)->update($data);
    }

    /*
     * Date: 03-09-2021
     * Author: Imran Khan
     * Parameter: data
     * Description:  this function will delete the journal baner
     */

    public static function DeleteBannerDetail($id,$type)
    {
        DB::table('tbl_banner_detail')->where('banner_id', $id)->where('content_type',$type)->delete();

    }

}
