<template>
  <div>
    <div>
      <functionBar class="F_bar" @reload="reload" />
    </div>
    <b-container fluid>
    <div style="margin-top: 80px">
    <b-row style="margin-top: 11px">
      <b-col cols="12">
            <div style="width: 800px; margin-left: 350px; color: black" >
                <b-form-group
                  label="關鍵字:"
                  label-for="filter-input"
                  label-cols="2"
                  label-align="right"
                  label-size="1px"
                >
                  <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>
    <b-row>
      <b-col>
        <!-- TABLE -->
        <b-table
        :items="fullList"
        :fields="fields"
        :filter="filter"
        thead-class="thClass"
        class="text-center"
        responsive="sm"
        striped
        bordered
        fixed
        :per-page="perPage"
        :current-page="currentPage"
        >
        <!-- HEAD -->
        <template #head()="{label}">
          <span style="font-weight: normal; color: white; font-size: 15px">{{
            label
          }}</span>
        </template>
        <!-- BODY -->
        <!-- 會議日期 -->
        <template #cell(date)="data">
            <span style="font-weight: normal; color: black; font-size: 15px">
              {{ getFormattedDate(data.item.conference_date) }}
              <!-- {{ data.item.conference_time }} -->
              <!-- {{ getFormattedTime(data.item.conference_time) }} -->
            </span>
          </template>
          <!-- 會議名稱 -->
          <template #cell(conference_name)="data">
            <router-link
              :to="{
                name: 'ConferenceDetail',
                query: { fid: data.item._id },
              }"
              style="font-weight: normal; color: blue; font-size: 15px"
            >
              <span v-b-popover.hover="{content: `${data.value}`, placement: 'left' , title: '會議名稱'}"
                >{{ data.value | ellipsis }}
              </span>
            </router-link>
          </template>
          <!-- 會議類別 -->
          <template #cell(conference_classification)="data">
            <span style="font-weight: normal; color: black; font-size: 15px">
              {{ data.value }}
            </span>
          </template>
          <!-- 會議地點 -->
          <template #cell(venue)="data">
            <span style="font-weight: normal; color: black; font-size: 15px">
              {{ data.value }}
            </span>
          </template>
          <!-- 相關說明 -->
          <template #cell(description)="data">
            <span
            style="font-weight: normal; color: black; font-size: 15px"
            v-b-popover.hover="{content: `${data.value}`, placement: 'left' , title: '相關說明'}"
            >
              說明:{{ data.value | ellipsis }}
            </span>
          </template>
          <!-- 列管數 -->
          <template #cell(issueId)="data">
            <span style="font-weight: normal; color: black; font-size: 15px">
              {{ data.value.length }}
            </span>
          </template>
        </b-table>
        <!-- 分頁符號 -->
        <b-pagination
          v-model="currentPage"
          pills
          :total-rows="rows"
          :per-page="perPage"
          aria-controls="my-table"
          style="margin-top:-30px"
        >
        </b-pagination>
        </b-col>
      </b-row>
    </div>
    </div>
    </b-container>
  </div>
</template>

<script>
import { mapActions, mapState } from 'vuex'
import functionBar from '@/views/ConferenceManagement/MainPage/functionBar.vue'
import moment from 'moment'

export default {
  data () {
    return {
      filter: null,
      perPage: 8,
      currentPage: 1,
      fields: [
        {
          key: 'date',
          label: '會議時間',
          sortable: false,
          tdClass: 'align-middle',
          thStyle: {background: '#1d325f', color: '#ffffff'}
        },
        {
          key: 'conference_name',
          label: '會議名稱',
          sortable: false,
          tdClass: 'align-middle',
          thStyle: {background: '#1d325f', color: '#ffffff'}
        },
        {
          key: 'conference_classification',
          label: '會議類別',
          sortable: false,
          tdClass: 'align-middle',
          thStyle: {background: '#1d325f', color: '#ffffff'}
        },
        {
          key: 'venue',
          label: '會議地點',
          sortable: false,
          tdClass: 'align-middle',
          thStyle: {background: '#1d325f', color: '#ffffff'}
        },
        {
          key: 'description',
          label: '相關說明',
          sortable: false,
          tdClass: 'align-middle',
          thStyle: {background: '#1d325f', color: '#ffffff'}
        },
        {
          key: 'issueId',
          label: '列管數',
          sortable: false,
          tdClass: 'align-middle',
          thStyle: {background: '#1d325f', color: '#ffffff'}
        }],
      issueIdList: [],
      fullList: []
    }
  },
  components: {
    functionBar
  },
  async mounted () {
    try {
      await this.getConferenceList()
      await this.getNewConferenceList()
    } catch (err) {
      console.log(err)
    }
  },
  methods: {
    ...mapActions('conference', ['getConferenceList', 'deleteConference']),
    getFormattedTime (data) {
      const time = data.conference_time
      return time.substr(0, time.length - 3)
    },
    getFormattedDate (date) {
      // 轉換日期格式
      let d = null
      if (date) {
        return moment(date).format('YYYY-MM-DD')
      } else {
        return d
      }
    },
    async reload () {
      await this.getConferenceList()
      this.getNewConferenceList()
    },
    getNewConferenceList () { // 先簡單處理，要優化地方太多有閒時間再說
      this.fullList = []
      for (let i = 0; i < this.conferenceList.length; i++) {
        const ele = this.conferenceList[i]
        const element = this.conferenceList[i].issueId
        this.issueIdList = []
        for (let i = 0; i < element.length; i++) {
          const ele = element[i]
          if (ele.issue_status !== '解除列管') {
            this.issueIdList.push(ele)
          }
        }
        this.fullList.push({
          conference_date: ele.conference_date,
          conference_name: ele.conference_name,
          description: ele.description,
          issueId: this.issueIdList,
          venue: ele.venue,
          conference_classification: ele.conference_classification,
          _id: ele._id
        })
      }
    }
  },
  filters: {
    ellipsis (value) {
      if (value.length > 18) { return value.slice(0, 10) + '...' }
      return value
    }
  },
  computed: {
    rows () {
      return this.conferenceList.length
    },
    ...mapState({
      conferenceList: (state) => state.conference.ConferenceList
    })
  }
}
</script>

<style>
.thClass /deep/ {
  font-size: 16px;
  background-color: #1d325f;
  color: white;
}
</style>

<style lang="scss" scoped>
.pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  padding-bottom: 20px;
  margin-top: 20px;
  color: #383838;
}
.text-center {
  color: #f7fafc;
  width: 100%;
  height: 100%;
  padding-left: 5%;
  padding-right: 5%;
  margin-top: -10px;
}
.btn1 {
  background-color: rgb(75, 102, 255);
  font-size: 16px;
  margin-left: 5px;
  padding-left: 7px;
  padding-right: 7px;
  padding-top: 5px;
  padding-bottom: 5px;
  color: #ffffff;
  box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.3);
  border: none;
}
.del {
  background-color: rgb(245, 19, 19);
  font-size: 16px;
  padding-left: 7px;
  padding-right: 7px;
  padding-top: 5px;
  padding-bottom: 5px;
  color: #ffffff;
  box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.3);
  border: none;
}
</style>
