<?php

/*
 * awframework from aw.inc present aw controller
 * it will help you develop easy and fast with secure tools
 * you can edit and add what you feel right
 * don't change in strucure of file
 */

require_once("imp/_imp.php");

class Controllers {

    //public vars
    public $smarty;
    public $hosturl;

    // it will run in everytime with
    // default requried
    public function __construct() {
        require_once("../public/aw_config.php");
        $this->smarty = $smarty;
        $this->hosturl = $hosturl;


        $controlname = 'services';
        $this->smarty->assign('services', 1);

        require_once("header.php");
        require_once("footer.php");
    }

    //here the frist page and
    //the frist action in this class
    public function index() {
        header::headershow($this->smarty, $this->hosturl);
        $id = (int) filter_input(INPUT_GET, 'ac');
        if (empty($id)) {
            $allService = R::findAll("services", "del =0 and hide = 0");
            $this->smarty->assign('allService', $allService);
            $this->smarty->display('templates/' . default_lang_interface . '/services.html');
        } else {
            $service = R::load('services', $id);
            if ($service->id != $id) {
                header('location:' . $this->hosturl);
            }
            $this->smarty->assign('service', $service);
            //$this->smarty->assign('folder', 'page');
            $this->smarty->display('templates/' . default_lang_interface . '/service.html');
        }

        footer::footershow($this->smarty, $this->hosturl);
    }

    public function orderService() {
        $name = filter_input(INPUT_POST, 'name');
        $email = filter_input(INPUT_POST, 'email');
        $subject = filter_input(INPUT_POST, 'subject');
        $phone = filter_input(INPUT_POST, 'phone');
        $message = filter_input(INPUT_POST, 'message');
        $serviceId = filter_input(INPUT_POST, 'serviceId');
        ### this part is for captcha  ######################
        $url = 'https://www.google.com/recaptcha/api/siteverify';
        $data = array(
            'secret' => '6LcfGKkUAAAAAOvg9yCKe7VgjvynHJYh_RMlr4a4',
            'response' => $_POST["g-recaptcha-response"],
            'remoteip' => $_SERVER['REMOTE_ADDR']
        );
        $options = array(
            'http' => array(
                //'header' => "Content-type: application/x-www-form-urlencoded\r\n",
                'method' => 'POST',
                'content' => http_build_query($data),
            )
        );
        $context = stream_context_create($options);
        $verify = file_get_contents($url, false, $context);
        $captcha_success = json_decode($verify);
//        if ($captcha_success->success == false) {
//            echo "<p>You are a bot! Go away!</p>";
//        } else if ($captcha_success->success == true) {
//            echo "<p>You are not not a bot!</p>";
//        }
        #############################################
        if ($name && $email && $phone && $message && $serviceId && $captcha_success->success == true) {
            $obj = R::dispense('servicesrequests');
            $obj->serviceid = $serviceId;
            $obj->name = $name;
            $obj->email = $email;
            $obj->title = ""; //$subject;
            $obj->phone = $phone;
            $obj->msg = $message;
            $obj->mdate = date('Y-m-d H:i:s');
            $obj->read = 0;
            $obj->del = 0;

//            $info = R::findOne('siteinfo', 'order by id desc');
//
//            $to = $info->email;
//            $subject = 'رسالة جديدة على موقع ' . $info->project_name;
//            $messag = "رسالة من " . $name . " <br/> " . $message;
//
//            $headers = 'MIME-Version: 1.0' . "\r\n";
//            $headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
//            $headers .= "From: " . $email;

            try {
                R::store($obj);
//                mail($to, $subject, $messag, $headers);
                echo 1;
            } catch (Exception $e) {
                echo -1;
            }
        } else {
            echo -1;
        }
    }

// at the end of class call
    public function __destruct() {
//close connection
        R::close();
    }

}
