<!-- 工程里程碑 -->
<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>
        <div>
          <b-row>
            <b-col>
              <b-table
                :items="tenderMileStoneList.mileStone"
                :fields="fields"
                :filter="filter"
                class="text-center"
                thead-class="thClass"
                small
                responsive="lg"
                ref="selectableTable"
                striped
                bordered
                :per-page="perPage"
                :current-page="currentPage"
                style="overflow-x: hidden; margin-left: 10px"
              >
                <!-- 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="aaa(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">
                  <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>
      </div>
    </b-container>
    <modal
      :isOpen="MilestoneEditModalOpen"
      title="里程碑資訊"
      @closeEvent="MilestoneEditModalOpen = false"
    >
      <milestoneEdit
        @closeEvent="MilestoneEditModalOpen = false"
        :isOpen="editOpen"
      />
    </modal>
  </div>
</template>
<script>
import moment from 'moment'
import functionBar from '@/views/ContractManagement/MainPage/functionBar.vue'
import milestoneEdit from '@/views/ContractManagement/ModalContent/milestoneEdit.vue'
import { mapActions, mapState } from 'vuex'

export default {
  components: {
    modal: () => import('@/views/Modal/index.vue'),
    functionBar,
    milestoneEdit
  },
  data () {
    return {
      MilestoneEditModalOpen: false,
      filter: null,
      perPage: 8,
      currentPage: 1,
      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.tenderMileStoneList.mileStone
    this.usePermission()
  },
  methods: {
    ...mapActions('tenders', ['getOtherList', 'lookupMileStone']),

    async aaa (index) {
      this.MilestoneEditModalOpen = true
      this.editOpen = this.extendedRecord.permission
      await this.lookupMileStone({
        tenderId: this.currentUser.defaultTender,
        mileStoneId: this.tenderMileStoneList.mileStone[index]._id
      })
    },
    getFormattedDate (date) {
      // 轉換日期格式
      let d = null
      if (date) {
        return moment(date).format('YYYY-MM-DD')
      } else {
        return d
      }
    },
    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,
      tenderMileStoneList: (state) => state.tenders.tenderList,
      mileStone: (state) => state.tenders.mileStone
    })
  }
}
</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;
}
.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;
}
/deep/ .align-middle {
  max-width: 400px;
  white-space: normal !important;
}
/deep/ .truncate {
  width: 400px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
</style>
