<template>
  <div class="register" style="color: black; padding-left: 15px">
    <!-- 文件ID -->
    <b-row style="height: 50px">
      <b-col cols="12">
        <b-form-group style="font-size: 20px">
          <b-row>
            <b-col cols="2">
          <div
            style="
              width: 100%;
              padding-top: 8px;
              margin-top: 2px;
              font-weight: bold;
            "
          >
            <b-form-select
              v-model="newDoc.document_type"
              v-if="isFix"
              class="form-control"
              :options="getEnumWord('document_typeEnum', newDoc.document_type)"
              style="
                width: 150px;
                height:38px;
                float: left;
                font-size: 16px;
                padding-top: 7px;
              "
            /><span v-else>[ {{ newDoc.document_type }} ]</span>
             </div>
            </b-col>
          <b-col cols="5" style="margin-left:-100px">
          <div class="auto-input">
            <span>{{ newDoc.document_name | ellipsis}}</span>
            <b-form-input
              type="text"
              v-if="isFix"
              v-model="newDoc.document_name"
              class="my-input"
              style="
                float: left
              "
            /><div v-else style="margin-top:-26px;"><b>{{ newDoc.document_name | ellipsis}}</b></div>
          </div></b-col>
          <b-col cols="2" style="margin-left:-10px">
            <div style="margin-top:10px">
            <b-button
              v-if="isFix == true"
              variant="primary"
              class="btn3"
              style="margin-top: 2px;margin-left:5px"
              @click="isSave"
            >
              <b-icon-cursor />儲存
            </b-button>
            <b-button
              v-else size="sm"
              @click="isEdit"
              class="btn3"
              style="magin-left:5px"
              :disabled="fileManagement.permission === 1"
            >
              <b-icon icon="pencil-square"></b-icon>編輯
            </b-button>
          </div></b-col></b-row>
        </b-form-group>
      </b-col>
    </b-row>
    <!-- 文件編號 | 審查日數 | 描述-->
    <b-row style="height: 50px">
      <b-col cols="5">
        <b-form-group
          label-cols-sm="2"
          label-align-sm="left"
          style="padding-left: 0px; font-size: 20px; font-weight: bold"
          label="文件編號:"
        >
          <div style="width: 100%; padding-top: 12px; margin-left: 0px">
            {{ newDoc.document_number }}
          </div>
        </b-form-group>
      </b-col>

      <b-col cols="3">
        <b-form-group
          label-cols-sm="2"
          label-align-sm="left"
          style="margin-left: -250px; font-size: 20px; font-weight: bold"
          label="審查日數:"
        >
          <div style="width: 100%; padding-top: 12px; margin-left: -60px"></div>
        </b-form-group>
      </b-col>
      <b-col cols="4">
        <b-form-group
          label-cols-sm="2"
          label-align-sm="left"
          style="margin-left: -250px; font-size: 20px; font-weight: bold; "
          label="文件描述:"
        >
        <div
            style="width: 100%; margin-top: 10px; margin-left: -10px"
          >
         <b-form-textarea
                v-model="newDoc.description"
                v-if="isFix"
                type="text"
                class="form-control"
                rows="8"
                no-resize
                style="
                  height:130px;
                  width: 70%;
                  float: left;
                  margin-top: 0px;
                  font-size: 20px;
                  text-align: left;
                  padding-left:0px;
                  padding-top: 0px;
                " />
                <span
                v-else
                v-b-popover.hover="{content: `${newDoc.description}`, placement: 'left' , title: '文件描述'}"
                >
                  {{ newDoc.description | discription}}
                </span>
          </div>
        </b-form-group>
      </b-col>
    </b-row>
    <!-- 歸檔編號 | 修正日數 -->
    <b-row style="height: 50px">
      <b-col cols="5">
        <b-form-group
          label-cols-sm="2"
          label-align-sm="left"
          style="padding-left: 0px; font-size: 20px; font-weight: bold"
          label="歸檔編號:"
        >
          <div style="width: 100%; padding-top: 12px; margin-left: 0px">
            {{ newDoc.file_name }}
          </div>
        </b-form-group>
      </b-col>

      <b-col cols="5">
        <b-form-group
          label-cols-sm="2"
          label-align-sm="left"
          style="margin-left: -250px; font-size: 20px; font-weight: bold"
          label="修正日數:"
        >
          <div style="width: 100%; padding-top: 12px; margin-left: -60px">
            {{ newDoc.revisionDays }}
          </div>
        </b-form-group>
      </b-col>
    </b-row>
    <!-- 權責 | 版次 -->
    <b-row style="height: 50px">
      <b-col cols="5">
        <b-form-group
          label-cols-sm="2"
          label-align-sm="left"
          style="padding-left: 0px; font-size: 20px; font-weight: bold;padding-right: 50px"
          label="承辦人員:"
        ><div style="width: 100%; padding-top: 10px; margin-left: 0px;">
          <b-form-select
                type="text"
                v-if="isFix"
                v-model="newDoc.authority"
                class="form-control"
                :options="
                    staffs.map((staff) => {
                      return { text: staff.name, value: staff.name };
                })"
                style="
                  width: 120px;
                  height:38px;
                  font-size: 15px;
                  margin-right: 50px;
                  padding-top: 7px;
                " /><div v-else style="margin-top:5px,
                  margin-left: 15px">{{ newDoc.authority }}</div></div>
        </b-form-group>
      </b-col>

      <b-col cols="5">
        <b-form-group
          label-cols-sm="2"
          label-align-sm="left"
          style="margin-left: -250px; font-size: 20px; font-weight: bold"
          label="版次:"
        >
          <div
            style="width: 100%; padding-top: 12px; margin-left: -100px"
          >
          </div>
        </b-form-group>
      </b-col>
    </b-row>
  </div>
