<template>
  <el-dialog
    :visible.sync="visible"
    v-bind="modalOptions"
    append-to-body
  >
    <el-form
      ref="formData"
      :model="formData"
      size="mini"
      label-position="left"
      label-suffix=":"
      label-width="200px"
    >
      <!-- 抽奖活动信息 -->
      <el-form-item label="ID" prop="id">
        {{ formData.id || '--' }}
      </el-form-item>
      <el-form-item label="抽奖活动名称" prop="topic">
        {{ formData.topic || '--' }}
      </el-form-item>
      <el-form-item label="活动状态" prop="state">
        {{ formData.state || '--' }}
      </el-form-item>
      <el-form-item label="抽奖页面链接" prop="link">
        {{ formData.link || '--' }}
      </el-form-item>
      <el-form-item label="图片" prop="images">
        <el-image
          v-if="formData.images" :src="formData.images" style="width:80px;height:80px"
          fit="cover" :preview-src-list="[ formData.images ]"
        />
        <span v-else>--</span>
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        {{ formData.startTime || '--' }}
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        {{ formData.endTime || '--' }}
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        {{ formData.createTime || '--' }}
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { getByIdLotteryActivities } from '@/api/lotteryManagement/lotteryActivities'

export default {
  name: 'DetailModal',
  data() {
    return {
      modalOptions: {
        closeOnClickModal: false,
        width: '800px',
        title: '查看抽奖活动详情'
      },
      visible: false,
      formData: {
        id: '',
        topic: '',
        state: '',
        link: '',
        images: '',
        startTime: '',
        endTime: '',
        createTime: ''
      }
    }
  },
  methods: {
    handleClose() {
      this.visible = false
    },
    handleOpen(params = {}) {
      this.formData = Object.assign(this.$options.data().formData, params)
      if (params.id) {
        this.getInfo(params.id)
      }
      this.visible = true
    },
    async getInfo(id) {
      const loading = this.$loading({ text: '加载中' })
      try {
        const res = await getByIdLotteryActivities({ id })
        this.formData = Object.assign(this.$options.data().formData, res.data, {
          id: res.data.id || '',
          topic: res.data.topic || '',
          state: res.data.state,
          link: res.data.link || '',
          images: res.data.images || '',
          startTime: res.data.startTime || '',
          endTime: res.data.endTime || '',
          createTime: res.data.createTime || ''
        })
      } finally {
        loading.close()
      }
    }
  }
}
</script>

