<template>
    <v-layout>
        <!-- Alert dialog -->
        <v-dialog v-model="dialogdeletealert" max-width="30%">
            <v-card>
                <v-card-title class="headline grey lighten-2 pt-2 pb-2" primary-title>
                    Peringatan !
                </v-card-title>
                <v-card-text class="pt-2 pb-2">
                    <v-layout row>
                        <v-flex xs12 d-flex>
                            <v-layout row>
                                <v-flex pb-1 xs12>
                                    <v-layout row>
                                        <v-flex pt-2 pr-2 xs12>
                                            {{ msgalert }}
                                        </v-flex>
                                    </v-layout>
                                </v-flex>
                            </v-layout>
                        </v-flex>
                    </v-layout>
                </v-card-text>
                <v-divider></v-divider>
                <v-card-actions>
                    <v-spacer></v-spacer>
                    <v-btn color="primary" flat @click="dialogdeletealert = false">
                        Tutup
                    </v-btn>
                    <v-btn color="primary" flat @click="deleteRow()">
                        Yakin lah
                    </v-btn>
                </v-card-actions>
            </v-card>
        </v-dialog>
        <!-- End alert dialog -->
        <!-- ERROR DIALOG -->
        <v-dialog v-model="dialog_error" max-width="500px">
            <v-card>
                <v-card-title>
                    <span>ERROR !</span>
                    <v-spacer></v-spacer>
                </v-card-title>
                <v-divider></v-divider>
                <div class="ma-3 red--text">{{ msgError }}</div>
                <v-divider></v-divider>
                <v-card-actions>
                    <v-spacer></v-spacer>
                    <v-btn color="primary" flat @click="dialog_error = false">Close</v-btn>
                </v-card-actions>
            </v-card>
        </v-dialog>
        <!-- END ERROR DIALOG -->
        <!-- Snackbar -->
        <v-snackbar v-model="snackbar" :timeout="5000" :multi-line="false" :vertical="false" :top="true" :color="clr"
            :value="snackbar">
            {{ msgsnackbar }}
            <v-btn flat @click="snackbar = false"> Tutup </v-btn>
        </v-snackbar>
        <!-- End Snackbar -->

        <!-- Form Dialog -->
        <v-dialog v-model="dialog_form" persistent width="50%">
            <v-card>
                <v-card-title>
                    <span class="title">Form Input Kelainan Lab</span>
                </v-card-title>
                <v-divider></v-divider>
                <v-progress-linear v-if="loading_save" :indeterminate="true"></v-progress-linear>
                <v-card-text class="pt-3 pb-0">
                    <v-layout row wrap>
                        <v-flex xs12>
                           <v-autocomplete :disabled="isEdit" label="Nama Pemeriksaan*" :items="xtests" v-model="vtest"
                                :search-input.sync="search_item_test"
                                auto-select-first no-filter item-text="codeNameTest" return-object
                                no-data-text="Pilih Nama Pemeriksaan" clearable
                                :error="test_error_message !== ''" 
                                :error-messages="test_error_message"
                                @blur="testValidation">
                                <template slot="item" slot-scope="{ item }">
                                    <v-list-tile-content>
                                        <v-list-tile-title v-text="item.codeNameTest"></v-list-tile-title>
                                    </v-list-tile-content>
                                </template>
                           </v-autocomplete>
                        </v-flex>
                    </v-layout>
                </v-card-text>

                <v-card-text class="pt-0 pb-0">
                    <v-layout row wrap>
                        <v-flex xs12>
                           <v-autocomplete :disabled="isEdit" label="Kelainan*" :items="xdatakelainans" v-model="vdatakelainan"
                                :search-input.sync="search_item_datakelainan"
                                auto-select-first no-filter item-text="Mcu_KelainanName" return-object
                                no-data-text="Pilih Nama Kelainan" clearable
                                :error="kelainan_error_message !== ''" 
                                :error-messages="kelainan_error_message"
                                @blur="kelainanValidation">
                                <template slot="item" slot-scope="{ item }">
                                    <v-list-tile-content>
                                        <v-list-tile-title v-text="item.Mcu_KelainanName"></v-list-tile-title>
                                    </v-list-tile-content>
                                </template>
                           </v-autocomplete>
                        </v-flex>
                    </v-layout>
                </v-card-text>

                <v-card-text class="pt-0 pb-0">
                    <v-layout row wrap>
                        <v-flex xs12>
                            <v-switch
                                v-model="isNilaiNormal"
                                class="mt-0"
                                color="green lighten-2"
                                hide-details
                                label="Berdasarkan nilai normal"
                                @change="handleSwitchChange"
                            ></v-switch>
                        </v-flex>
                    </v-layout>
                </v-card-text>

                <v-card-text class="pt-0 pb-0">
                    <v-layout wrap>
                        <v-flex xs12>
                            <v-radio-group xs12 v-model="xOperatorPerbandingan"
                                :error="operatorPerbandingan_error_message !== ''" 
                                :error-messages="operatorPerbandingan_error_message"
                                @blur="operatorValidation">
                                <template v-slot:label>
                                    <div>Operator Perbandingan</div>
                                </template>
                                <v-layout row>
                                    <v-flex xs4 wrap>
                                        <v-radio value="KD">
                                            <template v-slot:label>
                                            <div>Kurang dari</div>
                                            </template>
                                        </v-radio>
                                    </v-flex>
                                    <v-flex xs4 wrap>
                                        <v-radio value="LD">
                                            <template v-slot:label>
                                            <div>Lebih dari</div>
                                            </template>
                                        </v-radio>
                                    </v-flex>
                                    <v-flex xs4 wrap>
                                        <v-radio value="SD">
                                            <template v-slot:label>
                                            <div>Sama dengan</div>
                                            </template>
                                        </v-radio>
                                    </v-flex>
                                </v-layout>
                                <v-layout row pt-2>
                                    <v-flex xs4 wrap>
                                        <v-radio value="KDSD">
                                            <template v-slot:label>
                                            <div>Kurang dari sama dengan</div>
                                            </template>
                                        </v-radio>
                                    </v-flex>
                                    <v-flex xs4 wrap>
                                        <v-radio value="LDSD">
                                            <template v-slot:label>
                                            <div>Lebih dari sama dengan</div>
                                            </template>
                                        </v-radio>
                                    </v-flex>
                                    <v-flex xs4 wrap>
                                        <v-radio value="TSD">
                                            <template v-slot:label>
                                            <div>Tidak sama dengan</div>
                                            </template>
                                        </v-radio>
                                    </v-flex>
                                </v-layout>
                            </v-radio-group>
                        </v-flex>
                    </v-layout>
                </v-card-text>

                <v-card-text class="pt-2 pb-0">
                    <v-layout row wrap>
                        <v-flex v-show="this.isNilaiNormal == false ? true : false">
                            <v-text-field outline v-model="nilaiPerbandingan" label="Nilai perbandingan" required
                                ></v-text-field>
                        </v-flex>
                    </v-layout>
                </v-card-text>

                <v-card-text class="pt-2 pb-2">
                    <v-layout row wrap>
                        <v-flex xs12>
                            <span style="color: red;" class="font-weight-bold">{{ message_existing }}</span>
                        </v-flex>
                    </v-layout>
                </v-card-text>

                <v-divider></v-divider>
                <v-card-actions>
                    <v-spacer></v-spacer>
                    <v-btn color="primary" flat @click="dialog_form = false" :disabled="loading_save">Tutup</v-btn>
                    <v-btn v-if="xact === 'add'" color="primary" flat :disabled="loading_save"
                        @click="saveAdd()">Simpan</v-btn>
                    <v-btn v-if="xact === 'edit'" color="primary" flat :disabled="loading_save" @click="saveEdit()">Simpan
                        Perubahan</v-btn>
                </v-card-actions>
            </v-card>
        </v-dialog>
        <!-- End Form Dialog --> 

        <v-flex xs12>
            <v-card class="mb-2" color="white">
                <v-toolbar color="blue lighten-3" dark height="50px">
                    <v-toolbar-title>Master Kelainan Lab</v-toolbar-title>
                    <v-spacer></v-spacer>
                    <v-btn @click="openForm(0)" icon>
                        <v-icon>library_add</v-icon>
                    </v-btn>
                </v-toolbar>
                <v-layout row style="background:white;padding-top:5px;padding-left: 2px;">
                    <v-layout row justify-left>
                        <v-flex xs3>
                            <v-text-field class="ma-1" label="Cari" placeholder="Cari Pemeriksaan atau Kelainan" outline
                                v-model="xsearch" hide-details></v-text-field>
                        </v-flex>
                    </v-layout>
                </v-layout>
                <v-divider></v-divider>
                <v-layout row class="scroll-container" style="max-height:645px;overflow: auto;">
                    <v-flex xs12 pl-2 pr-2 pt-2 pb-2>
                        <v-data-table :headers="headers" :items="xkelainanlab" :loading="isLoading" hide-actions class="elevation-1">
                            <template slot="items" slot-scope="props">
                                <td class="text-xs-center pa-2" v-bind:class="{ 'amber lighten-4': isSelected(props.item) }"
                                    @click="selectMe(props.item)">{{ props.item.rownumber }}
                                </td>
                                <td class="text-xs-left pa-2" v-bind:class="{ 'amber lighten-4': isSelected(props.item) }"
                                    @click="selectMe(props.item)">
                                    <p class="font-weight-bold mb-0" style="font-size: medium;">{{ props.item.codeNameTest }}</p>
                                    <p class="mb-0 dot-text">Kelainan : {{ props.item.Mcu_KelainanName }}</p>
                                    <p v-if="props.item.Mcu_SummaryLabIsNormalValue == 'Y'" class="mb-0 dot-text">Berdasarkan nilai normal</p>
                                    <p v-if="props.item.Mcu_SummaryLabIsNormalValue == 'N'" class="mb-0 dot-text">Tidak berdasarkan nilai normal</p>
                                    <p class="mb-0 dot-text">Operator perbandingan : {{ props.item.summarylabtype }}</p>
                                    <p class="mb-0 dot-text">Nilai perbandingan : {{ props.item.Mcu_SummaryLabValue }}</p>
                                </td>
                                <td class="text-xs-center pa-2" v-bind:class="{ 'amber lighten-4': isSelected(props.item) }"
                                    @click="selectMe(props.item)">
                                    <v-tooltip bottom>
                                            <template v-slot:activator="{ on, attrs }">
                                                <v-icon 
                                                    v-bind="attrs"
                                                    v-on="on" 
                                                    class="mr-3"
                                                    @click="openEdit(props.item)" 
                                                    color="primary">edit
                                                </v-icon>
                                            </template>
                                            <span>Edit</span>
                                    </v-tooltip>
                                    <v-tooltip bottom>
                                            <template v-slot:activator="{ on, attrs }">
                                                <v-icon 
                                                    v-bind="attrs"
                                                    v-on="on" 
                                                    @click="confirmDelete(props.item)" 
                                                    color="error">delete
                                                </v-icon>
                                            </template>
                                            <span>Hapus</span>
                                    </v-tooltip>
                                </td>
                            </template>
                        </v-data-table>
                    </v-flex>
                </v-layout>
                <v-divider></v-divider>
                <v-layout>
                    <v-flex>
                        <v-pagination style="margin-top:10px;margin-bottom:10px" v-model="curr_page" :length="xtotal_page">
                        </v-pagination>
                    </v-flex>
                </v-layout>

            </v-card>
        </v-flex>

    </v-layout>
