<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\User;

class Mobile extends Model
{
    protected $fillable=['mobile','user_id','otp','sentcount','submitcount','isverified'];

    public function sentSMS($Mobile,$Message)
    {
        $url=env('SMS_URL');
        $api=env('SMS_API');
        $secret=env('SMS_SECRET');
        $usetype=env('SMS_USETYPE');
        $senderid=env('SMS_SENDERID');
        $message = urlencode($Message);// urlencode your message
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_POST, 1);// set post data to true
        curl_setopt($curl, CURLOPT_POSTFIELDS, "apikey=$api&secret=$secret&usetype=$usetype&phone=$Mobile&senderid=$senderid&message=$message");// post data
        // query parameter values must be given without squarebrackets.
        // Optional Authentication:
        curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec($curl);
        curl_close($curl);
        echo $result;
    }

    public function ValidateOTP($OTP)
    {
        $isValid=false;
        $User=auth()->user();
        $Mobile=$User->mobile;
        $otp=$Mobile->otp;
        if($otp==$OTP)
        {
            $isValid=true;
        }
        return $isValid;
    }

   public function user(){
       return $this->belongsTo('App\User');
   }
}
