<?php

/**
 * Created by PhpStorm.
 * User: Hani
 * Date: 8/11/2018
 * Time: 6:59 PM
 */

$current_url = admin_url().'admin.php?page=woo2app/discount/index.php&tab=roles';
//remove_role('mr2app_discount');
if(isset($_GET["action"])){
    if($_GET['action'] == 'delete'){
        remove_role($_GET["field"]);
        ?>
        <p>
            <?php _e('# Custom background cleared.', 'woo2app'); ?>
        </p>
        <a class="button button-primary" href="<?= $current_url; ?>">  <?php _e('Back', 'woo2app'); ?> </a>
        <?php
        exit();
    }
}
else {
    if(isset($_POST["value"]) && $_POST["value"] != "" && isset($_POST["label"]) && $_POST["label"] != ""){
    $result = add_role( $_POST["value"], __(
        $_POST["label"] ),
        array(
            'read' => true, // true allows this capability
            'mr2app_discount'=>true,
        )
    );
    if($result){
        echo '<p style="color: green;">' . _e('The role was recorded correctly.', 'woo2app') . '</p>';
    }
    else{
        echo '<p style="color: red;">' . _e('An error occurred, please enter the correct value .', 'woo2app') . '</p>';
    }
}
}

$roles = wp_roles();

//var_dump($roles);
//return;
?>

<h2> <?php _e('New Role', 'woo2app'); ?> </h2>
<form action="" method="post">
    <table class="form-table ">
        <tbody>
        <tr valign="top" class="">
            <th scope="row" class="titledesc">
                <label> <?php _e('Slug', 'woo2app'); ?></label>
            </th>
            <td class="forminp">
                <input type="text" class="regular-text" name="value" id="value" required placeholder=" <?php _e('Slug - Unique', 'woo2app'); ?>">
            </td>
        </tr>

        <tr valign="top" class="">
            <th scope="row" class="titledesc">
                <label> <?php _e('Title', 'woo2app'); ?> </label>
            </th>
            <td class="forminp">
                <input type="text" class="regular-text" name="label" id="label" placeholder="<?php _e('Role Title', 'woo2app'); ?>">
            </td>
        </tr>
        <tr>
            <th scope="row" class="titledesc">

            </th>
            <td class="forminp">
                <input type="submit" value="<?php _e('Save', 'woo2app'); ?>" name="submit_banner">
            </td>
        </tr>
        </tbody>
    </table>
</form>

<table class="wp-list-table widefat striped">
    <tbody>
    <tr valign="top" class="">
        <th scope="row" class="">
            <label > #  </label>
        </th>
        <th scope="row" class="">
            <label> <?php _e('Role Tag', 'woo2app'); ?>  </label>
        </th>
        <th scope="row" class="">
            <label>  <?php _e('Delete', 'woo2app'); ?>  </label>
        </th>
    </tr>
    <?php
    foreach ($roles->roles as $r => $rr){
        //if($r['value'] == )
        //var_dump($rr);continue;
       // $role = get_role( $r['value'] );
        //va
        $i = 0;
        foreach ($rr['capabilities'] as $cap => $val){
            if($cap == 'mr2app_discount'){
                $i ++;
                //var_dump($r);
                ?>
                <tr class="">
                    <td scope="row" class="">
                        <label > <?= $i; ?>  </label>
                    </td>
                    <td scope="row" class="">
                        <label > <?= $rr['name'];?>  </label>
                    </td>
                    <td scope="row" class="">
                        <a href="<?= $current_url . '&action=delete&field=' . $r ?>"
                           style="color: red;">  <?php _e('Delete', 'woo2app'); ?> </a>
                    </td>
                </tr>
                <?php
            }
        }
        ?>

        <?php
    }
    ?>
    </tbody>
</table>
