<?php

namespace App\Controllers;

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

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

    public function __construct()
    {
        $HomeModel = new homeModel();
        $this->MainModel = new Main();
        $this->data = $this->MainModel->DefaultVariable();
        $this->data['WebsitesDomain'] = $HomeModel->GetAllWebsitesDomain();
    }

    public function index()
    {
        $data = $this->data;
        $UserModel = new Users();
        $data['AllUsers'] = $UserModel->GetAllUsers();
        echo view('header', $data);
        echo view('users/users', $data);
        echo view('footer', $data);
    }

    public function save_add_user_form()
    {
        $data = $this->data;
        $Crud = new Crud();
        $fullname = $this->request->getVar('full_name');
        $username = $this->request->getVar('username');
        $password = $this->request->getVar('password');
        $user_type = $this->request->getVar('user_type');
        $websites = $this->request->getVar('websites');
        $status = 'active';
//        echo "<pre>";
//        print_r($_REQUEST);
//        exit;

        $records = array();
        $records['FullName'] = $fullname;
        $records['Username'] = $username;
        $records['Password'] = $password;
        $records['Type'] = $user_type;
        $records['Status'] = $status;

        $UserModel = new Users();
        $Insert_id = $UserModel->SaveUserRecords($records);

        foreach ($websites as $key => $value) {
            $DataMeta = array();
            $DataMeta['UserUID'] = $Insert_id;
            $DataMeta['WebsiteUID'] = $key;
            $UserModel->SaveUserMeta($DataMeta);
        }
        if (isset($Insert_id) && $Insert_id != '') {
            $response = array();
            $response['status'] = 'success';
            $response['message'] = 'User Added Successfully';
            echo json_encode($response);
        }

    }

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



        $Crud = new Crud();

        $update_id = $this->request->getVar('update_id');
        $fullname = $this->request->getVar('full_name');
        $username = $this->request->getVar('username');
        $password = $this->request->getVar('password');
        $user_type = $this->request->getVar('user_type');
        $websites = $this->request->getVar('websites');
        $status = 'active';


        $records = array();
        $records['FullName'] = $fullname;
        $records['Username'] = $username;
        $records['Password'] = $password;
        $records['Type'] = $user_type;
        $records['Status'] = $status;


//        echo "<pre>";
//        print_r($websites);
//        exit;

        $table = 'public."users"';
        $where = array('UID' => $update_id);
        $Insert_id = $Crud->UpdateRecord($table, $records, $where);

        $tableMeta = 'public."user_website"';
        $whereMeta = array('UserUID' => $update_id);
        $Crud->DeleteRecord($tableMeta, $whereMeta);
        $UserModel = new Users();
        foreach ($websites as $key => $value) {
            $DataMeta = array();
            $DataMeta['UserUID'] = $update_id;
            $DataMeta['WebsiteUID'] = $key;
            $UserModel->SaveUserMeta($DataMeta);
        }
        if (isset($update_id) && $update_id != '') {
            $response = array();
            $response['status'] = 'success';
            $response['message'] = 'User Updated Successfully';
            echo json_encode($response);
        }

    }

    public function Delete_user_by_id()
    {
        $UID = $this->request->getVar('UID');
        $Crud = new Crud();
        $table = 'public."users"';
        $where = array('UID' => $UID);

        $Crud->DeleteRecord($table, $where);

        $tableMeta = 'public."user_website"';
        $whereMeta = array('UserUID' => $UID);
        $Crud->DeleteRecord($tableMeta, $whereMeta);
        $response = array();
        $response['status'] = 'success';
        $response['message'] = 'User Added Successfully';
        echo json_encode($response);

    }

    public function get_user_by_id()
    {
        $UID = $this->request->getVar('UID');

        $HomeModel = new homeModel();
        $WebsitesDomain = $HomeModel->GetAllWebsitesDomain();


        $Crud = new Crud();
        $record = $Crud->ExecuteSQL('SELECT * FROM public."users" WHERE "UID" = \'' . $UID . '\'; ');
        echo '<form class="validate" method="post" action="#" id="UpdateUserForm">
                        <input name="update_id" id="update_id" type="hidden">
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                <input name="update_id" id="update_id" type="hidden" value="' . $record[0]['UID'] . '">
                                    <label for="full_name" class="col-form-label">Full Name:</label>
                                    <input type="text" class="form-control validate[required]" name="full_name"
                                           id="full_name" value="' . $record[0]['FullName'] . '">
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="password" class="col-form-label">Username:</label>
                                    <input type="text" name="username" class="form-control validate[required]"
                                           id="username" value="' . $record[0]['Username'] . '">
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="password" class="col-form-label">Password:</label>
                                    <input type="text" name="password" class="form-control validate[required]"
                                           id="password" value="' . $record[0]['Password'] . '">
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="user_type" class="col-form-label">Type:</label>
                                    <select class="form-control validate[required]" name="user_type" id="user_type" value="' . $record[0]['Type'] . '">
                                        <option value="">Please Select</option>
                                        <option value="admin"  ' . ($record[0]['Type'] == 'admin' ? 'Selected' : '') . '   >Admin</option>
                                        <option value="user"   ' . ($record[0]['Type'] == 'user' ? 'Selected' : '') . '    >User</option>
                                    </select>
                                </div>
                            </div>

                        </div>
                        <div class="row">
                            <div class="col-md-12">
                                <h5> Websites </h5>
                            </div>
                            <div class="col-md-12">
                                <hr style="margin-bottom: 10px;">
                            </div>
                            <div class="col-md-12"> ';
        $WebsitesBYUID = $HomeModel->GetAllWebsitesDomainByUID($record[0]['UID']);
        foreach ($WebsitesDomain as $value) {
            if (in_array($value['UID'], $WebsitesBYUID)) {
                $checked = 'checked';
            } else {
                $checked = '';
            }
            echo '<label class="checkbox-primary" style="margin-right: 10px;">
                                            <input id="' . $value['UID'] . '" type="checkbox" class="" value="1"
                                                  name="websites[' . $value['UID'] . ']" style="margin-right: 7px;" ' . $checked . ' >
                                             <strong>' . ucwords($value['WebSchema']) . '</strong>
                                       </label> ';
        }
        echo ' </div> 
                                    </div>
                                    <div class="row">
                                        <div class="col-md-12" id="Status">
                                        </div>
                                    </div>
                                </form>';


//        echo "<pre>";
//        print_r($_REQUEST);
//        exit;


    }

}
