<template>
  <div class="createPost-container">
    <el-form ref="postForm" :model="postForm" :rules="rules" class="form-container">
      <sticky :class-name="'sub-navbar '+postForm.status">
        <el-button v-loading="loading" style="margin-left: 10px;" type="success" @click="submitForm">保存
        </el-button>
      </sticky>

      <div class="createPost-main-container">
        <el-row :gutter="20">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>邀请文案</span>
            </div>
            <el-row>
              <el-col :span="24">
                <div class="postInfo-container">
                  <el-row>
                    <el-col :span="12">
                      <el-form-item v-if="isEdit" label-width="75px" label="文案ID:" class="postInfo-container-item">
                        <el-input
                          :rows="1"
                          v-model="postForm.id"
                          type="number"
                          class="article-textarea"
                          style="width: 230px"
                          disabled/>
                      </el-form-item>
                    </el-col>
                    <el-col :span="12">
                      <el-form-item v-if="isEdit" label-width="75px" label="创建时间:" class="postInfo-container-item">
                        <el-input
                          :rows="1"
                          v-model="postForm.create_time"
                          type="text"
                          class="article-textarea"
                          style="width: 230px"
                          disabled/>
                      </el-form-item>
                      <el-form-item v-if="isEdit" label-width="75px" label="更新时间:" class="postInfo-container-item">
                        <el-input
                          :rows="1"
                          v-model="postForm.update_time"
                          type="text"
                          class="article-textarea"
                          style="width: 230px"
                          disabled/>
                      </el-form-item>
                    </el-col>
                  </el-row>

                  <el-row>
                    <el-col :span="12">
                      <el-form-item
                        label-width="90px"
                        label="每次解锁数:"
                        class="postInfo-container-item"
                        style="margin-bottom: 20px"
                        prop="unlocks_num">
                        <el-input v-model="postForm.unlocks_num" type="number" style="width: 80%"/>
                      </el-form-item>
                    </el-col>
                    <el-col :span="12">
                      <el-form-item
                        label-width="100px"
                        label="邀请人数要求:"
                        class="postInfo-container-item"
                        style="margin-bottom: 20px"
                        prop="invitees_num">
                        <el-input v-model="postForm.invitees_num" type="number" style="width: 80%"/>
                      </el-form-item>
                    </el-col>
                  </el-row>
                  <el-form-item style="margin-bottom: 40px;" label-width="75px" label="文案内容:" porp="content">
                    <el-input
                      :rows="2"
                      v-model="postForm.content"
                      type="textarea"
                      class="article-textarea"
                      autosize
                      placeholder="使用{}替换数字"
                      maxlength="60"/>
                    <span v-show="contentShortLength" class="word-counter">{{ contentShortLength }}字</span>
                  </el-form-item>

                </div>
              </el-col>
            </el-row>
          </el-card>
        </el-row>
      </div>
    </el-form>

  </div>
</template>

<script>
import MDinput from '@/components/MDinput'
import Sticky from '@/components/Sticky' // 粘性header组件
import Dropzone from '@/components/Dropzone'
import waves from '@/directive/waves'
import { CreateAdvertise, fetchAdvertise } from '@/api/advertise'

const defaultForm = {
  status: 'draft',
  id: '',
  invitees_num: '',
  unlocks_num: '',
  content: '',
  create_time: '',
  update_time: ''

}

export default {
  name: 'AdvertiseDetail',
  components: { MDinput, Sticky, Dropzone },
  directives: { waves },
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    const validateRequire = (rule, value, callback) => {
      if (value === '') {
        this.$message({
          message: rule.field + '为必传项',
          type: 'error'
        })
        callback(new Error(rule.field + '为必传项'))
      } else {
        callback()
      }
    }
    return {
      postForm: Object.assign({}, defaultForm),
      loading: false,
      // 表单验证相关
      rules: {
        invitees_num: [{ validator: validateRequire, trigger: 'blur' }],
        unlocks_num: [{ validator: validateRequire, trigger: 'blur' }],
        content: [{ validator: validateRequire, trigger: 'blur' }]
      },
      tempRoute: {}
    }
  },
  computed: {
    contentShortLength() {
      return this.postForm.content.length
    },
    lang() {
      return this.$store.getters.language
    }
  },
  created() {
    if (this.isEdit) {
      const id = this.$route.params && this.$route.params.id
      this.fetchData(id)
    } else {
      this.postForm = Object.assign({}, defaultForm)
    }
    this.tempRoute = Object.assign({}, this.$route)
  },
  methods: {
    fetchData(id) {
      fetchAdvertise(id).then(response => {
        this.postForm = response.data.data
      }).catch(err => {
      })
    },
    submitForm() {
      this.$refs.postForm.validate(valid => {
        if (valid) {
          this.loading = true

          if (!this.postForm.content.length) {
            this.$message.error('内容不能为空')
            this.loading = false
            return false
          }

          CreateAdvertise(this.postForm).then(response => {
            if (response.data.data.code === 500) {
              this.$notify({
                title: '失败',
                message: response.data.data.message,
                type: 'danger',
                duration: 2000
              })
            } else {
              this.$notify({
                title: '成功',
                message: response.data.data.message,
                type: 'success',
                duration: 2000
              })
              setTimeout(() => {
                this.$router.push('/advertise/list')
              }, 1000)
            }
          }).catch(err => {
            this.$notify({
              title: '失败',
              message: '操作失败',
              type: 'danger',
              duration: 2000
            })
          })

          this.postForm.status = 'published'
          this.loading = false
        } else {
          return false
        }
      })
    }
  }
}

</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  @import "src/styles/mixin.scss";

  .edit-input {
    padding-right: 100px;
  }

  .real .error_icon {
    margin-top: 90px;
  }
</style>
