<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <el-form :rules="rules" ref="contentddForm" :model="updateContent" label-position="left" label-width="120px"
              style='margin-left:20px'>
      <el-row :gutter="24" >
        <el-col :xs="23" :sm="23" :md="12" :lg="8" :xl="8">
          <div class="grid-content bg-purple">
            <el-form-item  :label="$t('wxmini.wc_title')" prop="wc_title">
              <div class="block">
                <el-input style="width: 100%;" class="filter-item" v-model="updateContent.wc_title" :maxlength = 20>
                </el-input>
              </div>
            </el-form-item>
          </div>
        </el-col>
        <el-col :xs="23" :sm="23" :md="11" :lg="8" :xl="8">
          <div class="grid-content bg-purple" >
            <el-form-item  :label="$t('wxmini.wc_type')"  prop="wc_type">
              <div class="block">
                <el-select style="width: 100%;" class="filter-item" v-model="updateContent.wc_type" @change="contentChange()">
                  <el-option v-for="item in contentOption"  :key="item.wct_code" :label="item.wct_name" :value="item.wct_code" >
                  </el-option>
                </el-select>
              </div>
            </el-form-item>
          </div>
        </el-col>
        <el-col :xs="23" :sm="23" :md="12" :lg="7" :xl="7">
            <div class="grid-content bg-purple" >
              <el-form-item  :label="$t('wxmini.wc_showtime')" prop="wc_showtime" >
                <el-select style="width: 100%;" class="filter-item" v-model="updateContent.wc_showtime">
                  <el-option v-for="item in wcTypeOption" :key="item.value" :label="item.label" :value="item.value" >
                  </el-option>
                </el-select>
              </el-form-item>
            </div>
        </el-col>
      </el-row>
      <el-row :gutter="24" >
        <el-col :xs="23" :sm="23" :md="23" :lg="23" :xl="23">
          <div class="grid-content bg-purple">
            <el-form-item  :label="$t('wxmini.wc_content')" prop="wc_content">
              <div class="editor-container">
                <tinymce :height=200 ref="tinymce-comment" v-model="updateContent.wc_content"></tinymce>
              </div>
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer" align="center" style="width: 90%">
        <el-button type="primary" @click="saveUpdate()" :loading ="buttonloading">{{$t('table.save')}}</el-button>
        <el-button @click="resetForm()">{{$t('navbar.reset')}}</el-button>
      </div>
  </div>
