<?php

namespace App\Controllers;

use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\API\ResponseTrait;
//use CodeIgniter\Database\RawSql;

class Myfit extends BaseController
{
    use ResponseTrait;
    protected $request;

    public function __construct()
    {
        $this->request = $request = \Config\Services::request();
    }

    public function index()
    {
        $mBlogData = new \App\Models\myfitBlogData();
        $res1 = $mBlogData->getBlogData([]);
        return $this->response->setJson($res1);
    }

    public function contact()
    {
        /*$inx = array(
            'name' => $this->post('name'),
            'email' => $this->post('email'),
            'country' => $this->post('country'),
            'phone' => $this->post('phone'),
            'message' => $this->post('message')
        );
        $something = $this->request->getVar('name');
        */
        header('Access-Control-Allow-Origin: * ');
        //header("Access-Control-Allow-Origin: http://localhost:9057 ");
        header('Access-Control-Expose-Headers: Access-Control-Allow-Origin');
        header('Access-Control-Allow-Credentials: true ');
        //header("Access-Control-Allow-Headers: Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With");
        header('Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS');
        header('Content-type: application/json');
        $startInsert = false;
        $status = 0;
        $message_id = '';
        $raw_json = file_get_contents('php://input');
        $raw_array = json_decode($raw_json, true);
        $something = $raw_array['name'];

        $db = \Config\Database::connect('mermsemr_site');
        if (
            $raw_array['country'] != '' &&
            $raw_array['email'] != '' &&
            $raw_array['name'] != '' &&
            $raw_array['message'] != '' &&
            $raw_array['phone'] != ''
        ) {
            $startInsert = true;
        }
        else{
            $status = -2;
            $message_id = 'Parameters not recieved';
        }
        $data = [
            'country' => $raw_array['country'],
            'email' => $raw_array['email'],
            'name' => $raw_array['name'],
            'message' => $raw_array['message'],
            'phone' => $raw_array['phone'],
        ];

        if ($startInsert) {
            try {
                $db->table('myfit_contact')->insert($data);
                // $insert_id = $db->affectedRows();
                $insert_id = $db->insertId();
                $query = $db->query(
                    'SELECT uuid, id FROM myfit_contact WHERE id = ' .
                        $insert_id
                );
                $row = $query->getRow();
                $message_id = $row->uuid;
                $status = $row->id;
            } catch (Exception $e) {
                $status = -1;
                $message_id = '';
            }
        }

        $inx = [
            'message_id' => $message_id,
            'status' => $status,
        ];
        return $this->response->setJson($inx);
    }

    public function blogdata()
    {
        /*  header("Access-Control-Allow-Origin: *");
        //header("x-devicetoken : *");
        //header("Authorization : Token");
        header("Access-Control-Expose-Headers: Access-Control-Allow-Origin");
        header("Access-Control-Allow-Headers: Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With,x-session-id, client_id, x-float-device-location-latitude, x-float-device-location-longitude, x-devicetoken");
        header("Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS");
        header('Content-type: application/json');
        Access-Control-Allow-Origin

           header1 = ('Access-Control-Allow-Credentials','true')
          header2 = ('Access-Control-Allow-Origin', 'https://serviceview.example.com')
          
*/
        header('Access-Control-Allow-Origin: * ');
        //header("Access-Control-Allow-Origin: http://localhost:9057 ");
        header('Access-Control-Expose-Headers: Access-Control-Allow-Origin');
        header('Access-Control-Allow-Credentials: true ');
        //header("Access-Control-Allow-Headers: Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With");
        header('Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS');
        header('Content-type: application/json');

        $mBlogData = new \App\Models\myfitBlogData();
        $res1 = $mBlogData->getBlogData([]);
        return $this->response->setJson($res1);
        //  $this->setGetReturn(res1);
    }

    public function country()
    {
        //header("Access-Control-Allow-Origin: http://localhost:9057 ");
        header('Access-Control-Allow-Origin: * ');
        header('Access-Control-Expose-Headers: Access-Control-Allow-Origin');
        header('Access-Control-Allow-Credentials: true ');
        //header("Access-Control-Allow-Headers: Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With");
        header('Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS');
        header('Content-type: application/json');

        $mCountry = new \App\Models\myfitCountry();
        return $this->response->setJson($mCountry->getSiteCountries([]));
    }

    public function faq()
    {
        //header("Access-Control-Allow-Origin: http://localhost:9057 ");
        header('Access-Control-Allow-Origin: * ');
        header('Access-Control-Expose-Headers: Access-Control-Allow-Origin');
        header('Access-Control-Allow-Credentials: true ');
        //header("Access-Control-Allow-Headers: Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With");
        header('Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS');
        header('Content-type: application/json');

        $db = \Config\Database::connect('mermsemr_site');
        $query = $db->query(
            'SELECT * FROM myfit_faq ORDER BY id ASC LIMIT 15'
        );
        $data['faq_data'] = $query->getResultArray();

        $mFaq = new \App\Models\myfitFaqData();
    return $this->response->setJson($data['faq_data'] /*$mFaq->getSiteFaq([])*/);
    }

    public function pricing()
    {
        //header("Access-Control-Allow-Origin: http://localhost:9057 ");
        header('Access-Control-Allow-Origin: * ');
        header('Access-Control-Expose-Headers: Access-Control-Allow-Origin');
        header('Access-Control-Allow-Credentials: true ');
        //header("Access-Control-Allow-Headers: Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With");
        header('Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS');
        header('Content-type: application/json');

        $mPricing = new \App\Models\myfitPricing();
        return $this->response->setJson($mPricing->getSitePricing([]));
    }

    private function setGetReturn($resp)
    {
        header('Access-Control-Allow-Origin: * ');
        //header("Access-Control-Allow-Origin: http://localhost:9057 ");
        header('Access-Control-Expose-Headers: Access-Control-Allow-Origin');
        header('Access-Control-Allow-Credentials: true ');
        //header("Access-Control-Allow-Headers: Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With");
        header('Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS');
        header('Content-type: application/json');
        return $this->response->setJson($resp);
    }
}
