<template>
   <v-layout class="mb-2" column>

      <v-card   class="mb-2 pa-2 searchbox">
         <v-layout row>
               <!-- <v-text-field class="xs6 ma-1"
                             label=""
                             placeholder="Pencarian"
                             single-line
                             outline
                             v-model="query_mou"
                             hide-details
                             ></v-text-field>

               <v-select class="xs6 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 xs12>
                <v-text-field class="ma-1"
                        label=""
                        placeholder="Pencarian"
                        single-line
                        outline
                        v-model="query_price"
                        hide-details
                        @keyup.native="keySearch"
                        ></v-text-field>
            </v-flex>
            <v-spacer></v-spacer>
            <v-flex xs5 pr-0 pa-1>
                <v-btn color="success" class="ma-0 one-btn-icon" fill-height @click="search_price">
                    <span class="icon-search"></span>
                </v-btn>
                <v-btn class="ma-0 one-btn-icon" color="orange"
                    :disabled="!btn_add_enabled || prices.length > 0 || selected_mou.M_MouIsVerified == 'Y'"
                    :dark="!!btn_add_enabled && prices.length == 0 && selected_mou.M_MouIsVerified != 'Y'" @click="copy">
                     <span class="icon-import"></span>
                  </v-btn>
                <v-btn color="primary" class="ma-0 one-btn-icon" fill-height @click="add"
                    :disabled="!btn_add_enabled || selected_mou.M_MouIsVerified == 'Y'">
                    <span class="icon-add"></span>
                </v-btn>
            </v-flex>

            <!-- <v-btn class="xs3 ma-1" color="success"  @click="search_price" dark>Cari</v-btn> -->
            <!-- <v-btn class="xs3 ma-1" color="primary"  @click="add" :disabled="!btn_add_enabled" :dark="btn_add_enabled" >Tambah</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="prices"
                  :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)">
                        <span style="color:brown">{{props.item.T_TestSasCode}}</span>
                        {{ 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)">{{ one_money(props.item.T_PriceAmount) }}</td>
                     <td class="text-xs-center pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ total_disc(props.item) }}</td>
                     <td class="text-xs-center pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.T_PriceDisc }}</td>
                     <td class="text-xs-center pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ one_money(props.item.T_PriceOther) }}</td>
                     <td class="text-xs-center 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-center pa-0" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">
                        <v-btn icon @click="edit_price(props.item)" color="blue" :disabled="props.item.M_MouIsVerified == 'Y'" :dark="props.item.M_MouIsVerified != 'Y'" class="ma-0" small><v-icon small>create</v-icon></v-btn>
                        <v-btn icon @click="del_price(props.item)" color="red" :disabled="props.item.M_MouIsVerified == 'Y'" :dark="props.item.M_MouIsVerified != 'Y'" class="ma-0" small><v-icon small>clear</v-icon></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-pagination
                    v-model="curr_price_page"
                    :length="total_price_page"
                    :total-visible="10"
                    @input="change_page"
                ></v-pagination>
            </v-flex>
         </v-layout>
      </v-card>
       <one-md-price-new-dialog></one-md-price-new-dialog>
       <one-md-price-new-addon-dialog></one-md-price-new-addon-dialog>
   </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;
}

.one-btn-icon {
    font-size: 1.5em
}
</style>

