<?php 

if (file_exists('../../..' . $_REQUEST['tenant'] . '/config.php')) {
    require_once('../../..' . $_REQUEST['tenant'] . '/config.php');
}
if (file_exists('/config.php')) {
    require_once('/config.php');
}
if ($_REQUEST['FCN'] == "genNewLists") {
    $stmt = $GLOBALS['con']->prepare("SELECT * FROM `dd_plant` ORDER BY `code` ASC") or die(mysqli_error($GLOBALS['con']));
    $stmt->execute();
    $cc = $stmt->get_result();
    $stmt->close();
    while ($pd = mysqli_fetch_array($cc)) {
        $ca[] = $pd;
    }

    $ret = array();
    $dat = '';
    foreach ($ca as $px) {
        if ($c == $px['code']) {
            $s = "SELECTED";
        } else {
            $s = "";
        }
        $dat .= '<option value="' . $px['code'] . '" title="' . $px['description'] . '" ' . $s . '>' . $px['code'] . '</option>'; //.PHP_EOL;
    }
    $ret[] = $dat;
    $dat = '';
    foreach ($ca as $px) {
        if ($c == $px['code']) {
            $s = "SELECTED";
        } else {
            $s = "";
        }
        $dat .= '<option value="' . $px['description'] . '" title="' . $px['code'] . '" ' . $s . '>' . $px['description'] . '&nbsp;&nbsp;&nbsp;&nbsp;(' . $px['frequency'] . ' months)</option>'; //.PHP_EOL;
    }
    $ret[] = $dat;

    $return = json_encode($ret);
    echo $ret;
    exit();
}

if ($_REQUEST['FUNCTION'] == "save" || $_REQUEST['FCN'] == "save") {
    if ($_REQUEST['mode'] == "plant") {
        $check = mysqli_query($GLOBALS['con'], "SELECT `id` FROM `dd_plant` WHERE `code`='" . $_REQUEST['code'] . "'");
        $cnum = mysqli_num_rows($check);
        if ($cnum == 0) {
            $_REQUEST['desc'] = mysqli_real_escape_string($con, $_REQUEST['desc']);
            $stmt = $GLOBALS['con']->prepare("INSERT INTO `dd_plant` VALUES (NULL,'" . $_REQUEST['code'] . "','" . $_REQUEST['desc'] . "','" . $_REQUEST['freq'] . "','" . $_REQUEST['size'] . "','" . $_REQUEST['unit'] . "',NULL)") or die($e = mysqli_error($GLOBALS['con']));
            $stmt->execute();
            $rslt = $stmt->get_result();
            echo "s";
        } else {
            echo "e";
        }
        exit();
    }

    if ($_REQUEST['mode'] == "phrase") {
        $check = mysqli_query($GLOBALS['con'], "SELECT `id` FROM `dd_phrases` WHERE `phrasecode`='" . $_REQUEST['code'] . "'");
        $cnum = mysqli_num_rows($check);
        if ($cnum == 0) {
            $_REQUEST['phrase']= mysqli_real_escape_string($con, $_REQUEST['phrase']);
            $stmt = $GLOBALS['con']->prepare("INSERT INTO `dd_phrases` VALUES (NULL,'" . $_REQUEST['code'] . "','" . $_REQUEST['phrase'] . "',NULL)") or die($e = mysqli_error($GLOBALS['con']));
            $stmt->execute();
            echo "s";
        } else {
            echo "e";
        }
        exit();
    }

    if ($_REQUEST['mode'] == "elecPhrase") {
        $check = mysqli_query($GLOBALS['con'], "SELECT `id` FROM `dd_elecphrases` WHERE `phrasecode`='" . $_REQUEST['code'] . "'");
        $cnum = mysqli_num_rows($check);
        if ($cnum == 0) {
            $_REQUEST['phrase'] = mysqli_real_escape_string($con, $_REQUEST['phrase']);
            $stmt = $GLOBALS['con']->prepare("INSERT INTO `dd_elecphrases` VALUES (NULL,'" . $_REQUEST['code'] . "','" . $_REQUEST['phrase'] . "',NULL)") or die($e = mysqli_error($GLOBALS['con']));
            $stmt->execute();
            echo "s";
        } else {
            echo "e";
        }
        exit();
    }

    if ($_REQUEST['mode'] == "surveyorSkills") {
        $check = mysqli_query($GLOBALS['con'], "SELECT `id` FROM `dd_surveyorSkills` WHERE `description`='" . $_REQUEST['description'] . "' AND `discipline`='" . $_REQUEST['discipline'] . "'");
        $cnum = mysqli_num_rows($check);
        if ($cnum == 0) {
            $_REQUEST['description'] = mysqli_real_escape_string($con, $_REQUEST['description']);
            $stmt = $GLOBALS['con']->prepare("INSERT INTO `dd_surveyorSkills` VALUES (NULL,'" . $_REQUEST['discipline'] . "','" . $_REQUEST['description'] . "')") or die($e = mysqli_error($GLOBALS['con']));
            $stmt->execute();
            echo "s";
        } else {
            echo "e";
        }
        exit();
    }
}

