<!-- 專案查核點 -->
<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="tendersList.checkPoint"
          :fields="fields"
          :filter="filter"
          class="text-center"
          thead-class="thClass"
          small
          responsive="lg"
          ref="selectableTable"
          striped
          bordered
          style="overflow-x: hidden; margin-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(number)="data">
            <span
              style="
                font-weight: normal;
                color: blue;
                font-size: 15px;
                cursor: pointer;
              "
              @click="OpenCheckPointEdit(data.index)"
              >{{ data.value }}</span
            >
          </template>
          <!-- 原契約工期 -->
          <template #cell(contract_date)="data">
            <span style="font-weight: normal; color: black; font-size: 15px">{{
              data.value
            }}</span>
          </template>
          <!-- 完工日期 -->
          <template #cell(final_date)="data">
            <span style="font-weight: normal; color: black; font-size: 15px">{{
              getFormattedDate(data.value)
            }}</span>
          </template>
          <!-- 累積展延天數 -->
          <template #cell(extension_day)="data">
            <span style="font-weight: normal; color: black; font-size: 15px">{{
              data.value
            }}</span>
          </template>
          <!-- 契約總工期 -->
          <template #cell(contract_totalDay)="data">
            <span style="font-weight: normal; color: black; font-size: 15px">{{
              data.value
            }}</span>
          </template>
          <!-- 展延完工日期 -->
          <template #cell(extension_finalDate)="data">
            <span style="font-weight: normal; color: black; font-size: 15px">{{
              getFormattedDate(data.value)
            }}</span>
          </template>
          <!-- 說明 -->
          <template #cell(directions)="data">
            <div class="word">{{
              data.value
            }}</div>
          </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>
    <modal
      :isOpen="CheckpointEditModalOpen"
      title="專案查核點資訊"
      @closeEvent="CheckpointEditModalOpen = false"
    >
      <checkpointEdit
        @closeEvent="CheckpointEditModalOpen = false"
        :isOpen="editOpen"
      />
    </modal>
  </div>
</template>
<script>
import functionBar from '@/views/ContractManagement/MainPage/functionBar.vue'
import checkpointEdit from '@/views/ContractManagement/ModalContent/checkpointEdit.vue'
import { mapActions, mapState } from 'vuex'
import moment from 'moment'
export default {
  components: {
    modal: () => import('@/views/Modal/index.vue'),
    functionBar,
    checkpointEdit
  },
  data () {
    return {
      filter: null,
      perPage: 8,
      currentPage: 1,
      CheckpointEditModalOpen: false,
      fields: [
        {
          key: 'number',
          label: '專案編碼',
          sortable: false,
          tdClass: 'align-middle',
          thStyle: { background: '#1d325f', color: '#ffffff' }
        },
        {
          key: 'contract_date',
          label: '原契約工期',
          sortable: false,
          tdClass: 'align-middle',
          thStyle: { background: '#1d325f', color: '#ffffff' }
        },
        {
          key: 'final_date',
          label: '完工日期',
          sortable: false,
          tdClass: 'align-middle',
          thStyle: { background: '#1d325f', color: '#ffffff' }
        },
        {
          key: 'extension_day',
          label: '累積展延天數',
          sortable: false,
          tdClass: 'text-middle',
          thStyle: { background: '#1d325f', color: '#ffffff' }
        },
        {
          key: 'contract_totalDay',
          label: '契約總工期',
          sortable: false,
          tdClass: 'align-middle',
          thStyle: { background: '#1d325f', color: '#ffffff' }
        },
        {
          key: 'extension_finalDate',
          label: '展延完工日期',
          sortable: false,
          tdClass: 'align-middle',
          thStyle: { background: '#1d325f', color: '#ffffff' }
        },
        {
          key: 'directions',
          label: '說明',
          sortable: false,
          tdClass: 'align-middle',
          thStyle: { background: '#1d325f', color: '#ffffff' }
        }
      ],
      list: [],
      extendedRecord: {},
      editOpen: ''
    }
  },
  async mounted () {
    await this.getOtherList(this.currentUser.defaultTender)
    this.list = this.tendersList.checkPoint
    this.usePermission()
  },
  methods: {
    ...mapActions('tenders', ['getOtherList', 'lookupCheckPoint']),
    getFormattedDate (date) {
      // 轉換日期格式
      let d = null
      if (date) {
        return moment(date).format('YYYY-MM-DD')
      } else {
        return d
      }
    },
    async OpenCheckPointEdit (index) {
      this.CheckpointEditModalOpen = true
      this.editOpen = this.extendedRecord.permission
      await this.lookupCheckPoint({tenderId: this.currentUser.defaultTender, checkPointId: this.tendersList.checkPoint[index]._id})
    },
    usePermission () { // 確認權限
      Object.values(this.currentUser.tenderPermissions).forEach(obj => {
        if (obj.tenderId === this.currentUser.defaultTender) {
          let permission = obj.permission
            .filter(name => name.title === '文件管理')
            .map(val => { return val.functionItem })
          for (const element of permission) {
            for (const a of element) {
              if (a.name === '展延紀錄') this.extendedRecord = a
            }
          }
        }
      })
    }
  },
  computed: {
    rows () {
      return this.list.length
    },
    ...mapState({
      currentUser: state => state.user.current,
      tendersList: (state) => state.tenders.tenderList,
      checkPoint: (state) => state.tenders.checkPoint
    })
  }
}
</script>
<style>
.thClass {
  background-color: #1d325f;
  color: white;
  position: sticky;
  top:0
}
/* 把原B排序黑醜箭頭拔掉 */
.table.b-table > thead > tr > th,
.table.b-table > tfoot > tr > td {
  background-image: none !important;
}
</style>
<style lang="scss" scoped>
.word {
  font-weight: normal;
  font-size: 15px;
  color: black;
}
/deep/ .align-middle {
  max-width: 400px;
  white-space: normal !important;
}
/deep/ .truncate {
  width: 400px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
</style>
