<!-- 變更契約 -->
<template>
  <div>
    <functionBar class="F_bar"></functionBar>
    <b-container fluid>
      <div style="margin-top: 70px">
        <b-row style="margin-top: 5px">
          <b-col cols="12">
            <div style="margin-left: 11.8vw; margin-top: 1.5vh; color: black; width: 55vw">
              <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>
        <b-row>
          <b-col>
            <b-table
              :items="list"
              :fields="fields"
              :filter="filter"
              class="text-center"
              thead-class="thClass"
              small
              responsive="sm"
              ref="selectableTable"
              striped
              bordered
              fixed
              style="padding-left: 10px"
              :per-page="perPage"
              :current-page="currentPage"
            >
              <!-- HEAD -->
              <template #head()="{ label }">
                <span
                  style="font-weight: normal; color: white; font-size: 15px"
                  >{{ label }}</span
                >
              </template>
              <!-- 契約變更次數 -->
              <template #cell(changeNumber)="data">
                <span
                  style="font-weight: normal; color: black; font-size: 15px"
                  >{{ data.index + 1 }}</span
                >
              </template>
              <!-- 編號名稱 -->
              <template #cell(contractNumber)="data">
                <span
                  style="font-weight: normal; color: black; font-size: 15px"
                  >{{ data.value }}</span
                >
              </template>
              <!-- 變更內容及項目 -->
              <template #cell(detail)="data">
                <router-link
                  :to="{}"
                  @click.native="pushTo(data.item)"
                  style="font-weight: normal; color: blue; font-size: 15px"
                >
                  <span
                    v-b-popover.hover="{
                      content: `${data.value}`,
                      placement: 'left',
                      title: '內容',
                    }"
                    >{{ data.value }}
                  </span>
                </router-link>
              </template>
              <!-- 核准日期 -->
              <template #cell(approvalDate)="data">
                <span
                  style="font-weight: normal; color: black; font-size: 15px"
                  >{{ getFormattedDate(data.value) }}</span
                >
              </template>
              <!-- 核准文號 -->
              <template #cell(approval_number)="data">
                <span
                  style="font-weight: normal; color: black; font-size: 15px"
                  >{{ data.value.issue_text_number }}</span
                >
              </template>
              <!-- 變更金額 -->
              <template #cell(change_amount)="data">
                <span
                  style="font-weight: normal; color: black; font-size: 15px"
                  >{{ data.value }}</span
                >
              </template>
            </b-table>
            <!-- 分頁符號 -->
            <b-pagination
              v-model="currentPage"
              pills
              :total-rows="rows"
              :per-page="perPage"
              aria-controls="my-table"
              style="justify-content: center;"
            >
            </b-pagination>
          </b-col>
        </b-row>
      </div>
    </b-container>
  </div>
</template>

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

export default {
  data () {
    return {
      filter: null,
      perPage: 8,
      currentPage: 1,
      fields: [
        {
          key: 'changeNumber',
          label: '契約變更次數',
          sortable: false,
          tdClass: 'align-middle',
          thStyle: { background: '#1d325f', color: '#ffffff' }
        },
        {
          key: 'contractNumber',
          label: '編號名稱',
          sortable: false,
          tdClass: 'align-middle',
          thStyle: { background: '#1d325f', color: '#ffffff' }
        },
        {
          key: 'detail',
          label: '變更內容及項目',
          sortable: false,
          tdClass: 'align-middle',
          thStyle: { background: '#1d325f', color: '#ffffff' }
        },
        {
          key: 'approvalDate',
          label: '核准日期',
          sortable: false,
          tdClass: 'align-middle',
          thStyle: { background: '#1d325f', color: '#ffffff' }
        },
        {
          key: 'approval_number',
          label: '核准文號',
          sortable: false,
          tdClass: 'align-middle',
          thStyle: { background: '#1d325f', color: '#ffffff' }
        },
        {
          key: 'change_amount',
          label: '變更金額',
          sortable: false,
          tdClass: 'align-middle',
          thStyle: { background: '#1d325f', color: '#ffffff' }
        }
      ],
      list: []
    }
  },
  components: {
    functionBar
  },
  async mounted () {
    await this.getOtherList(this.currentUser.defaultTender)
    this.list = this.tenderContractList.changeContract
  },
  methods: {
    ...mapActions('tenders', ['getOtherList', 'lookupChange']),
    getFormattedDate (date) {
      // 轉換日期格式
      let d = null
      if (date) {
        return moment(date).format('YYYY-MM-DD')
      } else {
        return d
      }
    },
    async pushTo (data) {
      this.$router.push({
        name: 'ContractChangingEdit',
        query: { fid: data._id }
      })
    }
  },
  computed: {
    rows () {
      return this.list.length
    },
    ...mapState({
      tenderContractList: (state) => state.tenders.tenderList,
      currentUser: (state) => state.user.current
    })
  }
}
</script>
<style scoped>
</style>
