<template>
<div>
  <div v-show="$route.name === 'PaperPostCreate'">
      <b-row>
          <span>發文預設單位</span>
          <b-form-input
              id="input-lg"
              style="width: 95%"
              v-model="paramSetting.issuer"
            ></b-form-input>
      </b-row>
      <b-row>
          <span>發文預設字號</span>
          <b-form-input
              id="input-lg"
              style="width: 95%"
              v-model="paramSetting.issue_text_number"
            ></b-form-input>
      </b-row>
       <b-row>
          <span>歸檔編號</span>
          <b-form-input
              id="input-lg"
              style="width: 95%"
              v-model="paramSetting.archive_number"
            ></b-form-input>
            <!-- 自己改寫好了在這 -->
      </b-row>
      <b-row>
          <span>輸出Title</span>
          <b-form-input
              id="input-lg"
              style="width: 95%"
              v-model="paramSetting.title"
            ></b-form-input>
      </b-row>
      <b-row>
          <span>限辦天數</span>
          <b-form-input
              id="input-lg"
              style="width: 95%"
              v-model="paramSetting.issueHandlingDay"
            ></b-form-input>
      </b-row>
      <b-row>
          <span>輸出書函時，內部存檔用之名稱</span>
          <b-form-input
              id="input-lg"
              style="width: 95%"
              v-model="paramSetting.outputFileName"
            ></b-form-input>
      </b-row>

      <div class="btns2" style="float: right;padding-top: 15px">
      <b-button class="btn" @click="saveSetting"> 儲存 </b-button>
      <b-button class="btn" style="margin-left:10px" @click="closeModal"> 取消 </b-button>
    </div>
  </div>

  <div v-show="$route.name === 'PaperReceiveCreate'">
      <b-row>
          <span>限辦天數</span>
          <b-form-input
              id="input-lg"
              style="width: 95%"
              v-model="paramSetting.receiveHandlingDay"
            ></b-form-input>
      </b-row>
       <b-row>
          <span>歸檔編號</span>
          <b-form-input
              id="input-lg"
              style="width: 95%"
              v-model="paramSetting.archive_number"
            ></b-form-input>
            <!-- 自己改寫好了在這 -->
      </b-row>
      <div class="btns2" style="float: right;padding-top: 15px">
      <b-button class="btn" @click="saveSetting"> 儲存 </b-button>
      <b-button class="btn" style="margin-left:10px" @click="closeModal"> 取消 </b-button>
    </div>
  </div>
</div>
</template>

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

export default {
  data () {
    return {
      // paramSetting: {
      //   _id: '',
      //   paramsValue: [{
      //     issuer: '',
      //     issue_text_number: '',
      //     issueHandlingDay: '',
      //     title: '',
      //     outputFileName: '',
      //     receiveHandlingDay: ''
      //   }]
      // }
    }
  },
  async mounted () {
    try {
      await this.lookupProjectRoleParamSetting(this.current.projectRole)
      console.log(this.current.projectRole)
      console.log(this.paramSetting._id)
    } catch (err) {
      console.log(err)
    }
  },
  methods: {
    ...mapActions('project', ['updateProjectRoleParamSetting', 'addProjectRoleParamSettingRecord', 'lookupProjectRoleParamSetting']),
    async saveSetting () {
      var yes = confirm('確定儲存')
      if (yes) {
        await this.updateProjectRoleParamSetting({projectRoleId: this.paramSetting._id, data: this.paramSetting})
        await this.addProjectRoleParamSettingRecord(
          {
            projectRoleId: this.paramSetting._id,
            recordData: {
              staffName: this.current.name,
              todo: '修改'
            }
          }
        )
        this.officialDoc.issuer = this.paramSetting.issuer
        this.officialDoc.issue_text_number = this.paramSetting.issue_text_number
        this.officialDoc.archive_number = this.paramSetting.archive_number
        let afterProcessing = new Date()

        switch (this.officialDoc.distinguish) {
          case '發文':
            afterProcessing.setDate(new Date(this.officialDoc.issue_date).getDate() + Number(this.paramSetting.issueHandlingDay))
            this.officialDoc.handle_period = afterProcessing
            break
          case '來文':
            afterProcessing.setDate(new Date(this.officialDoc.issue_date).getDate() + Number(this.paramSetting.receiveHandlingDay))
            this.officialDoc.limited_date = afterProcessing
            break
        }

        alert('儲存成功')
        this.$emit('closeEvent')
      } else {
        alert('取消儲存')
        this.$emit('closeEvent')
      }
    },
    closeModal () {
      this.$emit('closeEvent')
    }
  },
  computed: {
    ...mapState('officialDocument', ['officialDoc']),
    ...mapState('user', ['current']),
    ...mapState({
      paramSetting: state => state.project.selectedProjectRole
    })
  }
}
</script>

<style scoped>
.btn {
  background-color: rgb(75, 102, 255);
  font-size: 14px;
  padding-left: 8px;
  padding-right: 8px;
  padding-top: 6px;
  padding-bottom: 6px;
  color: white;
  border: 0;
}
.btn:hover {
  color: black;
  background-color: rgb(228, 228, 228);
  transform: scale(1.1);
}
.form-control {
  color: black;
}
</style>