</template>
<script>
  import waves from '@/directive/waves' // 水波纹指令
  import { parseTime } from '@/utils'
  import { deleteWXDianPingImage, updateWXContent, getWXContentTypes, getWXContentByID } from '@/api/wxmini'
  // 富文本
  import Tinymce from '@/components/Tinymce'
  import Upload from '@/components/Upload/singleImage3'
  import MDinput from '@/components/MDinput'
  import Multiselect from 'vue-multiselect'// 使用的一个多选框组件，element-ui的select不能满足所有需求
  import Sticky from '@/components/Sticky' // 粘性header组件
  export default {
    // name: 'dianpingadd111', 不支持keep-alive
    components: { Tinymce, MDinput, Upload, Multiselect, Sticky },
    directives: {
      waves
    },
    data() {
      return {
        tableKey: 0,
        buttonloading: false,
        contentOption: [],
        wcTypeOption: [],
        updateContent: {
          id: null,
          wc_title: null,
          wc_content: null,
          wc_type: null,
          wc_showtime: null,
          wct_step: null,
          wc_create_user: null,
          wc_create_date: null
        },
        updateContentOld: {
          id: null,
          wc_title: null,
          wc_content: null,
          wc_type: null,
          wc_showtime: null,
          wct_step: null
        },
        dialogPvVisible: false,
        pvData: [],
        rules: {
          wc_title: [{ required: true, message: '请输入内容标题', trigger: 'blur' }],
          wc_type: [{ required: true, message: '请选择内容类型', trigger: 'blur' }],
          wc_showtime: [{ required: true, message: '请选择内容展示时间', trigger: 'blur' }],
          wc_content: [{ required: true, message: '请输入内容', trigger: 'blur' }]
        },
        downloadLoading: false,
        view: {
          name: undefined,
          path: undefined,
          title: undefined
        }
      }
    },
    destroyed() {
      this.view.name = 'wxcontentedit'
      this.view.path = '/wxmini/wxcontentedit'
      this.view.title = 'wxcontentedit'
      this.$store.dispatch('delVisitedViews', this.view)
    },
    created() {
      getWXContentTypes().then(response => {
        this.contentOption = response.data
      })
      this.getList()
    },
    methods: {
      getList() {
        var queryPara = {
          id: this.$route.params.contentID
        }
        getWXContentByID(queryPara).then(response => {
          this.updateContent.id = this.$route.params.contentID
          this.updateContent.wc_title = response.data.wc_title
          this.updateContent.wc_content = response.data.wc_content
          this.updateContent.wc_type = response.data.wc_type
          this.updateContent.wc_showtime = response.data.wc_showtime
          this.updateContent.wct_step = response.data.wct_step
          this.updateContentOld.id = this.$route.params.contentID
          this.updateContentOld.wc_title = response.data.wc_title
          this.updateContentOld.wc_content = response.data.wc_content
          this.updateContentOld.wc_type = response.data.wc_type
          this.updateContentOld.wc_showtime = response.data.wc_showtime
          this.updateContentOld.wct_step = response.data.wct_step
          this.wcTypeOption = this.range(1, response.data.wct_step, 1)
        })
      },
      resetForm(formName) {
        this.$refs['tinymce-comment'].setContent(this.updateContentOld.wc_content)
        this.updateContent.id = this.$route.params.contentID
        this.updateContent.wc_title = this.updateContentOld.wc_title
        this.updateContent.wc_content = this.updateContentOld.wc_content
        this.updateContent.wc_type = this.updateContentOld.wc_type
        this.updateContent.wc_showtime = this.updateContentOld.wc_showtime
        this.updateContent.wct_step = this.updateContentOld.wct_step
        this.wcTypeOption = this.range(1, this.updateContentOld.wct_step, 1)
      },
      saveUpdate() {
        this.$refs['contentddForm'].validate((valid) => {
          if (valid) {
            this.buttonloading = true
            var date1 = new Date()
            this.updateContent.wc_create_date = date1.getFullYear() + '-' + (date1.getMonth() + 1) + '-' + date1.getDate() + ' ' + date1.getHours() + ':' + date1.getMinutes() + ':' + date1.getSeconds()
            this.updateContent.wc_create_user = this.$store.state.user.introduction
            updateWXContent(this.updateContent).then(response => {
              if (response.data === 1) {
                this.buttonloading = false
                this.getList()
                this.$notify({
                  title: '成功',
                  message: '保存成功!',
                  type: 'success',
                  duration: 2000
                })
              } else {
                this.buttonloading = false
                this.$notify({
                  title: '失败',
                  message: '请联系管理员！',
                  type: 'error',
                  duration: 2000
                })
              }
            })
          }
        })
      },
      contentChange() {
        this.wcTypeOption = []
        this.updateContent.wc_showtime = null
        for (let i = 0; i < this.contentOption.length; i++) {
          if (this.contentOption[i].wct_code === this.updateContent.wc_type) {
            this.wcTypeOption = this.range(1, this.contentOption[i].wct_step, 1)
            break
          }
        }
      },
      range(start, end, step) {
        var arr = []
        for (let i = start; i <= end; i++) {
          var tempOption = {
            label: null,
            value: null
          }
          if (i % step === 0) {
            tempOption.value = i
            tempOption.label = i
            arr.push(tempOption)
          }
        }
        return arr
      },
      handleRemovecomment(file, fileList) {
        var removeImge = {
          filepath: null
        }
        removeImge.filepath = file.response
        deleteWXDianPingImage(removeImge).then(response => {
          if (response.data === 1) {
            this.fileListcomment.splice(this.fileListcomment.indexOf(file), 1)
          } else {
            this.$notify({
              title: '失败',
              message: '请联系管理员！',
              type: 'error',
              duration: 2000
            })
          }
        })
      },
      formatJson(filterVal, jsonData) {
        return jsonData.map(v => filterVal.map(j => {
          if (j === 'timestamp') {
            return parseTime(v[j])
          } else {
            return v[j]
          }
        }))
      }
    }
  }
</script>
<style>
.el-form--label-left .el-form-item__label{
  text-align:right
}
.el-rate{
  line-height: 0;
  margin-top:8px;
}
.el-checkbox.el-checkbox {
    margin-left: 30px;
}
</style>
<style rel="stylesheet/scss" lang="scss" scoped>
  @import "src/styles/mixin.scss";
  .title-prompt{
    position: absolute;
    right: 0px;
    font-size: 12px;
    top:10px;
    color:#ff4949;
  }
  .createPost-container {
    position: relative;
    .createPost-main-container {
      padding: 40px 45px 20px 50px;
      .postInfo-container {
        position: relative;
        @include clearfix;
        margin-bottom: 10px;
        .postInfo-container-item {
          float: left;
        }
      }
      .editor-container {
        min-height: 500px;
        margin: 0 0 30px;
        .editor-upload-btn-container {
            text-align: right;
            margin-right: 10px;
            .editor-upload-btn {
                display: inline-block;
            }
        }
      }
    }
    .word-counter {
      width: 40px;
      position: absolute;
      right: -10px;
      top: 0px;
    }
  }
</style>