<template>
    <div>
        <b-form-group class="container">
            <section class="section">


                <h1 class="title">{{ $t('Manage_recorders') }}</h1>
                <p class="lead">
                    {{ $t('List_create_and_delete') }} <strong>{{ $t('recorders') }}</strong> {{$t('and')}} {{ $t('recorder_models')
                    }}
                </p>
                <br/>

                <b-tabs v-model="tabIndex" card justified>
                    <b-tab title="Recorder list" active>
                        <template slot="title">
                            <font-awesome-icon icon="list"/>&nbsp;<font-awesome-icon icon="circle"/>
                            <strong>{{$t('Recorders')}}</strong>&nbsp;{{$t('list')}}
                        </template>
                        <p>{{ $tc('recorders_defined', recorders.length, {num: recorders.length})}}:</p>
                        <b-card-group deck>
                            <b-card class="mb-2" style="max-width: 30rem; min-width:20rem;"
                                    v-for="(recorder) in recorders"
                                    :header="recorder.name + ' (' + recorder.ip + ' / ' + recorder.network_name + ')'"
                                    v-bind:key="recorder.id">
                                <b-card-text>
                                    <h5 class="card-title"><strong>{{ $t('name') }}:&nbsp;</strong>
                                        <span v-if="!formEditField[recorder.id+'_name']">{{recorder.name}}&nbsp;
                                        <a class="float-right badge badge-pill badge-primary">
                                            <font-awesome-icon
                                                    @click="initRecorderUpdate(recorder, 'name')"
                                                    icon="pencil-alt"/>
                                        </a>
                                    </span>
                                        <b-input-group v-else>
                                            <b-form-input :id="recorder.id+'_name'" name="name"
                                                          v-model="updateValues[recorder.id+'_name']"
                                                          v-validate="'required|min:3'"
                                                          v-bind:class="{'is-danger': errors.has('name'), 'is-invalid': errors.has('name')}"
                                                          class="form-control" type="text"
                                                          :placeholder="'Name ('+recorder.name +')'"
                                                          @blur="updateRecorder(recorder.id, 'name')"
                                                          @keyup.enter="updateRecorder(recorder.id, 'name')"
                                                          required></b-form-input>
                                            <b-input-group-append>
                                                <b-button :disabled="errors.has('name')"
                                                          @click="updateRecorder(recorder.id, 'name')"
                                                          variant="outline-success">
                                                    <font-awesome-icon icon="check"></font-awesome-icon>
                                                </b-button>
                                            </b-input-group-append>
                                        </b-input-group>
                                    </h5>
                                    <hr/>
                                    <p class="card-text"><strong>{{ $t('Model') }}:</strong>&nbsp;
                                        {{recorder.model_name}}&nbsp;
                                    </p>
                                    <p class="card-text"><strong>{{ $t('firmware_version') }}:</strong>&nbsp;
                                        {{recorder.firmware_version}}&nbsp;
                                    </p>
                                    <p class="card-text"><strong>{{ $t('serial_number') }}:</strong>&nbsp;
                                        {{recorder.serial_number}}&nbsp;
                                    </p>
                                    <p class="card-text"><strong>{{ $t('network_name') }}:</strong>&nbsp;
                                        <span v-if="!formEditField[recorder.id+'_network_name']">{{recorder.network_name}}&nbsp;
                                        <a class="float-right badge badge-pill badge-info" @click="initRecorderUpdate(recorder, 'network_name')">
                                            <font-awesome-icon
                                                    icon="pencil-alt"/>
                                        </a>
                                    </span>
                                        <b-input-group v-else size="sm">
                                            <b-form-input name="network_name"
                                                          style="min-width: 80%;"
                                                          v-model="updateValues[recorder.id+'_network_name']"
                                                          v-validate="'required|ip_or_fqdn'"
                                                          v-bind:class="{'is-danger': errors.has('network_name'), 'is-invalid': errors.has('network_name')}"
                                                          class="form-control" type="text"
                                                          :placeholder="'Network name ('+recorder.network_name +')'"
                                                          @keyup.enter="updateRecorder(recorder.id, 'network_name')"></b-form-input>
                                            <b-input-group-append>
                                                <b-button :disabled="errors.has('network_name')"
                                                          @click="updateRecorder(recorder.id, 'network_name')"
                                                          variant="outline-success">
                                                    <font-awesome-icon icon="check"></font-awesome-icon>
                                                </b-button>
                                            </b-input-group-append>
                                            <span>{{ errors.first('network_name') }}</span>
                                        </b-input-group>
                                    </p>

                                    <p class="card-text"><strong>{{ $t('ip') }}:</strong>&nbsp;
                                        <span v-if="!formEditField[recorder.id+'_ip']">{{recorder.ip}}&nbsp;
                                        <a class="float-right badge badge-pill badge-info" @click="initRecorderUpdate(recorder, 'ip')">
                                            <font-awesome-icon
                                                    icon="pencil-alt"/>
                                        </a>
                                    </span>
                                        <b-input-group v-else size="sm">
                                            <b-form-input name="ip"
                                                          v-model="updateValues[recorder.id+'_ip']"
                                                          v-validate="'ip'"
                                                          v-bind:class="{'is-danger': errors.has('ip'), 'is-invalid': errors.has('ip')}"
                                                          class="form-control" type="text"
                                                          :placeholder="$t('ip address') +' ('+recorder.ip +')'"
                                                          @keyup.enter="updateRecorder(recorder.id, 'ip')"
                                                          required></b-form-input>
                                            <b-input-group-append>
                                                <b-button :disabled="errors.has('ip')"
                                                          @click="updateRecorder(recorder.id, 'ip')"
                                                          variant="outline-success">
                                                    <font-awesome-icon icon="check"></font-awesome-icon>
                                                </b-button>
                                            </b-input-group-append>
                                        </b-input-group>
                                        <span>{{ errors.first('ip') }}</span>
                                    </p>

                                    <p class="card-text"><strong>{{ $t('ip v6') }}:</strong>&nbsp;
                                        <span v-if="!formEditField[recorder.id+'_ip6']">{{recorder.ip6}}&nbsp;
                                        <a class="float-right badge badge-pill badge-info" @click="initRecorderUpdate(recorder, 'ip6')">
                                            <font-awesome-icon
                                                    icon="pencil-alt"/>
                                        </a>
                                    </span>
                                        <b-input-group v-else size="sm">
                                            <b-form-input name="ip6"
                                                          v-model="updateValues[recorder.id+'_ip6']"
                                                          v-validate="'ip:6'"
                                                          v-bind:class="{'is-danger': errors.has('ip6'), 'is-invalid': errors.has('ip6')}"
                                                          class="form-control" type="text"
                                                          :placeholder="$t('ip v6 address') +' ('+recorder.ip6 +')'"
                                                          @keyup.enter="updateRecorder(recorder.id, 'ip6')"
                                                          required></b-form-input>
                                            <b-input-group-append>
                                                <b-button :disabled="errors.has('ip6')"
                                                          @click="updateRecorder(recorder.id, 'ip6')"
                                                          variant="outline-success">
                                                    <font-awesome-icon icon="check"></font-awesome-icon>
                                                </b-button>
                                            </b-input-group-append>
                                        </b-input-group>
                                        <span>{{ errors.first('ip6') }}</span>
                                    </p>

                                    <p class="card-text"><strong>{{ $t('ssh_port') }}:</strong>&nbsp;
                                        <span v-if="!formEditField[recorder.id+'_ssh_port']">{{recorder.ssh_port}}&nbsp;
                                        <a class="float-right badge badge-pill badge-info" @click="initRecorderUpdate(recorder, 'ssh_port')">
                                            <font-awesome-icon
                                                    icon="pencil-alt"/>
                                        </a>
                                    </span>
                                        <b-input-group v-else size="sm">
                                            <b-form-input name="ssh_port"
                                                          v-model="updateValues[recorder.id+'_ssh_port']"
                                                          v-validate="'required|between:0,65535'"
                                                          v-bind:class="{'is-danger': errors.has('ssh_port'), 'is-invalid': errors.has('ssh_port')}"
                                                          class="form-control" type="text"
                                                          :placeholder="'Room number ('+recorder.ssh_port +')'"
                                                          @blur="updateRecorder(recorder.id, 'ssh_port')"
                                                          @keyup.enter="updateRecorder(recorder.id, 'ssh_port')"
                                                          required></b-form-input>
                                            <b-input-group-append>
                                                <b-button :disabled="errors.has('ssh_port')"
                                                          @click="updateRecorder(recorder.id, 'ssh_port')"
                                                          variant="outline-success">
                                                    <font-awesome-icon icon="check"></font-awesome-icon>
                                                </b-button>
                                            </b-input-group-append>
                                        </b-input-group>
                                        <span>{{ errors.first('ssh_port') }}</span>
                                    </p>

                                    <p class="card-text"><strong>{{ $t('telnet_port') }}:</strong>&nbsp;
                                        <span v-if="!formEditField[recorder.id+'_telnet_port']">{{recorder.telnet_port}}&nbsp;
                                        <a class="float-right badge badge-pill badge-info">
                                            <font-awesome-icon
                                                    @click="initRecorderUpdate(recorder, 'telnet_port')"
                                                    icon="pencil-alt"/>
                                        </a>
                                    </span>
                                        <b-input-group v-else size="sm">
                                            <b-form-input name="telnet_port"
                                                          v-model="updateValues[recorder.id+'_telnet_port']"
                                                          v-validate="'required|between:0,65535'"
                                                          v-bind:class="{'is-danger': errors.has('telnet_port'), 'is-invalid': errors.has('telnet_port')}"
                                                          class="form-control" type="text"
                                                          :placeholder="'Room number ('+recorder.telnet_port +')'"
                                                          @blur="updateRecorder(recorder.id, 'telnet_port')"
                                                          @keyup.enter="updateRecorder(recorder.id, 'telnet_port')"
                                                          required></b-form-input>
                                            <b-input-group-append>
                                                <b-button :disabled="errors.has('telnet_port')"
                                                          @click="updateRecorder(recorder.id, 'telnet_port')"
                                                          variant="outline-success">
                                                    <font-awesome-icon icon="check"></font-awesome-icon>
                                                </b-button>
                                            </b-input-group-append>
                                        </b-input-group>
                                        <span>{{ errors.first('telnet_port') }}</span>
                                    </p>

                                    <hr/>
                                    <div v-if="recorder.room && !formEditField[recorder.id+'_room_id']">
                                        <p class="card-text"><strong>{{ $t('Room') }}:</strong>&nbsp;{{recorder.room.name}}&nbsp;
                                            <a class="float-right badge badge-pill badge-info" @click="initRecorderUpdate(recorder, 'room_id')">
                                                <font-awesome-icon
                                                        icon="pencil-alt"/>
                                            </a></p>
                                    </div>
                                    <div v-else>
                                        <p class="card-text"><strong>{{ $t('Room') }}:</strong>&nbsp;
                                            <span v-if="recorder.room && recorder.room.name">{{recorder.room.name}}</span>
                                            <span v-else>{{$t('no_room_defined')}}</span>
                                        </p>
                                        <div class="form-group row">
                                            <div class="col-sm-8">
                                                <select class="form-control"
                                                        v-model="updateValues[recorder.id+'_room_id']"
                                                        @change="updateRecorder(recorder.id, 'room_id')">
                                                    <option value="">{{$t('No room selected')}}</option>
                                                    <option v-for="room in rooms" v-bind:value="room.id">
                                                        {{ room.name }}
                                                    </option>
                                                </select>
                                            </div>
                                            <label class="label col-sm-4 col-form-label">{{ $t('room') }}</label>
                                        </div>
                                        <div class="form-check">
                                            <input class="form-check-input" type="checkbox"
                                                   v-model="show_assigned_rooms" id="assignedRoomsCheck">
                                            <label class="form-check-label" for="assignedRoomsCheck">
                                                Show already assigned rooms
                                            </label>
                                        </div>
                                    </div>

                                    <hr/>
                                    <b-form-group label-cols-sm="4"
                                                  label-cols-lg="3"
                                                  description="Set to offline / maintenance mode."
                                                  label-for="offline"
                                                  :label="$t('offline')">
                                        <b-form-checkbox :id="'offline' + recorder.id"
                                                         v-model="recorder.offline" name="check-button_offline" switch
                                                         @input="()=>{initRecorderUpdate(recorder, 'offline'); updateRecorder(recorder.id, 'offline')}">
                                            <b>({{$t('Offline')}}: {{ recorder.offline }})</b>
                                        </b-form-checkbox>
                                    </b-form-group>

                                    <b-form-group label-cols-sm="4"
                                                  label-cols-lg="3"
                                                  description="Lock recorder: No changes can be made."
                                                  label-for="locked"
                                                  :label="$t('locked')">
                                        <b-form-checkbox :id="'locked' + recorder.id"
                                                         v-model="recorder.locked" name="check-button_locked" switch
                                                         @input="()=>{initRecorderUpdate(recorder, 'locked'); updateRecorder(recorder.id, 'locked')}">
                                            <b>({{$t('Locked')}}: {{ recorder.locked }})</b>
                                        </b-form-checkbox>
                                    </b-form-group>

                                    <strong>{{ $t('lock_message') }}:</strong>&nbsp;
                                    <span v-if="!formEditField[recorder.id+'_lock_message']">{{recorder.lock_message}}&nbsp;
                                        <a class="float-right badge badge-pill badge-info">
                                            <font-awesome-icon
                                                    @click="initRecorderUpdate(recorder, 'lock_message')"
                                                    icon="pencil-alt"/>
                                        </a>
                                        </span>
                                    <textarea class="textarea form-control" v-else name="lock_message"
                                              v-model="updateValues[recorder.id+'_lock_message']"
                                              :placeholder="$t('lock_message') +' ('+recorder.lock_message +')'"
                                              @blur="updateRecorder(recorder.id, 'lock_message')"></textarea>

                                    <hr/>
                                    <div v-if="recorder.recorder_model">
                                        <p class="card-text"><strong>{{ $t('Model Adadpter') }}:</strong>&nbsp;{{recorder.recorder_model.name}}&nbsp;
                                            <a class="float-right badge badge-pill badge-info">
                                                <font-awesome-icon icon="pencil-alt"/>
                                            </a></p>
                                    </div>
                                    <div v-else>
                                        <p class="card-text"><strong>{{ $t('Model Adadpter') }}:</strong>&nbsp;{{
                                            $t('no_model_defined')}}</p>
                                        <div class="form-group row">
                                            <div class="col-sm-8">
                                                <select class="form-control" v-model="form.recorder_model">
                                                    <option value="">{{$t('No Model Adadpter selected')}}</option>
                                                    <option v-for="recorderModel in recorderModels"
                                                            v-bind:value="recorderModel.id">
                                                        {{ recorderModel.name }}
                                                    </option>
                                                </select>
                                            </div>
                                            <label class="label col-sm-4 col-form-label">{{ $t('model') }}</label>
                                        </div>
                                    </div>
                                    <hr/>

                                    <strong>{{ $t('description') }}:</strong>&nbsp;
                                    <span v-if="!formEditField[recorder.id+'_description']">{{recorder.description}}&nbsp;
                                        <a class="float-right badge badge-pill badge-info" @click="initRecorderUpdate(recorder, 'description')">
                                            <font-awesome-icon
                                                    icon="pencil-alt"/>
                                        </a>
                                        </span>
                                    <textarea class="textarea form-control" v-else name="description"
                                              v-model="updateValues[recorder.id+'_description']"
                                              :placeholder="$t('description') +' ('+recorder.description +')'"
                                              @blur="updateRecorder(recorder.id, 'description')"></textarea>

                                    <hr/>

                                    <strong>{{ $t('virtual_commands') }}:</strong>&nbsp;

                                    <b-list-group v-if="recorder.virtual_commands.length"
                                                  style="max-height: 400px; overflow-y:scroll;">
                                        <b-list-group-item v-for="command in recorder.virtual_commands"
                                                           v-bind:key="command.id">
                                            {{command.name}}
                                        </b-list-group-item>

                                    </b-list-group>
                                    <span>
                                        {{$t('No virtual commands defined yet.')}}
                                    </span>

                                </b-card-text>
                                <div slot="footer">
                                    <small class="text-muted">
                                        <p>{{ $t('created')}}: {{recorder.created_at | moment("dddd, MMMM Do YYYY")}}<br/>
                                            {{ $t('last_time_modified')}}: {{recorder.last_time_modified | moment("dddd, MMMM Do YYYY")}}<br/>
                                            <span class="pull-right">
                                            <button type="button" v-on:click="deleteRecorder(recorder.id)"
                                                    class="btn btn-sm btn-danger">{{
                                        $t('delete') }}&nbsp;<font-awesome-icon
                                                        icon="trash"/>
                                            </button>
                                        </span>
                                        </p>
                                    </small>
                                </div>
                            </b-card>
                        </b-card-group>
                    </b-tab>
                    <b-tab title="Create recorder">
                        <template slot="title">
                            <font-awesome-icon icon="plus"/>
                            <i>{{ $t('create') }}</i>&nbsp;<font-awesome-icon icon="circle"/>
                            <strong>{{ $t('Recorder') }}</strong>
                        </template>
                        <b-card-text>
                            <p>{{ $t('Create_a_new_recorder')}}:</p>
                            <!-- form starts here -->
                            <form v-on:submit.prevent="saveRecorder()">
                                <section class="form">

                                    <div class="form-group row">
                                        <label class="label required col-sm-2 col-form-label">{{ $t('name') }}</label>
                                        <div class="col-sm-6">
                                            <input name="name"
                                                   v-model="form.name"
                                                   v-validate="'required|min:3'"
                                                   v-bind:class="{'is-danger': errors.has('name'), 'is-invalid': errors.has('name')}"
                                                   class="form-control" type="text" :placeholder="$t('Recorder_name')"
                                                   required>
                                        </div>
                                        <p class="col-sm-4" v-show="errors.has('name')">
                                            {{ errors.first('name') }}
                                        </p>
                                    </div>


                                    <div class="form-group row">
                                        <label class="label col-sm-2 col-form-label">{{ $t('description') }}</label>
                                        <div class="col-sm-6">
                                        <textarea class="textarea form-control"
                                                  :placeholder="$t('Comments') +', ' + $t('notes') + ', etc.'"
                                                  v-model="form.description"></textarea>
                                        </div>
                                    </div>

                                    <div class="form-group row">
                                        <label class="label required col-sm-2 col-form-label">{{ $t('network_name') }}</label>
                                        <div class="col-sm-6">
                                            <input name="network_name"
                                                   v-model="form.network_name"
                                                   v-validate="'required|ip_or_fqdn'"
                                                   v-bind:class="{'is-danger': errors.has('network_name'), 'is-invalid': errors.has('network_name')}"
                                                   class="form-control" type="text" :placeholder="$t('network_name')"
                                                   required
                                            >
                                        </div>
                                        <p class="col-sm-4" v-show="errors.has('network_name')">
                                            {{ errors.first('network_name') }}
                                        </p>
                                    </div>

                                    <div class="form-group row">
                                        <label class="label col-sm-2 col-form-label">{{ $t('mac') }}</label>
                                        <div class="col-sm-6">
                                            <input name="mac"
                                                   v-validate="'mac'"
                                                   v-model="form.mac"
                                                   v-bind:class="{'is-danger': errors.has('mac'), 'is-invalid': errors.has('mac')}"
                                                   class="form-control" type="text" :placeholder="$t('Recorder') + ' ' + $t('mac')">
                                        </div>
                                        <p class="col-sm-4" v-show="errors.has('mac')">
                                            {{ errors.first('mac') }}
                                        </p>
                                    </div>

                                    <div class="form-group row">
                                        <label class="label col-sm-2 col-form-label">{{ $t('ip') }}</label>
                                        <div class="col-sm-6">
                                            <input name="ip"
                                                   v-validate="'ip'"
                                                   v-model="form.ip"
                                                   v-bind:class="{'is-danger': errors.has('ip'), 'is-invalid': errors.has('ip')}"
                                                   class="form-control" type="text" :placeholder="$t('Recorder') + ' ' + $t('ip')">
                                        </div>
                                        <p class="col-sm-4" v-show="errors.has('ip')">
                                            {{ errors.first('ip') }}
                                        </p>
                                    </div>

                                    <div class="form-group row">
                                        <label class="label col-sm-2 col-form-label">{{ $t('ipv6') }}</label>
                                        <div class="col-sm-6">
                                            <input name="ip6"
                                                   v-validate="'ip:6'"
                                                   v-model="form.ip6"
                                                   v-bind:class="{'is-danger': errors.has('ip6'), 'is-invalid': errors.has('ip6')}"
                                                   class="form-control" type="text" :placeholder="$t('Recorder') + ' ' + $t('ipv6')">
                                        </div>
                                        <p class="col-sm-4" v-show="errors.has('ip6')">
                                            {{ errors.first('ip6') }}
                                        </p>
                                    </div>

                                    <div class="form-group row">
                                        <label class="label col-sm-2 col-form-label">{{ $t('telnet_port') }}</label>
                                        <div class="col-sm-6">
                                            <input name="telnet_port"
                                                   v-model="form.telnet_port"
                                                   v-validate="'required|between:0,65535'"
                                                   v-bind:class="{'is-danger': errors.has('telnet_port'), 'is-invalid': errors.has('telnet_port')}"
                                                   class="form-control" type="number" value="22"
                                                   placeholder="Telnet Port (23)">
                                        </div>
                                        <p class="col-sm-4" v-show="errors.has('telnet_port')">
                                            {{ errors.first('telnet_port') }}
                                        </p>
                                    </div>

                                    <div class="form-group row">
                                        <label class="label col-sm-2 col-form-label">{{ $t('ssh_port') }}</label>
                                        <div class="col-sm-6">
                                            <input name="ssh_port"
                                                   v-model="form.ssh_port"
                                                   v-validate="'required|between:0,65535'"
                                                   v-bind:class="{'is-danger': errors.has('ssh_port'), 'is-invalid': errors.has('ssh_port')}"
                                                   class="form-control" type="number" value="23"
                                                   placeholder="SSH Port (22)">
                                        </div>
                                        <p class="col-sm-4" v-show="errors.has('ssh_port')">
                                            {{ errors.first('ssh_port') }}
                                        </p>
                                    </div>

                                    <div class="form-group row">
                                        <label class="label col-sm-2 col-form-label">{{ $t('Model') }}</label>
                                        <div class="col-sm-6">
                                            <select class="form-control" v-model="form.recorder_model_id">
                                                <option disabled value="">{{$t('No_recorder_model_selected')}}</option>
                                                <option v-for="recorderModel in recorderModels"
                                                        v-bind:value="recorderModel.id">
                                                    {{ recorderModel.name }}
                                                </option>
                                            </select>
                                        </div>
                                    </div>

                                    <div class="form-group row">
                                        <label class="label col-sm-2 col-form-label">{{ $t('room') }}</label>
                                        <div class="col-sm-6">
                                            <select class="form-control" v-model="form.room_id">
                                                <option disabled value="">Not assigned to room</option>
                                                <option v-for="room in rooms" v-bind:value="room.id">
                                                    {{ room.name }}
                                                </option>
                                            </select>
                                        </div>
                                    </div>

                                    <div class="form-group row">
                                        <label class="label col-sm-2 col-form-label">{{ $t('username') }}</label>
                                        <div class="col-sm-6">
                                            <input name="username"
                                                   v-model="form.username"
                                                   v-bind:class="{'is-danger': errors.has('username'), 'is-invalid': errors.has('username')}"
                                                   class="form-control" type="text" :placeholder="$t('username')"
                                            >
                                        </div>
                                        <p class="col-sm-4">
                                            {{$t('may_be_left_blank')}} (-> {{$t('Model')}})
                                        </p>
                                    </div>

                                    <div class="form-group row">
                                        <label class="label col-sm-2 col-form-label">{{ $t('password') }}</label>
                                        <div class="col-sm-6">
                                            <input name="password"
                                                   v-model="form.password"
                                                   v-bind:class="{'is-danger': errors.has('password'), 'is-invalid': errors.has('password')}"
                                                   class="form-control" type="text" :placeholder="$t('password')"
                                            >
                                        </div>
                                        <p class="col-sm-4">
                                            {{$t('may_be_left_blank')}} (-> {{$t('Model')}})
                                        </p>
                                    </div>


                                    <div class="field is-grouped">
                                        <div class="control">
                                            <button
                                                    v-bind:disabled="errors.any()"
                                                    type="submit"
                                                    class="btn btn-primary">
                                                Create recorder
                                            </button>
                                        </div>
                                    </div>

                                </section>
                            </form>
                        </b-card-text>
                    </b-tab>
                    <b-tab title="Recorder model list">
                        <template slot="title">
                            <font-awesome-icon icon="list"/>&nbsp;{{$t('Recorder')}}&nbsp;<font-awesome-icon icon="scroll"/>
                            <strong>{{$t('Model')}}</strong>&nbsp;<i>{{$t('list')}}</i>
                        </template>
                        <p>{{ $tc('recorder_models_defined', recorderModels.length, {num: recorderModels.length} ) }}:</p>
                        <b-card-group deck>
                            <b-card class="mb-2" style="max-width: 30rem; min-width:20rem;"
                                    v-for="(recorderModel) in recorderModels"
                                    :header="recorderModel.name"
                                    v-bind:key="recorderModel.id">
                                <b-card-text>
                                    <p class="card-text">
                                        <strong>{{ $t('notes') }}:</strong>&nbsp;
                                        <span v-if="!formEditField['model_'+recorderModel.id+'notes']">{{recorderModel.notes}}&nbsp;
                                        <a class="float-right badge badge-pill badge-info" @click="initrecorderModelUpdate(recorderModel, 'notes')">
                                            <font-awesome-icon
                                                    icon="pencil-alt"/>
                                        </a>
                                        </span>
                                        <textarea class="textarea form-control" v-else name="notes"
                                                  v-model="updateValues['model_'+recorderModel.id+'_notes']"
                                                  :placeholder="'Notes ('+recorderModel.notes +')'"
                                                  @blur="updaterecorderModel(recorderModel.id, 'notes')"></textarea>

                                    </p>

                                    <p class="card-text">
                                        <strong>{{ $t('requires_username') }}:</strong>&nbsp;<span
                                            v-if="recorderModel.requires_username">{{$t('yes')}}</span><span v-else>{{$t('no')}}</span>
                                    </p>
                                    <p class="card-text">
                                        <strong>{{ $t('requires_password') }}:</strong>&nbsp;<span
                                            v-if="recorderModel.requires_password">{{$t('yes')}}</span><span v-else>{{$t('no')}}</span>
                                    </p>

                                    <hr/>

                                    <b-list-group style="max-height: 400px; overflow-y:scroll;">
                                        <strong>Recorder Model {{ $t('commands') }}:</strong>&nbsp;
                                        <b-list-group-item v-for="command in recorderModel.commands"
                                                           v-bind:key="command.id">
                                            {{command.name}}
                                            <b-badge v-for="(a_type, arg) in command.parameters"
                                                     v-bind:key="arg"
                                                     v-if="command.parameters !== null" style="margin-right: 10px;">
                                                <small>{{arg}}:&nbsp;{{a_type}}</small>
                                            </b-badge>

                                        </b-list-group-item>
                                    </b-list-group>

                                </b-card-text>
                                <div slot="footer">
                                    <small class="text-muted">
                                        <p>{{ $t('created')}}: {{recorderModel.created_at | moment("dddd, MMMM Do YYYY")}}</p>
                                    </small>
                                </div>
                            </b-card>
                        </b-card-group>
                    </b-tab>
                </b-tabs>
            </section>
        </b-form-group>
        <hr>

        <div class="column">
            <section class="section" id="results">
                <div class="box">
                    <ul>
                        <!-- loop through all the `form` properties and show their values -->
                        <li v-for="(item, k) in form">
                            <strong>{{ k }}:</strong> {{ item }}
                        </li>
                    </ul>
                </div>
            </section>
        </div>

        <div class="column">
            <section class="section">
                {{recorders}}
            </section>
        </div>

    </div>
