<?php

namespace App;
use DB;

use Illuminate\Database\Eloquent\Model;


class Document extends Model
{
    private $generic_condition;
  

    public function __construct()
    {
           
       $this->setCondition(['is_recycle' => '0']);
    }

    public function setCondition($condition = [])
    {
        $this->generic_condition = $condition;
    }

    public function getCondition()
    {
        return $this->generic_condition;
    }

      public function storeDocument($object)
    {
      

      return DB::transaction(function () use ($object)  {



            $document = new Document;

            $document->user_id           = $object['user_id'];

            $document->booking_id        = $object['booking_id'];

            $document->title             = $object['document_title'];

            $document->description       = $object['document_description'];

            $document->doc_file          = \EF::uploadFile($object['document']);

  

           return  $document->save();

        });
    }


      public function removeDocument($id)
    {
        return DB::transaction(function () use ($id) {

            $document = Document::find($id);

            if(isset($document->id))
            {
                $document->is_recycle   = '1';
               

                $document->update();

                return 1;
            }
            
            return 0;
        });
    }





    public function getBookingDocuments($booking_id)
    {

        $condition = $this->getCondition();

        $condition += ['booking_id'=>$booking_id];


       $documents_list = Document::where($condition)->orderBy('id','desc')->get();

       return $documents_list;
    }
}
