<?php

namespace App\Geo\State;

use \App\Core\Template\Output as Output;
use \App\Geo\State\Classes\State as State;

class StateController
{

    /**
     * Index
     *   Show the main State list
     */
    function index()
    {
        $list = array();

        for ($i = 0; $i <= 10; $i++) {
            $obj = array();
            $obj['id'] = $i;
            $obj['value'] = md5($i);
            $obj['description'] = sha1($i);
            $list[] = $obj;
        }
        Output::render('list', $list);
    }

    /**
     * Create
     *
     * Render the main State formulary
     */
    function create()
    {
        Output::setView('form', ['id' => 0]);
        Output::render();
    }

    /**
     * Store
     *
     * Store the param on the database
     * @param State $state
     */
    function store(State $state)
    {
    }

    /**
     * Search
     *
     * Store the param on the database
     * @param State $state
     */
    function search()
    {
        $states = State::findAll(['id', 'shortname']);

        $return = "<option value='0'>Selecione</option>";

        foreach ($states as $state) {
            if ($state->id == $_GET['value']) {
                $return .= "<option value='$state->id' selected>$state->shortname</option>";
            } else {
                $return .= "<option value='$state->id'>$state->shortname</option>";
            }
        }

        echo $return;
    }

    /**
     * Show
     *
     * Render one register
     *
     * @param State $state
     */
    function show(State $state)
    {
        \RR\Response::json($state)->send();
    }

    /**
     * Edit
     *
     * Render the formular for a database State
     *
     * @param State $state
     */
    function edit(State $state)
    {
        Output::setView('form', $state);
        Output::render();
    }

    /**
     * Update
     * Store the changes of the param on the database
     *
     * @param State $state
     */
    function update(State $state)
    {
    }

    /**
     * Destroy
     * If the object has soft delete.
     *
     * @param State $state
     */
    function destroy(State $state)
    {
    }

    /**
     * Purge
     * Remove object even with soft delete.
     *
     * @param State $state
     */
    function purge(State $state)
    {
    }

    function loadSelect()
    {
        //ddd(State::findMany(['country_id' => ['=', '25']])->get());
        Output::render(
            'select',
            [
                'states' => State::findMany(['country_id' => ['=', '25']])->get(),
                //'selected' => $_GET['value']
            ]
        );
    }
}
