<template>
  <v-layout>
    <template>
      <v-dialog v-model="dialogDelete" max-width="30%">
        <v-card>
          <v-card-title class="headline grey lighten-2 pt-2 pb-2" primary-title>
            Peringatan !
          </v-card-title>
          <v-card-text class="pt-2 pb-2">
            <v-layout row>
              <v-flex xs12 d-flex>
                <v-layout row>
                  <v-flex pb-1 xs12>
                    <v-layout row>
                      <v-flex pt-2 pr-2 xs12>
                        {{deleteMessage}}
                      </v-flex>
                    </v-layout>
                  </v-flex>
                </v-layout>
              </v-flex>
            </v-layout>
          </v-card-text>
          <v-divider></v-divider>
          <v-card-actions>
            <v-spacer></v-spacer>
            <v-btn color="primary" flat @click="dialogDelete= false">
              Tutup
            </v-btn>
            <v-btn color="primary" flat @click="do_deleteInstrument()">
              Yakin lah
            </v-btn>
          </v-card-actions>
        </v-card>
      </v-dialog>
    </template>


    <v-flex xs12>
      <v-card class="scroll-container" style="/*max-height:645px;overflow: auto;*/">
        <v-toolbar color="blue lighten-3" dark height="50px">
          <v-toolbar-title>INSTRUMENT</v-toolbar-title>
          <v-spacer></v-spacer>
          <v-btn @click="addInstrument()" icon>
            <v-icon>library_add</v-icon>
          </v-btn>
        </v-toolbar>
        <v-snackbar v-model="snackbar" :color="color" :timeout="5000" :multi-line="false" :vertical="false" :top="true">
          {{msgsnackbar}}
          <v-btn flat @click="updateAlert_success(false)">
            Tutup
          </v-btn>
        </v-snackbar>
        <v-layout row style="background:#bbdefb;padding-top:5px;" justify-center>
          <v-list-tile>
            <input type="text" v-model="xsearch" class="textinput" placeholder="Cari ..." />
            </v-list-tile-content>
          </v-list-tile>
        </v-layout>
        <v-divider></v-divider>
        <div style="overflow: auto;max-height:500px;">
          <div v-for="(vs,idx) in instruments" :key="vs.Nat_InstrumentID">
            <v-layout pa-2 v-if="isSelected(vs)" row>
              <v-flex xs12 @click="selectMe(vs)">
                <v-layout row>
                  <v-flex class="boxsolid" xs3>
                    <v-tooltip right>
                      <template v-slot:activator="{ on }">
                        <span v-on="on">{{vs.Nat_InstrumentCode}}</span>
                      </template>
                      <span>{{vs.Nat_InstrumentCode}}</span>
                    </v-tooltip>
                  </v-flex>
                  <v-flex class="boxsolid" xs6>
                    <v-tooltip right>
                      <template v-slot:activator="{ on }">
                        <span v-on="on">{{vs.Nat_InstrumentName}}</span>
                      </template>
                      <span>{{vs.Nat_InstrumentName}}</span>
                    </v-tooltip>
                  </v-flex>
                  <v-flex class="boxsolid text-center" style="padding-top:10px" pl-2 pb-2 pr-2 xs3>
                    <v-layout row align-center justify-space-between>
                      <v-icon style="color:#ffffff" @click="editInstrument(vs)">edit</v-icon>
                      <v-icon style="color:#ffffff" @click="deleteInstrument(vs)">clear</v-icon>
                    </v-layout>
                  </v-flex>
                </v-layout>
              </v-flex>
            </v-layout>



            <v-layout pa-2 v-if="!isSelected(vs)" row>
              <v-flex xs12 @click="selectMe(vs)">
                <v-layout row>
                  <v-flex class="boxoutline" xs3>
                    <v-tooltip right>
                      <template v-slot:activator="{ on }">
                        <span v-on="on">{{vs.Nat_InstrumentCode}}</span>
                      </template>
                      <span>{{vs.Nat_InstrumentCode}}</span>
                    </v-tooltip>
                  </v-flex>
                  <v-flex class="boxoutline" xs6>
                    <v-tooltip right>
                      <template v-slot:activator="{ on }">
                        <span v-on="on">{{vs.Nat_InstrumentName}}</span>
                      </template>
                      <span>{{vs.Nat_InstrumentName}}</span>
                    </v-tooltip>
                  </v-flex>
                  <v-flex class="boxoutline text-center" style="padding-top:10px" pl-2 pb-2 pr-2 xs3>
                    <v-layout row align-center justify-space-between>
                      <v-icon style="color:red" @click="editInstrument(vs)">edit</v-icon>
                      <v-icon style="color:red" @click="deleteInstrument(vs)">clear</v-icon>
                    </v-layout>
                  </v-flex>
                </v-layout>
              </v-flex>
            </v-layout>
            <v-divider></v-divider>
          </div>
        </div>
        <v-layout row>
          <v-flex class="text-xs-center" pt-3 xs12>
            <p style="margin-bottom:2px;color: rgba(0,0,0,0.54);font-size:12px;">
            Menampilkan <span class="red--text">{{total_instrument}}</span> 
            data</p>
            <v-btn small v-if="xshowall === 'N'" flat @click="updateShowAll('Y')" color="primary" dark>Tampilkan Semua</v-btn>
            <v-btn small v-if="xshowall === 'Y'" flat @click="updateShowAll('N')" color="primary" dark>Batasi data</v-btn>
          </v-flex>
        </v-layout>

        <template>
          <v-layout row justify-center>
            <v-dialog v-model="dialogEdit" persistent max-width="600px">
              <v-card>
                <v-card-title>
                  <span class="headline">Form Instrument</span>
                </v-card-title>
                <v-form ref="forminstrument" v-model="valid" lazy-validation >
                   <v-card-text class="pt-0 pb-0">
                       <v-layout wrap>
                         <v-flex xs12>
                           <v-text-field v-model="instrument.Nat_InstrumentCode" label="Kode" required></v-text-field>
                         </v-flex>
                         <v-flex xs12>
                           <v-text-field v-model="instrument.Nat_InstrumentName" label="Nama"  required></v-text-field>
                         </v-flex>
                         <v-flex>
                           <p class="error pl-2 pr-2" style="color:#fff" v-if="duplicateError === true">
                              Alat {{ instrument.Nat_InstrumentCode }} / {{ instrument.Nat_InstrumentName}} sudah ada 
                           </p>
                         </v-flex>
                       </v-layout>
                   </v-card-text>
                   <v-card-actions>
                     <v-spacer></v-spacer>
                     <v-btn color="blue darken-1" @click="dialogEdit = false" flat >Tutup</v-btn>
                     <v-btn color="blue darken-1" @click="saveInstrument" flat >Simpan</v-btn>
                   </v-card-actions>
                </v-form>
              </v-card>
            </v-dialog>
          </v-layout>
        </template>

      </v-card>
    </v-flex>
  </v-layout>
