<?php

defined('BASEPATH') or exit('No direct script access allowed');
  $where="";
  $current_Staff_role = get_current_staff_role($this, get_staff_user_id());
  $current_broker_id = get_current_staff_brokerage_id($this, get_staff_user_id());
  if($current_Staff_role == 2){ // broker
  // $where="(addedfrom =" . get_broker_agent_staffid($this, get_staff_user_id()) . ') ';
  $where = "FIND_IN_SET(" . get_staff_user_id() . ", addedfrom) > 0";
  $leads=$this->db->query("SELECT `id`,`name`,`email`,`status`,`last_status_change`,`dateadded`,`assign_group` FROM `tblleads` WHERE $where  ORDER BY `id` DESC LIMIT 100")->result_array();
  }else if($current_Staff_role == 6){
    $where = "FIND_IN_SET(" . $current_broker_id . ", brokerage_id) > 0";
    $leads=$this->db->query("SELECT `id`,`name`,`email`,`status`,`last_status_change`,`dateadded`,`assign_group` FROM `tblleads` WHERE $where  ORDER BY `id` DESC LIMIT 100")->result_array();
  }else{
    $leads=$this->db->query("SELECT `id`,`name`,`email`,`status`,`last_status_change`,`dateadded`,`assign_group` FROM `tblleads` WHERE active_case = 'Active' ORDER BY `id` DESC LIMIT 100")->result_array();
  }

	// echo $this->db->last_query();

?>
<?php
$groupFilterOptions = ['' => 'All Groups'];
foreach ($leads as $group) {
    $groupFilterOptions[$group['assign_group']] = $group['assign_group'];
}
if($current_Staff_role != 2 || $current_Staff_role != 6){ 
?>
<div class="mb-3 form-group">
    <label for="groupFilter" class="form-label">Filter by Group:</label>
    <select class="form-control" id="groupFilter" style="width: 139px;">
        <?php foreach ($groupFilterOptions as $value => $label): ?>
            <option value="<?php echo $value; ?>"><?php echo ($label == '') ? 'All groups' : $label; ?></option>
        <?php endforeach; ?>
    </select>
</div>
<?php }?>
<?php if(!empty($leads)){ ?>
<table class="table dt-table table-ticket-reports dt-inline dataTable no-footer">
  <thead>
    <tr>
      <th scope="col">ID</th>
      <th scope="col">Name</th>
      <th scope="col">Groups</th>
      <th scope="col">Case Created</th>
      <?php if($current_Staff_role != 2 || $current_Staff_role != 6){ ?>
      <th scope="col">Status</th>
      <?php }?>
    </tr>
  </thead>
  <tbody>
  <?php 
  foreach($leads as $k=>$v)
  {
	$datedChk = (trim($v['dateassigned'])!=='')?$v['dateassigned']:$v['dateadded'];
	
	$diff = date_diff(date_create(date('Y-m-d')) , date_create(date('Y-m-d',strtotime($datedChk))))->format('%d') ;
    $backcolor = "#FFF";
	// if($diff>0 and ($diff == 3) or ($diff == 8) or ($diff == 13) or ($diff == 24)) // on specific days
  //   { $backcolor = "rgb(255,255,0)";   // 2 days yellow
  //   }elseif($diff>0 and ($diff == 4) or ($diff == 9) or ($diff == 14) or ($diff == 25))
	// { $backcolor = "rgb(255,215,215)";  // 1 day red
  //   }
	
	$hrefAttr = 'href="' . admin_url('leads/index/' . $v['id']) . '" onclick="init_lead(' . $v['id'] . ');return false;"';
	// $created_date = ($v['dateadded'] != '0000-00-00 00:00:00' ? time_ago($v['dateadded']) : "A year ago");
  $created_date = ($v['dateadded'] != '0000-00-00 00:00:00' ? $v['dateadded'] : '-');
  $primary_name = explode(' ', $v['name'], 3);
  $firstname = $primary_name[0] ?? '';
  $middlename = $primary_name[1] ?? '';
  $lastname = $primary_name[2] ?? '';
  ?>
    <tr <?php echo 'style=background-color:'. $backcolor; ?>>
      <td><a <?php echo $hrefAttr ?>><?php echo $v['id'] ?></a></td>
      <td><a <?php echo $hrefAttr ?>><?php echo $lastname . ', ' . $firstname . ' ' . $middlename ?></a></td>
      <td><?php echo $v['assign_group'] ?></td>
      <td><span data-toggle="tooltip" data-title="<?php echo time_ago($created_date) ?>" class="text-has-action is-date"><?php echo $created_date ?></span></td>
      <?php if(get_current_staff_role($this, get_staff_user_id()) != 2 || get_current_staff_role($this,get_staff_user_id()) != 6){ ?>
      <td><?php echo $this->leads_model->get_status($v['status'])->name;?></td>
      <?php }?>
	  
    </tr>
  <?php }  ?>
  </tbody>
</table>
<?php }else{ echo '<div class="alert alert-info" role="alert">
  There are no any Cases</div>'; }   ?>