<?php
include_once __DIR__ . "/../config/web_mysqlconnect.php";

// Check if the action is to view a case report
if($_POST['action'] == 'break_details'){
	agent_break(); // Call function to view case report
  }

  function agent_break(){
    global $db, $link;

    // Retrieve the POST data from the AJAX request
    $logged_name = mysqli_real_escape_string($link, $_POST['logged_name']);
    $break_name = mysqli_real_escape_string($link, $_POST['break_name']);
    $break_start_time = mysqli_real_escape_string($link, $_POST['break_start_time']);
    $break_end_time = mysqli_real_escape_string($link, $_POST['break_end_time']);

    // Prepare the SQL query to insert break details into the agent_break table
    $query = "INSERT INTO $db.agent_break (break_name, username, startdatetime, enddatetime) 
              VALUES ('$break_name', '$logged_name', '$break_start_time', '$break_end_time')";

    // Execute the query and check if it was successful
    if (mysqli_query($link, $query)) {
        echo json_encode(['status' => 'success', 'message' => 'Break details inserted successfully']);
    } else {
        echo json_encode(['status' => 'error', 'message' => 'Error inserting break details: ' . mysqli_error($link)]);
    }
}
function classify_agent($userid)
{
	global $db,$link;
	$query ="select i_classify from $db.uniuserprofile where AtxUserID='$userid'; ";
	$res =mysqli_query($link,$query);
	$row=mysqli_fetch_array($res);
	return $row['i_classify'];

}
function module_license($module) {
    return '1';
}

function module_license_id($module, $groupId) {
    return 1;
}

function module_license2($module, $groupId) {
    return ['module_flag' => 1, 'group_Ids' => []];
}


function break_sched_start(){
	global $groupid, $startdatetime,$db,$link;
	if($groupid != '0000'){
	    $shift_date = date("Y-m-d H:i:s");
	    $agent_id = $_SESSION['unique_id'];
	    $sqlsource_count="select * from $db.tbl_wfm_agent_sched_instance where d_schedStartDate like '$startdatetime%' and i_agentID='$agent_id'";
	    $sourceresult_count=mysqli_query($link,$sqlsource_count);
	    $row_count=mysqli_fetch_array($sourceresult_count);
	    $row_count_row = mysqli_num_rows($sourceresult_count);
	    if($row_count_row>0){
	        $schdule_start_time = date("H:i",strtotime($row_count["d_schedStartDate"]));
	        $schdule_end_time = date("H:i",strtotime($row_count["d_schedEndDate"]));
	        $list = '';
	        $shift_break=explode(",",$row_count['v_breakList']);
	        for($s_break=0;$s_break<count($shift_break);$s_break++){ // for loop for comma (,) seperated values
	            $assigned_break_id=$shift_break[$s_break];
	            $sqlsource_abreak="select v_breakName,d_startbreak,d_endbreak from $db.tbl_wfm_mst_break where i_breakID='$assigned_break_id'";
	            $sourceresult_abreak=mysqli_query($link,$sqlsource_abreak);
	            while($row_abreak=mysqli_fetch_array($sourceresult_abreak)) 
	            {   
	                $brk_stime=date("H:i",strtotime($row_abreak['d_startbreak']));
	                $brk_etime=date("H:i",strtotime($row_abreak['d_endbreak']));
	                $list .= "<li><stronge style=\"color: #d63384;\">Break  </stronge> : $brk_stime to $brk_etime</li>";
	            }
	        }
	    }
   } 
    $list['list']=$list;
    $list['schdule_start_time']=$schdule_start_time;
    $list['schdule_end_time']=$schdule_end_time;
   return $list;
}
function channel_license($userId){
	global $link,$db;
	$query = "SELECT * FROM $db.user_channel_assignment WHERE userid='$userId'";
	$res =mysqli_query($link,$query);
	$data = array();
	if (mysqli_num_rows($res) > 0) {
       while ($adminrow = mysqli_fetch_assoc($res)) {
       	  $data[$adminrow['channel_type']] = $adminrow['channel_type'];
       }
    }
	return $data;
}

function get_telephony_flag($userid) {
	global $link,$dbname;
	$query = $link->query("SELECT telephony_flag FROM $dbname.tbl_mst_user_company WHERE I_UserID ='$userid' ");
	$row = $query->fetch_row();
	return $row[0];
}

/**
 * Fetch all active dispositions.
 *
 * @param mysqli $link MySQLi connection.
 * @param string $db Database name.
 * @return array Returns an array of disposition rows.
 */
function getChannelDispositions($link, $db){
    $data  = [];
    $query = "SELECT * FROM {$db}.channel_disposition_type WHERE status = '1' ORDER BY `name` ASC";
    $res = mysqli_query($link, $query);

    if ($res === false) {
        // Log SQL errors for debugging
        error_log('MySQL Error in getChannelDispositions: ' . mysqli_error($link));
        return $data;
    }

    while ($row = mysqli_fetch_assoc($res)) {
        $data[] = $row;
    }
    return $data;
}

/**
 * Fetch all active classifications.
 *
 * @param mysqli $link MySQLi connection.
 * @param string $db Database name.
 * @return array Returns an array of classifications rows.
 */
function getClassifications($link, $db){
    $data  = [];
    $query = "SELECT * FROM {$db}.tbl_classification WHERE status = '1' ORDER BY `id` ASC";
    $res = mysqli_query($link, $query);

    if ($res === false) {
        // Log SQL errors for debugging
        error_log('MySQL Error in tbl_classification: ' . mysqli_error($link));
        return $data;
    }

    while ($row = mysqli_fetch_assoc($res)) {
        $data[] = $row;
    }
    return $data;
}


