 <?php
    /**
     * Team list page file.
     *
     * Provides a class with all the properties and methods to display the page.
     *
     * @category Page
     */

    /**
     * Require dependent files if not present.
     */
    require_once(PATH::PAGE . "Page.php");
    require_once(PATH::ENTITY . "Team.php");

    /**
     * Team list page model.
     *
     * @category Page
     */
    class Teams_Page extends Page{

        /**
         * @var \Team[] Teams to show.
         */
        public $teams = [];

        /**
         * @var \Unit[] List of all units for team creation.
         */
        public $units = [];


        /**
         * Constructor.
         *
         * Retrieves the data and initializes the variables.
         *
         * @param resource $db Connection to the database.
         * @global int Player ID.
         */
        public function __construct($db){
            global $UID;
            parent::__construct($db);
            $this->view = PATH::VIEW . "teams.php";
            $this->parse_filters();
            $s = $this->build_query();
            $q = $this->db->query($s);
            while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                array_push($this->teams, new Team($db, $r["id"]));
            }
            $s = "
              SELECT id
              FROM unit
              WHERE unit.uid = '$UID';
            ";
            $q = $this->db->query($s);
            while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                array_push($this->units, new Unit($this->db, $r["id"], false));
            }
            $this->title = "Teams - SWDB";
            $this->description = "Teams";
            $this->canonical = URL::BASE . "teams/";
        }

        /**
         * Parses the request looking for the selected filters, validates them
         * and adds them to the $filters array.
         */
        private function parse_filters(){
            $this->filters = FILTER::TEAM;
            if (isset($_GET["area"]) && APPLICATION::valid_id("AREA_TYPE_ID", $_GET["area"])){
                $this->filters["AREA"] = $_GET["area"];
            }
            if (isset($_GET["name"]) && strlen($_GET["name"]) > 0){
                $this->filters["NAME"] = SQLite3::escapeString($_GET["name"]);
            }
            return;
        }

        /**
         * Builds the query to the rune table using the selected or default
         * filters.
         *
         * @return The query to be executed.
         */
        private function build_query(){
            global $ELEMENT;
            global $UID;
            $s = "
              SELECT id
              FROM team
              WHERE uid = '$UID'
            ";
            if (APPLICATION::valid_id("AREA_ID", $this->filters["AREA"])){
                $s = $s . " AND area  = '" . strtoupper($this->filters["AREA"]) . "') ";
            }
            if ($this->filters["NAME"] != ""){
                $s = $s . " AND upper(name) LIKE '%" . strtoupper($this->filters["NAME"]) . "%' ";
            }
            return $s;
        }

    }
?>

