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

        <v-card class="mb-2 pa-2 searchbox">
            <v-layout>
                <v-text-field class="xs4 ma-1" label="" @keyup.enter="search_packet" placeholder="Pencarian" single-line outline v-model="query_packet" hide-details :height="height"></v-text-field>

                <!-- <v-select class="xs4 ma-1" :items="companies" 
                  item-text="M_CompanyName"
                  item-value="M_CompanyID"
                  return-object
                  v-model="selected_company"
                  label="Company" outline hide-details></v-select> -->

                <v-flex pt-1 pb-1 pl-2 pr-1 xs4>
                    <v-autocomplete label="Perusahaan" v-model="selected_company" :items="companies" :search-input.sync="query_company" auto-select-first
                        no-filter return-object clearable item-text="M_CompanyName" :loading="is_loading" no-data-text="Pilih Company"
                        hide-details outline :height="height">
                        <template slot="item" slot-scope="{ item }">
                            <v-list-tile-content>
                                <v-list-tile-title v-text="item.M_CompanyName"></v-list-tile-title>
                                <!-- <v-list-tile-sub-title v-text="'aaa'"></v-list-tile-sub-title> -->
                            </v-list-tile-content>
                        </template>

                    </v-autocomplete>
                </v-flex>

                <v-select class="xs4 ma-1" :items="mous" item-text="M_MouName" item-value="M_MouID" return-object v-model="selected_mou"
                    label="Agreement" outline hide-details :height="height"></v-select>

              <!--  <v-btn class="xs3 ma-1 one-btn-icon" color="success" @click="search_packet"> 
                    <span class="icon-search"></span> -->
                </v-btn>
                <v-btn class="xs3 ma-1 one-btn-icon" color="primary" :disabled="!selected_mou || selected_mou.M_MouIsReleased == 'Y' || selected_mou.M_MouIsVerified == 'Y'"
                    @click="new_packet">
                    <span class="icon-add"></span>
                </v-btn>
                <!-- <v-btn class="xs3 ma-1" color="info"  @click="set" >Baru</v-btn> -->
            </v-layout>
        </v-card>
        <v-card>
            <v-layout row>
                <v-flex xs12 pl-2 pr-2 pt-2 pb-2>
                    <v-data-table :headers="headers" :items="packets" :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_PacketType == "PR" ? "Profile" : "Panel" }}</td>
                            <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.T_PacketName }}</td>
                            <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.T_PacketIsNota }}</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_PacketOriginalBruto) }}</td>
                            <td class="text-xs-right pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)"><strong>{{ one_money(props.item.T_PacketPrice) }}</strong></td>
                            <td class="text-xs-center pa-0" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">
                                <v-btn color="red" :disabled="selected_mou.M_MouIsReleased == 'Y' || selected_mou.M_MouIsVerified == 'Y'" :dark="selected_mou.M_MouIsReleased != 'Y' && selected_mou.M_MouIsVerified != 'Y'"
                                    class="one-btn-icon ma-0" small @click="del_packet(props.item)"><span class="icon-del"></span></v-btn>
                                <v-btn color="blue" :disabled="selected_mou.M_MouIsReleased == 'Y' || selected_mou.M_MouIsVerified == 'Y'" :dark="selected_mou.M_MouIsReleased != 'Y' && selected_mou.M_MouIsVerified != 'Y'"
                                    class="one-btn-icon ma-0" small @click="edit_packet(props.item)"><span class="icon-edit"></span></v-btn>
                            </td>
                            <!-- <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.status}}</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>
        <one-dialog-confirm :data="selected_packet.T_PacketID" v-if="dialog_confirm" @confirm="del_me" :text="'Anda Yakin mau menghapus PAKET : ' + selected_packet.T_PacketName + ' ?'"></one-dialog-confirm>
    </v-layout>
</template>

<style scoped>
    /* .searchbox .v-input.v-text-field .v-input__slot{
   min-height:50px;
} */

    /* .v-autocomplete.v-text-field--enclosed:not(.v-text-field--solo):not(.v-text-field--single-line) .v-select__slot>input {
    margin-top: 14px;
} */

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

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