</template>

<script>
    import {EventBus} from '@/utils';
    import PulseLoader from 'vue-spinner/src/PulseLoader.vue';
    import getRepository from '@/api/RepositoryFactory';

    const recorderRepository = getRepository('recorder');

    export default {
        components: {
            PulseLoader,
        },
        props: [],
        data() {
            return {
                tabIndex: 0,
                updateValues: {},
                formEditField: {},
                show_assigned_recorders: false,
                show_assigned_rooms: true,
                form: {
                    name: '',
                    description: '',
                    network_name: '',
                    ip: '',
                    ip6: '',
                    telnet_port: 23,
                    ssh_port: 22,
                    locked: false,
                    offline: false,
                    username: '',
                    password: '',
                    recorder_model: null,
                    room: null,
                },
            };
        },
        methods: {
            initRecorderUpdate(recorder, fieldName) {
                this.$log.debug(recorder[fieldName]); // THIS SHOULD BE TRUE, NOT FALSE!! grml
                this.$set(this.formEditField, recorder.id + '_' + fieldName, true);
                this.$set(this.updateValues, recorder.id + '_' + fieldName, recorder[fieldName]);
            },
            updateRecorder(id, fieldName) {
                this.$log.debug(id);
                this.$log.debug(fieldName);
                this.$parent.$data.isLoading = true;
                const data = {};
                this.$log.debug(this.updateValues[id + '_' + fieldName]);
                data[fieldName] = this.updateValues[id + '_' + fieldName];
                this.$log.debug(data);
                recorderRepository.updateRecorder(id, data)
                    .then(() => {
                        this.$store.dispatch('loadRecorders')
                            .then(() => {
                                this.$parent.$data.isLoading = false;
                                this.formEditField[id + '_' + fieldName] = false;
                            });
                    });
            },
            saveRecorder() {
                this.$parent.$data.isLoading = true;
                recorderRepository.createRecorder(this.form)
                    .then(() => {
                        this.$store.dispatch('loadRecorders')
                            .then(() => {
                                this.$parent.$data.isLoading = false;
                                this.tabIndex = 0;
                            });
                    }).catch((msg) => {
                    this.showErrorMessage('Could not safe recorder!');
                    this.$log.warn(msg);
                });
            },
            deleteRecorder(id) {
                this.$parent.$data.isLoading = true;
                recorderRepository.deleteRecorder(id)
                    .then(() => {
                        this.$store.dispatch('loadRecorders')
                            .then(() => {
                                this.$parent.$data.isLoading = false;
                            });
                    });
            },
            showErrorMessage(msg) {
                this.$parent.$data.isLoading = false;
                this.$parent.$data.showAlert = true;
                this.$parent.$data.alertMessage = msg;
            },
        },
        mounted() {
            this.$parent.$data.isLoading = true;
            this.$parent.$data.showAlert = false;
            this.$store.dispatch('loadRecorders')
                .then(() => {
                    this.$store.dispatch('loadRooms')
                        .then(() => {
                            this.$parent.$data.isLoading = false;
                        });
                });
            this.$store.dispatch('loadRecorderModels');
        },
        computed: {
            rooms() {
                return this.$store.state.rooms;
            },
            recorders() {
                return this.$store.state.recorders;
            },
            recorderModels() {
                return this.$store.state.recorderModels;
            },
            recorderCommands() {
                return this.$store.state.recorderCommands;
            },
        },
    };
</script>

<style>
    .comment {
        white-space: pre-wrap;
    }

    .card:hover {
        background-color: whitesmoke;
    }

    .required:after {
        content: " *";
    }

    b-form-input {
        min-width: 80%;
    }
</style>