<script>
module.exports = {
components : {
      'one-md-price-new-dialog': httpVueLoader('./oneMdPriceNewDialog.vue'),
      'one-md-price-new-addon-dialog': httpVueLoader('./oneMdPriceNewAddonDialog.vue')
   },

   mounted() {
      this.$store.dispatch("price/search_company")
   },

   methods : {
      one_money (p) {
         return window.one_money(p)
      },

      isSelected(p) {
         return false
      },

      search_price() {
         console.log("yiiihaaa")
         this.$store.dispatch("price/search_price")
      },

      selectMe(doc) {
         //console.log("apem")
         this.$store.commit('price/update_selected_price', doc)
         //this.search_price()
      },

      add() {
         this.$store.commit('price/update_new_price', {
            amount: 0, disc: 0, discrp: 0, other: 0, sub_total: 0, total: 0, cito: 'N'
         })
         this.$store.commit('price/update_others', {records:[], total:0})
         this.$store.commit('price/update_selected_px', {})

         this.$store.commit('price/update_edit_price', false)
         this.$store.commit('price/update_dialog_px_new', true)
      },

      edit_price(data) {

         this.$store.commit('price/update_others', {records:data.others, total:data.others.length})
         this.$store.commit('price/update_new_price', {
            amount: data.T_PriceAmount,
            disc: data.T_PriceDisc,
            discrp: data.T_PriceDiscRp,
            other: data.T_PriceOther,
            sub_total: data.T_PriceSubTotal,
            total: data.T_PriceTotal,
            cito: data.T_PriceIsCito
         })
         this.$store.commit('price/update_selected_px', data.px)
         this.$store.commit('price/update_edit_price', true)
         this.$store.commit('price/update_dialog_px_new', true)
      },

      del_price(data) {
         this.$store.dispatch('price/del_price', data.T_PriceID)
      },

      total_disc(data) {
         return this.one_money(data.T_PriceAmount - data.T_PriceSubTotal)
      },

        change_page(x) {
            this.curr_price_page = x
            this.$store.dispatch('price/search_price')
        },

        keySearch(e) {
            if (e.which == 13) {
                this.search_price()
            }
        },

        copy() {
            this.$store.commit('price_copy/update_dialog_copy', true)
        }
   },
   computed: {
        prices : {
            get () {
                return this.$store.state.price.prices
            },
            set (v) {
                this.$store.commit('price/update_prices', v)
            }
        },

        selected_price : {
            get () {
                return this.$store.state.price.selected_price
            },
            set (v) {
                this.$store.commit('price/update_selected_price', v)
            }
        },

        total_price () {
            return this.$store.state.price.total_price
        },

        total_price_page () {
            return this.$store.state.price.total_price_page
        },

        curr_price_page : {
            get () { return this.$store.state.price.curr_price_page },
            set (v) { this.$store.commit('price/update_curr_price_page', v) }
        },

        query_price : {
            get () {
                return this.$store.state.price.query_price
            },
            set (v) {
                this.$store.commit('price/update_query_price', v)
            }
        },

        isLoading () {
            return false
        },

        btn_add_enabled () {
            if (this.$store.state.price.selected_mou.M_MouID)
                return true

            return false
        },

        selected_mou () {
            return this.$store.state.price.selected_mou
        }
   },
   data() {
      return {
         headers: [
            {
               text: "NAMA",
               align: "left",
               sortable: false,
               value: "mr",
               width: "25%",
               class: "pa-2 blue lighten-3 white--text"
            },
            {
               text: "HARGA",
               align: "center",
               sortable: false,
               value: "lab",
               width: "10%",
               class: "pa-2 blue lighten-3 white--text"
            },
            {
               text: "DISKON",
               align: "center",
               sortable: false,
               value: "name",
               width: "10%",
               class: "pa-2 blue lighten-3 white--text"
            },
            {
               text: "DISKON(%)",
               align: "center",
               sortable: false,
               value: "name",
               width: "10%",
               class: "pa-2 blue lighten-3 white--text"
            },
            {
               text: "LAIN - LAIN",
               align: "center",
               sortable: false,
               value: "name",
               width: "10%",
               class: "pa-2 blue lighten-3 white--text"
            },
            {
               text: "TOTAL",
               align: "center",
               sortable: false,
               value: "name",
               width: "10%",
               class: "pa-2 blue lighten-3 white--text"
            },
            {
               text: "ACTION",
               align: "center",
               sortable: false,
               value: "name",
               width: "20%",
               class: "pa-2 blue lighten-3 white--text"
            }
         ],
         pagination:{
            descending: false,
            page: 1,
            rowsPerPage: 5,
            sortBy: 'T_TestName',
            totalItems: this.$store.state.price.total_mous
         }
      };
   }
}
</script>
