<?php if(file_exists('../../../'.$_REQUEST['tenant'].'/config.php')){require_once('../../../'.$_REQUEST['tenant'].'/config.php');}
if(file_exists('/config.php')){require_once('/config.php');}

switch($_REQUEST['FUNCTION']){
default:break;
case "saveDef":
$r=mysqli_query($GLOBALS['con'],"INSERT INTO `dd_".$_REQUEST['type']."` VALUES (NULL,'".$_REQUEST['code']."','".$_REQUEST['data']."','')")or die(mysqli_error($GLOBALS['con']));

exit();
break;
}


$fil="";
if($_REQUEST['type']=="elecphrases"){$fil='phrase';}
if($_REQUEST['type']=="results"){$fil='phrase';}
if($_REQUEST['type']=="examtype"){$fil='phrase';}
if($_REQUEST['type']=="plant"){$fil='description';}
$r=mysqli_query($GLOBALS['con'],"SELECT * FROM `dd_".$_REQUEST['type']."`  ORDER BY `$fil` ASC");?>

<div id="exportTitle" style="display: none">Phrases - <?= date('d-m-Y')?></div>
<table id="datatables" class="table table-striped table-sm table-bordered table-hover ">
    <thead>
        <tr>
            <th>ID</th>
            <th>Phrase</th>
            <th></th>
        </tr>
    </thead>
    <tbody>

        <?php while($rc=mysqli_fetch_array($r)){
if($_REQUEST['type']=="elecphrases"){
$phrase=$rc['phrase'];
$rc['phrase']=urlencode($rc['phrase']);?>
        <tr>
            <td><?= $rc['phrasecode']?></td>
            <td><?= universalDecode($phrase)?></td>
            <td class="text-center"><a class="btn btn-success text-light btn-lg" href="javascript:void(0)" onClick="updateField('<?= $rc['phrase']?>')"><i class="fad fa-copy"></i> </a></td>
        </tr>
        <?php }

if($_REQUEST['type']=="results"){
$phrase=$rc['phrase'];
$rc['phrase']=urlencode($rc['phrase']);?>
        <tr>
            <td><?= $rc['phrasecode']?></td>
            <td><?= universalDecode($phrase)?></td>
            <td class="text-center"><a class="btn btn-success text-light btn-lg" href="javascript:void(0)" onClick="updateField('<?= $rc['phrase']?>')"><i class="fad fa-copy"></i> </a></td>
        </tr>
        <?php }

if($_REQUEST['type']=="examtype"){
$phrase=$rc['phrase'];
$rc['phrase']=urlencode($rc['phrase']);?>
        <tr>
            <td><?= $rc['phrasecode']?></td>
            <td><?= universalDecode($phrase)?></td>
            <td class="text-center"><a class="btn btn-success text-light btn-lg" href="javascript:void(0)" onClick="updateField('<?= $rc['phrase']?>')"><i class="fad fa-copy"></i> </a></td>
        </tr>
        <?php }

if($_REQUEST['type']=="plant"){?>
        <tr>
            <td><?= $rc['code']?></td>
            <td><?= universalDecode($rc['description'])?></td>
            <td class="text-center"><a class="btn btn-success text-light btn-lg" href="javascript:void(0)" onClick="updateField('<?= $rc['code']?>')"><i class="fad fa-copy"></i> </a></td>
        </tr>
        <?php }
}?>


    </tbody>
</table>
<?php 
$table = "datatables";
$dateTarget = "0";
$sortTarget = "1";
$length = "25";
$sortCell = "";
$logo = "yes";
$scroller = "";
require_once('../tables/single_table.php');?>

<script>
function updateField(encodedString) {
    // Get the textarea element using form and source parameters
    const formElement = document.querySelector('#<?= $_REQUEST['form'] ?>');
    const sourceElement = document.getElementById('<?= $_REQUEST['source'] ?>');

    // Decode the URL-encoded string
    let decodedContent = decodeURIComponent(encodedString).replace(/\+/g, ' ');

    // Normalize newline sequences: replace all occurrences of \n with a single newline character
    decodedContent = decodedContent.replace(/\\n+/g, '\n').trim();

    // Get the current value of the textarea
    let currentValue = sourceElement.value.trim();

    // Combine current value with new content, ensuring only a single newline separates them
    let newValue = currentValue + (currentValue ? '\n' : '') + decodedContent;

    // Set the updated value in the textarea
    sourceElement.value = newValue;

    // Trigger keyup event to update any bindings or listeners
    const event = new Event('keyup');
    sourceElement.dispatchEvent(event);

    // Hide the modal using MDBootstrap modal instance
    const popupBox = document.getElementById('popupBox');
    const modalInstance = mdb.Modal.getInstance(popupBox);
    if (modalInstance) {
        modalInstance.hide();
    }
}
</script>