</template>

<script>
import { mapGetters, mapActions, mapState } from 'vuex'

export default {
  data () {
    return {
      isFix: false,
      fileManagement: {}
    }
  },
  async created () {
    if (this.$route.query.fid !== undefined) {
      await this.$store.dispatch(
        'newDocument/lookupDoc',
        this.$route.query.fid
      )
    }
  },
  async mounted () {
    try {
      this.staffs = await this.$store.dispatch('project/lookupStaffs', {
        selects: ['name']
      })
      this.usePermission()
    } catch (err) {
      console.log(err)
    }
  },
  methods: {
    ...mapActions('newDocument', ['patch']),
    // editDoc () {
    //   this.$router.push(
    //     {
    //       name: 'FileEdit',
    //       params: {
    //         toName: this.$route.params.toName
    //       }
    //     }
    //   )
    // },
    async isSave () {
      let wasNull = false
      if (
        this.newDoc.document_name === '' ||
        this.newDoc.document_number === ''
      ) { wasNull = true }
      if (wasNull) alert('文件名稱/文件編號尚未填寫！')
      else {
        let yes = confirm('確定儲存?')
        if (yes) {
          this.isFix = false
          this.$emit('isSaved')
          this.patchDoc()
          alert('修改成功！')
        }
      }
    },
    async patchDoc () {
      await this.patch(this.newDoc._id)
    },
    isEdit () {
      this.$emit('isEdit')
      this.isFix = true
    },
    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.fileManagement = a
            }
          }
        }
      })
    }
  },
  filters: {
    ellipsis (value) {
      if (value.length > 22) { return value.slice(0, 20) + '...' }
      return value
    },
    discription (value) {
      if (value.length > 22) { return value.slice(0, 120) + '...' }
      return value
    }
  },
  computed: {
    ...mapState({
      newDoc: (state) => state.newDocument.NewDoc,
      currentUser: (state) => state.user.current
    }),
    ...mapGetters(['getEnumWord'])
  }
}
</script>

<style lang="scss" scoped>
.form-control {
  color: black;
}
.register {
  padding-top: 5px;
}
.btn3 {
  background-color: #ffffff;
  font-size: 16px;
  padding-left: 7px;
  padding-right: 7px;
  padding-top: 5px;
  padding-bottom: 5px;
  color: rgb(75, 102, 255);
  box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.1);
  border: none;
  float: center;
  text-align: justify;
}
.auto-input{
  padding: 0;
  left: 0;
  top: 0;
  position: relative;
  display: inline-block;
  height: 38px;
  span {
    display: inline-block;
    height: 38px;
    visibility: hidden;
  }
  .my-input{
    position: absolute;
    display: inline-block;
    left: 0;
    width: 100%;
    height: 38px;
    min-width: 50px;
    font-size: 15px !important;
    padding: 0px;
    margin-top: 10px;
    margin-left: 10px;
    text-align: left;
  }
}
</style>
