<?php

namespace App\Repositories\Core;

use Illuminate\Support\Facades\Schema;
use Collective\Html\Eloquent\Form;

class Database
{
    public static function getForm($model)
    {
        $form = '';
        $data = self::getTableFields($model);
        foreach ($data as $item) {
            switch ($item['type']) {
            case 'integer':
                $form .= Form::number($item['name']);
                break;
            case 'string':
                $form .= Form::number($item['name']);
                break;
            case 'date':
                $form .= Form::date($item['name']);
                break;
            case 'boolean':
                $form .= Form::checkbox($item['name']);
                break;
            }
        }
        return $form;
    }

    public static function getTableFields($model)
    {
        $table = new $model();
        $data = [];
        // get the column names for the table
        $columns = Schema::getColumnListing($table->getTable());
        foreach ($columns as &$column) {
            $type = Schema::getColumnType($table->getTable(), $column);
            array_push($data, ['name' => $column, 'type' => $type]);
        }
        return $data;
    }

    public static function getPopulate($model, $route, $id)
    {
        echo Form::model($model, ['route' => [$route.'.update', $id]]);
    }
}
