<?php echo $this->extend('template/admin/main') ?>

<?php echo $this->section('content') ?>
    <?php echo $this->include('common/message') ?>
    
    <div class="card mb-4">
        <div class="card-body">

            <form action="<?php echo base_url(route_to('update-role', $role->id)) ?>" id="roleedit" method="post" class="row g-3" accept-charset="utf-8" enctype="multipart/form-data">
                <?php echo $this->include('common/securityupdate') ?>

                <div class="row justify-content-center">
                    <div class="col-12 col-md-6 col-lg-4">
                        <div class="row">
                            <?php if($logged_role == "1"){ ?>
                                <div class="col-12 mb-2">
                                    <label class="form-group mt-3" for="">
                                        <?php echo lang("Localize.role") ?> <?php echo lang("Localize.type") ?>
                                    </label>

                                    <div class="form-check">
                                        <input class="form-check-input" type="radio" name="role_type" id="role_type1" value="global" <?php if($role->role_type == "global"){ echo "checked"; } ?> >
                                        <label class="form-check-label" for="role_type1">
                                            <?php echo lang("Localize.global") ?>
                                        </label>
                                    </div>

                                    <div class="form-check">
                                        <input class="form-check-input" type="radio" name="role_type" id="role_type2" value="custome" <?php if($role->role_type == "custome"){ echo "checked"; } ?>>
                                        <label class="form-check-label" for="role_type2">
                                            <?php echo lang("Localize.custome") ?>
                                        </label>
                                    </div>
                                </div>

                                <div class="col-12 mb-2" id="company_div" style="<?php if($role->role_type == "global"){ echo "display:none;"; } ?>">
                                    <?php echo $this->include('common/filter/company') ?>
                                </div>
                            <?php } else if($logged_role == "7"){ ?>
                                <input type="hidden" name="company_id" id="company_id" value="<?php echo $logged_company_id; ?>" >
                                <input type="hidden" name="role_type" id="role_type" value="custome" >
                            <?php } ?>
                            <div class="col-12 mb-2">
                                <label for="name"><?php echo lang("Localize.role") ?> <?php echo lang("Localize.name") ?> <abbr title="Required field">*</abbr></label>
                                <input type="text" name="name" value="<?php echo esc(old('name') ?? $role->name) ?>" class="form-control" required />
                            </div>

                            <label class="form-label mt-3" for="">
                                <?php echo lang("Localize.status") ?>
                                <abbr title="Required field">*</abbr>
                            </label>

                            <?php if ($role->status == 1) : ?>
                                <div class="form-check">
                                    <input class="form-check-input" type="radio" name="status" id="status" value="1" checked>
                                    <label class="form-check-label" for="exampleRadios1">
                                        <?php echo lang("Localize.active") ?>
                                    </label>
                                </div>
                                <div class="form-check">
                                    <input class="form-check-input" type="radio" name="status" id="status" value="0">
                                    <label class="form-check-label" for="exampleRadios2">
                                        <?php echo lang("Localize.disable") ?>
                                    </label>
                                </div>
                            <?php else : ?>
                                <div class="form-check">
                                    <input class="form-check-input" type="radio" name="status" id="status" value="1">
                                    <label class="form-check-label" for="exampleRadios1">
                                        <?php echo lang("Localize.active") ?>
                                    </label>
                                </div>
                                <div class="form-check">
                                    <input class="form-check-input" type="radio" name="status" id="status" value="0" checked>
                                    <label class="form-check-label" for="exampleRadios2">
                                        <?php echo lang("Localize.disable") ?>
                                    </label>
                                </div>
                            <?php endif ?>

                            <div class="text-danger">
                                <?php if (isset($validation)) : ?>
                                    <?= $validation->listErrors(); ?>
                                <?php endif ?>
                            </div>

                            <br>
                            <div class="col-12 text-center">
                                <button type="submit" class="btn btn-success"><?php echo lang("Localize.submit") ?></button>
                            </div>

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

<?php echo $this->endSection() ?>
<?php echo $this->section('js') ?>
    
    <script type="text/javascript">
        $(document).ready(function () {
            
            $('input[name="role_type"]').change(function () {
                // Get the value of the selected radio button
                var selectedValue = $(this).val();
                if(selectedValue == "custome"){
                    
                    $('#company_id').val(""); 
                   // $('#company_id').attr('required', true);
                    $('label[for="company_id"]').html(function (_, currentHtml) {
                        return currentHtml + ' <abbr title="Required field">*</abbr>';
                    });
                    $("#company_div").show();

                } else {
                    
                    $('#company_id').val("");
                    $('#company_id').attr('required', false);
                    $("#company_div").hide();
                    $('label[for="company_id"] abbr').remove();
                }

            });
        });
    </script>
<?php echo $this->endSection() ?>