<template>
  <v-layout class="fill-height" column>
    <v-card class="mb-2 pa-2 searchbox">
      <v-layout row wrap>
        <v-flex xs5 pa-1>
          <v-text-field
            label="search"
            placeholder="ketikkan username"
            browser-autocomplete="new-password"
            name="q_search_user_mcu"
            readonly
            outline
            hide-details
            @focus="$event.target.removeAttribute('readonly')"
            @keyup.enter="doSearch(1)"
            v-model="username"
          ></v-text-field>
        </v-flex>
        <v-flex xs5 pa-1>
          <v-autocomplete
            label="Project"
            outline
            hide-details
            return-object
            item-text="label"
            :items="projectItems"
            :search-input.sync="projectSearch"
            v-model="project"
          ></v-autocomplete>
        </v-flex>
        <v-flex xs2 pa-1>
          <v-btn color="primary" block @click="doSearch(1)">Cari</v-btn>
        </v-flex>
      </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="users" :loading="isLoading" hide-actions class="elevation-1">
            <template slot="items" slot-scope="props">
              <td class="text-xs-left pa-2" @click="selectMe(props.item)" :class="{ 'amber lighten-4': isSelected(props.item) }">{{ props.item.User_Username }}</td>
              <td class="text-xs-left pa-2" @click="selectMe(props.item)" :class="{ 'amber lighten-4': isSelected(props.item) }">{{ props.item.User_DisplayName || '-' }}</td>
              <td class="text-xs-left pa-2" @click="selectMe(props.item)" :class="{ 'amber lighten-4': isSelected(props.item) }">
                <div v-if="props.item.projects && props.item.projects.length > 0">
                  <div v-for="(p, idx) in props.item.projects" :key="idx">{{ p.project_number }} - {{ p.project_name }}</div>
                </div>
                <div v-else>-</div>
              </td>
            </template>
          </v-data-table>
        </v-flex>
      </v-layout>
    </v-card>
  </v-layout>
</template>

<script>
module.exports = {
  data() {
    return {
      headers: [
        { text: 'USERNAME', align: 'Center', sortable: false, value: 'username', class: 'blue lighten-4' },
        { text: 'NAMA', align: 'Center', sortable: false, value: 'display_name', class: 'blue lighten-4' },
        { text: 'PROJECT', align: 'Center', sortable: false, value: 'project', class: 'blue lighten-4' }
      ]
    }
  },
  mounted() {
    this.doSearch(1)
  },
  computed: {
    users() { return this.$store.state.dashboard_user.users },
    isLoading() { return this.$store.state.dashboard_user.search_status == 1 },
    username: {
      get() { return this.$store.state.dashboard_user.search_username },
      set(val) { this.$store.commit('dashboard_user/update_search_username', val) }
    },
    project: {
      get() { return this.$store.state.dashboard_user.search_project },
      set(val) { this.$store.commit('dashboard_user/update_search_project', val || { mcu_id: 'all', project_number: 'All', project_name: 'Semua Project' }) }
    },
    projectSearch: {
      get() { return this.$store.state.dashboard_user.project_keyword },
      set(val) {
        let next = (val || '').trim()
        let prev = (this.$store.state.dashboard_user.project_keyword || '').trim()
        if (next === prev) return
        this.$store.commit('dashboard_user/update_project_keyword', val)
        this.thrSearchProject(next)
      }
    },
    projectItems() {
      let base = [{ mcu_id: 'all', project_number: 'All', project_name: 'Semua Project', label: 'All - Semua Project' }]
      let records = (this.$store.state.dashboard_user.project_options || []).map(p => {
        return Object.assign({}, p, { label: p.project_number + ' - ' + p.project_name })
      })
      return base.concat(records)
    }
  },
  methods: {
    thrSearchProject: _.debounce(function(keyword) {
      this.$store.dispatch('dashboard_user/search_project', { search: keyword || '' })
    }, 300),
    doSearch() {
      this.$store.dispatch('dashboard_user/search', {
        username: this.username || '',
        project: this.project && this.project.mcu_id ? this.project.mcu_id : 'all',
        page: 1,
        limit: this.$store.state.dashboard_user.limit
      })
    },
    selectMe(item) {
      this.$store.commit('dashboard_user/update_selected_user', item)
    },
    isSelected(item) {
      return this.$store.state.dashboard_user.selected_user.User_ID == item.User_ID
    }
  }
}
</script>
