<?php


namespace count_form\app\data;

class admin
{
    public $ip;
    public $my_ip;

    function __construct(){
        $this->ip = $_SERVER['REMOTE_ADDR'];
        $this->my_ip = '109.251.238.123';
        if($this->ip != $this->my_ip){
            header("Location:/count_form.php/");
        }
    }
    function index(){
        $model = new \count_form\app\db\Entity();

        $entity = $model->getInstance('question');

        $sub_question = $model->getInstance('sub_question');

        $sub_data = $sub_question->getAllData();

        $data = $entity->getAllData();

        $result = array('data'=>$data, 'sub_data'=>$sub_data);

        return $result;
    }

    function add(){

        $model = new \count_form\app\db\Entity();
        $questions = $model->getInstance('question');
        if(isset($_POST)&&!empty($_POST)){
            $question['text'] = $_POST['question_text'];
            $question['input_type'] = $_POST['question_input_type'];
            $question['status'] =  empty( $_POST['question_status'] ) ? 0 : 1;
            $question['required'] =  empty( $_POST['question_required'] ) ? 0 : 1;
            $question['weight'] = $_POST['question_weight'];
            $question['parent_id'] = isset($_POST['question_parent_id']) && !empty($_POST['question_parent_id']) ? $_POST['question_parent_id']: '0';

            $questions->addData($question);
            header("Location:/count_form.php/?p=admin");
        }

        $data = $questions->getAllData();

        $result = array('data'=>$data);

        return $result;
    }


    function edit(){


        $model = new \count_form\app\db\Entity();
        $questions = $model->getInstance('question');
        $sub = $model->getInstance('sub_question');
        $id = $_GET['id'];

        if(isset($_POST)&&!empty($_POST)){


            $question['text'] = $_POST['question_text'];
            $question['input_type'] = $_POST['question_input_type'];
            $question['weight'] = $_POST['question_weight'];
            $question['status'] =  empty( $_POST['question_status'] ) ? 0 : 1;
            $question['required'] =  empty( $_POST['question_required'] ) ? 0 : 1;
            $question['parent_id'] = isset($_POST['question_parent_id']) && !empty($_POST['question_parent_id']) ? $_POST['question_parent_id']: '0';
            $questions->UpdateData($question, $id);


            header("Location:/count_form.php/?p=admin");

        }

        $data = $questions->getAllData();
        $quest = $questions->getOneData($id);
        $sub_quest = $sub->getOneData($id);
        $result = array('data'=>$data, 'question'=>$quest, 'sub_question'=>$sub_quest);

        return $result;
    }

    function delete()
    {
        if(isset($_GET['id'])){

            $id = $_GET['id'];

            $model = new \count_form\app\db\Entity();

            $entity = $model->getInstance('question');

            $entity->deleteData($id);

            header("Location:/count_form.php/?p=admin");
        }


    }


}