<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>

          <el-col :span="24">
            <el-form-item style="margin-bottom: 40px;" prop="title">
              <MDinput v-model="postForm.title" :maxlength="100" name="name" required>
                推送标题
              </MDinput>
            </el-form-item>

            <div class="postInfo-container">
              <el-row>
                <el-col :span="8">
                  <el-form-item style="margin-bottom: 40px;" label-width="75px" label="推送ID:" prop="id" v-if="isEdit">
                    <el-input :rows="1" v-model="postForm.id" type="text" class="article-textarea"
                              style="width: 180px" readonly v-if="isEdit"/>
                  </el-form-item>
                </el-col>

                <el-col :span="8">
                  <el-form-item style="margin-bottom: 40px;" label-width="75px" label="创建时间:" prop="create_time" v-if="isEdit">
                    <el-input :rows="1" v-model="postForm.create_time" type="text" class="article-textarea"
                              style="width: 199px" readonly v-if="isEdit"/>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item style="margin-bottom: 40px;" label-width="75px" label="创建用户:"  v-if="isEdit">
                    <el-input :rows="1" v-model="postForm.user.name" type="text" class="article-textarea"
                              style="width: 180px" readonly v-if="isEdit"/>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="8">
                  <el-form-item style="margin-bottom: 40px;" label-width="75px" label="推送落地:" prop="url" >
                    <el-input :rows="1" v-model="postForm.url" type="text" class="article-textarea"
                              style="width: 180px"/>
                  </el-form-item>
                </el-col>

                <el-col :span="8">
                  <el-form-item label-width="80px" label="推送时间:" class="postInfo-container-item" prop="push_time" >
                    <el-date-picker
                      v-model="postForm.push_time"
                      type="datetime"
                      format="yyyy-MM-dd HH:mm:ss"
                      placeholder="选择日期时间"
                      style="width: 199px"
                      :picker-options="expireTimeOption"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                </el-col>
              </el-row>
            </div>
          </el-col>
        </el-row>

        <el-form-item style="margin-bottom: 40px;" label-width="75px" label="推送内容:" prop="content">
          <el-input :rows="1" v-model="postForm.content" type="textarea" class="article-textarea" autosize placeholder="请输入内容"/>
          <span v-show="contentShortLength" class="word-counter">{{ contentShortLength }}字</span>
        </el-form-item>

        <div style="margin-bottom: 20px;">
          <el-form-item style="margin-bottom: 40px;" label-width="75px" label="推送头像:" prop="icon">
            <span v-model="uploadType"></span>
          <Upload v-model="postForm.icon" :uploadType="uploadType"/>
            </el-form-item>
        </div>
      </div>
    </el-form>

  </div>
</template>

<script>
import Upload from '@/components/Upload/singleImage3'
import MDinput from '@/components/MDinput'
import Sticky from '@/components/Sticky' // 粘性header组件
import { validateURL } from '@/utils/validate'
import { fetchPushDetail, CreatePush } from '@/api/push'

const defaultForm = {
  status: 'draft',
  title: '',
  content: '',
  icon: '',
  push_time: '',
  url: '',
  name:'',
}

export default {
  name: 'PushDetail',
  components: { MDinput, Upload, Sticky},
  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,
      userListOptions: [],
      expireTimeOption: {
        disabledDate(date){
          return date.getTime() <= Date.now();
        }
      },
      rules: {
        title: [{ validator: validateRequire, trigger: 'blur'}],
        content: [{ validator: validateRequire, trigger: 'blur'}],
        url: [{ validator: validateRequire, trigger: 'blur'}],
        icon: [{ validator: validateRequire, trigger: 'blur'}],
        push_time: [{ validator: validateRequire, trigger: 'blur'}],
      },
      tempRoute: {},
      uploadType: 1  // 图片类型
    }
  },
  computed: {
    contentShortLength() {
      return this.postForm.content.length
    },
  },
  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) {
      fetchPushDetail(id).then(response => {
        this.postForm = response.data.data.data
       }).catch(err => {
        console.log(err)
      })
    },
    submitForm() {
      this.$refs.postForm.validate(valid => {
        if (valid) {
          this.loading = true
          CreatePush(this.postForm).then(response => {
              this.$notify({
                title: '成功',
                message: response.data.data.message,
                type: 'success',
                duration: 2000
              })
              setTimeout(() => {
                this.$router.push('/push/list')
              }, 1000)

            }).catch(err => {
              this.$notify({
                title: '失败',
                message: '操作失败',
                type: 'danger',
                duration: 2000
              })
            });

            this.postForm.status = 'published'
            this.loading = false
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
@import "src/styles/mixin.scss";
.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>
