<?php
namespace count_form\app\entity;

use count_form\app\db\Main;

class sub_question extends Main
{
    public function getAllData()
    {

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

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

    public function getOneData($id)
    {

        $sql =
            '
                SELECT * FROM
                    sub_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
                    sub_question
                WHERE
                    id  = :id
            ';
        $stmt = $this->db->prepare($sql);
        $stmt->bindValue(":id", $id);
        $stmt->execute();
    }

    public function addData($data)
    {

        $sql =
            '
                INSERT INTO
                    sub_question
                        (
                            text,
                            input,
                            required,
                            question_id,
                            key_num
                        )
                        VALUES
                        (
                            :text,
                            :input,
                            :required,
                            :question_id,
                            :key
                        )
            ';
        $stmt = $this->db->prepare($sql);
        $stmt->bindValue(":text", $data['text']);
        $stmt->bindValue(":input", $data['input']);
        $stmt->bindValue(":required", $data['required']);
        $stmt->bindValue(":question_id", $data['question_id']);
        $stmt->bindValue(":key", $data['key']);
        $stmt->execute();

    }

    public function UpdateData($data,$id)
    {

        $sql =
            '
                UPDATE
                   sub_question
                SET
                            text = :text,
                            input = :input,
                            required = :required,
                            key_num = :key
                WHERE
                    id = :id
            ';
        $stmt = $this->db->prepare($sql);
        $stmt->bindValue(":id", $id);
        $stmt->bindValue(":text", $data['text']);
        $stmt->bindValue(":input", $data['input']);
        $stmt->bindValue(":required", $data['required']);
        $stmt->bindValue(":key", $data['key']);
        $stmt->execute();
    }


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

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



}