<template>
    <v-layout>
        <template>

            <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="deleteSummaryFisik()">
                            Yakin lah
                        </v-btn>
                    </v-card-actions>
                </v-card>
            </v-dialog>

            <v-dialog v-model="xdialogsummary" persistent max-width="600px">
                <v-card>
                    <v-card-title>
                        <span class="headline">FORM SUMMARY FISIK</span></v-card-title>
                    <v-card-text class="pt-0 pb-0">
                        <v-form ref="formvalidate" v-model="valid" lazy-validation>
                            <v-layout wrap>
                                <v-flex xs12>
                                    <v-autocomplete 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"
                                        small
                                        :rules="kelainanRules">
                                        <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-flex xs12>
                                    <v-select item-text="fitnessname" return-object :items="xfitness" :rules="fitnessRules" v-model="selected_fitness" label="Fitness*"></v-select>
                                </v-flex>
                                <v-flex xs12>
                                    <v-radio-group v-model="formtype">
                                        <template v-slot:label>
                                            <div>Summary fisik type</div>
                                        </template>
                                        <v-layout row>
                                            <v-flex xs6 wrap>
                                                <v-radio value="AND">
                                                    <template v-slot:label>
                                                    <div>AND</div>
                                                    </template>
                                                </v-radio>
                                            </v-flex>
                                            <v-flex xs6 wrap>
                                                <v-radio value="OR">
                                                    <template v-slot:label>
                                                    <div>OR</div>
                                                    </template>
                                                </v-radio>
                                            </v-flex>
                                        </v-layout>
                                    </v-radio-group>
                                </v-flex>
                                <v-flex>
                                    <p v-for="(xerror,idx) in xerrors" class="error pl-2 pr-2" style="color:#fff">{{xerror.msg}}</p>
                                </v-flex>
                            </v-layout>
                    </v-card-text>
                    <v-card-actions>
                        <v-spacer></v-spacer>
                        <v-btn color="blue darken-1" flat @click="closeFormSummaryFisik()">Tutup</v-btn>
                        <v-btn v-if="xact === 'new'" color="blue darken-1" flat @click="saveFormSummaryFisik()">Simpan</v-btn>
                        <v-btn v-if="xact === 'edit'" color="blue darken-1" flat @click="updateFormSummaryFisik()">Simpan Perubahan</v-btn>
                    </v-card-actions>
                    </v-form>
                </v-card>
            </v-dialog>
        </template>


        <v-flex xs12>
            <v-card>
                <v-toolbar color="blue lighten-3" dark height="50px">
                    <v-toolbar-title>SUMMARY FISIK</v-toolbar-title>
                    <v-spacer></v-spacer>
                    <v-btn @click="openFormSummaryFisik(0)" icon>
                        <v-icon>library_add</v-icon>
                    </v-btn>
                </v-toolbar>
                <v-snackbar v-model="snackbar" :timeout="5000" :multi-line="false" :vertical="false" :top="true">
                    {{msgsnackbar}}
                    <v-btn flat @click="updateAlert_success(false)">
                        Tutup
                    </v-btn>
                </v-snackbar>
                <v-layout row style="background:#bbdefb;padding-top:5px;" justify-left>
                    <v-list-tile>
                        <input type="text" v-model="xsearch" class="textinput" label="Summary fisik" placeholder="Cari ..." />
                        </v-list-tile-content>
                    </v-list-tile>
                </v-layout>
                <v-divider></v-divider>
                <div>

                    <v-layout row class="scroll-container" style="max-height:600px;overflow: auto;">
                        <v-flex xs12 pl-2 pr-2 pt-2 pb-2>
                            <v-data-table :headers="headers" :items="xsummaryfisik" :loading="isLoading" hide-actions class="elevation-1">
                                <template slot="items" slot-scope="props">
                                    <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">
                                        {{ props.item.Mcu_KelainanName}}
                                        <p style="color:#800000" class="mb-0 font-weight-bold caption">{{ props.item.Mcu_KelainanClasification}}</p>
                                    </td>
                                    <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">
                                        {{ props.item.fitnessname}}
                                    </td>
                                    <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">
                                        {{ props.item.Mcu_FisikSummaryType}}
                                    </td>
                                    <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">
                                        <v-icon small class="ml-3" color="primary" @click="editSummaryFisik(props.item)">edit</v-icon>
                                        <v-icon small class="ml-3" color="error" @click="openDeleteSummaryFisik(props.item)">clear</v-icon>
                                    </td>
                                </template>
                            </v-data-table>
                        </v-flex>
                    </v-layout>



                    <v-divider></v-divider>
                    <v-pagination style="margin-top:10px;margin-bottom:10px" v-model="curr_page" :length="xtotal_page"></v-pagination>
                </div>




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

