<?php

namespace App\Controllers;

use App\Models\Crud;
use App\Models\Leads;
use App\Models\Users;
use App\Models\Main;

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

    public function __construct()
    {
        $this->data['template'] = TEMPLATE;
        $this->data['path'] = PATH;

        $this->MainModel = new Main();
        $this->data = $this->MainModel->DefaultVariable();

    }

    public function load_lead()
    {
        $data = $this->data;
        $UID = $_POST['uid'];
        $Lead = new Leads();
        $LeadRecord = $Lead->Record($UID);
        //print_r($LeadRecord);

        $DOB = (($LeadRecord['dob'] == '1970-01-01') ? '' : date("d M, Y - h:i A", strtotime($LeadRecord['dob'])));
        $modalbody = '
        <div class="" style="font-size: 130%;">
            <div class="row" style="margin-bottom: 15px;">
              <div class="col-4"><strong>Date Time:</strong></div>
              <div class="col-8">' . date("d M, Y - h:i A", strtotime($LeadRecord['system_datetime'])) . '</div>
            </div>  
            <div class="row" style="margin-bottom: 15px;">
              <div class="col-4"><strong>First Name:</strong></div>
              <div class="col-8">
                ' . $LeadRecord['first_name'] . '
                ' . CopyText($LeadRecord['first_name']) . '
              </div>
            </div>  
            <div class="row" style="margin-bottom: 15px;">
              <div class="col-4"><strong>Last Name:</strong></div>
              <div class="col-8">' . $LeadRecord['last_name'] . '
                ' . CopyText($LeadRecord['last_name']) . '</div>
            </div>  
            <div class="row" style="margin-bottom: 15px;">
              <div class="col-4"><strong>Phone:</strong></div>
              <div class="col-8">' . $LeadRecord['phone'] . '
                ' . CopyText($LeadRecord['phone']) . '</div>
            </div>  
            <div class="row" style="margin-bottom: 15px;">
              <div class="col-4"><strong>Email:</strong></div>
              <div class="col-8">' . $LeadRecord['email'] . '
                ' . CopyText($LeadRecord['email']) . '</div>
            </div>  
            <div class="row" style="margin-bottom: 15px;">
              <div class="col-4"><strong>DOB:</strong></div>
              <div class="col-8">' . $DOB . ' ' . CopyText($DOB) . '</div>
            </div>  
            <div class="row" style="margin-bottom: 15px;">
              <div class="col-4"><strong>Zip:</strong></div>
              <div class="col-8">' . $LeadRecord['zip'] . '
                ' . CopyText($LeadRecord['zip']) . '</div>
            </div>  
            <div class="row" style="margin-bottom: 15px;">
              <div class="col-4"><strong>State:</strong></div>
              <div class="col-8">' . $LeadRecord['state'] . '
                ' . CopyText($LeadRecord['state']) . '</div>
            </div>  
        </div> ';

        $BTN = '';
        if ($LeadRecord['status'] == 'original' || $LeadRecord['status'] == '') {
            $BTN = '<button type="button" class="btn btn-primary" onclick="ChangeLeadStatus(' . $LeadRecord['id'] . ', \'submit_for_411\')">Submit to 411</button>';
        }
        if ($LeadRecord['status'] == 'submit_for_411') {
            $BTN = '<button type="button" class="btn btn-primary" onclick="ChangeLeadStatus(' . $LeadRecord['id'] . ', \'final\')">Final Submit</button>';
        }

        $HTML = '
            <div class="modal-header">
                <h5 class="modal-title" id="LoadLeadModalTitle">Lead Ref.#: ' . RefID($LeadRecord['id']) . '</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">' . $modalbody . '
                <div id="LeadStatusAjaxResponse"></div>
            </div>
            <div class="modal-footer">
                ' . $BTN . '
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
            </div> ';

        echo $HTML;
    }

    public function update_lead_status()
    {
        $UID = $_POST['uid'];
        $Status = $_POST['status'];
        $Crud = new Crud();
        $Crud->UpdateRecord('leads', array('status' => $Status), array('id' => $UID));
        echo '<div class="alert alert-success" role="alert">Lead Status Changed Successfully...!</div>';

//        $Leads = new Leads();
//        $DEOID = $Leads->AssingLeadsDEO();
    }

    public function delete_user()
    {
        $UID = $_POST['id'];
        $Crud = new Crud();
        $Crud->DeleteRecord('users', array('id' => $UID));
        $response['status'] = "success";
        $response['message'] = '<div class="alert alert-success" role="alert">User Successfully Deleted...!</div>';
        echo json_encode($response);
    }

    public function html_user_data()
    {
        $UID = $_POST['id'];
        $Crud = new Crud();
        $Record = $Crud->SingleRecord('users', array('id' => $UID));
        $response['status'] = "success";
        $response['record'] = $Record;
        echo json_encode($response);
    }

    public function login_form_submit()
    {
        $session = session();
        //print_r($_POST);
        $username = $_POST['username'];
        $password = $_POST['password'];
        $Crud = new Crud();
        $Record = $Crud->SingleRecord('users', array("username" => $username, "password" => $password));
        if (isset($Record['id'])) {
            /////// success
            $loggedUser = [
                'id' => $Record['id'],
                'full_name' => $Record['full_name'],
                'user_name' => $Record['username'],
                'password' => $Record['password'],
                'user_type' => $Record['type'],
                'logged_in' => TRUE
            ];
            $session->set($loggedUser);

            ///// update last login datetime
            $Crud->UpdateRecord("users", array("lastlogin_at" => date("Y-m-d H:i:s")), array("id" => $Record['id']));

            $response['status'] = "success";
            $response['message'] = '<div class="alert alert-success" role="alert">Successfully Login...!</div>';
            echo json_encode($response);

        } else {
            ///////// fail
            $response = array();
            $response['status'] = 'fail';
            $response['message'] = '<div class="alert alert-danger" role="alert">Invalid Username or Password...!</div>';
            echo json_encode($response);
        }
    }

    public function submit_user_form()
    {
        $session = session();
        //print_r($_POST);
        $UID = $_POST['UID'];
        $full_name = $_POST['full_name'];
        $username = $_POST['username'];
        $password = $_POST['password'];
        $type = $_POST['type'];
        $Crud = new Crud();
        $response = array();

        if ($UID > 0) {
            ///////// UPDATE CASE
            $Crud->UpdateRecord('users', array(
                'username' => str_replace(" ", "_", $username),
                'password' => str_replace(" ", "_", $password),
                'type' => $type,
                'full_name' => $full_name,
            ), array('id' => $UID));
            $response['status'] = "success";
            $response['message'] = '<div class="alert alert-success" role="alert">System User Successfully Updated...!</div>';
            echo json_encode($response);

        } else {
            //////// INSERT CASE
            $Record = $Crud->SingleRecord('users', array("username" => $username));
            if (isset($Record['id'])) {
                $response['status'] = 'fail';
                $response['message'] = '<div class="alert alert-danger" role="alert">Duplicate User Name...!</div>';
                echo json_encode($response);
            } else {
                $RecordID = $Crud->AddRecord('users', array(
                    'username' => str_replace(" ", "_", $username),
                    'password' => str_replace(" ", "_", $password),
                    'type' => $type,
                    'full_name' => $full_name,
                ));
                $response['status'] = "success";
                $response['recordid'] = $RecordID;
                $response['message'] = '<div class="alert alert-success" role="alert">System User Successfully Added...!</div>';
                echo json_encode($response);
            }
        }
    }


    public function load_user()
    {
        $data = $this->data;
        $UID = $_POST['uid'];
        $Users = new Users();
        $Record = $Users->Record($UID);
        //print_r($LeadRecord);

        $modalbody = '
        <div class="" style="font-size: 130%;">
            <div class="row" style="margin-bottom: 15px;">
              <div class="col-4"><strong>Full Name:</strong></div>
              <div class="col-8">' . $Record['full_name'] . '</div>
            </div>  
            <div class="row" style="margin-bottom: 15px;">
              <div class="col-4"><strong>Username:</strong></div>
              <div class="col-8">' . $Record['username'] . '</div>
            </div> 
            <div class="row" style="margin-bottom: 15px;">
              <div class="col-4"><strong>Password:</strong></div>
              <div class="col-8">' . $Record['password'] . '</div>
            </div>
            <div class="row" style="margin-bottom: 15px;">
              <div class="col-4"><strong>Account Type:</strong></div>
              <div class="col-8">' . ucwords(str_replace("_", ' ', $Record['type'])) . '</div>
            </div>  
            <div class="row" style="margin-bottom: 15px;">
              <div class="col-4"><strong>Last Login:</strong></div>
              <div class="col-8">' . (($Record['lastlogin_at'] == null) ? '-' : date("d M, Y - h:i A", strtotime($Record['lastlogin_at']))) . '</div>
            </div>
            
        </div> ';

        $BTN = '';

        $HTML = '
            <div class="modal-header">
                <h5 class="modal-title" id="LoadLeadModalTitle">System User Ref.#: ' . RefID($Record['id'], 'ERR/SU') . '</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">' . $modalbody . '
                <div id="LeadStatusAjaxResponse"></div>
            </div>
            <div class="modal-footer">
                ' . $BTN . '
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
            </div> ';

        echo $HTML;
    }


}
