<template>
    <div v-if="xselected_patient?.details">
        <v-container class="bg-white rounded-lg" fluid>
            <v-row class="justify-space-between">
                <v-col cols="6">
                    <p class="text-h6"><strong>{{ xselected_patient.noreg }}</strong></p>
                </v-col>
                <v-col cols="6" class="text-right">
                    <v-chip class="rounded-lg px-4 py-2">
                        {{ xselected_patient.statusresult }}
                    </v-chip>
                </v-col>
            </v-row>
            <v-row>
                <v-col cols="5">
                    <v-text-field :label="$t('message.tableDetailPatient.navbar.name')" variant="outlined" hide-details
                        :model-value="xselected_patient.patient_fullname && xselected_patient.gender ? `${xselected_patient.name} / ${xselected_patient.gender}` : ''"
                        readonly>
                    </v-text-field>
                </v-col>
                <v-col cols="2.5">
                    <v-text-field :label="$t('message.tableDetailPatient.navbar.age')" variant="outlined"
                        :model-value="xselected_patient.age ? `${xselected_patient.age}` : ''"
                        hide-details></v-text-field>
                </v-col>
                <v-col cols="2.5">
                    <v-text-field :label="$t('message.tableDetailPatient.navbar.sender')"
                        :model-value="xselected_patient.sender ? `${xselected_patient.sender}` : ''" variant="outlined"
                        hide-details></v-text-field>
                </v-col>
            </v-row>
            <v-row class="bg-secondary-lighten rounded-lg mx-1 h-100 my-2">
                <v-col cols="7" class="d-flex align-center space-between">
                    <div><v-chip class="bg-grey-lighten-3 text-black rounded-lg px-4" style="height: 40px;">{{
                        xselected_patient.test_name
                            }}</v-chip></div>
                    <div><v-chip class="bg-cyan-lighten-5 text-cyan-darken-1 rounded-lg px-4 mx-2"
                            style="height: 40px;">
                            {{ xselected_patient.details ? xselected_patient.details.doctor_fullname : '' }}
                        </v-chip></div>
                    <div>
                        <v-menu close-on-content-click offset-y transition="scale-transition">
                            <template #activator="{ props }">
                                <v-chip v-bind="props"
                                    class="bg-blue-lighten-5 text-blue-darken-2 rounded-lg px-4 cursor-pointer"
                                    style="height: 40px;">
                                    {{ selectedLang }}
                                </v-chip>
                            </template>
                            <v-list>
                                <v-list-item v-for="lang in xselected_patient.details.langs" :key="lang"
                                    @click="selectLanguage(lang)">
                                    <v-list-item-title>{{ lang.name }}</v-list-item-title>
                                </v-list-item>
                            </v-list>
                        </v-menu>
                    </div>
                    <div class="px-2">
                        <v-btn fab small style="height: 2.5rem;" class="bg-info rounded-lg ">
                            <iconify-icon style="font-size:  2rem; color: #FFF;"
                                icon="fluent:clipboard-3-day-24-regular"></iconify-icon>
                        </v-btn>
                    </div>
                </v-col>
                <v-col cols="5">
                    <div class="d-flex justify-end">
                        <div v-if="xselected_patient.details.status == 'NEW'">
                            <v-btn fab small style="height: 2.5rem;" class="bg-warning rounded-lg mr-2"
                                @click="confirmDialog = true">
                                <iconify-icon style="font-size:  2rem; color: #FFF;"
                                    icon="fluent:clipboard-task-24-regular"></iconify-icon></v-btn>
                            <v-btn fab small style="height: 2.5rem;" class="bg-primary rounded-lg"
                                @click="save"><iconify-icon style="font-size:  2rem; color: #FFF;"
                                    icon="fluent:save-32-regular"></iconify-icon></v-btn>
                        </div>
                        <div v-if="xselected_patient.details.status == 'VAL1'">
                            <v-btn fab small style="height: 2.5rem;" class="bg-warning rounded-lg mr-2"
                                @click="confirm('yes')">
                                <iconify-icon style="font-size:  2rem; color: #FFF;"
                                    icon="fluent:print-24-regular"></iconify-icon></v-btn>
                            <v-btn fab small style="height: 2.5rem;" class="bg-error rounded-lg mr-2"
                                @click="cancelDialog = true">
                                <iconify-icon style="font-size:  2rem; color: #FFF;"
                                    icon="fluent:dismiss-24-filled"></iconify-icon></v-btn>
                        </div>
                        <div v-if="xselected_patient.details.status == 'VAL2'">
                            <v-btn fab small style="height: 2.5rem;" class="bg-warning rounded-lg mr-2"
                                @click="confirm('yes')">
                                <iconify-icon style="font-size:  2rem; color: #FFF;"
                                    icon="fluent:print-24-regular"></iconify-icon></v-btn>
                        </div>
                        <v-dialog v-model="confirmDialog" max-width="400">
                            <v-card>
                                <v-card-title>Konfirmasi</v-card-title>
                                <v-card-text>Apakah Anda yakin?</v-card-text>
                                <v-card-actions>
                                    <v-btn color="primary" text @click="confirm('yes')">Ya</v-btn>
                                    <v-btn color="error" text @click="confirm('no')">Tidak</v-btn>
                                </v-card-actions>
                            </v-card>
                        </v-dialog>
                        <v-dialog v-model="cancelDialog" max-width="400">
                            <v-card>
                                <v-card-title>Batal Validasi</v-card-title>
                                <v-card-text>Apakah Anda yakin?</v-card-text>
                                <v-card-actions>
                                    <v-btn color="primary" text @click="cancelValidate('yes')">Ya</v-btn>
                                    <v-btn color="error" text @click="cancelValidate('no')">Tidak</v-btn>
                                </v-card-actions>
                            </v-card>
                        </v-dialog>
                        <!-- Dialog Save -->
                        <v-dialog v-model="saveDialog" max-width="400">
                            <v-card>
                                <v-card-title>Berhasil</v-card-title>
                                <v-card-text>Data berhasil disimpan.</v-card-text>
                                <v-card-actions>
                                    <v-btn color="primary" text @click="saveDialog = false">Tutup</v-btn>
                                </v-card-actions>
                            </v-card>
                        </v-dialog>
                    </div>
                </v-col>
            </v-row>
            <v-list>
                <v-list-item v-for="(result, idxresult) in xselected_patient.details.details" :key="idxresult">
                    <v-list-item-content>
                        <v-row>
                            <v-col cols="1" class="d-flex align-center justify-center">
                                <v-switch :model-value="flagPrintBoolean(result.flag_print)"
                                    @click="changeFlagPrint(result, idxresult)" color="primary">
                                </v-switch>

                            </v-col>
                            <v-col cols="11">
                                <v-textarea filled outline hide-details :label="result.result_label" auto-grow
                                    @change="changeResult(result.result_value, idxresult, index)"
                                    :disabled="xselected_patient.details.status !== 'NEW' || xselected_patient.details.doctor_id === '0'"
                                    v-model="result.result_value" rows="3">
                                </v-textarea>
                            </v-col>

                        </v-row>
                    </v-list-item-content>
                </v-list-item>
            </v-list>

        </v-container>
    </div>
