<template>
    <v-layout class="fill-height" column>
        <v-dialog v-model="dialogsuccess" persistent max-width="290">
            <v-card>
                <v-card-title color="success" class="headline">Berhasil !</v-card-title>
                <v-card-text>
                    {{msgsuccess}}
                </v-card-text>
                <v-card-actions>
                    <v-spacer></v-spacer>
                    <v-btn color="green darken-1" flat @click="closeDialogSuccess">OK</v-btn>
                </v-card-actions>
            </v-card>
        </v-dialog>
        <v-card class="mb-2 pa-2 searchbox">
            <v-layout>
                <v-text-field class="xs6 ma-1" label="" placeholder="Kode" single-line outline v-model="scode" @keyup.enter="searchMethode"
                    hide-details></v-text-field>

                <v-text-field class="xs6 ma-1" label="" placeholder="Nama" single-line outline v-model="name" @keyup.enter="searchMethode" hide-details></v-text-field>
 <span  @click="setNewMethode"  class="icon-medium-fill-base xs1 white--text primary  ml-1 icon-add"></span>
            </v-layout>
        </v-card>
        <v-card>
            <v-layout row>
                <v-flex xs12 pl-2 pr-2 pt-2 pb-2>
                    <v-data-table disable-sort="true" :headers="headers" :items="methodes" :loading="isLoading" :pagination.sync="pagination" 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_MethodeCode}}</td>
                            <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.Nat_MethodeName}}</td>
                            <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.Nat_MethodeNameInResult}}</td>
                            <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.Nat_MethodePriority}}</td>
                        </template>
                        <template>
                            <div class="text-xs-center">
                                <v-pagination v-model="page" :length="15" :total-visible="4"></v-pagination>
                            </div>
                        </template>
                    </v-data-table>
                </v-flex>
            </v-layout>
        </v-card>
        <one-dialog-alert :status="openalertconfirmation" :msg="msgalertconfirmation" @forget-dialog-alert="forgetAlertConfirmation()"
            @close-dialog-alert="closeAlertConfirmation()"></one-dialog-alert>
    </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;
    }
</style>

<script>
    module.exports = {
        components: {
            'one-dialog-info': httpVueLoader('../../common/oneDialogInfo.vue'),
            'one-dialog-alert': httpVueLoader('../../common/oneDialogAlert.vue')
        },
        mounted() {
            this.$store.dispatch("methode/search", {
                name: this.name,
                scode: this.scode,
                lastid: -1
            })
        },
        methods: {
            isSelected(p) {
                return p.Nat_MethodeID == this.$store.state.methode.selected_methode.Nat_MethodeID
            },
            searchMethode() {
                this.$store.dispatch("methode/search", {
                    name: this.name,
                    scode: this.scode,
                    lastid: -1
                })
            },
            selectMe(sta) {
                if (this.$store.state.methode.no_save == 0) {
                    this.$store.commit("methode/update_selected_methode", sta)
                    this.$store.commit("methode/update_methode_name", sta.Nat_MethodeName)
                    this.$store.commit("methode/update_methode_nameresult", sta.Nat_MethodeNameInResult)
                    this.$store.commit("methode/update_code", sta.Nat_MethodeCode)
                    this.$store.commit("methode/update_priority", sta.Nat_MethodePriority)
                    this.$store.commit("methode/update_code_exist",'N')
                    this.$store.commit("methode/update_act", 'edit')
                } else {
                    this.$store.commit("methode/update_open_alert_confirmation", true)
                }

            },
            closeAlertConfirmation() {
                this.$store.commit("methode/update_open_alert_confirmation", false)
            },
            forgetAlertConfirmation() {
                this.$store.commit("methode/update_no_save", 0)
                this.$store.commit("methode/update_open_alert_confirmation", false)
            },
            updateAlert_success(val) {
                this.$store.commit("methode/update_alert_success", val)
            },
            setNewMethode() {
                this.$store.commit("methode/update_selected_methode", {})
                this.$store.commit("methode/update_methode_name", '')
                this.$store.commit("methode/update_methode_nameresult", '')
                this.$store.commit("methode/update_priority", '')
                this.$store.commit("methode/update_code", '')
                this.$store.commit("methode/update_code_exist",'N')
                this.$store.commit("methode/update_act", 'new')
            },
            closeDialogSuccess() {
                let arrmethode = this.$store.state.methode.methodes
                var idx = _.findIndex(arrmethode, item => item.Nat_MethodeID === this.$store.state.methode.last_id)
                console.log(idx)
                this.$store.dispatch("methode/search", {
                    name: this.name,
                    scode: this.scode,
                    lastid: idx
                })
                this.$store.commit("methode/update_dialog_success", false)
            }
        },
        computed: {
            dialogsuccess: {
                get() {
                    return this.$store.state.methode.dialog_success
                },
                set(val) {
                    this.$store.commit("methode/update_dialog_success", val)
                }
            },
            msgsuccess() {
                return this.$store.state.methode.msg_success
            },
            snackbar: {
                get() {
                    return this.$store.state.methode.alert_success
                },
                set(val) {
                    this.$store.commit("methode/update_alert_success", val)
                }
            },
            isLoading() {
                return this.$store.state.methode.search_status == 1
            },
            xstatuses() {
                return this.$store.state.methode.statuses
            },
            xselectedstatus: {
                get() {
                    return this.$store.state.methode.selected_status
                },
                set(val) {
                    this.$store.commit("methode/update_selected_status", val)
                }
            },
            methodes() {
                return this.$store.state.methode.methodes
            },
            openalertconfirmation: {
                get() {
                    return this.$store.state.methode.open_alert_confirmation
                },
                set(val) {
                    this.$store.commit("methode/update_open_alert_confirmation", val)
                }
            }
        },
        data() {
            return {
                msgalertconfirmation: "Perubahan yang telah dilakukan belum disimpan dong !",
                items: [],
                name: '',
                scode: '',
                page: 1,
                headers: [{
                        text: "KODE",
                        align: "center",
                        sortable: false,
                        value: "mr",
                        width: "10%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "NAMA",
                        align: "center",
                        sortable: false,
                        value: "lab",
                        width: "40%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "NAMA TERCETAK DI HASIL",
                        align: "center",
                        sortable: false,
                        value: "name",
                        width: "40%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "PRIORITAS",
                        align: "center",
                        sortable: false,
                        value: "name",
                        width: "10%",
                        class: "pa-2 blue lighten-3 white--text"
                    }
                ],
                pagination: {
                    descending: false,
                    page: 1,
                    rowsPerPage: 25,
                    sortBy: 'Nat_MethodeCode.toString()',
                    totalItems: this.$store.state.methode.total_methodes
                }
            };
        }
    }
</script>
