<template>
  <div>
    <functionBar class="F_bar"></functionBar>
    <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="tenderList.projectStatus"
        :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.value) }}
            </span>
          </template>
          <!-- 標題 -->
          <template #cell(title)="data">
            <span style="font-weight: normal; color: blue; font-size: 15px; cursor: pointer" @click="PushTo(data.index)">
            {{ data.value }}
            </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>
    <modal
      :is-open="EditSituationModalOpen"
      title="編輯現況"
      @closeEvent="EditSituationModalOpen = false"
    >
      <editSituation @closeEvent="EditSituationModalOpen = false" />
    </modal>
  </div>
</template>
<script>
import { mapActions, mapState } from 'vuex'
import functionBar from '@/views/BasicSetting/ProjectStatus/MainPage/functionBar.vue'
import moment from 'moment'
import editSituation from '@/views/BasicSetting/ProjectStatus/ModalContent/editSituation.vue'
export default {
  data () {
    return {
      EditSituationModalOpen: false,
      filter: null,
      keywords: [],
      selected: [], // Must be an array reference!
      perPage: 12,
      currentPage: 1,
      fields: [
        {
          key: 'date',
          label: '日期',
          sortable: true,
          tdClass: 'align-middle',
          thStyle: { background: '#1d325f', color: '#ffffff', width: '20px' }
        },
        {
          key: 'title',
          label: '標題',
          sortable: true,
          tdClass: 'align-middle',
          thStyle: { background: '#1d325f', color: '#ffffff', width: '90px' }
        }
      ],
      fullList: []
    }
  },
  async mounted () {
    await this.getOtherList(this.currentUser.defaultTender)
  },
  methods: {
    ...mapActions('tenders', ['getOtherList', 'lookupProjectStatus', 'initProjectStatus']),
    getFormattedDate (date) { // 轉換日期格式
      let d = null
      if (date) {
        return moment(date).format('YYYY-MM-DD')
      } else {
        return d
      }
    },
    async PushTo (index) {
      this.EditSituationModalOpen = true
      await this.initProjectStatus()
      await this.lookupProjectStatus({tenderId: this.currentUser.defaultTender, projectStatusId: this.tenderList.projectStatus[index]._id})
    }
  },
  components: {
    modal: () => import('@/views/Modal/index.vue'),
    functionBar,
    editSituation
  },
  computed: {
    rows () {
      return this.tenderList.projectStatus && this.tenderList.projectStatus.length || 0
    },
    ...mapState({
      tenderList: (state) => state.tenders.tenderList,
      currentUser: state => state.user.current
    })
  }
}
</script>
<style>
.thClass {
  font-size: 16px;
  background-color: #1d325f;
  color: white;
}
</style>
<style 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;
}
</style>
