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

        <v-card class="mb-2 pa-2 ">
            <v-layout align-center row>

                <v-flex xs3>
                    <v-text-field style="font-size:14px" label="Nomor Lab Lengkap" class="ml-1" on-key-ent outline
                        v-model="xsearch" hide-details></v-text-field>
                </v-flex>
                <v-flex xs3>
                    <v-text-field style="font-size:14px" label="W. Nama Test / W. Kode Test" class="ml-1" on-key-ent
                        outline v-model="test_name" hide-details></v-text-field>
                </v-flex>
                <v-flex xs1>
                    <v-btn color="warning" dark :disabled="!xsearch" class="ma-1 white--text"
                        @click="do_search">CARI</v-btn>
                </v-flex>

            </v-layout>
        </v-card>
        <v-card>
            <v-layout align-center row>
                <v-flex xs12 pl-2 pr-2 pt-2 pb-2>
                    <v-data-table :headers="headers" :items="xresult" :isLoading="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.api_ResultDate }}
                            </td>
                            <td class="text-xs-center pa-2" v-bind:class="{ 'amber lighten-4': isSelected(props.item) }"
                                @click="selectMe(props.item)">
                                {{ props.item.api_ResultM_BranchCode }}
                            </td>
                            <td class="text-xs-center pa-2" v-bind:class="{ 'amber lighten-4': isSelected(props.item) }"
                                @click="selectMe(props.item)">
                                {{ props.item.api_ResultNolab }}
                            </td>
                            <td class="text-xs-center pa-2" v-bind:class="{ 'amber lighten-4': isSelected(props.item) }"
                                @click="selectMe(props.item)">
                                {{ props.item.api_ResultTestCode }}
                            </td>
                            <td class="text-xs-center pa-2" v-bind:class="{ 'amber lighten-4': isSelected(props.item) }"
                                @click="selectMe(props.item)">
                                {{ props.item.api_ResultTestName }}
                            </td>
                            <td class="text-xs-center pa-2" v-bind:class="{ 'amber lighten-4': isSelected(props.item) }"
                                @click="selectMe(props.item)">
                                {{ props.item.Nat_TestName }} <span v-if="props.item.Nat_TestCode != ''">({{
                                    props.item.Nat_TestCode }})</span>
                            </td>
                            <td class="text-xs-center pa-2" v-bind:class="{ 'amber lighten-4': isSelected(props.item) }"
                                @click="selectMe(props.item)">
                                {{ props.item.api_ResultResult }}
                            </td>
                            <td class="text-xs-center pa-2" v-bind:class="{ 'amber lighten-4': isSelected(props.item) }"
                                @click="selectMe(props.item)">
                                <v-btn v-if="parseInt(props.item.api_ResultT_OrderDetailID) !== 0" color="#8ad6cc" small
                                    style="margin: 2px 4px;min-width:35px"><v-icon small dark>check</v-icon></v-btn>
                                <v-btn v-else color="#f97171" small style="margin: 2px 4px;min-width:35px"><v-icon small
                                        dark>close</v-icon></v-btn>
                            </td>
                        </template>
                    </v-data-table>
                    <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-flex>
                        </v-flex>
                    </v-layout>
                </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;
}

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

<script>
module.exports = {
    components: {

    },
    data() {
        return {
            menufilterdate: false,
            headers: [
                {
                    text: "Tanggal",
                    align: "center",
                    sortable: false,
                    value: "name",
                    width: "15%",
                    class: "pa-2 blue darken-2 white--text"
                },
                {
                    text: "BRANCH CODE",
                    align: "center",
                    sortable: false,
                    value: "name",
                    width: "15%",
                    class: "pa-2 blue darken-2 white--text"
                },
                {
                    text: "NOLAB",
                    align: "center",
                    sortable: false,
                    value: "name",
                    width: "15%",
                    class: "pa-2 blue darken-2 white--text"
                },
                {
                    text: "W. TEST CODE",
                    align: "center",
                    sortable: false,
                    value: "name",
                    width: "15%",
                    class: "pa-2 blue darken-2 white--text"
                },
                {
                    text: "W. TEST NAME",
                    align: "center",
                    sortable: false,
                    value: "name",
                    width: "10%",
                    class: "pa-2 blue darken-2 white--text"
                },
                {
                    text: "TEST NAME",
                    align: "center",
                    sortable: false,
                    value: "name",
                    width: "10%",
                    class: "pa-2 blue darken-2 white--text"
                },
                {
                    text: "RESULT",
                    align: "center",
                    sortable: false,
                    value: "name",
                    width: "30%",
                    class: "pa-2 blue darken-2 white--text"
                },
                {
                    text: "STATUS",
                    align: "center",
                    sortable: false,
                    value: "status",
                    width: "10%",
                    class: "pa-2 blue darken-2 white--text"
                }
            ],
        }
    },
    mounted() {
        this.$store.dispatch("result/lookup_branch")
        // this.$store.dispatch("result/lookup")
    },
    methods: {
        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')}`
        },
        isSelected(p) {
            return p.api_ResultID == this.$store.state.result.selected_result.api_ResultID
        },
        selectMe(selected) {
            this.$store.commit("result/update_selected_result", selected)
            this.$store.commit("result/update_last_id", selected.api_ResultID)
            this.$store.commit("result/update_selected_result_id", selected.api_ResultID)

        },

        do_search: _.debounce(function () {

            this.$store.commit("result/update_current_page", 1)
            this.$store.dispatch("result/lookup")
        }, 1000),
    },
    computed: {
        test_name: {
            get() {
                return this.$store.state.result.test_name
            },
            set(val) {
                this.$store.commit("result/update_test_name", val)
                //this.thr_search()
            }
        },
        xbranch() {
            return this.$store.state.result.branchs
        },
        selected_branch: {
            get() {
                return this.$store.state.result.selected_branch
            },
            set(val) {
                this.$store.commit("result/update_selected_branch", val)
                //this.thr_search()
            }
        },
        xresult() {
            return this.$store.state.result.results
        },
        isLoading() {
            return this.$store.state.result.lookup_status
        },
        filterComputedDateFormatted() {
            return this.formatDate(this.xdate)
        },
        xdate: {
            get() {
                return this.$store.state.result.datex
            },
            set(val) {
                this.$store.commit("result/update_datex", val)
                this.$store.commit("result/update_last_id", -1)
                //this.thr_search()
            }
        },
        xsearch: {
            get() {
                return this.$store.state.result.x_search
            },
            set(val) {
                this.$store.commit("result/update_x_search", val)
            }
        },
        curr_page: {
            get() {
                return this.$store.state.result.current_page
            },
            set(val) {
                this.$store.commit("result/update_current_page", val)
                this.$store.commit("result/update_last_id", -1)
                this.$store.dispatch("result/lookup")
            }
        },
        xtotal_page: {
            get() {
                return this.$store.state.result.total_result
            },
            set(val) {
                this.$store.commit("result/update_total_result", val)
            }
        },
    },
    watch: {
        xsearch(val, old) {
            this.xsearch = val
            this.thr_search()
        },
    },
}
</script>
