<?php
include("functions_addons.php");
/**
 * Techbase Solutions (K) Commercial License
 *
 * This software is licensed under the Techbase Solutions (K) Extended License as attached with this software
 * You should have received a copy of the license along with this file. If not,
 * please contact Techbase Solutions (K) at xcrud17@gmail.com for a copy.
 *
 * Commercial use of this software is permitted only under the terms of the
 * Techbase Solutions (K) Commercial License. Redistributions and use in source
 * and binary forms, with or without modification, are strictly prohibited
 * unless explicitly granted by the Techbase Solutions (K) Commercial License.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * DO NOT REMOVE THIS LICENSE INFORMATION
 *
 * @copyright 2023 Techbase Solutions (K)
 * @license   https://xcrud.net
 * @email   xcrud17@gmail.com
 ?>
 **/
include('../DB.php');
function service_call_action($xcrud)
{
	$bdd = new DB();
	
	$service_followup_id=(int)$xcrud->get('primary');
	$mobile_no = $xcrud->get('mobile');	
	$service_id = $xcrud->get('service_id');
	$lfd = $xcrud->get('nfd');
	$service_type = $xcrud->get('service_type');
	$service_no = $xcrud->get('service_no');
	$user_id = $xcrud->get('user_id');
	$calling_type = $xcrud->get('calling_type');
	$primary = $xcrud->get('primary');
	
	if($calling_type=='Android'){
		$values=array('Service',$user_id);
		$call_id = $bdd->getOne("SELECT id FROM call_customer WHERE  dept=? AND user_id=?",$values);
		if(!empty($call_id)){
			$values=array($call_id['id']);
			$call_id = $bdd->getOne("DELETE FROM call_customer WHERE id=?",$values);
		}
		$values=array($mobile_no,'Service',$user_id,$primary);
		$city_id = $bdd->lastId("INSERT into call_customer (mobile_no,dept,user_id,followup_id)  VALUES (?,?,?,?)",$values);
	}else if($calling_type=='Ipad'){
		echo "<script>{document.location.href = 'tel:+91$mobile_no';}</script>";
	}

	$bdd->disconnect();
}

function publish_action($xcrud)
{
    if ($xcrud->get('primary'))
    {
        $db = Xcrud_db::get_instance();
        $query = 'UPDATE base_fields SET `bool` = b\'1\' WHERE id = ' . (int)$xcrud->get('primary');
        $db->query($query);
    }
}

function get_pages(){
	foreach (new DirectoryIterator(__DIR__) as $file) {
	  if ($file->isFile()) {
	      print $file->getFilename() . "\n";
	  }
	}
}

function process_action($xcrud) {
    // Simulate a long process (10 seconds)
    echo "<div id='progress'>Process started...</div>";
    ob_flush();
    flush();  // Send output to the browser immediately

    // Process step 1
    sleep(2);
    echo "<script>updateProgress('Step 1: Reading files...');</script>";
   ob_flush();
    flush();

    // Process step 2
    sleep(3);
    echo "<script>updateProgress('Step 2: Processing data...');</script>";
    ob_flush();
    flush();

    // Process step 3
    sleep(3);
    echo "<script>updateProgress('Step 3: Finalizing process...');</script>";
    
    ob_flush();
    flush();

    // Process completed
    sleep(2);
    echo "<script>updateProgress('Process completed!');</script>";
    ob_flush();
    flush();
}

function bulk_delete_console($xcrud){

   //$xcrud->set_exception('Bulk Delete', 'Deleted successfully', 'success');
   GLOBAL $x;
   $x++;
   echo "$x ..";
}

function bulk_delete($xcrud)
{
	//print_r($xcrud.selected);
	$selected = $xcrud->get('selected');
	$table = $xcrud->get('table');
	$identifier = $xcrud->get('identifier');
	$cnt = count($selected); 
	
	$insertString = "";
	$count = 0;
	foreach($selected as $value){
		$count++;
		if($count == 1){	
			$insertString .= $value; 
		}else{
			$insertString .= "," . $value;
		}		
	}
	   
    $db = Xcrud_db::get_instance();
    $query = "DELETE from $table WHERE $identifier IN ($insertString)" ;
	//echo $query;
    $db->query($query);
   
    $xcrud->set_exception('Bulk Delete', 'You have Deleted ' . $cnt . ' items', 'error');
	//$xcrud->set_exception('You have deleted ' . $cnt . ' items', 'error');
}

function unpublish_action($xcrud)
{
    if ($xcrud->get('primary'))
    {
        $db = Xcrud_db::get_instance();
        $query = 'UPDATE base_fields SET `bool` = b\'0\' WHERE id = ' . (int)$xcrud->get('primary');
        $db->query($query);
    }
}

function exception_example($postdata, $primary, $xcrud)
{
    // get random field from $postdata
    $postdata_prepared = array_keys($postdata->to_array());
    shuffle($postdata_prepared);
    $random_field = array_shift($postdata_prepared);
    // set error message
    $xcrud->set_exception($random_field, 'This is a test error', 'error');
}


function test_column_callback($value, $fieldname, $primary, $row, $xcrud)
{
    return $value . ' - nice!';
}



function after_upload_example($field, $file_name, $file_path, $params, $xcrud)
{
    $ext = trim(strtolower(strrchr($file_name, '.')), '.');
    if ($ext != 'pdf' && $field == 'uploads.simple_upload')
    {
        unlink($file_path);
        $xcrud->set_exception('simple_upload', 'This is not PDF', 'error');
    }
}

