<?php

/**
 * tábor osztály.
 */
class camp
{
    private $camp_id;
    private $camp_name;
    private $camp_count;
    private $camp_price;
    private $camp_info;
    private $camp_city;
    private $camp_from;
    private $camp_to;
    private $camp_leader;
    private $camp_helpers;
    private $camp_camp_type_ct_id; //ID
    private $camp_type; //OBJ
    private $camp_deleted;
    private $camp_is_open;

    /**
     * Gets the value of camp_id.
     *
     * @return mixed
     */
    public function get_camp_id()
    {
        return $this->camp_id;
    }

    /**
     * Sets the value of camp_id.
     *
     * @param mixed $camp_id the camp id
     *
     * @return self
     */
    public function set_camp_id($camp_id)
    {
        $this->camp_id = $camp_id;

        return $this;
    }

    /**
     * Gets the value of camp_name.
     *
     * @return mixed
     */
    public function get_camp_name()
    {
        return $this->camp_name;
    }

    /**
     * Sets the value of camp_name.
     *
     * @param mixed $camp_name the camp name
     *
     * @return self
     */
    public function set_camp_name($camp_name)
    {
        $this->camp_name = $camp_name;

        return $this;
    }

    /**
     * Gets the value of camp_count.
     *
     * @return mixed
     */
    public function get_camp_count()
    {
        return $this->camp_count;
    }

    /**
     * Sets the value of camp_count.
     *
     * @param mixed $camp_count the camp count
     *
     * @return self
     */
    public function set_camp_count($camp_count)
    {
        $this->camp_count = $camp_count;

        return $this;
    }

    /**
     * Gets the value of camp_price.
     *
     * @return mixed
     */
    public function get_camp_price()
    {
        return $this->camp_price;
    }

    /**
     * Sets the value of camp_price.
     *
     * @param mixed $camp_price the camp price
     *
     * @return self
     */
    public function set_camp_price($camp_price)
    {
        $this->camp_price = $camp_price;

        return $this;
    }

    /**
     * Gets the value of camp_info.
     *
     * @return mixed
     */
    public function get_camp_info()
    {
        return $this->camp_info;
    }

    /**
     * Sets the value of camp_info.
     *
     * @param mixed $camp_info the camp info
     *
     * @return self
     */
    public function set_camp_info($camp_info)
    {
        $this->camp_info = $camp_info;

        return $this;
    }

    /**
     * Gets the value of camp_city.
     *
     * @return mixed
     */
    public function get_camp_city()
    {
        return $this->camp_city;
    }

    /**
     * Sets the value of camp_city.
     *
     * @param mixed $camp_city the camp city
     *
     * @return self
     */
    public function set_camp_city($camp_city)
    {
        $this->camp_city = $camp_city;

        return $this;
    }

    /**
     * Gets the value of camp_from.
     *
     * @return mixed
     */
    public function get_camp_from($_formatted = false)
    {
        return !$_formatted ? $this->camp_from : date('Y. m. d.', strtotime($this->camp_from));
    }

    public function get_camp_from_day()
    {
        $day = date('d', strtotime($this->camp_from));
        if ('0' == substr($day, 0, 1)) {
            return substr($day, 1, 1);
        }

        return date('d', strtotime($this->camp_from));
    }

    public function get_camp_to_day()
    {
        $day = date('d', strtotime($this->camp_to));
        if ('0' == substr($day, 0, 1)) {
            return substr($day, 1, 1);
        }

        return date('d', strtotime($this->camp_to));
    }

    /**
     * Sets the value of camp_from.
     *
     * @param mixed $camp_from the camp from
     *
     * @return self
     */
    public function set_camp_from($camp_from)
    {
        $this->camp_from = $camp_from;

        return $this;
    }

    /**
     * Gets the value of camp_to.
     *
     * @return mixed
     */
    public function get_camp_to($_formatted = false)
    {
        return !$_formatted ? $this->camp_to : date('Y. m. d.', strtotime($this->camp_to));
    }

    /**
     * Sets the value of camp_to.
     *
     * @param mixed $camp_to the camp to
     *
     * @return self
     */
    public function set_camp_to($camp_to)
    {
        $this->camp_to = $camp_to;

        return $this;
    }

    /**
     * Gets the value of camp_camp_type_ct_id.
     *
     * @return mixed
     */
    public function get_camp_camp_type_ct_id()
    {
        return $this->camp_camp_type_ct_id;
    }

    /**
     * Sets the value of camp_camp_type_ct_id.
     *
     * @param mixed $camp_camp_type_ct_id the camp camp type ct id
     *
     * @return self
     */
    public function set_camp_camp_type_ct_id($camp_camp_type_ct_id)
    {
        $this->camp_camp_type_ct_id = $camp_camp_type_ct_id;

        return $this;
    }

    /**
     * Gets the value of camp_type.
     *
     * @return mixed
     */
    public function get_camp_type()
    {
        return $this->camp_type;
    }