<style scoped>
    .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;
    }

    .boxoutline {
        color: red;
        border: 1px solid red;
        justify-content: center;
        height: 45px;
        line-height: 45px;
        padding-left: 10px;
        background: #ffffff;
        font-size: 14px;
        font-weight: 500;
        border-radius: 1px
    }

    .boxoutline:hover {
        background: rgba(0, 0, 0, 0.07) !important;
        font-size: 15px;
        font-weight: 700;
    }

    .boxsolid {
        color: #ffffff;
        border: 1px solid #ffffff;
        justify-content: center;
        height: 45px;
        line-height: 45px;
        padding-left: 10px;
        background: #f44336;
        font-size: 14px;
        font-weight: 500;
        border-radius: 1px
    }

    .boxsolid:hover {
        background: #f44336de;
        font-size: 15px;
        font-weight: 700;
    }

    .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;
    }

    /* if both the vertical and the horizontal bars appear, then perhaps the right bottom corner also needs to be styled */
</style>

<script>
    module.exports = {
        data() {
            return {
                msgalertconfirmation: "Perubahan yang telah dilakukan belum disimpan dong !",
                dialogdeletealert: false,
                msgalert: "",
                valid: false,
                xid: 0,
                search_instrument: '',
                items: [],
                xname: '',
                xcode: '',
                kelainanRules: [
                    v => !!v || 'Kelainan harus dipilih'
                ],
                fitnessRules: [
                    v => !!v || 'Fitness harus dipilih'
                ],
                name: '',
                isdefault: false,
                scode: '',
                search_doctor: '',
                search_testheader: '',
                search_template: '',
                page: 1,
                search_item_datakelainan: "",
                headers: [{
                        text: "kELAINAN",
                        align: "left",
                        sortable: false,
                        value: "mr",
                        width: "20%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "FITNESS KATEGORI",
                        align: "left",
                        sortable: false,
                        value: "mr",
                        width: "20%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "TYPE",
                        align: "left",
                        sortable: false,
                        value: "mr",
                        width: "15%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "AKSI",
                        align: "left",
                        sortable: false,
                        value: "mr",
                        width: "10%",
                        class: "pa-2 blue lighten-3 white--text"
                    }
                ],
                // pagination: {
                //     descending: true,
                //     page: 1,
                //     rowsPerPage: 100,
                //     sortBy: 'id DESC',
                //     totalItems: this.$store.state.kelainangroup.total_filter_kelainangroups
                // }
            };
        },
        mounted() {
            this.$store.dispatch("fisikheader/search", {
                search: this.xsearch,
                current_page: 1,
                last_id: -1
            })
            this.$store.dispatch("fisikheader/getfitness")
            this.$store.dispatch("fisikdetail/searchdetail", {
                fifiksummaryId: 0,
                search: this.$store.state.fisikdetail.x_search,
                current_page: 1,
                last_id: -1
            })
        },
        computed: {
            xsummaryfisik() {
                return this.$store.state.fisikheader.summaryfisik
            },
            isLoading() {
                return this.$store.state.fisikheader.search_status == 1
            },
            selected_summaryfisik: {
                get() {
                    return this.$store.state.fisikheader.selected_summaryfisik
                },
                set(val) {
                    this.$store.commit("summaryfiisk/update_selected_summaryfisik", val)
                }
            },
            xsearch: {
                get() {
                    return this.$store.state.fisikheader.x_search
                },
                set(val) {
                    this.$store.commit("fisikheader/update_x_search", val)
                }
            },
            curr_page: {
                get() {
                    return this.$store.state.fisikheader.current_page
                },
                set(val) {
                    this.$store.commit("fisikheader/update_current_page", val)
                    this.$store.dispatch("fisikheader/search", {
                        search: this.xsearch,
                        current_page: val,
                        last_id: -1
                    })
                }
            },
            xtotal_page: {
                get() {
                    return this.$store.state.fisikheader.total_summaryfisiks
                },
                set(val) {
                    this.$store.commit("fisikheader/update_total_summaryfisiks", val)
                }
            },
            xdatakelainans() {
                return this.$store.state.fisikheader.datakelainans
            },
            vdatakelainan: {
                get() {
                    return this.$store.state.fisikheader.selected_datakelainan
                },
                set(val) {
                    this.$store.commit("fisikheader/update_selected_datakelainan", val)
                }
            },
            xdialogsummary() {
                return this.$store.state.fisikheader.dialog_form_summary
            },
            xerrors() {
                return this.$store.state.fisikheader.errors
            },
            xact() {
                return this.$store.state.fisikheader.act
            },
            xfitness() {
                return this.$store.state.fisikheader.fitness
            },
            selected_fitness: {
                get() {
                    return this.$store.state.fisikheader.selected_fitness
                },
                set(val) {
                    this.$store.commit("fisikheader/update_selected_fitness", val)
                }
            },
            formtype: {
                get() {
                    return this.$store.state.fisikheader.formtype
                },
                set(val) {
                    this.$store.commit("fisikheader/update_formtype", val)
                }
            },
            snackbar: {
                get() {
                    return this.$store.state.fisikheader.alert_success
                },
                set(val) {
                    this.$store.commit("fisikheader/update_alert_success", val)
                }
            },
            msgsnackbar() {
                return this.$store.state.fisikheader.msg_success
            },
        },
        methods: {
            isSelected(p) {
                return p.Mcu_FisikSummaryID == this.$store.state.fisikheader.selected_summaryfisik.Mcu_FisikSummaryID
            },
            selectMe(sc) {
                this.$store.commit("fisikheader/update_selected_summaryfisik", sc)
                let alldata =  this.$store.state.fisikheader.summaryfisik
                let idx = _.findIndex(alldata, item => item.Mcu_FisikSummaryID == sc.Mcu_FisikSummaryID)
                this.$store.state.fisikheader.last_id = idx
                this.$store.state.fisikdetail.current_page = 1
                this.$store.dispatch("fisikdetail/searchdetail", {
                    fifiksummaryId: this.$store.state.fisikheader.selected_summaryfisik.Mcu_FisikSummaryID,
                    search: this.$store.state.fisikdetail.x_search,
                    current_page: this.$store.state.fisikdetail.current_page,
                    last_id: -1
                })

            },
            closeFormSummaryFisik() {
                this.$store.commit("fisikheader/update_dialog_form_summary", false)
                this.$store.commit("fisikheader/update_errors",[])
            },
            thr_search: _.debounce(function () {
                this.$store.dispatch("fisikheader/search", {
                    search: this.xsearch,
                    current_page: this.curr_page,
                    last_id: -1
                })
            }, 1000),
            thr_search_datakelainan: _.debounce(function () {
                this.$store.dispatch("fisikheader/searchkelainan", this.search_item_datakelainan)
            }, 2000),
            openFormSummaryFisik(val) {
                this.xid = val
                this.formtype = 'AND'
                // this.$refs.formvalidate.reset()
                // this.$refs.formvalidate.resetValidation()
                this.$store.commit("fisikheader/update_datakelainans",[])
                this.$store.commit("fisikheader/update_selected_datakelainan",{})
                // this.$store.commit("fisikheader/update_fitness",[])
                this.$store.commit("fisikheader/update_selected_fitness",{})
                this.$store.commit("fisikheader/update_act", 'new')
                this.$store.commit("fisikheader/update_dialog_form_summary", true)
            },
            saveFormSummaryFisik() {
                if (this.$refs.formvalidate.validate()) {
                    this.$store.dispatch("fisikheader/save", {
                        xid: this.xid,
                        kelainanId: this.vdatakelainan.Mcu_KelainanID,
                        fitnessId: this.selected_fitness.Mcu_FitnessCategoryID,
                        type: this.formtype,
                        kelainanname: this.vdatakelainan.Mcu_KelainanName,
                        fitnessname: this.selected_fitness.fitnessname,
                        search: this.xsearch
                    })
                }
            },
            updateAlert_success(val) {
                this.$store.commit("fisikheader/update_alert_success", val)
            },
            editSummaryFisik(data) {
                this.xid = data.Mcu_FisikSummaryID
                this.formtype = data.Mcu_FisikSummaryType
                this.$store.commit("fisikheader/update_datakelainans",[{
                    Mcu_KelainanID: data.Mcu_KelainanID,
                    Mcu_KelainanName: data.Mcu_KelainanName
                }])
                this.$store.commit("fisikheader/update_selected_datakelainan", {
                    Mcu_KelainanID: data.Mcu_KelainanID,
                    Mcu_KelainanName: data.Mcu_KelainanName
                })
                this.$store.commit("fisikheader/update_fitness",[{
                    Mcu_FitnessCategoryID: data.Mcu_FitnessCategoryID,
                    fitnessname: data.fitnessname
                }])
                this.$store.commit("fisikheader/update_selected_fitness", {
                    Mcu_FitnessCategoryID: data.Mcu_FitnessCategoryID,
                    fitnessname: data.fitnessname
                })
                this.$store.commit("fisikheader/update_errors",[])
                this.$store.commit("fisikheader/update_act", 'edit')
                this.$store.commit("fisikheader/update_dialog_form_summary", true)
                this.$store.dispatch("fisikheader/getfitness")
            },
            updateFormSummaryFisik() {
                if (this.$refs.formvalidate.validate()) {
                    this.$store.dispatch("fisikheader/update", {
                        xid: this.xid,
                        search: this.xsearch,
                        kelainanname: this.vdatakelainan.Mcu_KelainanName,
                        fitnessname: this.selected_fitness.fitnessname,
                        kelainanId: this.vdatakelainan.Mcu_KelainanID,
                        fitnessId: this.selected_fitness.Mcu_FitnessCategoryID,
                        type: this.formtype,
                    })
                }
            },
            openDeleteSummaryFisik(data) {
                this.xid = data.Mcu_FisikSummaryID
                var xdata = {
                    Mcu_KelainanID: data.Mcu_KelainanID,
                    Mcu_KelainanName: data.Mcu_KelainanName,
                    Mcu_KelainanClasification: data.Mcu_KelainanClasification
                }
                this.$store.commit("fisikheader/update_selected_datakelainan", xdata)
                this.msgalert = "Yakin, mau hapus summary fisik kelainan " + data.Mcu_KelainanName + ", fitness " + data.fitnessname + " ?"
                this.dialogdeletealert = true
            },
            deleteSummaryFisik() {
                this.$store.dispatch("fisikheader/delete", {
                    search: this.xsearch,
                    kelainanname: this.vdatakelainan.Mcu_KelainanName, 
                    id: this.xid                 
                })
                this.dialogdeletealert = false
            }
        },
        watch: {
            xsearch(val, old) {
                this.xsearch = val
                this.thr_search()
            },
            search_item_datakelainan(val, old) {
                if (val == old) return
                if (!val) return

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

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