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

        <v-card class="mb-2 pa-2 searchbox" color="grey">
            <!-- Loading indicator -->
            <v-progress-linear
                :active="isLoading"
                :indeterminate="isLoading"
                color="white"
                height="3"
                class="ma-0"
                style="position: absolute; top: 0; left: 0; right: 0;"
            ></v-progress-linear>
            
            <v-layout row wrap class="pa-2 mb-3">
                <h3 class="title white--text">Tambah / Ubah Detail Paket</h3>
            </v-layout>
            <v-layout>
                <v-text-field class="xs4 ma-1" label="" placeholder="Pencarian" single-line outline v-model="query_test" hide-details dark></v-text-field>

                <v-flex pt-1 pb-1 pl-2 pr-1 xs4>
                    <v-text-field label="Perusahaan & MOU" v-model="company_name" readonly outline hide-details dark></v-text-field>
                </v-flex>

                <v-btn class="xs2 ma-1 one-btn-icon" color="success" @click="search_test">
                    <span class="icon-search"></span>
                </v-btn>

            </v-layout>


        </v-card>

        <v-card color="grey">
            <v-layout row>
                <v-flex xs12 pl-2 pr-2 pt-2 pb-2>
                    <v-data-table :headers="headers" :items="tests" :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.T_TestCode }}</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-right pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.T_PriceAmount}}</td>
                            <td class="text-xs-right pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.T_PriceDisc}}</td>

                            <td class="text-xs-right pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.T_PriceDiscRp}}</td>
                            <td class="text-xs-right pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ one_money(props.item.T_PriceTotal) }}</td>
                            <td class="text-xs-right pt-0 pb-0 pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">
                                <v-btn color="success" small class="one-btn-icon ma-0" @click="addMe(props.item)">»</v-btn>
                            </td>
                        </template>
                        <template>
                            <div class="text-xs-center">
                                <v-pagination :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: 50px;
    }

    table.v-table tbody td,
    table.v-table tbody th {
        height: 40px;
    }

    table.v-table thead tr {
        height: 40px;
    }

    .one-btn-icon {
        font-size: 1.5em
    }

    /* Overwrite */

    .v-text-field--box>.v-input__control>.v-input__slot,
    .v-text-field--full-width>.v-input__control>.v-input__slot,
    .v-text-field--outline>.v-input__control>.v-input__slot {
        align-items: stretch;
        min-height: 50px
    }

    .v-text-field--box .v-label,
    .v-text-field--full-width .v-label,
    .v-text-field--outline .v-label {
        top: 12px;
    }

    .v-text-field.v-text-field--enclosed .v-input__append-inner {
        margin-top: 10px;
    }

    .v-text-field--outline.v-text-field--single-line input {
        margin-top: 6px;
    }

    .v-select.v-text-field--enclosed:not(.v-text-field--single-line) .v-select__selections {
        padding-top: 16px;
    }

    .v-text-field--outline input {
        margin-top: 14px;
    }
</style>

