<?php $stmt = $GLOBALS['con']->prepare("SELECT * FROM `contractors` WHERE `contractor_number`=?")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('i',$_REQUEST['contractor_number']);$stmt->execute();$r=$stmt->get_result();$stmt->close();
$cd=mysqli_fetch_array($r);
$email_values = [
    0 => '0', //client
    1 => $cd['forename'].' '.$cd['surname'], //type
    2 => $cd['email'], //recipient
    3 => '', //subject
    4 => '', //message
    5 => '', //attachment
    6 => '' //source
];
$email64_values = base64_encode(json_encode($email_values));
?>
<style>
.pwstrength_viewport_progress .progress {
    height: 15px !important
}
</style>

<div class="row mb-4">
    <div class="col-12 col-md-3 order-md-2 order-1">
        <div class="float-end">
            <?php if($cd['phone1']!=""){?>
            <a data-mdb-tooltip-init data-mdb-placement="bottom" data-mdb-trigger="hover" data-mdb-trigger="hover" title="Phone <?= $cd['forename']?> <?= $cd['surname']?>" id="phone" href="tel:<?= $cd['phone1']?>"
               class="btn btn-secondary noprint"><i class="fad fa-phone"></i></a>
            <?php }?>


            <?php if(!filter_var($cd['email'], FILTER_VALIDATE_EMAIL) === false) {?>
            <a data-mdb-tooltip-init data-mdb-placement="bottom" data-mdb-trigger="hover" data-mdb-trigger="hover" title="Email <?= $cd['forename']?> <?= $cd['surname']?>" href="javascript:void(0)"
               class="btn btn-secondary emailButton"
               data-values="<?= $email64_values?>" data-href="<?=ROOT_URL?>/includes/master/mailer.php"><i class="fad fa-envelope"></i></a>
            <?php }
                if($cd['statuslive']==1){?>
            <a data-mdb-tooltip-init data-mdb-placement="bottom" data-mdb-trigger="hover" data-mdb-trigger="hover" title="Archive Contractor" href="javascript:void(0)"
               onClick="archiveSurveyor('surveyor','id','<?= $cd['id']?>','surveyor_functions.php','archiveSupplier')"
               class="btn btn-secondary"><i class="fad fa-archive"></i></a>
            <?php } else {?>
            <a data-mdb-tooltip-init data-mdb-placement="bottom" data-mdb-trigger="hover" data-mdb-trigger="hover" title="Archive Contractor" href="javascript:void(0)"
               onClick="reinstateSurveyor('surveyor','id','<?= $cd['id']?>','surveyor_functions.php','reinstateSupplier')"
               class="btn btn-secondary"><i class="fad fa-upload"></i></a>
            <?php }?>
        </div>
    </div>

    <div class="col-12 col-md-9 order-md-1 order-2">

        <div class="row mb-3">
            <div class="note note-secondary">
                Contractor settings <small class="muted">Manage access permissions and profile details of your contractors.</small>
            </div>
        </div>
    </div>
