<?php

namespace App\Controllers;

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

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

    public function __construct()
    {
        $session = session();
        $HomeModel = new homeModel();
        $this->MainModel = new Main();
        $this->data = $this->MainModel->DefaultVariable();
//        echo $this->data['segment_a'];
//        echo $this->data['segment_b'];
        if ($this->data['segment_a'] != 'admin-submit' && $this->data['segment_a'] != 'login') {
            if (!isset($this->data['session']['logged_in'])) {
                $session->destroy();
                echo view('login', $this->data);
                exit;
            }
        }

    }

    public function index()
    {
        $data = $this->data;

        if ($data['session']['logged_type'] == 'master_admin') {
            echo view('master_admin_index', $data);
        } else {
            echo view('index', $data);
        }


    }

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

    public function logout()
    {
        $data = $this->data;
        $session = session();
        $session->destroy();
        header("Location: " . $data['path'] . "login");
        exit;
    }


    public function use_login_submit()
    {
        $username = $this->request->getVar('username');
        $password = $this->request->getVar('password');
        $session = session();
//        echo $username." -- ".$password;
//        exit;

        if ($username == 'rbt_admin' && $password == 'rbt_errands') {
            $SessionArray = [
                'logged_type' => 'master_admin',
                'logged_in' => TRUE
            ];

            $session->set($SessionArray);

            $response['status'] = "success";
            $response['message'] = "You are successfully logged";

        } else if ($username == 'admin' && $password == 'test') {

            $SessionArray = [
                'logged_type' => 'admin',
                'logged_in' => TRUE
            ];

            $session->set($SessionArray);

            $response['status'] = "success";
            $response['message'] = "You are successfully logged";

        } else {
            $response['status'] = "fail";
            $response['message'] = "Invalid Login Credentials, Please Try again...";
        }

        echo json_encode($response);
    }


    public function all_records()
    {
        $data = $this->data;
        $data['db_schema'] = $data['segment_a'];
        echo view('header', $data);
        echo view('all_records', $data);
        echo view('footer', $data);
    }

    public function fetch_all_records($schema)
    {
        $data = $this->data;

        $Limit = $_REQUEST['length'];
        $Offset = $_REQUEST['start'];
        $Keyword = $_REQUEST['search']['value'];

        $HomeModel = new homeModel();
        $filterRecords = $HomeModel->FetchAllRecords($schema, '', '');
        $Records = $HomeModel->FetchAllRecords($schema, $Limit, $Offset);

        $cnt = $_POST['start'];
        $DataArray = array();
        foreach ($Records as $value) {
            $cnt++;
            $SubArray = array();
            $SubArray[] = $cnt;
            $SubArray[] = date('Y-m-d H:m:s', strtotime($value['SystemDate']));
            $SubArray[] = $value['FirstName'];
            $SubArray[] = $value['LastName'];
            $SubArray[] = $value['Phone'];
            $SubArray[] = $value['State'];
            $SubArray[] = $value['ZipCode'];
            $SubArray[] = $value['JornayaID'];

            $DataArray[] = $SubArray;
        }

        $output = array(
            "draw" => intval($_POST["draw"]),
            "recordsTotal" => count($Records),
            "recordsFiltered" => count($filterRecords),
            "data" => $DataArray,
        );

        echo json_encode($output);
    }

    public function fetch_all_record()
    {
        $data = $this->data;

        $Limit = $_REQUEST['length'];
        $Offset = $_REQUEST['start'];
        $Keyword = $_REQUEST['search']['value'];


        $HomeModel = new homeModel();
        $filterRecords = $HomeModel->FetchAllRecord('', '', $Keyword);
        $Records = $HomeModel->FetchAllRecord($Limit, $Offset, $Keyword);

        $cnt = $_POST['start'];
        $DataArray = array();
        foreach ($Records as $value) {
            $cnt++;
            $SubArray = array();
            $SubArray[] = $cnt;
            $SubArray[] = date('Y-m-d H:m:s', strtotime($value['SystemDate']));
            $SubArray[] = $value['bidID'];
            $SubArray[] = $value['phoneNumber'];
            $SubArray[] = $value['reqPhoneNumber'];
            $SubArray[] = $value['areaState'];
            $SubArray[] = $value['bidAmount'];
            $SubArray[] = $value['reqZipCode'];
            $SubArray[] = $value['rejectReason'];

            $DataArray[] = $SubArray;
        }

        $output = array(
            "draw" => intval($_POST["draw"]),
            "recordsTotal" => count($Records),
            "recordsFiltered" => count($filterRecords),
            "data" => $DataArray,
        );

        echo json_encode($output);
    }
    public function fetch_all_master_record(){

        $data = $this->data;

        $Limit = $_REQUEST['length'];
        $Offset = $_REQUEST['start'];
        $Keyword = $_REQUEST['search']['value'];

        $HomeModel = new homeModel();
        $filterRecords = $HomeModel->FetchAllMasterRecord('', '', $Keyword);
        $Records = $HomeModel->FetchAllMasterRecord($Limit, $Offset, $Keyword);

        $cnt = $_POST['start'];
        $DataArray = array();
        foreach ($Records as $value) {
            $cnt++;
            $SubArray = array();
            $SubArray[] = $cnt;
            $SubArray[] = date('Y-m-d H:m:s', strtotime($value['SystemDate']));
            $SubArray[] = $value['bidID'];
            $SubArray[] = $value['bidExpireDT'];
            $SubArray[] = $value['bidAmount'];
            $SubArray[] = $value['phoneNumber'];
            $SubArray[] = $value['reqPhoneNumber'];
            $SubArray[] = $value['areaState'];
            $SubArray[] = $value['reqZipCode'];
            $SubArray[] = $value['rejectReason'];

            $DataArray[] = $SubArray;
        }

        $output = array(
            "draw" => intval($_POST["draw"]),
            "recordsTotal" => count($Records),
            "recordsFiltered" => count($filterRecords),
            "data" => $DataArray,
        );

        echo json_encode($output);
    }
    public
    function create_filters_session()
    {
//        echo "<pre>";
//        print_r($_REQUEST);
//        exit;
        $session = session();
        $SessionKey = $this->request->getVar('SessionKey');
        $session->set($SessionKey, '');
        $session->set($SessionKey, $_REQUEST);

        $response = array();
        $response['status'] = 'success';
        $response['msg'] = 'Session Filters Created Successfully';
        $response['session'] = $session->get($SessionKey);

        echo json_encode($response);
    }
    public
    function clear_filters_session()
    {

        $session = session();
        $SessionKey = $this->request->getVar('SessionKey');
        $session->set($SessionKey, '');

        $response = array();
        $response['status'] = 'success';
        $response['msg'] = 'Session Filters Clear Successfully';
        echo json_encode($response);
    }


}