<script>
    module.exports = {
        components: {

        },

        mounted() {

        },

        methods: {
            isSelected(p) {
                return false
            },

            search_test() {
                this.$store.dispatch("test/search_test")
            },

            selectMe(doc) {
                this.$store.commit('test/update_selected_test', doc)

            },

            cancel() {
                return
            },

            one_money(x) {
                return window.one_money(x)
            },

            addMe(x) {
                let y = this.$store.state.px.pxs
                let z = this.$store.state.px.total_px
                let nt = this.$store.state.px.nat_tests

                let nt_found = false
                if (nt.length > 0) {
                    for (let i in x.nat_tests) {
                        if (nt.indexOf(x.nat_tests[i]) > -1)
                            nt_found = true
                    }

                    if (nt_found) {
                        alert('Pemeriksaan tersebut sudah ada !')
                        return
                    }
                }

                x.T_PacketDetailID = 0
                x.T_PacketDetailPriceAmount = x.T_PriceTotal
                x.T_PacketDetailPriceDisc = 0
                x.T_PacketDetailPriceDiscRp = 0
                x.T_PacketDetailPriceSubTotal = x.T_PriceTotal
                x.T_PacketDetailPrice = x.T_PriceTotal
                y.push(x)
                z++

                this.$store.commit('px/update_pxs', {
                    records: y,
                    total: z
                })

                // update nat tests
                for (let i in x.nat_tests) {
                    nt.push(x.nat_tests[i])
                }
                this.$store.commit('px/update_nat_tests', nt)

                // add price (update total price only)
                // Use BRUTO (original price before discounts)
                let m = this.$store.state.px.estimate_total_price
                m = parseFloat(m) + parseFloat(x.T_PacketDetailPriceAmount)
                if (m < 0) m = 0
                this.$store.commit('px/update_estimate_total_price', m)

                // DON'T update packet_price here
                // Let the watcher in oneMdPacketPxList.vue handle packet_price update
                // The watcher will respect lock state and edit mode properly

                // remove from test
                let te = this.$store.state.test.exclude
                let ti = this.$store.state.test.include
                let td = ti.indexOf(x.T_TestID)
                if (td > -1)
                    ti.splice(td, 1)
                else
                    te.push(x.T_TestID)

                this.$store.commit('test/update_exclude', te)
                this.$store.commit('test/update_include', ti)
                this.$store.dispatch('test/search_test')
            }
        },

        computed: {
            query_test: {
                get() {
                    return this.$store.state.test.query_test
                },
                set(v) {
                    this.$store.commit('test/update_query_test', v)
                }
            },

            company_name() {
                let x = this.$store.state.packet.selected_packet.T_PacketType
                // if (x == "PR") {
                //    if (this.$store.state.packet.selected_company)
                return this.$store.state.packet.selected_company.M_CompanyName +
                    " » " + this.mou_name
                // } else {
                //    if (this.$store.state.packet.default_company)
                //       return this.$store.state.packet.default_company.M_CompanyName +
                //          " » " + this.mou_name
                // }

                return ""
            },

            mou_name() {
                let x = this.$store.state.packet.selected_packet.T_PacketType
                // if (x == "PR") {
                if (this.$store.state.packet.selected_mou)
                    return this.$store.state.packet.selected_mou.M_MouName
                // } else {
                //    if (this.$store.state.packet.default_mou)
                //       return this.$store.state.packet.default_mou.M_MouName
                // }

                return ""
            },

            tests: {
                get() {
                    return this.$store.state.test.tests
                },
                set(v) {
                    this.$store.commit('test/update_tests', v)
                }
            },

            isLoading() {
                // Return true if search_status is 1 (loading/in progress)
                return this.$store.state.test.search_status === 1
            },

            is_loading() {
                return this.isLoading
            }
        },
        data() {
            return {
                headers: [{
                        text: "KODE TEST",
                        align: "left",
                        sortable: false,
                        value: "mr",
                        width: "15%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "NAMA TEST",
                        align: "left",
                        sortable: false,
                        value: "lab",
                        width: "25%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "Bruto",
                        align: "right",
                        sortable: false,
                        value: "name",
                        width: "15%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "Disc %",
                        align: "right",
                        sortable: false,
                        value: "name",
                        width: "15%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "Disc Rp",
                        align: "right",
                        sortable: false,
                        value: "name",
                        width: "15%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "HARGA",
                        align: "right",
                        sortable: false,
                        value: "name",
                        width: "15%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "ACTION",
                        align: "right",
                        sortable: false,
                        value: "name",
                        width: "10%",
                        class: "pa-2 blue lighten-3 white--text"
                    }
                ],

                pagination: {
                    descending: false,
                    page: 1,
                    rowsPerPage: 5,
                    sortBy: 'T_TestSasCode',
                    totalItems: this.$store.state.test.total_test
                }
            };
        },

        watch: {

        }
    }
</script>