<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="message_id">
        <el-input v-model="form.message_id" :placeholder="__('消息编号')" />
      </el-form-item>
      <el-form-item :label="__('分类编号')" prop="category_id">
        <el-input v-model="form.category_id" :placeholder="__('分类编号')" />
      </el-form-item>
      <el-form-item :label="__('消息内容')" prop="category_content">
        <el-input
          v-model="form.category_content"
          :placeholder="__('消息内容')"
        />
      </el-form-item>
      <el-form-item :label="__('公司所在地')" prop="message_area">
        <el-input v-model="form.message_area" :placeholder="__('公司所在地')" />
      </el-form-item>
      <el-form-item :label="__('公司所在地')" prop="message_full_area">
        <el-input
          v-model="form.message_full_area"
          :placeholder="__('公司所在地')"
        />
      </el-form-item>
      <el-form-item :label="__('地址')" prop="message_address">
        <el-input v-model="form.message_address" :placeholder="__('地址')" />
      </el-form-item>
      <el-form-item :label="__('副图')" prop="message_image_other">
        <el-input
          v-model="form.message_image_other"
          :placeholder="__('副图')"
        />
      </el-form-item>
      <el-form-item :label="__('用户编号')" prop="user_id">
        <el-input v-model="form.user_id" :placeholder="__('用户编号')" />
      </el-form-item>
      <el-form-item :label="__('关键字')" prop="message_keywords">
        <el-input v-model="form.message_keywords" :placeholder="__('关键字')" />
      </el-form-item>
      <el-form-item :label="__('联系人')" prop="message_username">
        <el-input v-model="form.message_username" :placeholder="__('联系人')" />
      </el-form-item>
      <el-form-item :label="__('联系人手机号')" prop="message_mobile">
        <el-input
          v-model="form.message_mobile"
          :placeholder="__('联系人手机号')"
        />
      </el-form-item>
      <el-form-item
        :label="__('是否置顶（0 否 1 过期置顶  3 是）')"
        prop="stick_status"
      >
        <el-input
          v-model="form.stick_status"
          :placeholder="__('是否置顶（0 否 1 过期置顶  3 是）')"
        />
      </el-form-item>
      <el-form-item :label="__('置顶时间')" prop="stick_time">
        <el-input v-model="form.stick_time" :placeholder="__('置顶时间')" />
      </el-form-item>
      <el-form-item :label="__('创建时间')" prop="created_at">
        <el-input v-model="form.created_at" :placeholder="__('创建时间')" />
      </el-form-item>
      <el-form-item :label="__('更新时间')" prop="updated_at">
        <el-input v-model="form.updated_at" :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 { mapActions, mapGetters } from 'vuex'
  import { translateTitle as __ } from '@/utils/i18n'

  import { doEdit } from '@/api/user/service/message'

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

      this.rules = {
        message_id: [
          { required: true, trigger: 'blur', message: this.__('消息编号') },
        ],
        category_id: [
          { required: true, trigger: 'blur', message: this.__('分类编号') },
        ],
        category_content: [
          { required: true, trigger: 'blur', message: this.__('消息内容') },
        ],
        message_area: [
          { required: true, trigger: 'blur', message: this.__('公司所在地') },
        ],
        message_full_area: [
          { required: true, trigger: 'blur', message: this.__('公司所在地') },
        ],
        message_address: [
          { required: true, trigger: 'blur', message: this.__('地址') },
        ],
        message_image_other: [
          { required: true, trigger: 'blur', message: this.__('副图') },
        ],
        user_id: [
          { required: true, trigger: 'blur', message: this.__('用户编号') },
        ],
        message_keywords: [
          { required: true, trigger: 'blur', message: this.__('关键字') },
        ],
        message_username: [
          { required: true, trigger: 'blur', message: this.__('联系人') },
        ],
        message_mobile: [
          { required: true, trigger: 'blur', message: this.__('联系人手机号') },
        ],
        stick_status: [
          {
            required: true,
            trigger: 'blur',
            message: this.__('是否置顶（0 否 1 过期置顶  3 是）'),
          },
        ],
        stick_time: [
          { required: true, trigger: 'blur', message: this.__('置顶时间') },
        ],
        created_at: [
          { required: true, trigger: 'blur', message: this.__('创建时间') },
        ],
        updated_at: [
          { required: true, trigger: 'blur', message: this.__('更新时间') },
        ],
      }

      this.$forceUpdate()
    },
    methods: {
      __,
      showEdit(row) {
        if (!row) {
          this.title = this.__('添加')
        } else {
          this.title = this.__('编辑')
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      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>
