<template>
<div class="row">
    <div class="col">
        <div class="scroll">
            <table class="table mb-5" ref="table">
                <thead>
                <tr>
                    <th> </th>
                    <th>Name</th>
                    <th>Caption</th>
                    <th></th>
                </tr>
                </thead>
                <tbody>
                <tr v-for="row, n in rows" @mousedown="setCurrentRow(row)" :class="row==currentRow?'active-row':''">
                    <td>
                    <span v-if="row==currentRow">
                        <CopyPasteMenu :row="row" :clipboard="clipboard" @copy="doAction('copy', $event)" @paste="doAction('paste', $event)">⯈</CopyPasteMenu>
                    </span>
                    <span v-else>&nbsp;&nbsp;</span>
                    </td>
                    <td><input v-model="row.name" ref="name" @focus="currentField='name'" class="form-control form-control-sm"></td>
                    <td><input v-model="row.caption" ref="caption" @focus="currentField='caption'" class="form-control form-control-sm"></td>
                    <th nowrap>
                        <button class="btn btn-sm btn-outline-secondary me-1" @click="$emit('up', row)">⯅</button>
                        <button class="btn btn-sm btn-outline-secondary me-1" @click="$emit('down', row)">⯆</button>
                        <button class="btn btn-sm btn-outline-secondary btn-danger" @click="$emit('delete', row)">⮿</button>
                    </th>
                </tr>
                </tbody>
            </table>
            <div class="text-end">
                <button @click="$emit('add')" class="btn btn-sm btn-outline-secondary">Add nozzle</button>
            </div>
        </div>
    </div>
    <div class="col" v-if="isCurrent">
        <div class="card">
            <div class="card-header">Nozzle {{ currentRow.name }} (#{{ currentRow.id }})</div>
            <div class="card-body">

                <div class="mb-3">
                    <label class="form-label">Name</label>
                    <div class="row mb-2">
                        <div class="col">
                            <input v-model="currentRow.name" type="text" class="form-control form-control-sm">
                        </div>
                    </div>
                </div>

                <div class="mb-3">
                    <label class="form-label">Caption</label>
                    <div class="row mb-2">
                        <div class="col">
                            <input v-model="currentRow.caption" type="text" class="form-control form-control-sm">
                        </div>
                    </div>
                </div>

            </div>
        </div>

        <div class="card mt-1">
            <div class="card-header">Nozzle calibration table</div>
            <div class="card-body">
                <NozzlesCalibrateTable
                    :rows="currentRow.calibrationData"
                    :currentRow="currentCalibrateRow"
                    :machinePos="machinePos"
                    @set="currentCalibrateRow=$event"
                    @setPos="$emit('setPos', $event)">
                </NozzlesCalibrateTable>
                <button :disabled="disabled" class="btn btn-sm btn-outline-secondary m-1" @click="$emit('autocalibrate', currentRow)">Autocalibrate</button>
            </div>
        </div>

    </div>
</div>
</template>

<script>
    import tableMixin from '../js/rows_table.js'
    import NozzlesCalibrateTable from "../components/NozzlesCalibrateTable.vue"
    import CopyPasteMenu from "../components/CopyPasteMenu.vue"
    import deepMerge from '../js/deep_merge.js'

    export default {
        mixins: [tableMixin, deepMerge],
        props: {
            settings: Object,
            machineState: Object,
            machinePos: Object,
            disabled: Boolean
        },
        components: {
            NozzlesCalibrateTable,
            CopyPasteMenu
        },
        computed: {
            isCurrent() {
                return this.currentRow && (this.rows.indexOf( this.currentRow) > -1)
            }
        },
        data: function () {
            return {
                currentField: 'name',
                currentCalibrateRow: this.currentCalibrateRow,
                clipboard: this.clipboard
            }
        },
        methods: {
            doAction(event, row) {
                if (event == 'copy') {
                    this.clipboard = Object.assign({}, row)
                }

                if (event == 'paste') {
                    this.clipboard.id = row.id
                    row = this.deepMerge(row, this.clipboard)
                }
            }
        }
    }
</script>

<style scoped>
.active-row {
    background-color: #F6F6F6;
}

.scroll {
    max-height: 85vh;
    overflow: auto;
}
</style>