</template>

<style scoped>
.pointer {
    cursor: pointer;
}

.v-table .v-table__wrapper>table>tbody>tr:not(:last-child)>td,
.v-table .v-table__wrapper>table>tbody>tr:not(:last-child)>th,
.v-table .v-table__wrapper>table>thead>tr>th {
    border-bottom: dashed 1px #EEEEEE;
}
</style>

<script type="module">
export default {
    name: "DetailPatient",
    data() {
        return {
            menu: false,
            selectedLang: '',
            cancelDialog: false,
            confirmDialog: false,
            saveDialog: false,
            isValidated: false,
            Validation: false,
            headers: [
                {
                    align: 'start',
                    key: 'name',
                    sortable: false,
                    width: "15%",
                    title: this.$t('message.tableDetailPatient.header.name'),
                    class: "bg-secondary-lighten font-weight-bold",
                },
                {
                    align: 'start',
                    key: 'result',
                    sortable: false,
                    width: "10%",
                    title: this.$t('message.tableDetailPatient.header.result'),
                    class: "bg-secondary-lighten font-weight-bold",
                },
                {
                    align: 'start',
                    key: 'flag',
                    sortable: false,
                    width: "10%",
                    title: this.$t('message.tableDetailPatient.header.flag'),
                    class: "bg-secondary-lighten font-weight-bold",
                },
                {
                    align: 'start',
                    key: 'nilai',
                    sortable: false,
                    width: "10%",
                    title: this.$t('message.tableDetailPatient.header.normalvalue'),
                    class: "bg-secondary-lighten font-weight-bold",
                },
                {
                    align: 'start',
                    key: 'unit',
                    sortable: false,
                    width: "10%",
                    title: this.$t('message.tableDetailPatient.header.unit'),
                    class: "bg-secondary-lighten font-weight-bold",
                },
                {
                    align: 'start',
                    key: 'metode',
                    sortable: false,
                    width: "15%",
                    title: this.$t('message.tableDetailPatient.header.method'),
                    class: "bg-secondary-lighten font-weight-bold",
                },
                {
                    align: 'start',
                    key: 'note',
                    sortable: false,
                    width: "15%",
                    title: this.$t('message.tableDetailPatient.header.note'),
                    class: "bg-secondary-lighten font-weight-bold",
                },
                {
                    align: 'start',
                    key: 'validasi',
                    sortable: false,
                    width: "5%",
                    title: this.$t('message.tableDetailPatient.header.documentation'),
                    class: "bg-secondary-lighten font-weight-bold",
                },
            ],
        };
    },
    computed: {
        isFlagPrintActive() {
            return this.$store.state.documentation.selected_patient === 'Y';
        },
        xselected_patient() {
            // Ambil data pasien dari Vuex store
            const selectedPatient = this.$store.state.documentation.selected_patient;
            return selectedPatient;
        },
        xdetails() {
            return this.$store.state.documentation.details
        },
    },
    mounted() {
    },
    methods: {
        flagPrintBoolean(flag) {
            return flag === 'Y';
        },
        changeFlagPrint(result, idxresult) {
            // Toggle antara 'Y' dan 'N'
            result.flag_print = result.flag_print === 'Y' ? 'N' : 'Y';
            this.$store.state.documentation.selected_patient.details.details[idxresult].flag_print = result.flag_print
            // Misalnya, Anda dapat mengirim perubahan ke backend atau melakukan update state lainnya.
        },
        cancelValidate(action) {
            if (action === "yes") {
                console.log("batal validasi");
                this.isValidated = false;
            }
            this.cancelDialog = false;
        },
        confirm(action) {
            if (action === "yes") {
                console.log("ya");
                this.isValidated = true;
            }
            this.confirmDialog = false;
        },
        save() {
            this.saveDialog = true;
        },
        selectLanguage(lang) {
            this.selectedLang = lang.name;
        },
        formatDate() {
            if (!this.date) return null;

            return moment(this.date).format("DD-MM-YYYY");
        },
        deFormatedDate(date) {
            if (!date) return null;

            const [day, month, year] = date.split("-");
            return `${year}-${month.padStart(2, "0")}-${day.padStart(2, "0")}`;
        },
    },
    watch: {
        xselected_patient(newVal) {
            console.log("ini new val", newVal)
            if (newVal && newVal.details && newVal.details.langs && newVal.details.langs.length > 0) {
                this.selectedLang = newVal.details.langs[0].name;
            } else {
                this.selectedLang = '';
            }
        },
    }
}
</script>