    /**
     * Sets the value of camp_type.
     *
     * @param mixed $camp_type the camp type
     *
     * @return self
     */
    public function set_camp_type($camp_type)
    {
        $this->camp_type = $camp_type;

        return $this;
    }

    public function get_camp_deleted()
    {
        return $this->camp_deleted;
    }

    public function set_camp_deleted($_item)
    {
        $this->camp_deleted = $_item;

        return $this;
    }

    public function get_camp_is_open()
    {
        return $this->camp_is_open;
    }

    public function set_camp_is_open($_item)
    {
        $this->camp_is_open = $_item;

        return $this;
    }

    public function get_camp_leader()
    {
        return $this->camp_leader;
    }

    public function set_camp_leader($_item)
    {
        $this->camp_leader = $_item;

        return $this;
    }

    public function get_camp_helpers()
    {
        return $this->camp_helpers;
    }

    public function set_camp_helpers($_item)
    {
        $this->camp_helpers = $_item;

        return $this;
    }

    public function has_pending_apply()
    {
        global $sql;

        return $sql->num_of_rows('select * from camp join camp_apply on capp_camp_id = camp_id where capp_status = 2 AND camp_id = '.$this->get_camp_id());
    }

    public function has_deleted_apply()
    {
        global $sql;

        return $sql->num_of_rows('select * from camp join camp_apply on capp_camp_id = camp_id where capp_status = 5 AND camp_id = '.$this->get_camp_id());
    }

    public function get_camp_applies()
    {
        //visszaadja az elfogadott jelentkezéseket ABC sorrendben
        global $sql;
        $apply_assoc_array = $sql->assoc_array('SELECT * FROM camp_apply JOIN camp_kid ON ck_id = capp_camp_kid_ck_id WHERE capp_status = 3 AND capp_camp_id = '.$this->get_camp_id().' ORDER BY ck_name ASC;');
        $apply_array = [];
        foreach ($apply_assoc_array as $apply) {
            $new_apply = new camp_apply();
            $new_apply->set_capp_data_by_id($apply['capp_id']);
            $apply_array[] = $new_apply;
        }

        return $apply_array;
    }

    public function set_camp_data_by_id($_camp_id)
    {
        global $sql;
        $camp_data_assoc_array = $sql->assoc_array('select * from camp where camp_id = '.$_camp_id);
        $camp_data_array = $camp_data_assoc_array[0];
        //alapadatok
        foreach ($camp_data_array as $field => $value) {
            $function_name = 'set_'.$field;
            $this->$function_name($value);
            if ('camp_camp_type_ct_id' == $field) {
                $new_ct = new camp_type();
                $new_ct->set_ct_data_by_id($value);
                $this->set_camp_type($new_ct);
            }
        }
    }

    public static function create_camp($_city, $_from, $_to, $_leader, $_helpers, $_is_open, $_ct_id, $_name, $_count, $_price, $_info)
    {
        global $sql;

        if ('' === $_name) {
            $_name = 'null';
        }

        if ('' === $_count) {
            $_count = 'null';
        }

        if ('' === $_price) {
            $_price = 'null';
        }

        if ('' === $_info) {
            $_info = 'null';
        }

        $new_camp_id = $sql->insert_into('camp', [
            'camp_name' => $_name,
            'camp_count' => $_count,
            'camp_price' => $_price,
            'camp_info' => $_info,
            'camp_city' => $_city,
            'camp_from' => $_from,
            'camp_to' => $_to,
            'camp_leader' => $_leader,
            'camp_helpers' => $_helpers,
            'camp_is_open' => $_is_open,
            'camp_camp_type_ct_id' => $_ct_id,
        ]);
    }

    public static function update_camp($_city, $_from, $_to, $_leader, $_helpers, $_is_open, $_ct_id, $_name, $_count, $_price, $_info, $_camp_id)
    {
        global $sql;

        if ('' === $_name) {
            $_name = 'null';
        }

        if ('' === $_count) {
            $_count = 'null';
        }

        if ('' === $_price) {
            $_price = 'null';
        }

        if ('' === $_info) {
            $_info = 'null';
        }

        $new_camp_id = $sql->update_table('camp', [
            'camp_name' => $_name,
            'camp_count' => $_count,
            'camp_price' => $_price,
            'camp_info' => $_info,
            'camp_city' => $_city,
            'camp_from' => $_from,
            'camp_to' => $_to,
            'camp_leader' => $_leader,
            'camp_helpers' => $_helpers,
            'camp_is_open' => $_is_open,
            'camp_camp_type_ct_id' => $_ct_id,
            ],
            [
            'camp_id' => $_camp_id,
            ]
        );
    }

    public function get_camp_accepted_applies_count()
    {
        global $sql;

        return $sql->single_variable("
            SELECT count(distinct capp_id) as capp_count
            FROM camp_apply
            WHERE capp_camp_id = {$this->get_camp_id()}
            AND capp_status = 3
            ");
    }
}
