<template>
    <div>
        <v-layout class="mb-2" column>
            <v-dialog v-model="dialogconfirmationdelete" persistent max-width="290">
                <v-card>
                    <v-card-title dark class="headline error pt-2 pb-2" primary-title style="color:white">
                        <h4 dark>Konfirmasi</h4>
                    </v-card-title>
                    <v-card-text>
                        {{msgconfirmationdelete}}
                    </v-card-text>
                    <v-card-actions>
                        <v-spacer></v-spacer>
                        <v-btn small color="error darken-1 text-sm-left" flat @click="doDeleteData()">Hapus</v-btn>
                        <v-btn small color="primary darken-1 text-sm-right" flat @click="dialogconfirmationdelete = false">Batal</v-btn>
                    </v-card-actions>
                </v-card>
            </v-dialog>
            <v-card>
                <v-layout row>
                    <v-flex xs12>
                        <v-subheader red--text text--lighten-1> TEST FAVORITE
                            <v-flex text-md-right>
                                <v-btn v-if="xact === 'new'" @click="saveNewTestFavorite()" small color="info">Simpan</v-btn>
                                <v-btn v-if="xact === 'edit'" @click="deleteData()" small color="error">Hapus</v-btn>
                                <v-btn v-if="xact === 'edit'" @click="saveData()" small color="primary">Simpan</v-btn>
                            </v-flex>
                        </v-subheader>
                        <v-divider></v-divider>
                        <v-layout row wrap>
                            <v-flex xs12 pa-2>
                                <v-layout row>
                                    <v-flex xs6 pa-1>
                                        <v-select item-text="T_FavoriteTypeName" return-object :items="xtypes" :disabled="xact === 'edit'" v-model="xtype" label="Type"></v-select>
                                    </v-flex>
                                    <v-flex xs6 pa-1>
                                        
                                    </v-flex>
                                </v-layout>
                                <v-layout row>
                                    <v-flex xs6 pa-1>
                                        <v-autocomplete label="Test" v-model="test" :items="xtests" :disabled="this.$store.state.testfavorite.selected_type.T_FavoriteType === 'PN' || this.$store.state.testfavorite.selected_type.T_FavoriteType === 'PR'" :search-input.sync="search_test" auto-select-first no-filter
                                            item-text="T_TestName" return-object no-data-text="Pilih Test">
                                            <template slot="item" slot-scope="{ item }">
                                                <v-list-tile-content>
                                                    <v-list-tile-title v-text="item.T_TestName"></v-list-tile-title>
                                                </v-list-tile-content>
                                            </template>
                                        </v-autocomplete>
                                    </v-flex>
                                    <v-flex xs6 pa-1>
                                        <v-autocomplete label="Paket" v-model="packet" :items="xpackets" :disabled="this.$store.state.testfavorite.selected_type.T_FavoriteType === 'PX'" :search-input.sync="search_packet" auto-select-first no-filter
                                            item-text="T_PacketName" return-object no-data-text="Pilih Paket">
                                            <template slot="item" slot-scope="{ item }">
                                                <v-list-tile-content>
                                                    <v-list-tile-title v-text="item.T_PacketName"></v-list-tile-title>
                                                </v-list-tile-content>
                                            </template>
                                        </v-autocomplete>
                                    </v-flex>
                                </v-layout>

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


            <template>

                <v-dialog v-model="dialogerrormsg" max-width="30%">
                    <v-card>
                        <v-card-title class="headline red en-1 pt-2 pb-2" primary-title>
                            <h4 style="color:#FFEBEE">Peringatan !</h4>
                        </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 v-html="errormsg" xs12>

                                                </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="error" flat @click="dialogerrormsg = false">
                                Tutup
                            </v-btn>
                        </v-card-actions>
                    </v-card>
                </v-dialog>

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

<style scoped>
    table,
    td,
    th {
        border: 1px solid #ddd;
        text-align: left;
    }

    table {
        border-collapse: collapse;
        width: 100%;
    }

    th,
    td {
        padding-top: 5px;
        padding-bottom: 5px;
        padding-left: 8px;
        padding-right: 5px;
    }

    .mini-input .v-input {
        margin-top: 0px;
    }

    .mini-input .v-input,
    .mini-input .v-input--selection-controls,
    .mini-input .v-input__slot {
        margin-top: 0px;
        margin-bottom: 0px;
        margin-left: 3px;
    }

    .mini-input .v-messages {
        min-height: 0px;
    }

    input.fhm-input {
        border: 1px solid black;
        border-radius: 2px;
        -webkit-box-shadow: inset 0 0 2px rgba(0, 0, 0, 0.1),
        0 0 4px rgba(0, 0, 0, 0.1);
        -moz-box-shadow: inset 0 0 2px rgba(0, 0, 0, 0.1),
        0 0 4px rgba(0, 0, 0, 0.1);
        box-shadow: inset 0 0 2px rgba(0, 0, 0, 0.1),
        0 0 4px rgba(0, 0, 0, 0.1);
        padding: 2px 4px;
        background: rgba(255, 255, 255, 0.5);
        margin: 0 0 1px 0;
        width: 30px;
        text-align: center;
    }
