<template>
  <div class="container">
    <div class="LeftContainer">
      <b-row>
        <b-col class="label_col" style="padding: 10px 15px 0 30px" cols="3">
          <span>起始日期:</span>
        </b-col>
        <b-col cols="9">
          <div style="margin-left: -20px;">
            <b-form-datepicker
              style="width: 95%; font-size: 16px"
              v-model="schedule.start"
            />
          </div>
        </b-col>
      </b-row>
      <b-row style="margin-top: 12px">
        <b-col class="label_col" style="padding: 10px 15px 0 30px" cols="3">
          <span>結束日期:</span>
        </b-col>
        <b-col cols="9">
          <div style="margin-left: -20px;">
            <b-form-datepicker
              style="width: 95%; font-size: 16px"
              v-model="schedule.end"
            />
          </div>
        </b-col>
      </b-row>
      <b-row style="margin-top: 12px">
        <b-col class="label_col" style="padding: 10px 15px 0 30px" cols="3">
          <span>標題:</span>
        </b-col>
        <b-col cols="9">
          <div style="margin-left: -20px;">
            <b-form-input
              style="width: 95%; font-size: 16px"
              placeholder="請填寫事件標題"
              v-model="schedule.title"
            />
          </div>
        </b-col>
      </b-row>
      <b-row style="margin-top: 12px">
        <b-col class="label_col" style="padding: 10px 15px 0 30px" cols="3">
          <span>類別:</span>
        </b-col>
        <b-col cols="7">
          <div style="margin-left: -20px;">
            <multiselect
              v-model="scheduleType"
              style="width: 95%; margin-top: 5px"
              placeholder="請選擇事件類別"
              deselect-label="已選擇"
              :options="eventCategory.paramsItems"
              :multiple="false"
              label="name"
              selectLabel="選擇"
              selectedLabel="已選擇"
              deselectLabel="取消選擇"
            >
              <span slot="noResult">查無此事件類別</span>
            </multiselect>
          </div>
        </b-col>
        <b-col class="mt-3" style="padding: 0; margin-left: -10px;" cols="2">
          <input
            type="checkbox"
            style="width: 15px; height: 15px"
            v-model="schedule.chronology"
            @click="checkedChronology"
          />
          大事紀
        </b-col>
      </b-row>
      <b-row style="margin-top: 18px">
        <b-col class="label_col" style="padding-left: 30px" cols="3">
          <span>說明:</span>
        </b-col>
        <b-col cols="9">
          <div style="margin-left: -20px;">
            <b-form-textarea
              v-model="schedule.thing"
              placeholder="請輸入說明"
              rows="6"
              no-resize
              style="
                width: 95%;
                height: 210px;
                padding: 8px 12px 10px;
                font-size: 16px;
              "
            />
          </div>
        </b-col>
      </b-row>
      <b-row style="margin-top: 10px; float: right; padding-right: 37px">
        <b-button class="btn" @click="eventSave">新增</b-button>
      </b-row>
    </div>
    <div class="RightContainer" v-show="this.schedule.chronology === true">
      <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: 10px;" />
      <div class="pb-2" style="overflow-y: auto; overflow-x: hidden; height: 430px;">
        <div style="margin-top: 5px">
          <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'
import Multiselect from 'vue-multiselect'
import 'vue-multiselect/dist/vue-multiselect.min.css'

export default {
  components: {
    Multiselect
  },
  data () {
    return {
      eventCategory: {
        _id: '',
        paramsItems: []
      },
      scheduleType: null,
      images: []
    }
  },
  async mounted () {
    this.eventCategory = await this.$store.dispatch(
      'paramsSetting/lookupParam',
      'schedule_classification'
    )
    this.eventCategory.paramsItems = this.eventCategory.paramsItems.filter(obj =>
      obj.name !== '會議' && obj.backgroundColor !== '#eb9114'
    )
  },
  methods: {
    ...mapActions('schedule', ['post', 'getScheduleList']),
    ...mapActions('paramsSetting', ['lookupParam']),
    eventSave () {
      let startDate = new Date(this.schedule.start)
      let endDate = new Date(this.schedule.end)
      if (
        Date.parse(startDate).valueOf() > Date.parse(endDate).valueOf() ||
        this.schedule.title === undefined ||
        this.schedule.scheduleType === undefined ||
        this.schedule.scheduleType === null ||
        this.schedule.scheduleType === ''
      ) {
        alert('「日期填寫有誤」或「尚有內容未填」!!')
      } else {
        let yes = confirm('確定新增?')
        if (yes) {
          this.postScheduleEvent()
        } else {
          alert('取消新增')
        }
      }
    },
    async postScheduleEvent () {
      try {
        await this.post()
        this.$emit('closeEvent')
        await this.getScheduleList()
      } catch (error) {
        console.log(error)
        this.$emit('closeEvent')
      }
    },
    checkedChronology () {
      try {
        if (this.schedule.chronology === false) {
          this.schedule.chronology = true
        } else {
          this.schedule.chronology = false
        }
      } catch (error) {
        // console.log(error)
      }
    },
    addImage (imgFile) {
      if (this.images.length < 6) {
        this.images.push({
          src: imgFile.src,
          _id: Math.random().toString().split('.').pop(),
          file: imgFile.file,
          description: ''
        })
      }
      this.schedule.imageFile = this.images
    },
    removeImage (imgFile) {
      this.schedule.imageFile.splice(
        this.schedule.imageFile.findIndex((image) => image._id === imgFile._id),
        1
      )
    }
  },
  computed: {
    ...mapState({
      schedule: (state) => state.schedule.Schedule,
      scheduleList: (state) => state.schedule.ScheduleList
    })
  },
  watch: {
    scheduleType: {
      handler (newVal, oldValue) {
        if (newVal === null) {
          this.schedule.scheduleType = ''
          this.schedule.color = ''
        } else {
          this.schedule.scheduleType = newVal.name
          this.schedule.color = newVal.backgroundColor
        }
      },
      immediate: true
    }
  }
}
</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;
}
.active-tab-class {
  background-color: #000000;
}
.form-control {
  color: black !important;
}
.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);
}
.container {
  margin-right: 20px;
  width: auto;
  height: auto;
  padding: 0px;
  overflow: hidden;
  display: flex;
  justify-content: space-between;
}
.LeftContainer {
  width: 600px;
  height: 520px;
}
.RightContainer {
  width: 650px;
  height: 510px;
  background: rgba(98, 113, 221, 0.1);
  padding: 8px 20px;
  border-radius: 5px;
  /* border-left-style: outset;
  border-color: #cfcaca;
  border-left-width: 2px; */
}
.label_col {
  color: black !important;
}
.col-8 {
  padding-left: 0px;
}
</style>
