<template>
  <el-dialog
    :title="!dataForm.id ? $t('brand.add') : $t('user.editBtn')"
    :close-on-click-modal="false"
    :visible.sync="visible"
    top="5vh"
    width="780px"
  >
    <el-form
      class="dialog-form"
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      :label-width="this.$i18n.t('language') === 'language' ? '150px' : '80px'"
    >
      <el-form-item :label="$t('shop.announcementTitle')" prop="title">
        <el-input size="small" maxlength="36" v-model="dataForm.title"></el-input>
      </el-form-item>

      <el-form-item :label="$t('sysManagement.type')" prop="type">
        <!-- <el-popover
          placement="top"
          width="200"
          trigger="hover"
          :content="this.$i18n.t('shopFeature.notice.announcementTips3')"
        > -->
          <!-- <i class="question-icon" slot="reference">?</i>
        </el-popover> -->
        <el-radio-group v-model="dataForm.type">
          <el-radio :label="1">{{ $t("shop.showOnPlatform") }}</el-radio>
          <el-radio :label="2">{{ $t("shop.showOnMall") }}</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item :label="$t('publics.status')" prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="1">{{ $t("publics.publicar") }}</el-radio>
          <el-radio :label="0">{{ $t("publics.cancel") }}</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item :label="$t('publics.isTop')" prop="isTop">
        <el-radio-group v-model="dataForm.isTop">
          <el-radio :label="1">{{ $t("publics.yes") }}</el-radio>
          <el-radio :label="0">{{ $t("publics.no") }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="$t('shop.noticeContent')" prop="content" class="notice-content">
        <tiny-mce v-model="dataForm.content" v-if="visible" :tinymceHeight="250"></tiny-mce>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <div class="default-btn" @click="visible = false">{{
        $t("remindPop.cancel")
      }}</div>
      <div class="default-btn primary-btn" @click="dataFormSubmit()">{{
        $t("remindPop.confirm")
      }}</div>
    </span>
  </el-dialog>
</template>

<script>
import TinyMce from '@/components/tiny-mce'
import {isHtmlNull} from '@/utils/index.js'
export default {
  data () {
    var validateTitle = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$i18n.t('shop.titCanNoBlank')))
      } else {
        callback()
      }
    }
    return {
      visible: false,
      dataForm: {
        title: null,
        content: null,
        url: null,
        status: 1,
        type: 1,
        isTop: 0
      },
      isSubmit: false,
      dataRule: {
        title: [
          { required: true, message: this.$i18n.t('shop.titCanNoBlank'), trigger: 'blur' },
          {validator: validateTitle, trigger: 'blur'}
        ]
      }
    }
  },
  components: {
    TinyMce
  },
  methods: {
    init (id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.isSubmit = false
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl('/platform/notice/info/' + this.dataForm.id),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            this.dataForm = data
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (this.isSubmit) {
            return false
          }
          if (isHtmlNull(this.dataForm.content)) {
            this.$message.error(this.$i18n.t('shopProcess.noticeTip'))
            return
          }
          this.isSubmit = true
          this.$http({
            url: this.$http.adornUrl('/platform/notice'),
            method: this.dataForm.id ? 'put' : 'post',
            data: this.$http.adornData(this.dataForm)
          }).then(({ data }) => {
            this.$message({
              message: this.$i18n.t('remindPop.success'),
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
                this.dataForm.content = ''
              }
            })
          }).catch((e) => {
            this.isSubmit = false
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.dialog-form {
  ::v-deep .el-form-item {
    margin-bottom: 4px;
  }
}
</style>

<style lang="scss" scoped>
.dialog-form {
  ::v-deep .el-form-item {
    margin-bottom: 4px;
  }
}
.question-icon {
  position: relative;
  right: 30px;
  // top: -13px;
  display: inline-block;
  width: 14px;
  height: 14px;
  line-height: 14px;
  font-size: 12px;
  text-align: center;
  color: #fff;
  background: #C8C9CC;
  border-radius: 50%;
  margin-left: 5px;
  cursor: default;
}.notice-content {
  ::v-deep .el-form-item__label:before{
    content: '*';
    color: #F56C6C;
    margin-right: 4px;
  }
}
</style>