</template>

<style scoped>
.dot-text {
    position: relative;
    padding-left: 1.5em; /* Menambahkan padding untuk memberi ruang pada titik */
}

.dot-text::before {
    content: '•'; /* Menambahkan titik di depan teks */
    position: absolute;
    left: 1em; /* Atur posisi titik */
    top: 0;
    transform: translateX(-100%);
}

.searchbox .v-input.v-text-field .v-input__slot {
    min-height: 60px;
}

.searchbox .v-btn {
    min-height: 60px;
}

table.v-table tbody td,
table.v-table tbody th {
    height: 40px;
}

table.v-table thead tr {
    height: 40px;
}

.textinput {
    -webkit-transition: width 0.4s ease-in-out;
    transition: width 0.4s ease-in-out;
    background-color: white;
    background-position: 10px 10px;
    background-repeat: no-repeat;
    padding-left: 40px;
    width: 100%;
    padding: 8px 10px;
    margin-bottom: 5px;
    box-sizing: border-box;
    border: 1px solid #607d8b;

}

.textinput:focus {
    width: 100%;
}

.textinput:focus::-webkit-input-placeholder {
    color: transparent;
}

.textinput:focus::-moz-placeholder {
    color: transparent;
}

.textinput:-moz-placeholder {
    color: transparent;
}

