<template>
    <v-layout>
        <!-- ERROR DIALOG -->
        <v-dialog v-model="dialog_error" max-width="500px">
            <v-card>
                <v-card-title>
                    <span>ERROR !</span>
                    <v-spacer></v-spacer>
                </v-card-title>
                <v-divider></v-divider>
                <div class="ma-3 red--text">{{ msgError }}</div>
                <v-divider></v-divider>
                <v-card-actions>
                    <v-spacer></v-spacer>
                    <v-btn color="primary" flat @click="dialog_error = false">Close</v-btn>
                </v-card-actions>
            </v-card>
        </v-dialog>
        <!-- END ERROR DIALOG -->
        <!-- Snackbar -->
        <v-snackbar v-model="snackbar" :timeout="5000" :multi-line="false" :vertical="false" :top="true" :color="clr"
            :value="snackbar">
            {{ msgsnackbar }}
            <v-btn flat @click="snackbar = false"> Tutup </v-btn>
        </v-snackbar>
        <!-- End Snackbar -->

        <v-flex xs12>
            <v-card>
                <v-toolbar color="blue lighten-3" dark height="50px">
                    <v-toolbar-title>NAT TEST MAP</v-toolbar-title>
                    <v-spacer></v-spacer>
                </v-toolbar>
                <v-layout row style="background:white">
                    <v-layout row justify-left>
                        <v-flex xs4>
                            <v-text-field class="ma-2 ml-2" label="Cari" placeholder="Ketikkan Kode atau Nama atau Kode Mapping" 
                            v-model="xsearch" outline hide-details></v-text-field>
                        </v-flex>
                        <v-flex xs4>
                            <v-select class="ma-2 ml-2" :items="xstatus" v-model="selected_status" item-text="name"
                                outline item-value="statusid" return-object>
                            </v-select>
                        </v-flex>
                        <v-flex xs4>
                            <div class="ma-2 ml-2" style="font-size: medium; font-weight: 700; color: black;">Test is Price belum termapping {{ total_belum_price }}/{{ total_price }}</div>
                            <div class="ma-2 ml-2" style="font-size: medium; font-weight: 700; color: black;">Test is Result belum termapping {{ total_belum_result }}/{{ total_result }}</div>
                        </v-flex>
                        <v-flex xs4 class="text-xs-right pr-3 pt-5"><kbd>Tekan enter untuk simpan</kbd></v-flex>
                    </v-layout>
                </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="xnattestmap" :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.Nat_TestCode }}
                                    </td>
                                    <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">
                                        {{ props.item.Nat_TestName }}
                                    </td>
                                    <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">
                                        {{ props.item.TestMapCode }}
                                    </td>
                                    <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">
                                        <v-tooltip bottom>
                                            <template v-slot:activator="{ on, attrs }">
                                                <v-text-field 
                                                    v-bind="attrs" 
                                                    v-on="on" 
                                                    class=" 
                                                    ma-1" 
                                                    label="" 
                                                    placeholder="Kode Mapping" 
                                                    single-line outline 
                                                    v-model="props.item.codeMapping" 
                                                    @keyup.enter="saveRow(props.item)" hide-details>
                                                </v-text-field>
                                            </template>
                                            <span>Tekan enter untuk simpan</span>
                                        </v-tooltip>
                                    </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 {
                clr: "success",
                headers: [{
                        text: "KODE",
                        align: "left",
                        sortable: false,
                        value: "name",
                        width: "25%",
                        class: "blue lighten-3 white--text"
                    },
                    {
                        text: "NAMA",
                        align: "left",
                        sortable: false,
                        value: "name",
                        width: "30%",
                        class: "blue lighten-3 white--text"
                    },
                    {
                        text: "KODE MAPPING",
                        align: "left",
                        sortable: false,
                        value: "name",
                        width: "25%",
                        class: "blue lighten-3 white--text"
                    },
                    {
                        text: "",
                        align: "left",
                        sortable: false,
                        value: "name",
                        width: "20%",
                        class: "blue lighten-3 white--text"
                    },
                ],
            };
        },
        mounted() {
            this.$store.dispatch("nattest/search")
        },
        computed: {
            xnattestmap() {
                return this.$store.state.nattest.nattestmaps
            },
            isLoading() {
                return this.$store.state.nattest.search_status
            },
            curr_page: {
                get() {
                    return this.$store.state.nattest.current_page
                },
                set(val) {
                    this.$store.commit("nattest/update_current_page", val)
                    this.$store.commit("nattest/update_last_id", -1)
                    this.$store.dispatch("nattest/search")
                }
            },
            xtotal_page: {
                get() {
                    return this.$store.state.nattest.total_nattestmaps
                },
                set(val) {
                    this.$store.commit("nattest/update_total_nattestmaps", val)
                }
            },
            xsearch: {
                get() {
                    return this.$store.state.nattest.x_search
                },
                set(val) {
                    this.$store.commit("nattest/update_x_search", val)
                }
            },
            snackbar: {
                get() {
                    return this.$store.state.nattest.alert_success
                },
                set(val) {
                    this.$store.commit("nattest/update_alert_success", val)
                }
            },
            msgsnackbar() {
                return this.$store.state.nattest.msg_success
            },
            dialog_error: {
                get() {
                    return this.$store.state.nattest.alert_error
                },
                set(val) {
                    this.$store.commit("nattest/update_alert_error", val)
                },
            },
            msgError() {
                return this.$store.state.nattest.error_message
            },
            xstatus() {
                return this.$store.state.nattest.status
            },
            selected_status: {
                get() {
                    return this.$store.state.nattest.selected_status
                },
                set(val) {
                    this.$store.commit("nattest/update_selected_status", val)
                    this.$store.dispatch("nattest/search")
                }
            },
            total_result: {
                get() {
                    return this.$store.state.nattest.total_result
                },
                set(val) {
                    this.$store.commit("nattest/update_total_result", val)
                }
            },
            total_belum_result: {
                get() {
                    return this.$store.state.nattest.total_belum_result
                },
                set(val) {
                    this.$store.commit("nattest/update_total_belum_result", val)
                }
            },
            total_price: {
                get() {
                    return this.$store.state.nattest.total_price
                },
                set(val) {
                    this.$store.commit("nattest/update_total_price", val)
                }
            },
            total_belum_price: {
                get() {
                    return this.$store.state.nattest.total_belum_price
                },
                set(val) {
                    this.$store.commit("nattest/update_total_belum_price", val)
                }
            }
        },
        methods: {
            saveRow(item){
                this.$store.dispatch("nattest/saverow", item)
            },
            isSelected(p) {
                return p.Nat_TestID == this.$store.state.nattest.selected_nattestmap.Nat_TestID
            },
            selectMe(selected) {
                this.$store.commit("nattest/update_selected_nattestmap", selected)
                this.$store.commit("nattest/update_last_id", selected.Nat_TestID)

            },
            thr_search: _.debounce(function () {
                this.$store.dispatch("nattest/search")
            }, 1000),
        },
        watch: {
            xsearch(val, old) {
                this.xsearch = val
                this.thr_search()
            },
        }
    }
</script>