<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-form-item :label="__('标题')" prop="adv_name">
        <el-input v-model="form.adv_name" :placeholder="__('标题')" />
      </el-form-item>

      <el-form-item prop="adv_is_top">
        <el-switch
          v-model="form.adv_is_top"
          active-text="置顶"
          inactive-text="正常"
          style="display: block"
        />
      </el-form-item>

      <el-form-item :label="__('广告图片')" prop="adv_image">
        <el-upload
          ref="adv_image"
          :action="action"
          :before-upload="BeforeUpload"
          class="avatar-uploader"
          :on-success="(value) => handleAvatarSuccess('adv_image', value)"
          :show-file-list="false"
        >
          <img v-if="form.adv_image" class="avatar" :src="form.adv_image" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

      <el-form-item prop="adv_enable">
        <el-switch
          v-model="form.adv_enable"
          active-text="是"
          inactive-text="否"
          style="display: block"
        />
      </el-form-item>

      <el-form-item prop="adv_open_mode">
        <el-switch
          v-model="form.adv_open_mode"
          active-text="窗口打开"
          inactive-text="弹窗打开"
          style="display: block"
        />
      </el-form-item>

      <el-form-item :label="__('地址链接')" prop="adv_link">
        <el-input v-model="form.adv_link" :placeholder="__('地址链接')" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">{{ __('取消') }}</el-button>
      <el-button type="primary" @click="save">{{ __('确定') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'

  import { doEdit } from '@/api/story/advertisement'
  import VabQuill from '@/extra/VabQuill'
  import { URL } from '@/config'

  export default {
    name: 'StoryAdvertisementEdit',
    // eslint-disable-next-line vue/no-unused-components
    components: { VabQuill },

    data() {
      return {
        action: URL.action,
        form: {
          adv_is_top: true,
        },
        rules: {},
        title: '',
        dialogFormVisible: false,
        adv_enable: false,
        adv_open_mode: false,
      }
    },
    created() {},
    mounted() {
      this.rules = {
        id: [{ required: true, trigger: 'blur', message: this.__('广告id') }],
        adv_name: [
          { required: true, trigger: 'blur', message: this.__('标题') },
        ],
        adv_is_top: [
          { required: true, trigger: 'blur', message: this.__('置顶') },
        ],
        adv_image: [
          { required: true, trigger: 'blur', message: this.__('广告图片') },
        ],
        adv_enable: [
          { required: true, trigger: 'blur', message: this.__('是否显示') },
        ],
        adv_link: [
          { required: true, trigger: 'blur', message: this.__('地址链接') },
        ],

        adv_open_mode: [
          { required: true, trigger: 'blur', message: this.__('打开方式') },
        ],
      }

      this.$forceUpdate()
    },
    methods: {
      __,
      showEdit(row) {
        if (!row) {
          this.title = this.__('添加')
        } else {
          this.title = this.__('编辑')

          const obj = Object.assign({}, row)
          this.form = {
            id: obj.id,
            adv_name: obj.adv_name,
            adv_is_top: obj.adv_is_top,
            adv_image: obj.adv_image,
            adv_enable: obj.adv_enable,
            adv_link: obj.adv_link,
            adv_open_mode: obj.adv_open_mode,
          }
        }
        this.dialogFormVisible = true
      },
      BeforeUpload(file) {
        let isRightSize = file.size / 1024 / 1024 < 2
        if (!isRightSize) {
          this.$message.error('文件大小超过 2MB')
        }
        return isRightSize
      },
      handleAvatarSuccess(name, res) {
        this.form[name] = res.data
      },
      close() {
        this.form = this.$options.data().form
        this.$refs['form'].resetFields()
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg, status } = await doEdit(this.form)
            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
