<div class="row">
    <div class="col-sm-11">
        <div class="page-title-box">
            <h2 class="page-title">Edit KDS User</h2>
        </div>
    </div>
    <div class="col-sm-1">
        <div class="page-title-box">
            <h2 class="page-title">
                <a href="<?= $this->Html->url(['action' => 'kds_users']) ?>"><button class='btn btn-danger' type="button">Back</button></a>
            </h2>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-sm-12">
        <div class="card">
            <div class="card-body">

                <?= $this->Form->create('Kds_user', ['type' => 'file', 'url' => ['controller' => 'Dashboard', 'action' => 'edit_kds_users']]) ?>


                <?= $this->Form->input('id', [
                    'type' => 'hidden',
                    'value' => $userDetail['Kds_user']['id']
                ]) ?>

                <div class="form-group row">
                    <div class="col-sm-8">
                        <?= $this->Form->input('username', [
                            'div'=>['class'=>'form-group'],
                            'label'=>['class'=>'form-label'],
                            'class'=>'form-control',
                            'required'=>true,
                            'value' => $userDetail['Kds_user']['username']
                        ]) ?>

                        <?= $this->Form->input('password', [
                            'div'=>['class'=>'form-group'],
                            'label'=>['class'=>'form-label'],
                            'class'=>'form-control',
                            'required'=>false, // optional password update
                            'placeholder' => '******',
                        ]) ?>

                        <?php
                            foreach ($locations as $location) {
                                $options[$location['Location']['id']] = $location['Location']['name'];
                            }

                            echo $this->Form->input('location', [
                                'div'=>['class'=>'form-group'],
                                'label'=>'Select Location',
                                'class'=>'form-control',
                                'options'=>$options,
                                'id'=>'location',
                                'required'=>true,
                                'selected' => $userDetail['Kds_user']['location_id']
                            ]);

                            $roleOptions = ['Preparation' => 'Preparation', 'Expo' => 'Expo'];
                            echo $this->Form->input('role', [
                                'div'=>['class'=>'form-group'],
                                'label'=>'Select Role',
                                'class'=>'form-control',
                                'options'=>$roleOptions,
                                'id'=>'role',
                                'required'=>true,
                                'selected' => $userDetail['Kds_user']['role']
                            ]);
                        ?>

                        <label><strong>Stations</strong></label>
                        <div id="stationContainer" class="row mb-2">
                            <?php 
                                foreach($stations as $station){
                                $id = $station['Pos_printer']['id'];
                                $name = $station['Pos_printer']['name'];  
                                ?>
                                <div class="col-md-3">
                                    <input type="checkbox" name="station_ids[<?=$id?>]" value="<?=$id?>" id="station_<?=$id?>" <?= in_array($id, $selectedStations) ? 'checked' : '' ?>>
                                    <label for="station_<?=$id?>"><?=$name?></label>
                                </div>
                            <?php } ?>
                        </div>
                    </div>
                </div>

                <a href="<?= $this->Html->url(['action' => 'kds_users']) ?>"><button class='btn btn-danger' type="button">Cancel</button></a>
                <?= $this->Form->button('Update', ['class'=>'btn btn-success', 'type'=>'submit', 'id'=>'submitBtn']); ?>
                <?= $this->Form->end(); ?>
            </div>
        </div>
    </div>
</div>

