<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;


class IqCallHistory extends BaseModel
{
    use HasFactory;
    protected $table = 'iq_call_histories';
    protected $fillable=['ip','port','page','error_code','status','created_at'];
    public $timestamps = false;

    public function insertHistory($ip,$port,$page,$error_code,$status)
    {
        $result=[];
        $inputData = $this->modelMapping($ip,$port,$page,$error_code,$status);
        try {
            $result['data'] = self::create($inputData);
        }catch (\Exception $ex){}

        $result['message']='save successfully';
        $result['status']=true;
        return $result;

    }

    public function getHistories($inputData)
    {
        $fromDate = $inputData['fdate'];
        $toDate = $inputData['tdate'];

        $queryData = IqCallHistory::query();

        if(!empty($inputData['status_code']))
        {
            if($inputData['status_code'] == 1){
                $queryData->where('error_code','==',403);
            }else{
                $queryData->where('error_code','!=',403);
            }

        }

        if(!empty($fromDate) && !empty($toDate))
        {
            $queryData->whereBetween('created_at',[$fromDate.' 00:00:00', $toDate.' 23:59:59']);
        }

        return $queryData->paginate($inputData['pageLimit']);

    }

    public function modelMapping($ip,$port,$page,$error_code,$status){
        $modelData['ip'] = $ip;
        $modelData['port'] = $port;
        $modelData['page'] = $page;
        $modelData['error_code'] = $error_code;
        $modelData['status'] = $status;
        $modelData['created_at'] = getCurrentDateTime();
        return $modelData;

    }

}