if ($_REQUEST['FUNCTION'] == "remove") {
    $stmt = $GLOBALS['con']->prepare("DELETE FROM `dd_" . $_REQUEST['table'] . "` WHERE `id`='" . $_REQUEST['id'] . "'") or die($e = mysqli_error($GLOBALS['con']));
    $stmt->execute();
    echo "s" . $e;
    exit();
}


if ($_REQUEST['FUNCTION'] == "update") {

    if ($_REQUEST['mode'] == "plant") {
        $_REQUEST['desc'] = mysqli_real_escape_string($con, $_REQUEST['desc']);
        $stmt = $GLOBALS['con']->prepare("UPDATE `dd_plant` SET
`code`='" . $_REQUEST['code'] . "',
`description`='" . $_REQUEST['desc'] . "',
`frequency`='" . $_REQUEST['freq'] . "',
`size`='" . $_REQUEST['size'] . "',
`units`='" . $_REQUEST['units'] . "'
WHERE `id`='" . $_REQUEST['id'] . "'
") or die($e = mysqli_error($GLOBALS['con']));
        $stmt->execute();
        $rslt = $stmt->get_result();
    }

    if ($_REQUEST['mode'] == "phrases") {
        $_REQUEST['phrase'] = mysqli_real_escape_string($con, $_REQUEST['phrase']);
        $stmt = $GLOBALS['con']->prepare("UPDATE `dd_phrases` SET
`phrasecode`='" . $_REQUEST['phrasecode'] . "',
`phrase`='" . $_REQUEST['phrase'] . "'
WHERE `id`='" . $_REQUEST['id'] . "'
") or die($e = mysqli_error($GLOBALS['con']));
        $stmt->execute();
        $rslt = $stmt->get_result();
    }

    if ($_REQUEST['mode'] == "elecphrases") {
        $_REQUEST['phrase'] = mysqli_real_escape_string($con, $_REQUEST['phrase']);
        $stmt = $GLOBALS['con']->prepare("UPDATE `dd_elecphrases` SET
    `phrasecode`='" . $_REQUEST['phrasecode'] . "',
    `phrase`='" . $_REQUEST['phrase'] . "'
    WHERE `id`='" . $_REQUEST['id'] . "'
    ") or die($e = mysqli_error($GLOBALS['con']));
        $stmt->execute();
        $rslt = $stmt->get_result();
    }
    if ($_REQUEST['mode'] == "surveyorSkills") {
$_REQUEST['description'] = mysqli_real_escape_string($con, $_REQUEST['description']);

        $stmt = $GLOBALS['con']->prepare("UPDATE `dd_surveyorSkills` SET
    `discipline`='" . $_REQUEST['discipline'] . "',
    `description`='" . $_REQUEST['description'] . "'
    WHERE `id`='" . $_REQUEST['id'] . "'
    ") or die(mysqli_error($GLOBALS['con']));
        $stmt->execute();
        $rslt = $stmt->get_result();
    }

    echo "s";
    exit();
}

function getItems($type, $format)
{
    $stmt = $GLOBALS['con']->prepare("SELECT * FROM `dd_" . $type . "`") or die(mysqli_error($GLOBALS['con']));
    $stmt->execute();
    $rslt = $stmt->get_result();
    switch ($type) {
        default:
            break;


        case "phrases": ?>

<div class="col-md-12 pt-4">
    <table id="phraseTable" class="table table-striped table-hover responsive" style="width:100%">
        <thead>
            <tr>
                <th width="10%">Code</th>
                <th>Phrase</th>
                <th class="no-sort"></th>
            </tr>
        </thead>
        <tbody>
            <?php while ($d = mysqli_fetch_array($rslt)) { ?>

            <tr>
                <td width="10%">
                    <form id="phr<?= $d['id'] ?>"></form>
                    <span style="display: none"><?= $d['phrasecode'] ?></span>
                    <input type="text" class="form-control form-control-sm" aria-label="phrasecode" name="phrasecode"
                        form="phr<?= $d['id'] ?>" value="<?= $d['phrasecode'] ?>" />
                </td>
                <td>
                    <span style="display: none"><?= $d['phrase'] ?></span>
                    <textarea type="text" class="form-control form-control-sm" aria-label="phrase" name="phrase"
                        rows="3" form="phr<?= $d['id'] ?>"><?= universalDecode($d['phrase']) ?></textarea>
                </td>
                <td class="text-end"><button class="btn btn-outline-success me-3"
                        onClick="update('phr<?= $d['id'] ?>','phrases','<?= $d['id'] ?>')" type="button"><i
                            class="fad fa-save" aria-hidden="true"></i></button><button class="btn btn-outline-danger"
                        type="button" onClick="remove('phrases','<?= $d['id'] ?>')"><i class="fad fa-trash"
                            aria-hidden="true"></i></button>
                    <input type="hidden" form="phr<?= $d['id'] ?>" name="id" value="<?= $d['id'] ?>">
                    <input type="hidden" form="phr<?= $d['id'] ?>" name="table" value="phrases">
                    <input type="hidden" form="phr<?= $d['id'] ?>" name="mode" value="phrases">
                    <input type="hidden" form="phr<?= $d['id'] ?>" name="FUNCTION" value="update">
                    <input type="hidden" form="phr<?= $d['id'] ?>" name="tenant" value="<?= TENANT_URL ?>">
                </td>
            </tr>


            <?php } ?>
        </tbody>
    </table>

</div>
<?php $table = "phraseTable";
$pageTitle="Engineering Phrases List";
            $dateTarget = "";
            $length = "25";
            $sortTarget = "";
            $sortCell = "nth-child(2)";
            $logo = "yes";
            $scroller = "";
            require_once('../tables/single_table.php');
            break;


        case "elecphrases": ?>

<div class="col-md-12 pt-4">
    <table id="elecPhraseTable" class="table table-striped table-hover responsive" style="width:100%">
        <thead>
            <tr>
                <th width="10%">Code</th>
                <th>Phrase</th>
                <th class="no-sort"></th>
            </tr>
        </thead>
        <tbody>
            <?php while ($d = mysqli_fetch_array($rslt)) { ?>

            <tr>
                <td width="10%">
                    <form id="ephr<?= $d['id'] ?>"></form>
                    <span style="display: none"><?= $d['phrasecode'] ?></span>
                    <input type="text" class="form-control form-control-sm" aria-label="phrasecode" name="phrasecode"
                        form="ephr<?= $d['id'] ?>" value="<?= $d['phrasecode'] ?>" />
                </td>
                <td>
                    <span style="display: none"><?= $d['phrase'] ?></span>
                    <textarea type="text" class="form-control form-control-sm" aria-label="phrase" name="phrase"
                        rows="3" form="ephr<?= $d['id'] ?>"><?= universalDecode($d['phrase']) ?></textarea>
                </td>
                <td class="text-end"><button class="btn btn-outline-success me-3"
                        onClick="update('ephr<?= $d['id'] ?>','elecphrases','<?= $d['id'] ?>')" type="button"><i
                            class="fad fa-save" aria-hidden="true"></i></button><button class="btn btn-outline-danger"
                        type="button" onClick="remove('elecphrases','<?= $d['id'] ?>')"><i class="fad fa-trash"
                            aria-hidden="true"></i></button>
                    <input type="hidden" form="ephr<?= $d['id'] ?>" name="id" value="<?= $d['id'] ?>">
                    <input type="hidden" form="ephr<?= $d['id'] ?>" name="table" value="elecphrases">
                    <input type="hidden" form="ephr<?= $d['id'] ?>" name="mode" value="elecphrases">
                    <input type="hidden" form="ephr<?= $d['id'] ?>" name="FUNCTION" value="update">
                    <input type="hidden" form="ephr<?= $d['id'] ?>" name="tenant" value="<?= TENANT_URL ?>">
                </td>
            </tr>


            <?php } ?>
        </tbody>
    </table>

</div>
<?php $table = "elecPhraseTable";
$pageTitle="Electrical Phrases List";
            $dateTarget = "";
            $length = "25";
            $sortTarget = "";
            $sortCell = "nth-child(2)";
            $logo = "yes";
            $scroller = "";
            require_once('../tables/single_table.php');

            break;

        case "plant": ?>

<div class="col-md-12 pt-4">
    <table id="plantTable" class="table table-striped table-hover table-sm responsive" style="width:100%">
        <thead>
            <tr>
                <th width="10%">Code</th>
                <th>Description</th>
                <th>Frequency</th>
                <th>Size</th>
                <th>Units</th>
                <th class="no-sort"></th>
            </tr>
        </thead>
        <tbody>
            <?php while ($d = mysqli_fetch_array($rslt)) { ?>
            <tr id="R<?= $d['code'] ?>">
                <td><span><?= $d['code'] ?></span></td>
                <td><span><?= $d['description'] ?></span></td>
                <td><span><?= $d['frequency'] ?></span></td>
                <td><span><?= $d['size'] ?></span></td>
                <td><span><?= $d['units'] ?></span></td>
                <td class="text-end"><button class="btn btn-outline-info me-3" onClick="edit('<?= $d['id'] ?>')"
                        type="button"><i class="fad fa-pencil" aria-hidden="true"></i></button><button
                        class="btn btn-outline-danger" type="button" onClick="remove('plant','<?= $d['id'] ?>')"><i
                            class="fad fa-trash" aria-hidden="true"></i></button>
                </td>

            </tr>

            <?php } ?>
        </tbody>
    </table>

</div>
<?php $table = "plantTable";
$pageTitle="Plant List";
            $dateTarget = "";
            $length = "25";
            $sortTarget = "";
            $sortCell = "nth-child(2)";
            $logo = "yes";
            $scroller = "";
            require_once('../tables/single_table.php');


            break;

        case "results":
        ?>
<div class="col-md-12 pt-4">
    <form>
        <table id="resultsTable" class="table table-striped table-hover table-sm table-responsive" style="width:100%">
            <thead>
                <tr>
                    <th width="10%">Code</th>
                    <th>Phrase</th>
                    <th>Discipline</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <?php while ($d = mysqli_fetch_array($rslt)) { ?>
                <tr>
                    <td>
                        <span style="display: none"><?= $d['phrasecode'] ?></span>
                        <input type="text" class="form-control form-control-sm" aria-label="phrasecode"
                            value="<?= $d['phrasecode'] ?>" />
                    </td>
                    <td>
                        <span style="display: none"><?= $d['phrase'] ?></span>
                        <input type="text" class="form-control form-control-sm" aria-label="phrase"
                            value="<?= $d['phrase'] ?>" />
                    </td>
                    <td>
                        <span style="display: none"><?= $d['discipline'] ?></span>
                        <input type="text" class="form-control form-control-sm" aria-label="discipline"
                            value="<?= $d['discipline'] ?>" />
                    </td>
                    <td class="text-center"><button class="btn btn-outline-success me-3" type="button"><i
                                class="fad fa-save" aria-hidden="true"></i></button><button
                            class="btn btn-outline-danger " type="button"><i class="fad fa-trash"
                                aria-hidden="true"></i></button></td>
                </tr>
                <?php } ?>
            </tbody>
        </table>
    </form>
</div>
<?php $table = "resultsTable";
$pageTitle="Results List";
            $dateTarget = "";
            $length = "25";
            $sortTarget = "";
            $sortCell = "nth-child(2)";
            $logo = "yes";
            $scroller = "";
            require_once('../tables/single_table.php');


            break;



        case "surveyorSkills": ?>

<div class="col-md-12 pt-4">

    <table id="skillsTable" class="table table-striped table-hover responsive" style="width:100%">
        <thead>
            <tr>
                <th>Discipline</th>
                <th>Description</th>
                <th class="no-sort"></th>
            </tr>
        </thead>
        <tbody>
            <?php while ($d = mysqli_fetch_array($rslt)) { ?>
            <tr>
                <td width="10%">
                    <form id="plsf<?= $d['id'] ?>"></form>
                    <span style="display: none"><?= $d['discipline'] ?></span>
                    <input form="plsf<?= $d['id'] ?>" name="discipline" type="text" class="form-control form-control-sm"
                        aria-label="discipline" value="<?= $d['discipline'] ?>" />
                </td>
                <td>
                    <span style="display: none"><?= $d['description'] ?></span>
                    <input form="plsf<?= $d['id'] ?>" type="text" class="form-control form-control-sm"
                        aria-label="description" name="description" value="<?= $d['description'] ?>" />
                </td>
                <td class="text-center"><button class="btn btn-outline-success me-3"
                        onClick="update('plsf<?= $d['id'] ?>')" type="button"><i class="fad fa-save"
                            aria-hidden="true"></i></button><button class="btn btn-outline-danger" type="button"
                        onClick="remove('surveyorSkills','<?= $d['id'] ?>')"><i class="fad fa-trash"
                            aria-hidden="true"></i></button>

                    <input type="hidden" form="plsf<?= $d['id'] ?>" name="id" value="<?= $d['id'] ?>">
                    <input type="hidden" form="plsf<?= $d['id'] ?>" name="table" value="surveyorSkills">
                    <input type="hidden" form="plsf<?= $d['id'] ?>" name="mode" value="surveyorSkills">
                    <input type="hidden" form="plsf<?= $d['id'] ?>" name="FUNCTION" value="update">
                    <input type="hidden" form="plsf<?= $d['id'] ?>" name="tenant" value="<?= TENANT_URL ?>">
                </td>
            </tr>
            <?php } ?>
        </tbody>
    </table>

</div>
<?php $table = "skillsTable";
$pageTitle="Surveyor Skills List";
            $dateTarget = "";
            $length = "25";
            $sortTarget = "";
            $sortCell = "nth-child(2)";
            $logo = "yes";
            $scroller = "";
            require_once('../tables/single_table.php');
            break;
    }
    $stmt->free_result();
    $stmt->close();
}


if (isset($_REQUEST['FUNCTION'])) {
    echo getItems($_REQUEST['type'], $_REQUEST['format']);
}


?>