<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="user_id">
        <el-input v-model="form.user_id" :placeholder="__('会员编号')" />
      </el-form-item>
      <el-form-item :label="__('原帖编号')" prop="story_id">
        <el-input v-model="form.story_id" :placeholder="__('原帖编号')" />
      </el-form-item>
      <el-form-item :label="__('评论内容')" prop="comment_content">
        <el-input
          v-model="form.comment_content"
          :placeholder="__('评论内容')"
        />
      </el-form-item>

      <el-form-item prop="comment_state">
        <el-switch
          v-model="form.comment_state"

          active-text="正常"
          :active-value="1"

          inactive-text="屏蔽"
          :inactive-value="0"
        />
      </el-form-item>

      <el-form-item :label="__('用户编号')" prop="to_user_id">
        <el-input v-model="form.to_user_id" :placeholder="__('回复用户编号')" />
      </el-form-item>
      <el-form-item :label="__('点赞数量')" prop="comment_like_count">
        <el-input
          v-model="form.comment_like_count"
          :placeholder="__('点赞数量')"
        />
      </el-form-item>

      <el-form-item :label="__('')" prop="comment_is_show">
        <el-switch
          v-model="form.comment_is_show"

          active-text="显示"
          :active-value="1"

          inactive-text="不显示"
          :inactive-value="0"
        />
      </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/comment'

  export default {
    name: 'StoryCommentEdit',
    data() {
      return {
        form: {
          comment_is_show: 1,
          comment_state: 1,
        },
        rules: {},
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    mounted() {
      this.form = {
        unit_name: '',
      }

      this.rules = {
        comment_id: [
          { required: true, trigger: 'blur', message: this.__('评论编号') },
        ],
        user_id: [
          { required: true, trigger: 'blur', message: this.__('会员编号') },
        ],
        story_id: [
          { required: true, trigger: 'blur', message: this.__('原帖编号') },
        ],
        comment_content: [
          { required: true, trigger: 'blur', message: this.__('评论内容') },
        ],
        comment_state: [
          { required: true, trigger: 'blur', message: this.__('状态') },
        ],
        to_user_id: [
          { required: true, trigger: 'blur', message: this.__('回复用户编号') },
        ],
        comment_like_count: [
          { required: true, trigger: 'blur', message: this.__('点赞数量') },
        ],
        comment_time: [
          { required: true, trigger: 'blur', message: this.__('添加时间') },
        ],
        comment_is_show: [
          { 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 = {
            comment_id: obj.comment_id,
            user_id: obj.user_id,
            comment_like_count: obj.comment_like_count,
            to_user_id: obj.to_user_id,
            comment_state: obj.comment_state,
            comment_is_show: obj.comment_is_show,
            story_id: obj.story_id,
            comment_content: obj.comment_content,
          }
        }
        this.dialogFormVisible = true
      },
      close() {
        this.form = this.$options.data().form
        this.$refs['form'].resetFields()
        this.dialogFormVisible = false
      },
      async save() {
        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()
      },
    },
  }
</script>
