<?php
$formname    = "form_admin_user_card";
$translation = \DynCom\mysyde\common\classes\Registry::get("translation");
$inputname   = "input_id";
$disabled    = TRUE;
$disabledSite = TRUE;

$query       = "SELECT id, code, name FROM main_site ORDER BY id ASC";
$result      = @mysqli_query($GLOBALS['mysql_con'], $query);
$sites       = array();
$site_values = array();
$i           = 0;
while ($row = @mysqli_fetch_array($result)) {
    $sites[]         = $row['name'];
    $site_values[$i] = $row['id'];
    $i++;
}

// $query       = "SELECT id, name FROM backend_settings ORDER BY id ASC";
// $result      = @mysqli_query($GLOBALS['mysql_con'], $query);
$settings_name = array();
$settings_value = array();
$i           = 0;
while ($row = @mysqli_fetch_array($result)) {
    $settings_name[]         = $row['name'];
    $settings_value[$i] = $row['id'];
    $i++;
}

function create_adminuser_site_checkbox( $user_id,$disabledSite = FALSE ) {

    if($user_id == "") {
        $siteAdminLink = false;
    } else {
        $query             = "SELECT * FROM main_site_admin_user_link where main_admin_user_id = '" . $user_id."'";
        $result            = @mysqli_query($GLOBALS['mysql_con'], $query);
        $siteAdminAvailableSites = array();
        if (@mysqli_num_rows($result) > 0) {
            $siteAdminLink = true;
            while ($row = @mysqli_fetch_array($result)) {
                $siteAdminAvailableSites[] = $row['main_site_id'];
            }
        } else {
            $siteAdminLink = false;
        }
    }

    $query = "SELECT * from main_site";
    $result = @mysqli_query($GLOBALS['mysql_con'], $query);
    while ($siterow = @mysqli_fetch_assoc($result)) {
        if ($siteAdminAvailableSites !== null) {
            if( in_array($siterow['id'], $siteAdminAvailableSites)) {
                $ckecked = 1;
            } else {
            
                $ckecked = 0;
            }
        }
        

        input($siterow['name'], "input_site_admin_link_" . $siterow["id"], "checkbox", $ckecked, NULL, $disabledSite);
    }
}

function create_adminuser_shop_checkbox( $user_id ) {

    if($user_id == "") {
        $shopAdminLink = false;
    } else {
        $query             = "SELECT * FROM main_admin_user_shop_link where main_admin_user_id = '" . $user_id."'";
        $result            = @mysqli_query($GLOBALS['mysql_con'], $query);
        $siteAdminAvailableShops = array();
        if (@mysqli_num_rows($result) > 0) {
            $shopAdminLink = true;
            while ($row = @mysqli_fetch_array($result)) {
                if($row['active'] == 1) {
                    $siteAdminAvailableShops[] = $row['shop_code'];
                }
            }
        } else {
            $shopAdminLink = false;
        }
    }

    $query = "SELECT * from shop_shop";
    $result = @mysqli_query($GLOBALS['mysql_con'], $query);
    while ($siterow = @mysqli_fetch_assoc($result)) {
        if( in_array($siterow['code'], $siteAdminAvailableShops)) {
            $ckecked = 1;
           
        } else {
            $ckecked = 0;
            echo 'hello';
        }

        input($siterow['description'], "input_shop_admin_link_" . $siterow["id"], "checkbox", $ckecked);
    }
}

?>

<div id="overlaycrumb">
    <?php if ($input_admin_user["id"] == "") {
        echo $translation->get("new_user");
    } else {
        echo $translation->get("edit_user");
    }
    ?>
    <div id="closeoverlay" onclick="disableOverlay();"></div>
</div>

<? if ($GLOBALS['admin_user']['right_create_user'] == 1 || $GLOBALS['admin_user']['is_super_user'] == 1) {
    $disabled = FALSE;
}?>
<? if ($GLOBALS['admin_user']['right_website_admin'] == 1 || $GLOBALS['admin_user']['is_super_user'] == 1) {
    $disabledSite = FALSE;
}?>
    <ul class="toolbar_menu">
        <?= button("save", $translation->get("save"), $formname, "loadCard('save', true)"); ?>
        <?= button("save", $translation->get("save_and_close"), $formname, "loadCard('save', true, '', true)"); ?>
        <li>
            <ul>
                <?php
                if ($input_admin_user["id"] != "" && ($GLOBALS['admin_user']['right_create_user'] == 1 || $GLOBALS['admin_user']['is_super_user'] == 1)) {
                    echo button("delete", $translation->get("delete"), $formname, "loadCard('delete', true, '{$translation->get('delete_user_confirm')}', true)", "", FALSE);
                }
                ?>
            </ul>
        </li>
    </ul>
    <div class="clearfix"></div>
<? //} ?>

