document.addEventListener('alpine:init', () => {
    Alpine.data('settingsPage', () => ({
        // 0. Init dijalankan sebelum inisialisasi
        init(){

        },
        // 1. Inisialisasi Start
        showDialogAdd: false,
        showDialogEdit: false,
        objAddForm: {
            // idJenis 1
            port: '',
            idInterface: -1,
            namaInterface: '',
            idInstrument: -1,
            namaInstrument: '',
            // idJenis 2 ketambahan server
            server: '',
            // idJenis 3 
            idSerial: -1,
            namaSerial: '',
            speed: '',
            idParity: -1,
            namaParity: '',
            idDataBit: -1,
            namaDataBit: '',
            idStopBit: -1,
            namaStopBit: ''
        },
        dataSetting: [
            {
                idSetting: 1,
                idJenis: 1,
                namaJenis: 'TCP Server',
                info: 'Port 5001',
                port: '5001',
                idInterface: 1,
                namaInterface: 'XN550',
                idInstrument: 1,
                namaInstrument: 'Hema 01',
                server: '',
                idSerial: -1,
                namaSerial: '',
                speed: '',
                idParity: -1,
                namaParity: '',
                idDataBit: -1,
                namaDataBit: '',
                idStopBit: -1,
                namaStopBit: ''
            },
            {
                idSetting: 2,
                idJenis: 2,
                namaJenis: 'TCP Client',
                info: 'Server 192.168.0.1, Port 4000',
                port: '4000',
                idInterface: 2,
                namaInterface: 'Axsym',
                idInstrument: 2,
                namaInstrument: 'Axsym',
                server: '192.168.0.1',
                idSerial: 1,
                namaSerial: 'ttyS0',
                speed: '',
                idParity: -1,
                namaParity: '',
                idDataBit: -1,
                namaDataBit: '',
                idStopBit: -1,
                namaStopBit: ''
            },
            {
                idSetting: 3,
                idJenis: 3,
                namaJenis: 'RS232',
                info: 'ttyS01, 9600, N, 8, 1',
                port: '',
                idInterface: 3,
                namaInterface: 'C311',
                idInstrument: 3,
                namaInstrument: 'Cobas C311',
                server: '',
                idSerial: 1,
                namaSerial: 'ttyS0',
                speed: '19200',
                idParity: 1,
                namaParity: 'None',
                idDataBit: 2,
                namaDataBit: '6',
                idStopBit: 2,
                namaStopBit: '1'
            },
        ],
        selectedJenis: {
            idJenis: -1,
            namaJenis: 'Jenis'
        },
        openJenis: false,
        dataJenis: [
            {
                idJenis: 1,
                namaJenis: 'TCP Server',
            },
            {
                idJenis: 2,
                namaJenis: 'TCP Client',
            },
            {
                idJenis: 3,
                namaJenis: 'RS232',
            },
        ],
        selectedInterface: {
            idInterface: -1,
            namaInterface: 'Interface'
        },
        openInterface: false,
        dataInterface: [
            {
                idInterface: 1,
                namaInterface: 'XN550',
            },
            {
                idInterface: 2,
                namaInterface: 'Axsym',
            },
            {
                idInterface: 3,
                namaInterface: 'C311',
            },
        ],
        selectedInstrument: {
            idInstrument: -1,
            namaInstrument: 'Instrument'
        },
        openInstrument: false,
        dataInstrument: [
            {
                idInstrument: 1,
                namaInstrument: 'Hema 01',
            },
            {
                idInstrument: 2,
                namaInstrument: 'Axsym',
            },
            {
                idInstrument: 3,
                namaInstrument: 'Cobas C311',
            },
        ],
        selectedSerial: {
            idSerial: -1,
            namaSerial: 'Serial'
        },
        openSerial: false,
        dataSerial: [
            {
                idSerial: 1,
                namaSerial: 'ttyS0'
            },
            {
                idSerial: 2,
                namaSerial: 'psx3'
            },
        ],
        selectedParity: {
            idParity: -1,
            namaParity: 'Parity'
        },
        openParity: false,
        dataParity: [
            {
                idParity: 1,
                namaParity: 'None'
            },
            {
                idParity: 2,
                namaParity: 'All'
            },
        ],
        selectedDataBit: {
            idDataBit: -1,
            namaDataBit: 'DataBit'
        },
        openDataBit: false,
        dataDataBit: [
            {
                idDataBit: 1,
                namaDataBit: '1'
            },
            {
                idDataBit: 2,
                namaDataBit: '6'
            },
        ],
        selectedStopBit: {
            idStopBit: -1,
            namaStopBit: 'StopBit'
        },
        openStopBit: false,
        dataStopBit: [
            {
                idStopBit: 1,
                namaStopBit: '0'
            },
            {
                idStopBit: 2,
                namaStopBit: '1'
            },
        ],

        // 1. Inisialisasi End
        // 2. Fungsi Start
        closeDialog() {
            this.openInterface = false;
            this.openInstrument = false;
            this.openSerial = false;
            this.openParity = false;
            this.openDataBit = false;
            this.openStopBit = false;
            this.selectedJenis = {
                idJenis: -1,
                namaJenis: 'Jenis'
            };
            this.selectedInterface = {
                idInterface: -1,
                namaInterface: 'Interface'
            };
            this.selectedInstrument = {
                idInstrument: -1,
                namaInstrument: 'Instrument'
            }
            this.selectedSerial = {
                idSerial: -1,
                namaSerial: 'Serial'
            },
            this.selectedParity = {
                idParity: -1,
                namaParity: 'Parity'
            },
            this.selectedDataBit = {
                idDataBit: -1,
                namaDataBit: 'DataBit'
            },
            this.selectedStopBit = {
                idStopBit: -1,
                namaStopBit: 'StopBit'
            },
            this.showDialogAdd = false
        },
        onChangeJenis(item) {
            this.openJenis = false;
            this.selectedJenis = item;

            // clear form
            this.objAddForm = {
                port: '',
                idInterface: -1,
                namaInterface: '',
                idInstrument: -1,
                namaInstrument: '',
                server: '',
                idSerial: -1,
                namaSerial: '',
                speed: '',
                idParity: -1,
                namaParity: '',
                idDataBit: -1,
                namaDataBit: '',
                idStopBit: -1,
                namaStopBit: ''
            };

            // clear selected
            this.selectedInterface = {
                idInterface: -1,
                namaInterface: 'Interface'
            };
            this.selectedInstrument = {
                idInstrument: -1,
                namaInstrument: 'Instrument'
            }
            this.selectedSerial = {
                idSerial: -1,
                namaSerial: 'Serial'
            },
            this.selectedParity = {
                idParity: -1,
                namaParity: 'Parity'
            },
            this.selectedDataBit = {
                idDataBit: -1,
                namaDataBit: 'DataBit'
            },
            this.selectedStopBit = {
                idStopBit: -1,
                namaStopBit: 'StopBit'
            }
        },
        onChangeInterface(item) {
            this.openInterface = false;
            this.selectedInterface = item;
        },
        onChangeInstrument(item) {
            this.openInstrument = false;
            this.selectedInstrument = item;
        },
        onChangeSerial(item) {
            this.openSerial = false;
            this.selectedSerial = item;
        },
        onChangeParity(item) {
            this.openParity = false;
            this.selectedParity = item;
        },
        onChangeDataBit(item) {
            this.openDataBit = false;
            this.selectedDataBit = item;
        },
        onChangeStopBit(item) {
            this.openStopBit = false;
            this.selectedStopBit = item;
        },
        openModalEdit(item) {
            this.selectedJenis = {
                idJenis: item.idJenis,
                namaJenis: item.namaJenis,
            },
            this.port = item.port
            this.server = item.server
            this.selectedInterface = {
                idInterface: item.idInterface,
                namaInterface: item.namaInterface
            }
            this.selectedInstrument = {
                idInstrument: item.idInstrument,
                namaInstrument: item.namaInstrument
            }
            this.selectedSerial = {
                idSerial: item.idSerial,
                namaSerial: item.namaSerial
            },
            this.speed = item.speed
            this.selectedParity = {
                idParity: item.idParity,
                namaParity: item.namaParity
            },
            this.selectedDataBit = {
                idDataBit: item.idDataBit,
                namaDataBit: item.namaDataBit
            },
            this.selectedStopBit = {
                idStopBit: item.idStopBit,
                namaStopBit: item.namaStopBit
            },

            this.showDialogEdit = !this.showDialogEdit;
        },
        closeDialogEdit() {
            this.showDialogEdit = !this.showDialogEdit;
        },
        editMobilePage(item) {
            var json = JSON.stringify(item);
            localStorage.setItem('itemEditMobile', json);
            window.location.href = "settings_mobile_edit.html"
        },
        // 2. Fungsi End
    }))
})