</template>

<style scoped>
  .textinput {
    -webkit-transition: width 0.4s ease-in-out;
    transition: width 0.4s ease-in-out;
    background-color: white;
    background-position: 10px 10px;
    background-repeat: no-repeat;
    padding-left: 40px;
    width: 100%;
    padding: 8px 10px;
    margin-bottom: 5px;
    box-sizing: border-box;
    border: 1px solid #607d8b;

  }

  .textinput:focus {
    width: 100%;
  }

  .textinput:focus::-webkit-input-placeholder {
    color: transparent;
  }

  .textinput:focus::-moz-placeholder {
    color: transparent;
  }

  .textinput:-moz-placeholder {
    color: transparent;
  }

  .boxoutline {
    color: red;
    border: 1px solid red;
    justify-content: center;
    height: 45px;
    line-height: 45px;
    padding-left: 10px;
    background: #ffffff;
    font-size: 14px;
    font-weight: 500;
    border-radius: 1px
  }

  .boxoutline:hover {
    background: rgba(0, 0, 0, 0.07) !important;
    font-size: 15px;
    font-weight: 700;
  }

  .boxsolid {
    color: #ffffff;
    border: 1px solid #ffffff;
    justify-content: center;
    height: 45px;
    line-height: 45px;
    padding-left: 10px;
    background: #f44336;
    font-size: 14px;
    font-weight: 500;
    border-radius: 1px
  }

  .boxsolid:hover {
    background: #f44336de;
    font-size: 15px;
    font-weight: 700;
  }

  .scroll-container {
    scroll-padding: 50px 0 0 50px;
  }

  ::-webkit-scrollbar {
    width: 7px;
  }

  /* this targets the default scrollbar (compulsory) */

  ::-webkit-scrollbar-track {
    background-color: #73baf3;
  }

  /* the new scrollbar will have a flat appearance with the set background color */

  ::-webkit-scrollbar-thumb {
    background-color: #2196f3;
  }

  /* this will style the thumb, ignoring the track */

  ::-webkit-scrollbar-button {
    background-color: #0079da;
  }

  /* optionally, you can style the top and the bottom buttons (left and right for horizontal bars) */

  ::-webkit-scrollbar-corner {
    background-color: black;
  }

  /* if both the vertical and the horizontal bars appear, then perhaps the right bottom corner also needs to be styled */
