<?php

namespace App\Controllers;

use App\Models\Crud;
use App\Models\homeModel;
use App\Models\Main;

class Home extends BaseController
{
    var $data = array();
    var $MainModel;

    public function __construct()
    {
        $HomeModel = new homeModel();
        $this->MainModel = new Main();
        $this->data = $this->MainModel->DefaultVariable();

        $auth = 0;
        if (!isset($_SERVER['PHP_AUTH_USER'])) {
            header('WWW-Authenticate: Basic realm="RBT Ringba"');
            header('HTTP/1.0 401 Unauthorized');
            print "you need to login";
            exit;
        } else {
            if ($_SERVER['PHP_AUTH_USER'] == "admin" and $_SERVER['PHP_AUTH_PW'] == "rbt_portal") {
                $auth = 1;
            } else {
                header('WWW-Authenticate: Basic realm="RBT Ringba"');
                header('HTTP/1.0 401 Unauthorized');
                print "you need to login";
                exit;
            }
        }

        if ($auth == 0) {
            print "you need to login";
            exit;
        }


    }

    public function index()
    {
        $data = $this->data;
        echo view('front_page', $data);
    }


    public function form_submit()
    {
        $phone_number = $this->request->getVar('phone_number');
        $zip_code = $this->request->getVar('zip_code');

        $temparray = array();
        $temparray['CID'] = "+1' . $phone_number . '";
        $temparray['areacode'] = substr($phone_number, 0, 3);
        if (isset($zip_code) && $zip_code != '') {
            $temparray['zipcode'] = $zip_code;
        }

        $response = CurlRUN('https://rtb.ringba.com/v1/production/3379b91dc73342b3bb9beed77a2d602f.json', json_encode($temparray));
        //print_r($response); exit;
        $Crud = new Crud();
        $table = 'public."records"';
        $record = [
            'ResponseJSON' => json_encode($response),
            'bidID' => $response['bidId'],
            'bidAmount' => $response['bidAmount'],
            'expireInSeconds' => $response['expireInSeconds'],
            'bidExpireDT' => date("Y-m-d H:i:s"),
            'bidExpireEpoch' => $response['bidExpireEpoch'],
            'phoneNumber' => $response['phoneNumber'],
            'phoneNumberNoPlus' => $response['phoneNumberNoPlus'],
            'sipAddress' => $response['sipAddress'],
            'reqPhoneNumber' => $phone_number,
            'reqZipCode' => $zip_code,
            'rejectReason' => ((isset($response['rejectReason'])) ? $response['rejectReason'] : '')
        ];

        $Record = $Crud->AddRecord($table, $record);
        $bidLimit = 15;

        if (isset($response['rejectReason']) || $response['bidAmount'] < $bidLimit) {

            if (isset($response['rejectReason'])) {
                echo '<div class="alert alert-danger">ERROR: ' . ucwords($response['rejectReason']) . '</div>';
            } else if ($response['bidAmount'] < $bidLimit) {
                echo '<div class="alert alert-danger">No BID Available</div>';
            }

        } else {
            //echo "<pre>"; print_r($record); echo "</pre>";
            echo '<div class="alert alert-success">Phone Number: ' . $record['phoneNumberNoPlus'] . ' | Expire in "' . $record['expireInSeconds'] . '" Sec</div>';
        }
    }
}