.scroll-container {
    scroll-padding: 50px 0 0 50px;
}

::-webkit-scrollbar {
    width: 7px;
}

/* this targets the default scrollbar (compulsory) */

::-webkit-scrollbar-track {
    background-color: #73baf3;
}

/* the new scrollbar will have a flat appearance with the set background color */

::-webkit-scrollbar-thumb {
    background-color: #2196f3;
}

/* this will style the thumb, ignoring the track */

::-webkit-scrollbar-button {
    background-color: #0079da;
}

/* optionally, you can style the top and the bottom buttons (left and right for horizontal bars) */

::-webkit-scrollbar-corner {
    background-color: black;
}
</style>

<script>
module.exports = {
    data() {
        return {
            search_item_test: "",
            search_item_datakelainan: "",
            xOperatorPerbandingan: "",
            nilaiPerbandingan: "",
            test_error_message: "",
            kelainan_error_message: "",
            operatorPerbandingan_error_message: "",
            clr: "success",
            xid: "",
            vtestcode: "",
            dialogdeletealert: false,
            msgalert: "",
            headers: [{
                text: "NO",
                align: "Center",
                sortable: false,
                value: "mr",
                width: "5%",
                class: "pa-2 blue lighten-3 white--text"
            },
            {
                text: "KELAINAN",
                align: "Center",
                sortable: false,
                value: "mr",
                width: "80%",
                class: "pa-2 blue lighten-3 white--text"
            },
            {
                text: "AKSI",
                align: "Center",
                sortable: false,
                value: "mr",
                width: "10%",
                class: "pa-2 blue lighten-3 white--text"
            }]

        };
    },
    mounted() {
        this.$store.dispatch("kelainan/search")
    },
    computed: {
        isEdit() {
            return this.xact === 'edit';
        },
        xkelainanlab() {
            return this.$store.state.kelainan.kelainanlab
        },
        isLoading() {
            return this.$store.state.kelainan.search_status == 1
        },
        curr_page: {
            get() {
                return this.$store.state.kelainan.current_page
            },
            set(val) {
                this.$store.commit("kelainan/update_current_page", val)
                this.$store.commit("kelainan/update_last_id", -1)
                this.$store.dispatch("kelainan/search")
            }
        },
        xtotal_page: {
            get() {
                return this.$store.state.kelainan.total_kelainanlab_page
            },
            set(val) {
                this.$store.commit("kelainan/update_total_kelainanlab_page", val)
            }
        },
        xsearch: {
            get() {
                return this.$store.state.kelainan.x_search
            },
            set(val) {
                this.$store.commit("kelainan/update_x_search", val)
            }
        },
        dialog_form: {
            get() {
                return this.$store.state.kelainan.dialog_form
            },
            set(val) {
                this.$store.commit("kelainan/update_dialog_form", val)
            },
        },
        loading_save: {
            get() {
                return this.$store.state.kelainan.loading_save
            },
            set(val) {
                this.$store.commit("kelainan/update_loading_save", val)
            },
        },
        xact: {
            get() {
                return this.$store.state.kelainan.act
            },
            set(val) {
                this.$store.commit("kelainan/update_act", val)
            }
        },
        xtests() {
            return this.$store.state.kelainan.tests
        },
        vtest: {
            get() {
                return this.$store.state.kelainan.selected_test
            },
            set(val) {
                this.$store.commit("kelainan/update_selected_test", val)
            }
        },
        xdatakelainans() {
            return this.$store.state.kelainan.datakelainans
        },
        vdatakelainan: {
            get() {
                return this.$store.state.kelainan.selected_datakelainan
            },
            set(val) {
                this.$store.commit("kelainan/update_selected_datakelainan", val)
            }
        },
        isNilaiNormal: {
            get() {
                return this.$store.state.kelainan.selected_isNilaiNormal
            },
            set(val) {
                this.$store.commit("kelainan/update_selected_isNilaiNormal", val)
            }
        },
        snackbar: {
            get() {
                return this.$store.state.kelainan.alert_success
            },
            set(val) {
                this.$store.commit("kelainan/update_alert_success", val)
            }
        },
        msgsnackbar() {
            return this.$store.state.kelainan.msg_success
        },
        dialog_error: {
            get() {
                return this.$store.state.kelainan.alert_error
            },
            set(val) {
                this.$store.commit("kelainan/update_alert_error", val)
            },
        },
        msgError() {
            return this.$store.state.kelainan.error_message
        },
        message_existing: {
            get() {
                return this.$store.state.kelainan.message_existing
            },
            set(val) {
                this.$store.commit("kelainan/update_message_existing", val)
            },
        }
    },
    methods: {
        isSelected(p) {
            return p.Mcu_SummaryLabID == this.$store.state.kelainan.selected_kelainanlab.Mcu_SummaryLabID
        },
        selectMe(selected) {
            this.$store.commit("kelainan/update_selected_kelainanlab", selected)
            this.$store.commit("kelainan/update_last_id", selected.Mcu_SummaryLabID)

            // set value selected test
            this.$store.commit("kelainan/update_tests", [
                {
                    Nat_TestID: selected.Nat_TestID,
                    codeNameTest: selected.codeNameTest,
                    Nat_TestCode: selected.Nat_TestCode
                }
            ])
            this.$store.commit("kelainan/update_selected_test", {
                Nat_TestID: selected.Nat_TestID,
                codeNameTest: selected.codeNameTest,
                Nat_TestCode: selected.Nat_TestCode
            })

            // set value selected kelainan
            this.$store.commit("kelainan/update_datakelainans", [
                {
                    Mcu_KelainanID: selected.Mcu_KelainanID,
                    Mcu_KelainanName: selected.Mcu_KelainanName
                }
            ])
            this.$store.commit("kelainan/update_selected_datakelainan", {
                Mcu_KelainanID: selected.Mcu_KelainanID,
                Mcu_KelainanName: selected.Mcu_KelainanName
            })
        },
        thr_search: _.debounce(function () {
            this.$store.dispatch("kelainan/search")
        }, 1000),
        thr_search_test: _.debounce(function () {
            this.$store.dispatch("kelainan/searchtest", this.search_item_test)
        }, 2000),
        thr_search_datakelainan: _.debounce(function () {
            this.$store.dispatch("kelainan/searchkelainan", this.search_item_datakelainan)
        }, 2000),
        testValidation() {
            if (_.isEmpty(this.vtest)) {
                this.test_error_message = 'Anda belum memilih pemeriksaan'
            }
            if (this.vtest) {
                this.test_error_message = ""
            }
        },
        kelainanValidation() {
            if (_.isEmpty(this.vdatakelainan)) {
                this.kelainan_error_message = 'Anda belum memilih kelainan'
            }
            if (this.vdatakelainan) {
                this.kelainan_error_message = ""
            }
        },
        operatorValidation() {
            if (_.isEmpty(this.xOperatorPerbandingan)) {
                this.operatorPerbandingan_error_message = 'Anda belum memilih operator perbandingan'
            }
            if (this.xOperatorPerbandingan) {
                this.operatorPerbandingan_error_message = ""
            }
        },
        resetAllInput() {
            this.vtest = ""
            this.vdatakelainan = ""
            this.xOperatorPerbandingan = ""
            this.isNilaiNormal = true
            this.nilaiPerbandingan = ""
            this.test_error_message = ""
            this.kelainan_error_message = ""
            this.operatorPerbandingan_error_message = ""
            this.message_existing = ""
        },
        resetAllError() {
            this.test_error_message = ""
            this.kelainan_error_message = ""
            this.operatorPerbandingan_error_message = ""
            this.message_existing = ""
        },
        openForm() {
            this.dialog_form = true
            this.$store.commit("kelainan/update_act", "add")
            this.$store.commit("kelainan/update_last_id", -1)
            this.resetAllInput()
        },
        handleSwitchChange() {
            if (this.isNilaiNormal) {
                this.nilaiPerbandingan = '';
            }
        },
        saveAdd() {
            this.testValidation()
            this.kelainanValidation()
            this.operatorValidation()
            if (this.test_error_message === "" && 
                this.operatorPerbandingan_error_message === "" &&
                this.kelainan_error_message === ""
            ) {
                let prm = {
                    testId: this.vtest.Nat_TestID,
                    testCode: this.vtest.Nat_TestCode,
                    kelainanId: this.vdatakelainan.Mcu_KelainanID,
                    type: this.xOperatorPerbandingan,
                    normalValue: this.isNilaiNormal === true ? 'Y' : 'N',
                    value: this.isNilaiNormal === true ? '' : this.nilaiPerbandingan
                }

                // console.log('cek prm',prm)
                this.$store.dispatch("kelainan/add", prm)
            }
        },
        openEdit(data) {
            let operator;
            
            switch (data.Mcu_SummaryLabType) {
                case "<":
                    operator = 'KD';
                    break;
                case ">":
                    operator = 'LD';
                    break;
                case "=":
                    operator = 'SD';
                    break;
                case "<=":
                    operator = 'KDSD';
                    break;
                case ">=":
                    operator = 'LDSD';
                    break;
                case "!=":
                    operator = 'TSD';
                    break;
                default:
                    operator = ''; // Atur default jika diperlukan
            }

            this.selectMe(data)
            this.dialog_form = true
            this.$store.commit("kelainan/update_act", "edit")
            this.xid = data.Mcu_SummaryLabID
            this.vtest = data.Nat_TestID
            this.vdatakelainan = data.Mcu_KelainanID
            this.xOperatorPerbandingan = operator
            this.isNilaiNormal = data.Mcu_SummaryLabIsNormalValue === 'Y' ? true : false
            this.nilaiPerbandingan = data.Mcu_SummaryLabValue
            this.$store.commit("kelainan/update_last_id", data.Mcu_SummaryLabID)
            this.resetAllError()
        },
        saveEdit() {
            this.testValidation()
            this.kelainanValidation()
            this.operatorValidation()
            if (this.test_error_message === "" && 
                this.operatorPerbandingan_error_message === "" &&
                this.kelainan_error_message === ""
            ) {
                let prm = {
                    xid: this.xid,
                    type: this.xOperatorPerbandingan,
                    normalValue: this.isNilaiNormal === true ? 'Y' : 'N',
                    value: this.isNilaiNormal === true ? '' : this.nilaiPerbandingan
                }
                this.$store.dispatch("kelainan/edit", prm)
            }
        },
        confirmDelete(data) {
            this.selectMe(data)
            this.xid = data.Mcu_SummaryLabID
            this.msgalert = "Yakin, mau hapus kelainan lab " + data.codeNameTest + " ?"

            this.dialogdeletealert = true
        },
        deleteRow() {
            this.$store.commit("kelainan/update_last_id", -1)
            let prm = {
                xid: this.xid
            }
            this.$store.dispatch("kelainan/deleterow", prm)
            this.dialogdeletealert = false
        }
    },
    watch: {
        xsearch(val, old) {
            this.xsearch = val
            this.thr_search()
        },

        search_item_test(val, old) {
            if (val == old) return
            if (!val) return

            if (val.length < 1) {
                this.$store.dispatch("kelainan/searchtest", this.search_item_test)
                return this.thr_search_test()
            }

            if (val.length > 2) {
                this.$store.dispatch("kelainan/searchtest", this.search_item_test)
                return this.thr_search_test()
            }
        },

        search_item_datakelainan(val, old) {
            if (val == old) return
            if (!val) return

            if (val.length < 1) {
                this.$store.dispatch("kelainan/searchkelainan", this.search_item_datakelainan)
                return this.thr_search_datakelainan()
            }

            if (val.length > 2) {
                this.$store.dispatch("kelainan/searchkelainan", this.search_item_datakelainan)
                return this.thr_search_datakelainan()
            }
        },
    }
}
</script>