<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="searchTest" hide-details></v-text-field>

                <v-text-field class="xs6 ma-1" label="" placeholder="Nama" single-line outline v-model="name" @keyup.enter="searchTest" hide-details></v-text-field>
     
            </v-layout>
        </v-card>
        <v-card>
            <v-layout row>
                <v-flex xs12 pl-2 pr-2 pt-2 pb-2>
                    <v-data-table hide-actions :headers="headers" :items="tests" :loading="isLoading" 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.T_TestSasCode}}</td>
                            <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.T_TestName}}</td>
                            <td class="text-xs-center pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.Nat_TestTypeName}}</td>
                            <td class="text-xs-center pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.Nat_GroupName}}</td>
                            <td class="text-xs-center pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.Nat_SubGroupName}}</td>
                            <td class="text-xs-center pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.Nat_SubSubGroupName}}</td>
                            <td class="text-xs-center pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.T_TestIsNonLabs}}</td>
                        </template>
                    </v-data-table>
                    <v-divider></v-divider>
                    <v-pagination
                           style="margin-top:10px;margin-bottom:10px"
                           v-model="curr_page"
                           :length="xtotal_page"
                       
                        
                        ></v-pagination>
                </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("test/getsexreg")
            this.$store.dispatch("test/search", {
                current_page:this.curr_page,
                name: this.name,
                scode: this.scode,
                lastid: -1
            })
        },
        methods: {
            isSelected(p) {
                return p.T_TestID == this.$store.state.test.selected_test.T_TestID
            },
            searchTest() {
                this.$store.dispatch("test/search", {
                    current_page:this.curr_page,
                    name: this.name,
                    scode: this.scode,
                    lastid: -1
                })
            },
            selectMe(sta) {
                if (this.$store.state.test.no_save == 0) {
                    this.$store.commit("test/update_selected_test", sta)
                    this.$store.commit("test/update_selected_group", {
                        Nat_GroupID: sta.T_TestNat_GroupID,
                        Nat_GroupName: sta.Nat_GroupName
                    })
                    this.$store.commit("test/update_selected_type", {
                        Nat_TestTypeID: sta.Nat_TestNat_TestTypeID,
                        Nat_TestTypeName: sta.Nat_TestTypeName
                    })
                    this.$store.commit("test/update_selected_nonlab", {
                        T_TestIsNonLabID: sta.T_TestIsNonLabID,
                        T_TestIsNonLab: sta.T_TestIsNonLabs
                    })
                    this.$store.commit("test/update_test_name", sta.T_TestName)
                    this.$store.commit("test/update_selected_subgroup", {
                        Nat_SubGroupID: sta.T_TestNat_SubgroupID,
                        Nat_SubGroupName: sta.Nat_SubGroupName
                    })
                    this.$store.commit("test/update_sampletypees", [{
                            T_SampleTypeID: sta.T_TestT_SampleTypeID,
                            T_SampleTypeName: sta.T_SampleTypeName
                        }])
                      this.$store.commit("test/update_sampletype_address", {
                            T_SampleTypeID: sta.T_TestT_SampleTypeID,
                            T_SampleTypeName: sta.T_SampleTypeName
                        })

                    this.$store.commit("test/update_code", sta.T_TestSasCode)
                    this.$store.commit("test/update_shortname", sta.T_TestShortName)
                    this.$store.commit("test/update_unit", sta.Nat_UnitName)
                    this.$store.commit("test/update_flaglow", sta.Nat_TestFlagLow)
                    this.$store.commit("test/update_flaghigh", sta.Nat_TestFlagHigh)
                    this.$store.commit("test/update_namebarcode", sta.T_TestShortNameBarcode)
                    this.$store.commit("test/update_worklistname", sta.T_TestWorklistName)
                    this.$store.commit("test/update_fontsize", sta.T_TestFontSize)
                    this.$store.commit("test/update_fontcolor", sta.T_TestFontColor)
                    this.$store.commit("test/update_maxdiscount", sta.T_TestMaxDiscount)
                    this.$store.commit("test/update_flagbold", sta.T_TestIsBold)
                    this.$store.commit("test/update_flagitalic", sta.T_TestIsItalic)
                    this.$store.commit("test/update_flagquantitative", sta.T_TestIsQuantitative)
                    this.$store.commit("test/update_deltacheck", sta.T_TestIsDeltaCheck)
                    this.$store.commit("test/update_trendanalysis", sta.T_TestIsTrendAnalysis)
                    this.$store.commit("test/update_isresult", sta.T_TestIsResult)
                    this.$store.commit("test/update_isprice", sta.T_TestIsPrice)
                    this.$store.commit("test/update_printresult", sta.T_TestIsPrintResult)
                    this.$store.commit("test/update_printnote", sta.T_TestIsPrintNota)
                    this.$store.commit("test/update_isworklist", sta.T_TestIsWorklist)
                    this.$store.commit("test/update_act", 'edit')
                } else {
                    this.$store.commit("test/update_open_alert_confirmation", true)
                }

            },
            closeAlertConfirmation() {
                this.$store.commit("test/update_open_alert_confirmation", false)
            },
            forgetAlertConfirmation() {
                this.$store.commit("test/update_no_save", 0)
                this.$store.commit("test/update_open_alert_confirmation", false)
            },
            updateAlert_success(val) {
                this.$store.commit("test/update_alert_success", val)
            },
            setNewTest() {
                this.$store.commit("test/update_selected_test", {})
                this.$store.commit("test/update_selected_group", {})
                this.$store.commit("test/update_test_name", '')
                this.$store.commit("test/update_dob", '')
                this.$store.commit("test/update_selected_subgroup", {})
                this.$store.commit("test/update_address", '')
                this.$store.commit("test/update_city_address", {})
                this.$store.commit("test/update_sample", {})
                this.$store.commit("test/update_code", '')
                this.$store.commit("test/update_shortname", '')
                this.$store.commit("test/update_unit", '')
                this.$store.commit("test/update_selected_religion", {})
                this.$store.commit("test/update_startdate", '')
                this.$store.commit("test/update_enddate", '')
                this.$store.commit("test/update_selected_position", {})
                this.$store.commit("test/update_namebarcode", '')
                this.$store.commit("test/update_worklistname", '')
                this.$store.commit("test/update_fontsize", '')
                this.$store.commit("test/update_fontcolor", '')
                this.$store.commit("test/update_maxdiscount", '')

                this.$store.commit("test/update_act", 'new')
            },
            closeDialogSuccess() {
                let arrtest = this.$store.state.test.tests
                var idx = _.findIndex(arrtest, item => item.T_TestID === this.$store.state.test.last_id)
                console.log(idx)
                this.$store.dispatch("test/search", {
                    name: this.name,
                    scode: this.scode,
                    lastid: idx
                })
                this.$store.commit("test/update_dialog_success", false)
            }
        },
        computed: {
            dialogsuccess: {
                get() {
                    return this.$store.state.test.dialog_success
                },
                set(val) {
                    this.$store.commit("test/update_dialog_success", val)
                }
            },
            msgsuccess() {
                return this.$store.state.test.msg_success
            },
            snackbar: {
                get() {
                    return this.$store.state.test.alert_success
                },
                set(val) {
                    this.$store.commit("test/update_alert_success", val)
                }
            },
            isLoading() {
                return this.$store.state.test.search_status == 1
            },
            xstatuses() {
                return this.$store.state.test.statuses
            },
            xselectedstatus: {
                get() {
                    return this.$store.state.test.selected_status
                },
                set(val) {
                    this.$store.commit("test/update_selected_status", val)
                }
            },
            tests() {
                return this.$store.state.test.tests
            },
            openalertconfirmation: {
                get() {
                    return this.$store.state.test.open_alert_confirmation
                },
                set(val) {
                    this.$store.commit("test/update_open_alert_confirmation", val)
                }
            },
            curr_page: {
                get() {
                    return this.$store.state.test.current_page
                },
                set(val) { 
                    this.$store.commit("test/update_current_page",val)
                    this.$store.dispatch("test/search",{
                        current_page:val,
                        name: this.name,
                        scode: this.scode,
                        lastid: -1
                    })
                }
            },
            xtotal_page: {
                get() {
                    return this.$store.state.test.total_tests
                },
                set(val) {
                    this.$store.commit("test/update_total_tests",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: "25%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "TIPE",
                        align: "center",
                        sortable: false,
                        value: "name",
                        width: "15%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "GRUP",
                        align: "center",
                        sortable: false,
                        value: "name",
                        width: "10%",
                        class: "pa-2 blue lighten-3 white--text"
                    },

                    {
                        text: "SUB GRUP",
                        align: "center",
                        sortable: false,
                        value: "status",
                        width: "10%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "SUB SUB GRUP",
                        align: "center",
                        sortable: false,
                        value: "status",
                        width: "10%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "TIPE HASIL",
                        align: "center",
                        sortable: false,
                        value: "status",
                        width: "15%",
                        class: "pa-2 blue lighten-3 white--text"
                    }
                ],
                pagination: {
                    descending: false,
                    page: 1,
                    rowsPerPage: 100,
                    sortBy: 'T_TestID',
                    totalItems: this.$store.state.test.total_tests
                }
            };
        }
    }
</script>