<script>
    module.exports = {
        components: {
            'one-dialog-confirm': httpVueLoader('../../../common/oneDialogConfirm.vue')
        },

        mounted() {
            this.$store.dispatch('packet/search_default_mou')
        },

        methods: {
            isSelected(p) {
                let x = this.$store.state.packet.selected_packet
                if (!x)
                    return false

                if (x.T_PacketID == p.T_PacketID)
                    return true

                return false
            },

            search_packet() {
                this.$store.commit('packet/update_selected_packet', 0)
                this.$store.dispatch("packet/search_packet")
            },

            selectMe(doc) {
                this.$store.commit('packet/update_selected_packet', doc)
                this.$store.dispatch('px/search_px')
            },

            thr_search: _.debounce(function () {
                this.$store.dispatch("packet/search_company")

            }, 700),

            new_packet() {
                this.$store.commit('packet_new/update_state_edit', false)
                this.$store.commit('packet_new/update_dialog_packet', true)
            },

            edit_packet(x) {
                this.$store.commit('packet/update_selected_packet', x)
                this.$store.commit('packet_new/update_state_edit', true)
                this.$store.commit('packet_new/update_dialog_packet', true)
            },

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

            del_packet(x) {
                this.$store.commit('packet/update_selected_packet', x)
                this.$store.commit('update_dialog_confirm', true)
            },

            del_me(x) {
                this.$store.dispatch('packet/del_packet', x.data).then(() => {
                    // Clear px list after deleting packet
                    this.$store.commit('px/update_pxs', {
                        records: [],
                        total: 0
                    })
                    this.$store.commit('px/update_estimate_total_price', 0)
                    this.$store.commit('px/update_packet_price', 0)
                })
                this.$store.commit('update_dialog_confirm', false)
            }
        },

        computed: {
            query_company: {
                get() {
                    return this.$store.state.packet.query_company
                },
                set(v) {
                    this.$store.commit('packet/update_query_company', v)
                }
            },

            search_mou: {
                get() {
                    return this.$store.state.packet.search_mou
                },
                set(v) {
                    this.$store.commit('packet/update_search_mou', v)
                }
            },

            companies: {
                get() {
                    return this.$store.state.packet.companies
                },
                set(v) {
                    this.$store.commit('packet/update_packets', v)
                }
            },

            mous: {
                get() {
                    return this.$store.state.packet.mous
                },
                set(v) {
                    this.$store.commit('packet/update_mous', v)
                }
            },

            packets: {
                get() {
                    return this.$store.state.packet.packets
                },
                set(v) {
                    this.$store.commit('packet/update_packets', v)
                }
            },

            selected_company: {
                get() {
                    return this.$store.state.packet.selected_company
                },
                set(v) {
                    this.$store.commit('packet/update_selected_company', v)
                    this.search_packet()
                }
            },
            selected_mou: {
                get() {
                    return this.$store.state.packet.selected_mou
                },
                set(v) {
                    this.$store.commit('packet/update_selected_mou', v)
                    this.$store.commit('test/update_mou_id', v.M_MouID)
                    this.search_packet()
                    console.log(v)
                }
            },
            selected_packet() {
                return this.$store.state.packet.selected_packet
            },

            query_mou: {
                get() {
                    return this.$store.state.packet.query_mou
                },
                set(v) {
                    this.$store.commit('packet/update_query_mou', v)
                }
            },

            isLoading() {
                return false
            },

            is_loading() {
                return false
            },

            dialog_confirm() {
                return this.$store.state.dialog_confirm
            },

            query_packet: {
                get() {
                    return this.$store.state.packet.query_packet
                },
                set(v) {
                    this.$store.commit('packet/update_query_packet', v)
                }
            }
        },
        data() {
            return {
                headers: [{
                        text: "TIPE PAKET",
                        align: "left",
                        sortable: false,
                        value: "mr",
                        width: "15%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "NAMA PAKET",
                        align: "left",
                        sortable: false,
                        value: "lab",
                        width: "35%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "CETAK NOTA",
                        align: "left",
                        sortable: false,
                        value: "lab",
                        width: "10%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                   
                    {
                        text: "HARGA NORMAL",
                        align: "right",
                        sortable: false,
                        value: "name",
                        width: "15%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "HARGA PAKET",
                        align: "right",
                        sortable: false,
                        value: "name",
                        width: "15%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "ACTION",
                        align: "center",
                        sortable: false,
                        value: "name",
                        width: "10%",
                        class: "pa-2 blue lighten-3 white--text"
                    }
                ],

                pagination: {
                    descending: true,
                    page: 1,
                    rowsPerPage: 5,
                    sortBy: 'T_PacketID',
                    totalItems: this.$store.state.packet.total_packet
                },

                height: 50
            };
        },

        watch: {
            query_company(val, old) {

                if (val == null || typeof val == 'undefined') val = ""
                // console.log("1-val:"+val)
                if (val == old) return
                // console.log("2-val:"+val)
                // if (! val) return
                // console.log("3-val:"+val)
                // if (val.length < 1 ) return
                // console.log("4-val:"+val)
                if (this.$store.state.packet.search_status == 1) return
                // console.log("5-val:"+val)
                this.$store.commit("packet/update_query_company", val)
                this.thr_search()
            }

        }
    }
</script>