<?php

namespace App;

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

class Sms 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 storeSms($object)
    {
       return DB::transaction(function () use ($object)  {

        $sms  = new Sms;
        $sms->number = $object['contact'];
        $sms->message = $object['message'];
        $sms->user_id = $object['user_id'];

        $sms->booking_id = $object['booking_id'];
        $sms->installment_id = $object['installment_id'];
        $sms->sms_type = $object['type'];
        $sms->amount = $object['amount'];

        $sms->status = 0;

        $sms->save();
        // if($sms->save())
        // {
        //     $path = $_SERVER["DOCUMENT_ROOT"].'/sms/safaa.jar safaa_group';

        //     if(exec('java -cp '.$path))
        //     {
        //         return 1;
        //     }
        // }

        // return 0;
        return with($sms);
    });
    }



    public function getSmsByStatus($status = 0)
    {

          $condition_in = ['status',$status];

        return sms::where($this->getCondition())->whereIn('status',$condition_in)->get();
    }


    public function updateSmsStatus($id)
    {
        $sms = sms::find($id);
        $sms->status = 1;
        $sms->update();
    }

    public function countSmsByType($type_of_id,$type)
    {

        $conditon = $this->getCondition();

        if($type == "Booking")
        {
            $conditon += ['sms_type' => 'Booking', 'booking_id' => $type_of_id];
        }
        else if($type == "Installment")
        {
            $conditon += ['sms_type' => 'Installment', 'installment_id' => $type_of_id];
        }
        else if($type == "Birthday")
        {
            $conditon += ['sms_type' => 'Birthday', 'user_id' => $type_of_id];
        }

        return sms::where($conditon)->count();
    }
}
