<?php

namespace App\Helpers;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

class BibClass
{
    static function createSelect($HTMLLabel, $tableName, $valueField, $displayField, $condition = "", $defaultValue = "", $HTMLName = "", $HTMLId = "", $HTMLClass = "", $HTMLRequired = "")
    {
        $tableName = strtolower($tableName);
        $query = "SELECT $valueField, $displayField FROM $tableName";
        if ($condition != "") {
            $query .= " WHERE $condition";
        }

        $results = DB::select(DB::raw($query));
?>
        <label for="<?php echo $HTMLId; ?>" class="form-label col-form-label"> <?php echo label($HTMLLabel); ?> </label>
        <select class="form-select  <?php echo $HTMLClass ?>" name="<?php echo $HTMLName; ?>" data-search="true" id="<?php echo $HTMLId; ?>" aria-label="Default select example" <?php echo ($HTMLRequired) ? "Required" : ""; ?>>
            <option value=""><?php label("Select Option"); ?></option>
            <?php foreach ($results as $item) { ?>
                <option value="<?php echo $item->$valueField ?>" <?php echo $item->$valueField == $defaultValue ? 'selected' : '' ?>><?php echo $item->$displayField ?></option>
            <?php } ?>
        </select>

        <p id='error_<?php echo $HTMLName; ?>' class='text-danger custom-error'></p>
    <?php
    }
    static function lookupField($tableName, $field, $refField, $refValue)
    {
        $tableName = strtolower($tableName);
        $t = "select $field from $tableName where $refField = '$refValue'";
        $Value = DB::select($t);

        if (!empty($Value)) {
            return $Value[0]->$field;
        } else {
            return "Not Found in Table";
        }
    }
    static function getRow($tableName, $condition = "1")
    {
        $tableName = strtolower($tableName);
        $t = "select * from $tableName where $condition";
        $Value = DB::select($t);
        return (empty($Value) ? "Not Found" : $Value[0]);
    }
    static function getRowByQuery($query)
    {
        $Value = DB::select($query);
        return (empty($Value) ? false : $Value[0]);
    }
    static function getTableByQuery($query)
    {
        $Value = DB::select($query);

        return (empty($Value) ? false : $Value);
    }
    static function updateRow($tableName, $fieldName, $fieldValue, $referenceField, $referenceValue)
    {
        $tableName = strtolower($tableName);
        $t = "update $tableName set $fieldName='$fieldValue'  where $referenceField=$referenceValue";
        return  DB::select($t);
    }
    public static function pre($array)
    {
        echo "<pre>";
        print_r($array);
        echo "</pre>";
    }
    public static function addButton($path, $text)
    {
    ?>
        <a href="<?php echo url($path); ?>" class="btn btn-primary btn-sm pull-right">
            <em class="icon ni ni-plus"></em><span><?php echo $text; ?></span>
        </a>
    <?php
    }
    public static function addRowActions($pk)
    {


        echo "<ul class=\"d-flex flex-wrap\">
        <li><a href=\"#\" type=\"button\" class=\"btn btn-color-success btn-hover-success btn-icon btn-soft\" ><em class=\"icon ni ni-eye\"></em></a></li>
        <li><a href=\"form2.php\" type=\"button\" class=\"btn btn-color-primary btn-hover-primary btn-icon btn-soft\"  data-bs-toggle=\"tooltip\" data-bs-placement=\"top\" data-bs-custom-class=\"custom-tooltip\" title=\"Edit\"> <em class=\"icon ni ni-edit\"></em></a></li>
        <li><button type=\"button\" class=\"btn btn-color-danger btn-hover-danger btn-icon btn-soft\"><em class=\"icon ni ni-trash\"></em></button></li>
        </ul>";
        BibClass::addButton("edit/$pk", 'Edit');
        BibClass::addButton("view/$pk", 'View');
        BibClass::addButton("destroy/$pk", 'Delete');
    }

    public static function getController()
    {
        $routeArray = app('request')->route()->getAction();
        $controllerAction = class_basename($routeArray['controller']);
        list($controller, $action) = explode('@', $controllerAction);

        print_r($controller);
    }
    public static function createSidebarMenu($link, $name, $target = "")
    {
    ?>
        <li class="nk-menu-item"><a href="<?php echo $link; ?>" class="nk-menu-link" <?php echo ($target != "") ? "target=\"_blank\"" : ""; ?>><span class="nk-menu-text"><?php echo $name; ?></span></a></li>
    <?php
    }

    public static function dataTable($TableRows, $TableName)
    {
        $TableName = strtolower($TableName);
        $Table_pk = str_replace("tbl_", "", $TableName) . "_id";
        $TableCols = array_keys((array)$TableRows[0]);

        //BibClass::pre($TableCols);
    ?>
        <table class="datatable-init table" data-nk-container="table-responsive table-border">
            <thead>
                <tr>
                    <?php foreach ($TableCols as $TableCol) : //echo $TableCol;
                    ?>
                        <?php switch ($TableCol) {
                            case $Table_pk:
                            case 'created_by':
                            case 'created_on':
                            case 'remarks':
                            case 'status':
                            case 'created_at':
                            case 'updated_at':
                                break;
                            default:
                        ?>
                                <th class="text-nowrap"><span class="overline-title"><?php echo label($TableCol); ?></span>
                                </th>
                        <?php
                        }
                        ?>

                    <?php endforeach; ?>

                </tr>
            </thead>
            <tbody>
                <?php foreach ($TableRows as $TableRow) : ?>
                    <tr>
                        <?php foreach ($TableCols as $TableCol) : //echo $TableCol;
                        ?>
                            <?php switch ($TableCol) {
                                case $Table_pk:
                                case 'created_by':
                                case 'created_on':
                                case 'remarks':
                                case 'status':
                                case 'created_at':
                                case 'updated_at':
                                    break;
                                default:
                            ?>
                                    <th class="text-nowrap"><span class="overline-title"><?php echo $TableRow->$TableCol; ?></span>
                                    </th>
                            <?php
                            }
                            ?>

                        <?php endforeach; ?>
                    </tr>
                <?php endforeach; ?>


            </tbody>
        </table>

<?php
    }
    public static function  tableEntryForm($tableName)
    {
        $tableName = strtolower($tableName);
        $Table_pk = str_replace("tbl_", "", $tableName) . "_id";
        $tableFields = DB::select("describe " . $tableName);
        foreach ($tableFields as $tableField) {
            $tableField = $tableField->Field;
            switch ($tableField) {
                case $Table_pk:
                case 'status':
                case 'created_at':
                case 'updated_at':
                    break;
                default:
                    createInput("text", $tableField, $tableField, $tableField, "", "", "");
            }
        }
    }
}