<?php

namespace App\Helpers;
use App\Models\Branches;
use App\Models\Categories;
use App\Models\Menu;
use App\Models\Services;
use PhpOffice\PhpSpreadsheet\Calculation\Category;

class Common
{
  public static function getServices($id, $selected = null)
  {
    $services = Services::where('parent', 0)
      ->where('country', $id)
      ->get();
    $html = '';
    $select = '';
    foreach ($services as $service) {
      if ($selected == $service->id) {
        $select = 'selected';
      }
      $html .= '<option value="' . $service->id . '" ' . $select . '><b>' . $service->name . '</b></option>';
      $select = '';
      foreach ($service->subService as $sub) {
        if ($selected == $sub->id) {
          $select = 'selected';
        }
        $html .=
          '<option value="' . $sub->id . '" ' . $select . '>&nbsp;⮞&nbsp<small>' . $sub->name . '</small></option>';
        $select = '';
      }
    }
    return $html;
  }

  public static function userServices($service_id)
  {
    $ids = explode(',', $service_id);
    $services = Services::whereIn('id', $ids)->get();
    $data = [];
    foreach ($services as $service) {
      array_push($data, $service->name);
    }
    return implode(',', $data);
  }

  public static function ActivityType($id = null)
  {
    $steps = [
      1 => 'Call',
      2 => 'Meeting',
      3 => 'Message',
      4 => 'Email',
      5 => 'Close',
      6 => 'Transfer',
      7 => 'Re Marketing',
      8 => 'Re Qualify',
      9 => 'Contacted Again',
      /*  10 => 'Generate Sale', */
      11 => 'Query Generated',
      12 => 'Ticket Generated',
    ];
    if ($id) {
      return $steps[$id];
    } else {
      return $steps;
    }
  }

  public static function ReferenceType($type = null, $id = null)
  {
    $steps = [
      1 => 'TICKET # ' . $id,
      2 => 'QUERY # ' . $id,
      3 => 'SALE # ' . $id,
    ];
    if ($type) {
      return $steps[$type];
    } else {
      return $steps;
    }
  }

  public static function Status($id = null)
  {
    $steps = [
      1 => 'Call',
      2 => 'Meeting',
      3 => 'Message',
      4 => 'Email',
      5 => 'Close',
      6 => 'Transfer',
      7 => 'Re Marketing',
      8 => 'Re Qualify',
      9 => 'Duplicate',
      10 => 'Generate Sale',
    ];
    if ($id) {
      return $steps[$id];
    } else {
      return $steps;
    }
  }

  public static function InquiryType($id = null)
  {
    $steps = [
      1 => 'Open',
      2 => 'Close',
    ];
    if ($id) {
      return $steps[$id];
    } else {
      return $steps;
    }
  }

  public static function getStatus($status_id = null){
    $array =  [
        0 => 'Inactive',
        1 => 'Active'
    ];
    if($status_id){
        $array = $array[$status_id];
    }
    return $array;
}

public static function DiscountType($type = null){
  $array =  [
      1 => 'Percentage',
      2 => 'Fixed'
  ];
  if($type){
      $array = $array[$type];
  }
  return $array;
}

public static function getBranches(){
  return Branches::where('company_id',auth()->user()->company_id)->where('status',IConstants::ACTIVE)->pluck('name','id');
}
public static function getMenus(){
  /* if(auth()->user()->hasRole(IConstants::ROLE_ADMIN)){
    return Menu::select(\DB::raw("CONCAT(branches.name,' - ',menus.name) AS menu_name,menus.id,menus.status"))
    ->join('branches','branches.company_id','=','branch_id')
    ->where('menus.status',IConstants::ACTIVE)
    ->where('branches.company_id',auth()->user()->id)
    ->pluck('menu_name','menus.id');
  }else{ */
    return Menu::where('branch_id',auth()->user()->branch_id)->where('status',IConstants::ACTIVE)->pluck('name','id');

  /* } */
}
public static function getCategories(){
  return Categories::where('branch_id',auth()->user()->branch_id)->where('status',IConstants::ACTIVE)->get()->pluck('menu_name','id');
}

public static function DateFormat($date){
  return date('d M Y',strtotime($date));

}

public static function DateTimeFormat($date){
  return date('d M Y h:i A',strtotime($date));

}

}


