<template>
  <div class="container">
    <div class="LeftContainer">
      <b-row>
        <b-col class="label_col" cols="3">
          <span>標題:</span>
        </b-col>
        <b-col cols="9">
          <div>
            <b-form-input
              v-model="projectStatus.title"
              style="width: 95%; font-size: 16px"
              placeholder="請填寫標題"
            >
            </b-form-input>
          </div>
        </b-col>
      </b-row>
      <b-row style="margin-top: 12px">
        <b-col class="label_col" cols="3">
          <span>日期:</span>
        </b-col>
        <b-col cols="9">
          <div>
            <b-form-datepicker
              v-model="projectStatus.date"
              placeholder="請輸入日期"
              style="width: 95%; font-size: 16px"
            ></b-form-datepicker>
          </div>
        </b-col>
      </b-row>
      <b-row style="margin-top: 18px">
        <b-col class="label_textarea" cols="3">
          <span>現況說明:</span>
        </b-col>
        <b-col cols="9">
          <b-form-textarea
            v-model="projectStatus.description"
            placeholder="請輸入說明"
            rows="6"
            no-resize
            style="
              width: 95%;
              height: 250px;
              padding: 8px 12px 10px;
              font-size: 16px;
            "
          ></b-form-textarea>
        </b-col>
      </b-row>
      <b-row style="margin-top: 14px; float: right; padding-right: 37px">
        <b-button class="btn" @click="post">新增</b-button>
      </b-row>
    </div>
    <div class="RightContainer">
      <span style="color: black"><span style="font-weight: 900;">圖片說明</span>&nbsp;&nbsp;
        <span style="color: red; font-size: 14px;">*僅限上傳6張照片</span>
      </span>
      <hr style="margin-top: 10px; margin-bottom: 7px;" />
      <div class="pb-2" style="overflow-y: auto; overflow-x: hidden; height: 430px;">
        <div style="margin-top: 0px">
          <div v-for="img in images" :key="img._id" style="margin-top: 3px">
            <b-row style="padding-top: 10px; padding-right: 15px">
              <b-col cols="4">
                <lyztw-image-container
                  :data="img"
                  @on-delete="removeImage"
                  edit-mode
                  size="130"
                  deleteBtnIcon="delete"
                />
              </b-col>
              <b-col cols="8">
                <div style="margin-left: -18px;">
                  <b-form-textarea
                    v-model="img.description"
                    placeholder="針對圖片說明"
                    rows="6"
                    no-resize
                    style="
                      width: 100%;
                      height: 130px;
                      padding: 8px 12px 10px;
                      font-size: 16px;
                    "
                  />
                </div>
              </b-col>
            </b-row>
          </div>
        </div>
        <div v-show="this.images.length < 6">
          <lyztw-add-image-btn
            size="130"
            @on-select="addImage"
            @on-drop="addImage"
            @on-paste="addImage"
          >
            <div>拖曳或是雙擊上傳圖片</div>
          </lyztw-add-image-btn>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapActions, mapState } from 'vuex'
export default {
  data () {
    return {
      images: []
    }
  },
  async mounted () {
    await this.getOtherList(this.currentUser.defaultTender)
  },
  methods: {
    ...mapActions('tenders', ['addProjectStatus', 'getOtherList']),
    post () {
      var wasNull = false
      if (this.projectStatus.title === '') {
        wasNull = true
      }
      if (wasNull) alert('標題尚未填寫!')
      else {
        var yes = confirm('確定新增?')
        if (yes) {
          this.postProjectStatus()
          alert('新增成功')
          this.$emit('closeEvent')
        } else {
          alert('取消新增')
        }
      }
    },
    async postProjectStatus () {
      try {
        await this.addProjectStatus(this.currentUser.defaultTender)
        this.$emit('closeEvent')
        await this.getOtherList(this.currentUser.defaultTender)
      } catch (error) {
        console.log(error)
        this.$emit('closeEvent')
        alert('新增錯誤!!')
      }
    },
    addImage (imgFile) {
      if (this.images.length < 6) {
        this.images.push({
          src: imgFile.src,
          _id: Math.random().toString().split('.').pop(),
          file: imgFile.file,
          description: ''
        })
      }
      this.projectStatus.imageFile = this.images
    },
    removeImage (imgFile) {
      this.projectStatus.imageFile.splice(
        this.projectStatus.imageFile.findIndex((image) => image._id === imgFile._id),
        1
      )
    }
  },
  computed: {
    ...mapState({
      tendersList: (state) => state.tenders.tenderList,
      projectStatus: (state) => state.tenders.projectStatus,
      currentUser: (state) => state.user.current
    })
  }
}
</script>
<style scoped>
@import url("https://fonts.googleapis.com/icon?family=Material+Icons");

::-webkit-scrollbar {
  width: 3px;
}
::-webkit-scrollbar-track {
  border-radius: 10px;
  background: #f1f1f1;
}
::-webkit-scrollbar-thumb {
  border-radius: 15px;
  background: #888;
}
::-webkit-scrollbar-thumb:hover {
  background: #555;
}
.custom-select {
  color: black !important;
  box-shadow: none;
  -webkit-box-shadow: none;
}

.custom-select:disabled {
  color: black !important;
  opacity: 1;
  background-color: #ffffff;
  background-image: none !important;
  border: 0;
}

.active-tab-class {
  background-color: #000000;
}

.form-control {
  color: black !important;
}
.form-control:disabled {
  background-color: #ffffff;
  background: none;
  border: 0;
  color: black;
}

.container {
  margin-right: 20px;
  width: auto;
  height: auto;
  padding: 0px;
  overflow: hidden;
  display: flex;
  justify-content: space-between;
}
.LeftContainer {
  width: 650px;
  height: 480px;
}
.btn {
  background-color: rgb(75, 102, 255);
  font-size: 16px;
  margin-top: 15px;
  margin-left: 15px;
  padding-left: 7px;
  padding-right: 7px;
  padding-top: 5px;
  padding-bottom: 5px;
  color: #ffffff;
  box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.1);
  border: none;
}
.btn:hover {
  color: rgb(255, 255, 255);
  background-color: rgb(49, 68, 175);
  transform: translateY(1);
}
.label_col {
  margin-top: 8px;
  padding: 0 15px 0 40px;
  display: flex;
  align-items: center;
  color: black !important;
}
.col-9 {
  padding-left: 0px;
}
.RightContainer {
  width: 650px;
  height: 480px;
  background: rgba(98, 113, 221, 0.1);
  padding: 8px 20px;
  border-radius: 5px;
}
.col-8 {
  padding-left: 0px;
}
.label_textarea {
  margin-top: 8px;
  padding: 0 15px 0 40px;
  color: black !important;
}
.h-auto{
  background-color: transparent !important;
}
</style>
