<?php

namespace App\View\Helper;

use Cake\View\Helper;
use Cake\ORM\TableRegistry;
use DateTimeZone;
use DateTime;
class CustomHelper extends Helper
{
    public $helpers = ['Html'];

    // public function makeEdit($title, $url)
    // {
    //     // Use the HTML helper to output
    //     // Formatted data:

    //     $link = $this->Html->link($title, $url, ['class' => 'edit']);

    //     return '<div class="editOuter">' . $link . '</div>';
    // }
    public function dateTime($d)
    {
        return date('d M, Y h:i A', strtotime($d));
    }
    public function getCategoryName($id = null)
    {
        $Cat = TableRegistry::get('Category')->get($id)->toArray();
        return  $Cat['name'];
        // $category[0]='Both';
    }
    public function getSubjectName($id)
    {
        $Sub = TableRegistry::get('Subjects')->get($id)->toArray();
        return  $Sub['name'];
        // $category[0]='Both';
    }
    public function getSubjects($data)
    {
        $subjects = TableRegistry::get('Subjects')->find('list')->where(['status' => 1])->toArray();
        $proffArr = explode(',', $data);
        // dd($subjects);
        foreach ($proffArr as $key => $proff) {
            if (isset($subjects[$proff])) {
                $sub[]= $subjects[$proff];
            }
        }
        return implode(',',$sub);
    }
    public function ProjectStage($name)
    {
        // dd($name);
        $ProjectStatus = TableRegistry::get('ProjectStatus')->find('all')->select(['name','stage'])->where(['name like' => $name])->first()->toArray();
        return  ($ProjectStatus['stage']);
        // $category[0]='Both';
    }
    public function FreelancerName($id)
    {
        $Freelancer = TableRegistry::get('Freelancers')->get($id)->toArray();
        return  $Freelancer['name'];
        // $category[0]='Both';
    }
    public function crmUserName($id)
    {
        $User = TableRegistry::get('Users')->get($id)->toArray();
        return  $User['username'];
        // $category[0]='Both';
    }
    function select_Timezone() {
        $time_zone_list = DateTimeZone::listIdentifiers(DateTimeZone::ALL);
        return  $time_zone_list;
    }
    function getChatTime($dateTimeString) {
        $dateTime = new DateTime($dateTimeString);
        $interval = $dateTime->diff(new DateTime());
      
        if ($interval->y > 0) {
          return $interval->format("%y year" . ($interval->y > 1 ? "s" : "") . " ago");
        } else if ($interval->m > 0) {
          return $interval->format("%m month" . ($interval->m > 1 ? "s" : "") . " ago");
        } else if ($interval->d > 0) {
          return $interval->format("%d day" . ($interval->d > 1 ? "s" : "") . " ago");
        } else if ($interval->h > 0) {
          return $interval->format("%h hour" . ($interval->h > 1 ? "s" : "") . " ago");
        } else if ($interval->i > 0) {
          return $interval->format("%i minute" . ($interval->i > 1 ? "s" : "") . " ago");
        } else {
          return "just now";
        }
      }
}