</div>
<div id="text" class="mb-3"></div>
<form id="contractorForm" class="form-horizontal">
    <fieldset>
        <?php require_once(ROOT_URL.'/forms/elements/contractor_details.frm');?>


        <div class="row">
            <div class="col-12 col-md-4">
                <div class="card">
                    <div class="card-header">Access</div>
                    <?php $stmt2 = $GLOBALS['con']->prepare("SELECT AES_DECRYPT(password,email) AS pass, email FROM `contractors` WHERE `contractor_number`=?")or die(mysqli_error($GLOBALS['con']));
                    $stmt2->bind_param('i',$_REQUEST['contractor_number']);$stmt2->execute();$p=$stmt2->get_result();$stmt2->close();
                    $data=mysqli_fetch_array($p);
                    $cd['password']=$data['pass'];
                    if($data['pass']=="" && $data['email']!="" ){?>
                    <script>
                    $(document).ready(function() {
                        $('#text').html(
                            '<div class="alert warning-color-dark text-center text-light p-0"><p style="margin-top:10px">There has been no access password set<br><i>This user will not be able to access the system</i></p></div>'
                        )
                        on()
                    })
                    </script>
                    <?php }
                     $email_values = [
                        0 => '0', // client
                        1 => $cd['forename'].' '.$cd['surname'], // name
                        2 => $cd['email'], // recipient
                        3 => getCompanyDetails('name').' login Details', // subject
                        4 => 'Your login details for access to EnRep are as follows:
                    URL: <a href="'.getCompanyDetails('web').'">'.getCompanyDetails('web').'</a>
                    Username: '.$cd['email'].'
                    Password: '.$data['pass'], // message
                        5 => '', // attachment
                        6 => '' // source
                    ];
                    $email64_values = base64_encode(json_encode($email_values));?>
                    <div class="card-body text-center">
                        <div class="row mb-3">
                            <div class="col-12 col-md-3">
                                <label class="form-label" for="password">Password</label>
                            </div>
                            <div id="pcont" class="col-12 col-md-10">
                                <div class="input-group">
                                    <input class="form-control form-control-sm" type="text" name="password" id="password" value="<?= $data['pass'] ?>" autocomplete="new-password" required />
                                    <button type="button" class="btn btn-info btn-sm m-0 px-3 py-2 emailButton"
                                            data-values="<?= $email64_values ?>"
                                            data-href="<?= ROOT_URL ?>/includes/master/mailer.php"
                                            data-mdb-tooltip-init
                                            title="Email surveyor login details"
                                            data-mdb-placement="bottom"
                                            data-mdb-trigger="hover">
                                        <i class="fad fa-envelope"></i>
                                    </button>
                                </div>
                                <div class="row mb-3">
                                    <div class="col-12" style="padding-top: 30px;">
                                        <div class="pwstrength_viewport_progress" style="height:10px"></div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <!-- Messages section -->
                        <div class="row">
                            <div class="col-12" id="messages"></div>
                        </div>
                    </div>
                </div>
            </div>


            <div class="col-12 col-md-4 text-center mb-5">
                <div class="card">
                    <div class="card-header">Status</div>
                    <div class="card-body row">
                        <!-- Live Surveyor Status -->
                        <div class="form-group col-6">
                            <?php $checked = ($cd['statuslive'] == "1") ? "checked" : ""; ?>
                            <label class="form-label" for="statuslive">Live Contractor</label>
                            <div id="statusliveBox" class="form-check form-switch">
                                <input class="form-check-input" type="checkbox" <?= $checked ?> name="statuslive" id="statuslive">
                                <label class="form-check-label" for="statuslive"></label>
                            </div>
                        </div>

                    </div>
                </div>
            </div>

            <div class="col-12 col-md-12 text-center">
                <div class="form-group text-center">
                    <input type="hidden" name="contractor_number" value="<?= $_REQUEST['contractor_number']?>">
                    <input type="hidden" name="FUNCTION" value="update">
                    <input id="function" type="hidden" name="tenant" value="<?=TENANT_URL?>">
                    <input id="btn_update" type="submit" name="submit" value="Update" style="display:none" />
                </div>
            </div>
        </div>
    </fieldset>
</form>

<script type="text/javascript" src="<?=ROOT_URL?>/packages/jqpass/pwstrength-bootstrap.min.js"></script>
<script>
$(document).ready(function() {
    "use strict";
    var options = {
        container: "#pcont",
        colorClasses: ["danger", "danger", "danger", "warning", "warning", "success"]
    };
    options.ui = {
        container: "#pcont",
        viewports: {
            progress: ".pwstrength_viewport_progress"

        },
        colorClasses: ["danger", "danger", "danger", "warning", "warning", "success"],
        showVerdictsInsideProgressBar: true
    }
    $('#password').pwstrength(options);
});

$(document).ready(function() {
    var contractorName = "<?= addslashes($cd['contractor_name']) ?>"; // Safely add PHP value to JS

    $('#navBox').html(
        '<ol class="breadcrumb">' +
        '<li class="breadcrumb-item">' +
        '<a href="index.php?t=includes/master&p=contractor.php&view=list">Contractors</a>' +
        '<i class="fad fa-angle-double-right mx-2 mt-1" aria-hidden="true"></i>' +
        '</li>' +
        '<li class="breadcrumb-item">' +
        '<a href="javascript:void(0)">Edit Contractor</a>' +
        '<i class="fad fa-angle-double-right mx-2 mt-1" aria-hidden="true"></i>' +
        '</li>' +
        '<li class="breadcrumb-item active">' + contractorName + '</li>' +
        '</ol>'
    );

    $('#footerControls').html('<button class="btn btn-secondary" onclick="triggerAction(\'btn_update\')">UPDATE</button>');
});
</script>