<template>
    <v-layout class="fill-height" column>

        <v-card class="mb-2 pa-2 searchbox">
            <v-layout row>
                <v-flex xs6>
                    <v-menu 
                        v-model="menufilterdatestart"
                        :close-on-content-click="false" :nudge-right="40" lazy
                        transition="scale-transition" offset-y full-width max-width="290px" min-width="290px">
                        <template v-slot:activator="{ on }">
                        <v-text-field 
                            v-model="filterComputedDateFormattedStart"
                            class="mr-3" label="Tanggal Awal"
                            outline hide-details readonly v-on="on"
                            @blur="date = deFormatedDate(filterComputedDateFormattedStart)"></v-text-field>
                        </template>
                        <v-date-picker no-title 
                            v-model="xdatestart" @input="menufilterdatestart = false"></v-date-picker>
                    </v-menu>
                </v-flex>
                <v-flex xs6>
                    <v-menu 
                        v-model="menufilterdateend"
                        :close-on-content-click="false" :nudge-right="40" lazy
                        transition="scale-transition" offset-y full-width max-width="290px" min-width="290px">
                        <template v-slot:activator="{ on }">
                        <v-text-field 
                            v-model="filterComputedDateFormattedEnd"
                            label="Tanggal Akhir"
                            outline hide-details readonly v-on="on"
                            @blur="date = deFormatedDate(filterComputedDateFormattedEnd)"></v-text-field>
                        </template>
                        <v-date-picker no-title
                            v-model="xdateend" @input="menufilterdateend = false"></v-date-picker>
                    </v-menu>
                </v-flex>
            </v-layout>

            <v-layout row class="pt-2">
                <v-flex xs6>
                    <v-text-field style="font-size:14px" label="No Reg / Nama" class="mr-3" outline
                        v-model="vsearch" hide-details></v-text-field>
                </v-flex>
                <v-flex xs6>
                    <v-autocomplete label="Kel. Pelanggan" v-model="selected_company" :items="xcompanys"
                        :search-input.sync="search_company" auto-select-first hide-details style="font-size:14px" outline
                        no-filter item-text="name" return-object no-data-text="Semua Kel. Pelanggan">
                        <template slot="item" slot-scope="{ item }">
                        <v-list-tile-content>
                            <v-list-tile-title v-text="item.name"></v-list-tile-title>
                        </v-list-tile-content>
                        </template>
                    </v-autocomplete>
                </v-flex>
            </v-layout>
        </v-card>
        <v-card>
            <v-layout row style="max-height:650px;overflow: auto;">
                <v-flex xs12 pl-2 pr-2 pt-2 pb-2>
                    <v-data-table :headers="headers" :items="xpatients" :isLoading="isLoading" :pagination.sync="pagination" class="elevation-1 fixed_header">
                        <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)">
                                {{ props.item.T_OrderHeaderLabNumber }}
                            </td>
                            <td class="text-xs-center pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">
                                {{ props.item.patient_fullname }}
                            </td>

                        </template>
                        <template>
                            <div class="text-xs-center">
                                <v-pagination v-model="page" :length="15" :total-visible="7"></v-pagination>
                            </div>
                        </template>
                    </v-data-table>
                </v-flex>
            </v-layout>
        </v-card>

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

    .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_company: "",
                menufilterdatestart: false,
                menufilterdateend:false,
                date: new Date().toISOString().substr(0, 10),
                headers: [{
                        text: "NO",
                        align: "center",
                        sortable: false,
                        value: "mr",
                        width: "5%",
                        class: "pa-2 teal lighten-3 white--text"
                    },
                    {
                        text: "NO REG",
                        align: "center",
                        sortable: false,
                        value: "mr",
                        width: "8%",
                        class: "pa-2 teal lighten-3 white--text"
                    },
                    {
                        text: "NAMA",
                        align: "center",
                        sortable: false,
                        value: "mr",
                        width: "20%",
                        class: "pa-2 teal lighten-3 white--text"
                    },
                ],
                page: 1,
                pagination: {
                    descending: false,
                    page: 1,
                    rowsPerPage: 5,
                    sortBy: 'T_OrderHeaderLabNumber',
                    totalItems: this.$store.state.patient.total_patients
                },
            };
        },
        mounted() {
            this.$store.dispatch("patient/search", {
                startdate: this.$store.state.patient.start_date,
                enddate: this.$store.state.patient.end_date,
                search: this.vsearch,
                companyid: this.selected_company.id,
                last_id: -1
            })
        },
        computed: {
            xcompanys() {
                return this.$store.state.patient.companys
            },
            selected_company: {
                get() {
                    return this.$store.state.patient.selected_company
                },
                set(val) {
                    this.$store.commit("patient/update_selected_company", val)
                    this.searchPatient()
                }
            },
            xpatients() {
                return this.$store.state.patient.patients
            },
            isLoading() {
                return this.$store.state.patient.search_status == 1
            },
            vsearch: {
                get() {
                    return this.$store.state.patient.x_search
                },
                set(val) {
                    this.$store.commit("patient/update_x_search", val)
                }
            },
            xdatestart: {
                get() {
                    return this.$store.state.patient.start_date
                },
                set(val) {
                    this.$store.commit("patient/update_start_date",val)
                    this.$store.commit("patient/update_last_id", -1)
                }
            },
            xdateend: {
                get() {
                    return this.$store.state.patient.end_date
                },
                set(val) {
                    this.$store.commit("patient/update_end_date",val)
                    this.$store.commit("patient/update_last_id", -1)
                }
            },
            filterComputedDateFormattedStart () {
                return this.formatDate(this.xdatestart)
            },
            filterComputedDateFormattedEnd () {
                return this.formatDate(this.xdateend)
            },
            selected_patient: {
                get() {
                    return this.$store.state.patient.selected_patient
                }
            }
        },
        methods: {
            isSelected(p) {
                return p.T_OrderHeaderID == this.$store.state.patient.selected_patient.T_OrderHeaderID
            },
            selectMe(selected) {
                this.$store.commit("patient/update_selected_patient", selected)
                this.$store.commit("patient/update_last_id", selected.T_OrderHeaderID)
                this.$store.commit("consumable/update_last_id", -1)
            },
            thr_search_company: _.debounce(function () {
                this.$store.dispatch("patient/getcompany", this.search_company)
            }, 1000),
            thr_search: _.debounce(function () {
                this.$store.dispatch("patient/search", {
                    startdate: this.$store.state.patient.start_date,
                    enddate: this.$store.state.patient.end_date,
                    search: this.vsearch,
                    companyid: this.selected_company.id,
                    last_id: -1
                })
            }, 1000),
            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')}`
            },
            searchPatient() {
                    this.$store.dispatch("patient/search", {
                    startdate: this.$store.state.patient.start_date,
                    enddate: this.$store.state.patient.end_date,
                    search: this.vsearch,
                    companyid: this.selected_company.id,
                    last_id: -1
                })
            }
        },
        watch: {
            search_company(val, old) {
                if (val == old) return;
                if (!val) return;
                if (val.length < 1) return;
                if (this.$store.state.patient.update_autocomplete_status == 1) return;
                this.thr_search_company();
            },
            vsearch(val, old) {
                this.vsearch = val
                this.thr_search()
            },
            xdatestart(val, old) {
                this.xdatestart = val
                this.thr_search()
            },
            xdateend(val, old) {
                this.xdateend = val
                this.thr_search()
            },
            selected_patient(val, old) {
                this.$store.commit("consumable/update_selected_orderheaderid", val.T_OrderHeaderID)
            }
        }
    }
</script>