<template>
  <div>
    <functionBar class="F_bar"></functionBar>
    <div>
      <div class="container" fluid></div>
      <div
          style="margin-left: 20%; margin-top: 15px; color: black; width: 60%"
        >
          <!-- 通訊錄關鍵字 -->
          <b-row style="margin-top: 11px">
            <b-col cols="12">
              <div >
                <b-form-group
                  label="關鍵字:"
                  label-for="filter-input"
                  label-cols="1"
                  label-align="right"
                  label-size="1px"
                  class="mb-0"
                >
                  <b-input-group size="1px">
                    <b-form-input
                      id="filter-input"
                      v-model="filter"
                      type="search"
                      placeholder="請輸入關鍵字"
                    ></b-form-input>
                  </b-input-group>
                </b-form-group>
              </div>
            </b-col>
          </b-row>
        </div>
        <div style="margin-left:80px;width:90%;overflow: auto;margin-top: 10px;height:600px;">
        <b-table
          id="my-table"
          class="text-center"
          thead-class="thClass"
          small
          ref="selectableTable"
          striped
          fixed
          bordered
          :fields="fields"
          :filter="filter"
          :per-page="perPage"
          :current-page="currentPage"
          :items="list"
          :sort-desc.sync="sortDesc"
          :sort-by.sync="sortBy"
          align="center"
        >
          <!-- HEAD -->
          <template #head()="{ label, field: { key, sortable }}">
            <span style="font-weight: normal; color: white; font-size: 15px">{{
              label
            }}</span>
            <template v-if="sortable">
                <b-icon font-scale="1.3" v-if="sortBy !== key" icon="arrow-down-up"></b-icon>
                <b-icon font-scale="1.3" v-else-if="sortDesc" icon="arrow-down"></b-icon>
                <b-icon font-scale="1.3" v-else icon="arrow-up"></b-icon>
            </template>
          </template>
          <!-- BODY -->
          <!-- 公司名稱 -->
          <template #cell(company)="data">
            <span style="font-weight:normal; color:black; font-size:14px">{{data.value}}</span>
          </template>

          <!-- 標案組織 -->
          <template #cell(projectTitle)="data">
            <span style="font-weight:normal; color:black; font-size:14px">{{data.value}}</span>
          </template>

          <!-- 計畫職稱 -->
          <template #cell(title)="data">
            <span style="font-weight:normal; color:black; font-size:14px">{{data.value}}</span>
          </template>

          <!-- 人員名稱 -->
          <template #cell(name)="data">
            <span style="font-weight:normal; color:blue; font-size:14px; cursor: pointer" @click="open(data.index)">{{data.value}}</span>
          </template>

          <!-- 聯絡電話 -->
          <template #cell(contactNumber)="data">
            <span style="font-weight:normal; color:black; font-size:14px">{{data.value}}</span>
          </template>

          <!-- Email -->
          <template #cell(email)="data">
            <span style="font-weight:normal; color:black; font-size:14px">{{data.value}}</span>
          </template>

          <!-- Gmail -->
          <template #cell(gmail)="data">
            <span style="font-weight:normal; color:black; font-size:14px">{{data.value}}</span>
          </template>

          <!-- line -->
          <template #cell(line)="data">
            <span style="font-weight:normal; color:black; font-size:14px">{{data.value}}</span>
          </template>

          <!-- 備註 -->
          <template #cell(remark)="data">
            <span style="font-weight:normal; color:black; font-size:14px">{{data.value}}</span>
          </template>
        </b-table>
        </div>
      </div>
      <modal
      :isOpen="EditDirectoryModalOpen"
      title="編輯聯絡人"
      @closeEvent="EditDirectoryModalOpen = false"
    >
      <editDirectory @closeEvent="EditDirectoryModalOpen = false" />
    </modal>
      <b-pagination
        v-model="currentPage"
        :total-rows="rows"
        :per-page="perPage"
        aria-controls="my-table"
      ></b-pagination>
  </div>
</template>

<script>
import functionBar from '@/views/BasicSetting/DirectorySetting/MainPage/functionBar.vue'
import { mapActions, mapState } from 'vuex'
import editDirectory from '@/views/BasicSetting/DirectorySetting/ModalContent/editDirectory.vue'

export default {
  components: {
    functionBar,
    modal: () => import('@/views/Modal/index.vue'),
    editDirectory
  },
  data () {
    return {
      EditDirectoryModalOpen: false,
      filter: null,
      perPage: 10,
      currentPage: 1,
      sortBy: null,
      sortDesc: false,
      fields: [
        {
          key: 'company',
          label: '公司名稱',
          tdClass: 'align-middle'
        },
        {
          key: 'projectTitle',
          label: '標案組織',
          tdClass: 'align-middle'
        },
        {
          key: 'title',
          label: '計畫職稱',
          tdClass: 'align-middle'
        },
        {
          key: 'name',
          label: '人員名稱',
          tdClass: 'align-middle'
        },
        {
          key: 'contactNumber',
          label: '聯絡電話',
          tdClass: 'align-middle'
        },
        {
          key: 'email',
          label: 'Email',
          tdClass: 'align-middle'
        },
        {
          key: 'gmail',
          label: 'Gmail',
          tdClass: 'align-middle'
        },
        {
          key: 'line',
          label: 'LINE ID',
          tdClass: 'align-middle'
        },
        {
          key: 'remark',
          label: '備註',
          tdClass: 'align-middle'
        }
      ],
      selectMode: 'single'
    }
  },
  async mounted () {
    await this.$store.dispatch('project/lookupDirectories')
  },
  methods: {
    ...mapActions('project', ['getDirectories']),
    async open (index) {
      this.EditDirectoryModalOpen = true
      await this.getDirectories(this.list[index]._id)
    }
  },
  computed: {
    ...mapState({
      list: (state) => state.project.list
    }),
    rows () {
      return this.list.length
    }
  }
}
</script>

<style>
.thClass {
  background-color: #1d325f;
  color: white;
}
.table.b-table > thead > tr > th,
.table.b-table > tfoot > tr > td {
  background-image: none !important;
}
</style>
<style lang="scss" scoped>
/deep/ .align-middle {
  white-space: normal !important;
  overflow-wrap: break-word;
}
.container {
  height: 30px;
  margin-top: 50px;
  margin-left: 5%;
  padding-top: 10px;
  padding-left: 1%;
  padding-right: 1%;
}
.text-center {
  width: 100%;
  height: auto;
  padding-left: 5%;
  padding-right: 5%;
  margin-top: 10px;
}
.text {
  font-weight: bold;
  overflow: hidden;
  text-overflow: ellipsis;
}
.btn1{
  background-color: rgb(75, 102, 255);
  font-size: 16px;
  color: #ffffff;
  box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.1);
  border: none;
}
.btn1:hover {
  color: rgb(255, 255, 255);
  background-color: rgb(49, 68, 175);
  transform: translateY(0);
}
.btn2 {
  background-color: #f7fafc;
  font-size: 14px;
  padding-left: 7px;
  padding-right: 7px;
  padding-top: 5px;
  padding-bottom: 5px;
  margin-left: 10px;
  color: #1d325f;
  box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.1);
}
.btn2:hover {
  color: black;
  background-color: rgb(228, 228, 228);
  transform: scale(1.1);
}
.pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  padding-bottom: 20px;
  margin-top: 20px;
  color: #383838;
}
</style>