function movetop($xcrud)
{
    if ($xcrud->get('primary') !== false)
    {
        $primary = (int)$xcrud->get('primary');
        $db = Xcrud_db::get_instance();
        $query = 'SELECT `officeCode` FROM `offices` ORDER BY `ordering`,`officeCode`';
        $db->query($query);
        $result = $db->result();
        $count = count($result);

        $sort = array();
        foreach ($result as $key => $item)
        {
            if ($item['officeCode'] == $primary && $key != 0)
            {
                array_splice($result, $key - 1, 0, array($item));
                unset($result[$key + 1]);
                break;
            }
        }

        foreach ($result as $key => $item)
        {
            $query = 'UPDATE `offices` SET `ordering` = ' . $key . ' WHERE officeCode = ' . $item['officeCode'];
            $db->query($query);
        }
    }
}
function movebottom($xcrud)
{
    if ($xcrud->get('primary') !== false)
    {
        $primary = (int)$xcrud->get('primary');
        $db = Xcrud_db::get_instance();
        $query = 'SELECT `officeCode` FROM `offices` ORDER BY `ordering`,`officeCode`';
        $db->query($query);
        $result = $db->result();
        $count = count($result);

        $sort = array();
        foreach ($result as $key => $item)
        {
            if ($item['officeCode'] == $primary && $key != $count - 1)
            {
                unset($result[$key]);
                array_splice($result, $key + 1, 0, array($item));
                break;
            }
        }

        foreach ($result as $key => $item)
        {
            $query = 'UPDATE `offices` SET `ordering` = ' . $key . ' WHERE officeCode = ' . $item['officeCode'];
            $db->query($query);
        }
    }
}

function show_description($value, $fieldname, $primary_key, $row, $xcrud)
{
    $result = '';
    if ($value == '1')
    {
        $result = '<i class="fa fa-check" />' . 'OK';
    }
    elseif ($value == '2')
    {
        $result = '<i class="fa fa-circle-o" />' . 'Pending';
    }
    return $result;
}

function custom_field($value, $fieldname, $primary_key, $row, $xcrud)
{
    return '<input type="text" readonly class="xcrud-input" name="' . $xcrud->fieldname_encode($fieldname) . '" value="' . $value .
        '" />';
}
function unset_val($postdata)
{
    $postdata->del('Paid');
}

function before_list_example($list, $xcrud)
{
    var_dump($list);
}

function after_update_test($pd, $pm, $xc)
{
    $xc->search = 0;
}

/*function after_upload_test($field, &$filename, $file_path, $upload_config, $this)
{
    $filename = 'bla-bla-bla';
}*/


function after_insert($postdata, $primary, $xcrud){

}

function before_insert($postdata, $xcrud){


        $file_f = Xcrud_config::$upload_folder_def;
        //$postdata->set('password', sha1( $postdata->get('pdf_file') ));
        $file_path = $postdata->get('pdf_file');
        $full_f = $file_f . "/" . $file_path;

        echo $full_f;

        $fileContent = file_get_contents($full_f);

        // Get the MIME type of the file
        $finfo = finfo_open(FILEINFO_MIME_TYPE);
        $mimeType = finfo_file($finfo, $full_f);
        finfo_close($finfo);

        // Create a blob from the file contents
        $blob = new \stdClass();
        $blob->content = base64_encode($fileContent);
        $blob->mime = $mimeType;
        $postdata->set('pdf_blob', json_encode($blob));

}

function before_update($postdata, $xcrud){


        $file_f = Xcrud_config::$upload_folder_def;
        //$postdata->set('password', sha1( $postdata->get('pdf_file') ));
        $file_path = $postdata->get('pdf_file');
        $full_f = $file_f . "/" . $file_path;

        echo $full_f;

        $fileContent = file_get_contents($full_f);

        // Get the MIME type of the file
        $finfo = finfo_open(FILEINFO_MIME_TYPE);
        $mimeType = finfo_file($finfo, $full_f);
        finfo_close($finfo);

        // Create a blob from the file contents
        $blob = new \stdClass();
        $blob->content = base64_encode($fileContent);
        $blob->mime = $mimeType;
        $postdata->set('pdf_blob', json_encode($blob));

}

function show_pdf($value, $fieldname, $primary_key, $row, $xcrud)
{

   $blob = json_decode($value);

    // Embed a PDF viewer using HTML
    $html = '<embed src="data:' . $blob->mime . ';base64,' . $blob->content . '" type="' . $blob->mime . '" width="100%" height="600px" />';

    return $html;
}


//events
function populate_calendar($postdata, $primary, $xcrud){

    $xcrud_event_id = $primary;

    // Step 2: Determine calendar entries based on start_date and end_date
    $start_datetime = new DateTime($start_date);
    $end_datetime = new DateTime($end_date);

    $current_date = clone $start_datetime;

    $calendar_entries = [];

    while ($current_date <= $end_datetime) {
        $day_name = $current_date->format('l'); // Get the day name (e.g., Monday)
        $day_value = $current_date->format('d_m_Y'); // Format for the column value (e.g., 09_07_2024)

        // Prepare the calendar entry for the current date
        $calendar_entries[] = "('$day_value', $event_id, " . $current_date->format('Y') . ", " . $current_date->format('m') . ", " . $current_date->format('W') . ")";

        // Move to the next day
        $current_date->modify('+1 day');
    }

    // Step 3: Insert into xcrud_event_calendar table
    $query_calendar = "INSERT INTO xcrud_event_calendar (monday, tuesday, wednesday, thursday, friday, saturday, sunday, xcrud_event_id, year, month, week)
                       VALUES ";

    $query_calendar .= implode(',', $calendar_entries);

    $result_calendar = $db->query($query_calendar);

    if (!$result_calendar) {
        die("Error inserting calendar entries: " . $db->error());
    }
}