<?php
if ($messages !== null) {
    if (count($messages)) {
        echo '<div id="overlayMessages">' . join("\r\n", $messages) . '</div>';
    }
}

 
?>

<form id="<?= $formname ?>" name="<?= $formname ?>" method="post">
    <input name="input_id" type="hidden" value="<?= $input_admin_user["id"] ?>">

    <table class="cardform" border="0" cellspacing="0" cellpadding="0">
        <tr>
            <td>
                <? input($translation->get("name"), "input_name", "hidden", $input_admin_user["name"], 45) ?>
                <? input($translation->get("email"), "input_email", "hidden", $input_admin_user["email"], 100) ?>
                <? input($translation->get("login"), "input_login", "hidden", $input_admin_user["login"], 20) ?>
                <? input($translation->get("create_new_user_right"), "input_right_create_user", "hidden", $input_admin_user["right_create_user"], 45) ?>


                <? input($translation->get("name"), "input_name", "text", $input_admin_user["name"], 45, $disabled) ?>
                <? input($translation->get("email"), "input_email", "text", $input_admin_user["email"], 100, $disabled) ?>
                <br />
                <? input($translation->get("login"), "input_login", "code", $input_admin_user["login"], 20, $disabled) ?>
                <? if($input_admin_user['id'] == $GLOBALS['admin_user']['id']) { input($translation->get("old_password"), "input_old_password", "password","", 45, false);} ?>
                <? input($translation->get("password"), "input_password", "password","", 45, false) ?>
                <? input($translation->get("password_repeat"), "input_password2", "password","", 45, false) ?>
                <? input($translation->get("image"), "user_image", "file", $input_admin_user["image"], 45, $disabled);
                        $valFile = '';
                        if($input_admin_user['image'] != '' && $input_admin_user['image'] != NULL){
                            $valFile = $input_admin_user['image'];
                        }
                ?>
                      <input type="hidden" name="user_image_old" value="<?=$valFile;?>">
                <?php
                        if($input_admin_user["image"] != NULL && $input_admin_user["image"] != ''){
                            echo "<div class='img_container intranet_user_image'><img class='img-fluid' src='/userdata/collection/resize/".$input_admin_user["image"]."'></div>";
                            echo button("delete", $translation->get("delete"), $formname, "loadCard('delete_image', true, '{$translation->get('delete_user_picture')}', false)", "", FALSE);
                        }
                ?>
                <? input($translation->get("create_new_user_right"), "input_right_create_user", "checkbox", $input_admin_user["right_create_user"], 45, $disabled) ?>
                <? input($translation->get("create_new_website_right"), "right_website_admin", "checkbox", $input_admin_user["right_website_admin"], 45, $disabledSite) ?>
                <? input($translation->get("is_betriebsrat"), "is_betriebsrat", "checkbox", $input_admin_user["is_betriebsrat"], 45, $disabledSite) ?>
               <div style="display:none;"> <? input_select($translation->get("edit_mode"), "input_edit_mode", array(0, 1), array($translation->get('live_edit'), $translation->get('backend_edit')), $input_admin_user["edit_mode"]); ?></div>
            </td>
            <td>
                <? input_select($translation->get("main_website"), "main_site_id", $values = $site_values, $value_names = $sites, $input_admin_user["main_site_id"], $disabledSite); ?>
                <? input_select($translation->get("backend_language"), "main_language", $values = array(0 => 'de', 1 => 'en'), $value_names = array('Deutsch', 'Englisch'), $input_admin_user["main_language"]); ?>
            </td>
            <td class="website-relation" colspan="2">
                <h3><?php echo $translation->get("admin_user_site"); ?></h3>
                <?php create_adminuser_site_checkbox($input_admin_user["id"], $disabledSite); ?>
            </td>
        </tr>
    


    </table>

</form>
<link rel="stylesheet" type="text/css" href="/components/password-strength-meter/dist/password.min.css">
<script src="/components/password-strength-meter/dist/password.min.js"></script>
<script type="text/javascript">

    $('#input_password').password({
        shortPass: '<?= $GLOBALS['tc']['short_password'] ?> ',
        badPass: '<?= $GLOBALS['tc']['weak_password'] ?> ',
        goodPass:  '<?= $GLOBALS['tc']['good_password'] ?> ',
        strongPass:  '<?= $GLOBALS['tc']['strong_password'] ?> ',
        containsUsername: '',
        enterPass: '',
        showPercent: false,
        showText: true, // shows the text tips
        animate: true, // whether or not to animate the progress bar on input blur/focus
        animateSpeed: 'fast', // the above animation speed
        username: false, // select the username field (selector or jQuery instance) for better password checks
        usernamePartialMatch: true, // whether to check for username partials
        minimumLength: 6 // minimum password length (below this threshold, the score is 0)
    });


</script>