</style>

<script>
    module.exports = {
        data: () => ({
            dialogerrormsg: false,
            errormsg: '',
            search_test: '',
            search_packet: '',
            startdate: '',
            enddate: '',
            menustartdate: false,
            menuenddate: false,
            date: new Date().toISOString().substr(0, 10)
        }),
        computed: {
            dialogconfirmationdelete: {
                get() {
                    return this.$store.state.testfavorite.dialog_confirmation_delete
                },
                set(val) {
                    this.$store.commit("testfavorite/update_dialog_confirmation_delete", val)
                }
            },
            msgconfirmationdelete() {
                return this.$store.state.testfavorite.msg_confirmation_delete
            },
            xact() {
                return this.$store.state.testfavorite.act
            },
            detail() {
                return this.$store.state.testfavorite.selected_testfavorite
            },
            xtypes() {
                return this.$store.state.testfavorite.types
            },
            xtype: {
                get() {
                    return this.$store.state.testfavorite.selected_type
                },
                set(val) {
                    this.$store.commit("testfavorite/update_selected_type", val)
                    this.$store.commit("testfavorite/update_no_save", 1)
                }
            },
            xtestfavoritename: {
                get() {
                    return this.$store.state.testfavorite.testfavorite_name
                },
                set(val) {
                    this.$store.commit("testfavorite/update_testfavorite_name", val)
                    this.$store.commit("testfavorite/update_no_save", 1)
                }
            },
            xnik: {
                get() {
                    return this.$store.state.testfavorite.nik
                },
                set(val) {
                    this.$store.commit("testfavorite/update_nik", val)
                    this.$store.commit("testfavorite/update_no_save", 1)
                }
            },
            xdob: {
                get() {
                    return this.$store.state.testfavorite.dob
                },
                set(val) {
                    this.$store.commit("testfavorite/update_dob", val)
                    this.$store.commit("testfavorite/update_no_save", 1)
                }
            },
            xsexes() {
                return this.$store.state.testfavorite.sexes
            },
            xsex: {
                get() {
                    return this.$store.state.testfavorite.selected_sex
                },
                set(val) {
                    this.$store.commit("testfavorite/update_selected_sex", val)
                    this.$store.commit("testfavorite/update_no_save", 1)
                }
            },
            xreligions() {
                return this.$store.state.testfavorite.religions
            },
            xreligion: {
                get() {
                    return this.$store.state.testfavorite.selected_religion
                },
                set(val) {
                    this.$store.commit("testfavorite/update_selected_religion", val)
                    this.$store.commit("testfavorite/update_no_save", 1)
                }
            },
            xphone: {
                get() {
                    return this.$store.state.testfavorite.phone
                },
                set(val) {
                    this.$store.commit("testfavorite/update_phone", val)
                    this.$store.commit("testfavorite/update_no_save", 1)
                }
            },
            xhp: {
                get() {
                    return this.$store.state.testfavorite.hp
                },
                set(val) {
                    this.$store.commit("testfavorite/update_hp", val)
                    this.$store.commit("testfavorite/update_no_save", 1)
                }
            },
            xaddress: {
                get() {
                    return this.$store.state.testfavorite.address
                },
                set(val) {
                    this.$store.commit("testfavorite/update_address", val)
                    this.$store.commit("testfavorite/update_no_save", 1)
                }
            },
            xtests() {
                return this.$store.state.testfavorite.tests
            },
            test: {
                get() {
                    return this.$store.state.testfavorite.test
                },
                set(val) {
                    this.$store.commit("testfavorite/update_test", val)
                }
            },
            xpackets() {
                return this.$store.state.testfavorite.packets
            },
            packet: {
                get() {
                    return this.$store.state.testfavorite.packet
                },
                set(val) {
                    this.$store.commit("testfavorite/update_packet", val)
                }
            },
            xsubareas() {
                return this.$store.state.testfavorite.subareas
            },
            subareaaddress: {
                get() {
                    return this.$store.state.testfavorite.subarea_address
                },
                set(val) {
                    this.$store.commit("testfavorite/update_subarea_address", val)
                }
            },
            xpositions() {
                return this.$store.state.testfavorite.positions
            },
            xposition: {
                get() {
                    return this.$store.state.testfavorite.selected_position
                },
                set(val) {
                    this.$store.commit("testfavorite/update_selected_position", val)
                    this.$store.commit("testfavorite/update_no_save", 1)
                }
            },
            xtimework: {
                get() {
                    return this.$store.state.testfavorite.timework
                },
                set(val) {
                    this.$store.commit("testfavorite/update_timework", val)
                    this.$store.commit("testfavorite/update_no_save", 1)
                }
            },
            xtimeworksaturday: {
                get() {
                    return this.$store.state.testfavorite.timeworksaturday
                },
                set(val) {
                    this.$store.commit("testfavorite/update_timeworksaturday", val)
                    this.$store.commit("testfavorite/update_no_save", 1)
                }
            },
            xblood: {
                get() {
                    return this.$store.state.testfavorite.blood
                },
                set(val) {
                    this.$store.commit("testfavorite/update_blood", val)
                    this.$store.commit("testfavorite/update_no_save", 1)
                }
            },
            xstudy: {
                get() {
                    return this.$store.state.testfavorite.study
                },
                set(val) {
                    this.$store.commit("testfavorite/update_study", val)
                    this.$store.commit("testfavorite/update_no_save", 1)
                }
            },
            xispacket: {
                get() {
                    return this.$store.state.testfavorite.ispacket
                },
                set(val) {
                    this.$store.commit("testfavorite/update_ispacket", val)
                    this.$store.commit("testfavorite/update_no_save", 1)
                }
            },
            startComputedDateFormatted() {
                return this.formatDate(this.xstartdate)
            },
            xstartdate: {
                get() {
                    return this.$store.state.testfavorite.startdate
                },
                set(val) {
                    this.$store.commit("testfavorite/update_startdate", val)
                }
            },
            endComputedDateFormatted() {
                return this.formatDate(this.xenddate)
            },
            xenddate: {
                get() {
                    return this.$store.state.testfavorite.enddate
                },
                set(val) {
                    this.$store.commit("testfavorite/update_enddate", val)
                }
            },
        },
        methods: {
            changeBtnFlagPacket(value) {
                var newval = (value === 'Y') ? 'N' : 'Y'
                this.xispacket = newval
            },
            changeBtnFlagDefaultPacket(value) {
                var newval = (value === 'Y') ? 'N' : 'Y'
                this.xdispacket = newval
            },
            saveData() {
                var prm = {}
                prm.T_FavoriteID = this.$store.state.testfavorite.selected_testfavorite.T_FavoriteID
                prm.T_FavoriteType = this.$store.state.testfavorite.selected_type.T_FavoriteType
                prm.T_FavoriteT_TestID = this.test.T_TestID
                prm.T_FavoriteT_PacketID = this.packet.T_PacketID
                prm.T_FavoriteIsPacket = this.xispacket
                this.$store.dispatch("testfavorite/save", prm)

            },
            saveNewTestFavorite() {
                var error_count = 0
                var error_msg = ''
                if (error_count > 0) {
                    this.errormsg = error_msg
                    this.dialogerrormsg = true
                } else {
                    var prm = {}
                    var tipe = this.$store.state.testfavorite.selected_type.T_FavoriteType
                    if(tipe === 'PX'){
                        prm.T_FavoriteID = 0
                    prm.T_FavoriteType = this.$store.state.testfavorite.selected_type.T_FavoriteType
                    prm.T_FavoriteT_TestID = this.test.T_TestID
                    prm.T_FavoriteT_PacketID = 0
                    prm.T_FavoriteIsPacket = this.xispacket
                    this.$store.dispatch("testfavorite/newtestfavorite", prm)
                    } else{
                        prm.T_FavoriteID = 0
                    prm.T_FavoriteType = this.$store.state.testfavorite.selected_type.T_FavoriteType
                    prm.T_FavoriteT_TestID = 0
                    prm.T_FavoriteT_PacketID = this.packet.T_PacketID
                    prm.T_FavoriteIsPacket = this.xispacket
                    this.$store.dispatch("testfavorite/newtestfavorite", prm)
                    }
                    
                }
            },
            thr_search_test: _.debounce(function () {
                this.$store.dispatch("testfavorite/searchtest", this.search_test)
            }, 2000),
            thr_search_packet: _.debounce(function () {
                var prm = {}
                prm.search = this.search_packet
                prm.tipe = this.$store.state.testfavorite.selected_type.T_FavoriteType
                this.$store.dispatch("testfavorite/searchpacket", prm)
            }, 2000),
            formatDate(date) {
                if (!date) return null

                const [year, month, day] = date.split('-')
                return `${day}-${month}-${year}`
            },
            deFormatedDate(date) {
                if (!date) return null

                const [day, month, year] = date.split('-')
                return `${year}-${month.padStart(2, '0')}-${day.padStart(2, '0')}`
            },
            deleteData() {
                let msg = "Yakin, akan menghapus data pasien " + this.xtestfavoritename + " ?"
                this.$store.commit("testfavorite/update_msg_confirmation_delete", msg)
                this.$store.commit("testfavorite/update_dialog_confirmation_delete", true)
            },
            doDeleteData() {
                var prm = {}
                prm.T_FavoriteID = this.$store.state.testfavorite.selected_testfavorite.T_FavoriteID
                prm.T_FavoriteName = this.xtestfavoritename
                this.$store.dispatch("testfavorite/delete", prm)
            }
        },
        watch: {
            search_test(val, old) {
                if (val == old) return
                if (!val) return
                if (val.length < 1) return
                if (this.$store.state.testfavorite.update_autocomplete_status == 1) return
                this.thr_search_test()
            },
            search_packet(val, old) {
                if (val == old) return
                if (!val) return
                if (val.length < 1) return
                if (this.$store.state.testfavorite.update_autocomplete_status == 1) return
                this.thr_search_packet()
            }
        }
    }
</script>