<?php
namespace count_form\app\entity;

use count_form\app\db\Main;

class question extends Main
{
    public function getAllData()
    {

        $sql =
            '
                SELECT * FROM
                    question
                ORDER BY
                     weight ASC';
        $result = $this->db->query($sql);

        return $result->fetchAll(\PDO::FETCH_ASSOC);
    }

    public function getAllActiveData()
    {

        $sql =
            '
                SELECT * FROM
                    question
                WHERE
                    status = 1
                AND
                  parent_id = 0
                ORDER BY
                     weight ASC';
        $result = $this->db->query($sql);

        return $result->fetchAll(\PDO::FETCH_ASSOC);
    }






    public function getOneData($id)
    {

        $sql =
            '
                SELECT * FROM
                    question
                WHERE
                    id = :id
            ';
        $stmt = $this->db->prepare($sql);
        $stmt->bindValue(":id", $id);
        $stmt->execute();
        return $stmt->fetch(\PDO::FETCH_ASSOC);
    }

    public function deleteData($id){
        $sql =
            '   DELETE
                FROM
                    question
                WHERE
                    id  = :id
            ';
        $stmt = $this->db->prepare($sql);
        $stmt->bindValue(":id", $id);
        $stmt->execute();
    }

    public function addData($data)
    {

        $sql =
            '
                INSERT INTO
                    question
                        (
                            text,
                            input_type,
                            weight,
                            status,
                            required,
                            parent_id
                        )
                        VALUES
                        (
                            :text,
                            :input_type,
                            :weight,
                            :status,
                            :required,
                            :parent_id
                        )
            ';
        $stmt = $this->db->prepare($sql);
        $stmt->bindValue(":text", $data['text']);
        $stmt->bindValue(":input_type", $data['input_type']);
        $stmt->bindValue(":weight", $data['weight']);
        $stmt->bindValue(":status", $data['status']);
        $stmt->bindValue(":required", $data['required']);
        $stmt->bindValue(":parent_id", $data['parent_id']);
        $stmt->execute();
        return $this->db->lastInsertId();



    }

    public function UpdateData($data,$id)
    {
        $sql =
            '
                UPDATE
                   question
                SET
                            text = :text,
                            input_type = :input_type,
                            weight = :weight,
                            status = :status,
                            required = :required,
                            parent_id = :parent_id
                WHERE
                    id              = :id
            ';
        $stmt = $this->db->prepare($sql);
        $stmt->bindValue(":id", $id);
        $stmt->bindValue(":text", $data['text']);
        $stmt->bindValue(":input_type", $data['input_type']);
        $stmt->bindValue(":weight", $data['weight']);
        $stmt->bindValue(":status", $data['status']);
        $stmt->bindValue(":required", $data['required']);
        $stmt->bindValue(":parent_id", $data['parent_id']);
        $stmt->execute();
    }

    function getSelectData($id)
    {
        $sql =
            '
                SELECT * FROM
                    question
                WHERE
                    id IN ('.$id.')
                ORDER BY
                     weight ASC';
        $result = $this->db->query($sql);

        return $result->fetchAll(\PDO::FETCH_ASSOC);
    }

    function getChosenData($parent_id){

        $sql =
            '
                SELECT * FROM
                    question
                WHERE
                    status = 1
                AND
                  parent_id = :parent_id
                ORDER BY
                     weight ASC';
        $stmt = $this->db->prepare($sql);
        $stmt->bindValue(":parent_id", $parent_id);
        $stmt->execute();
        return $stmt->fetchAll(\PDO::FETCH_ASSOC);

    }


}