</style>

<script>
  module.exports = {
    data: () => ({
      color: "success",
      valid: false,
      instrumentname: '',
      dialogDelete: false,
      dialogEdit: false,
      duplicateError: false,
      deleteMessage: "",
      xid: 0,
      xsearch: "",
      in_editing: false
    }),
    async mounted() {
       this.loadInstrument()
    },
    computed: {
      xact() {
        return this.$store.state.instrument.act
      },
      xerrorname() {
        return this.$store.state.instrument.error_name
      },
      xshowall() {
        return this.$store.state.instrument.show_all
      },
      instrument: {
         get() { return this.$store.state.instrument.selected_instrument }
         ,set(v) { this.$store.commit("instrument/update_selected_instrument", v) }
      },
      instruments() {
        return this.$store.state.instrument.instruments
      },
      total_instrument() {
        return this.$store.state.instrument.total_instrument
      },
      snackbar: {
        get() {
          return this.$store.state.instrument.alert_success
        },
        set(val) {
          this.$store.commit("instrument/update_alert_success", val)
        }
      },
      msgsnackbar() {
        return this.$store.state.instrument.msg_success
      },
      lookupstatus() {
        return this.$store.state.instrument.lookup_instrument
      }

    },
    methods: {
      async loadInstrument() {
         await this.$store.dispatch("instrument/lookup", {
           search: this.xsearch,
           all: this.xshowall
         })
         if ( this.instruments.length > 0 ) {
            let first_inst = this.instruments[0]
            this.$store.commit("instrument/update_selected_instrument",first_inst)
         }
      },
      addInstrument(inst) {
         this.in_editing = false
         this.selectMe({})
         this.dialogEdit= true
      },
      editInstrument(inst) {
         this.in_editing = true
         this.selectMe(inst)
         this.dialogEdit= true
      },
      deleteInstrument(inst) {
         this.selectMe(inst)
         this.dialogDelete = true
         this.deleteMessage = "Konfirm delete " + inst.Nat_InstrumentName
      },
      async do_deleteInstrument() {
         let prm = {Nat_InstrumentID : this.instrument.Nat_InstrumentID}
         await this.$store.dispatch("instrument/delete", prm ) 
         this.dialogDelete = false
         this.loadInstrument()
      }
      ,async saveInstrument() {
         let inst = this.instrument
         let idx = _.findIndex(this.instruments, function(i) { return i.Nat_InstrumentCode == inst.Nat_InstrumentCode })
         if ( this.in_editing ) {
            idx = _.findIndex(this.instruments, function(i) { return i.Nat_InstrumentCode == inst.Nat_InstrumentCode 
            && i.Nat_InstrumentID != inst.Nat_InstrumentID })
         }
         if (idx >= 0 ) {
            this.duplicateError = true
            let self = this
            setTimeout(function() {
               self.duplicateError = false
            },3000)
            return
         }
         let instID = 0
         let prm = {
            Nat_InstrumentID : 0,
            Nat_InstrumentName : this.instrument.Nat_InstrumentName,
            Nat_InstrumentCode : this.instrument.Nat_InstrumentCode
         }
         if (this.in_editing) {
             prm.Nat_InstrumentID = this.instrument.Nat_InstrumentID 
         }
         await this.$store.dispatch("instrument/save",prm)
         this.loadInstrument()
         this.dialogEdit = false
      },
      isSelected(p) {
        return p.Nat_InstrumentID == this.$store.state.instrument.selected_instrument.Nat_InstrumentID
      },
      selectMe(sc) {
        this.$store.commit("instrument/update_selected_instrument", sc)
      },
      updateAlert_success(val) {
        this.$store.commit("instrument/update_alert_success", val)
      },
      thr_search: _.debounce(function () {
        this.$store.dispatch("instrument/lookup", {
          search: this.xsearch,
          all: this.xshowall
        })
      }, 1000)
    },
    watch: {
      xsearch(val, old) {
        if (val !== old && this.lookupstatus !== 1) {
          this.xsearch = val
          this.thr_search()
        }
      }
    }
  }
</script>
