<template>
  <el-dialog
    :visible.sync="visible"
    v-bind="modalOptions"
  >
    <el-form
      ref="formData"
      :model="formData"
      :rules="formRules"
      size="mini"
      label-suffix=":"
      label-width="150px"
    >
      <el-form-item label="抽奖活动名称" prop="topic">
        <el-input v-model="formData.topic" placeholder="请输入抽奖活动名称" maxlength="30" show-word-limit />
      </el-form-item>
      <el-form-item label="活动状态" prop="state">
        <el-select v-model="formData.state" size="mini" placeholder="请选择活动状态">
          <el-option label="上线" :value="1" />
          <el-option label="下线" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="抽奖页面链接" prop="link">
        <el-input v-model="formData.link" placeholder="请输入抽奖页面链接" />
      </el-form-item>
      <el-form-item label="logo" prop="images">
        <ImageUpload v-model="formData.images" :limit="1" />
      </el-form-item>
      <el-form-item label="抽奖时间" prop="dateTime1">
        <el-date-picker
          v-model="formData.dateTime1" type="datetimerange" range-separator="至" start-placeholder="开始时间"
          end-placeholder="结束时间" value-format="yyyy-MM-dd HH:mm:ss"
        />
      </el-form-item>

    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button size="mini" @click="handleClose">取 消</el-button>
        <el-button type="primary" size="mini" @click="handleSubmit">确 定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import ImageUpload from '@/components/ImageUpload'
import { saveLotteryActivities, updateLotteryActivities, getByIdLotteryActivities } from '@/api/lotteryManagement/lotteryActivities'

export default {
  name: 'EditModal',
  components: { ImageUpload },
  data() {
    return {
      modalOptions: {
        closeOnClickModal: false,
        width: '820px',
        title: ''
      },
      visible: false,
      formData: {
        dateTime1: [], // 后端不接收
        id: '',
        topic: '',
        state: '',
        link: '',
        images: '',
        startTime: '',
        endTime: '',
        createTime: ''
      },
      formRules: {
        topic: [
          { required: true, message: '请输入抽奖活动名称' }
        ],
        state: [
          { required: true, message: '请选择活动状态' }
        ],
        dateTime1: [
          { type: 'array', required: true, message: '请选择抽奖时间' }
        ]
      }
    }
  },
  methods: {
    handleClose() {
      this.visible = false
    },
    initList() {
    },
    handleOpen(params = {}) {
      this.modalOptions.title = params.id ? '编辑抽奖活动' : '添加抽奖活动'
      this.formData = Object.assign(this.$options.data().formData, params, {
        dateTime1: params.startTime || params.endTime ? [params.startTime, params.endTime] : ''
      })
      this.visible = true
      this.initList()
      if (params.id) {
        this.getInfo(params.id)
      } else {
        this.$refs.formData && this.$refs.formData.resetFields()
      }
    },
    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 || ''
        }, {
          dateTime1: res.data.startTime || res.data.endTime ? [res.data.startTime, res.data.endTime] : ''
        })
        this.$nextTick(() => {
          this.$refs.formData && this.$refs.formData.validate()
        })
      } finally {
        loading.close()
      }
    },
    handleSubmit() {
      this.$refs.formData.validate(async (valid) => {
        if (valid) {
          const loading = this.$loading({ text: '加载中' })
          try {
            const { dateTime1, ...otps } = this.formData
            const params = {
              ...otps,
              startTime: dateTime1 && dateTime1[0],
              endTime: dateTime1 && dateTime1[1]
            }
            this.formData.id ? await updateLotteryActivities(params) : await saveLotteryActivities(params)
            loading.close()
            this.$message({ message: `${this.formData.id ? '编辑' : '添加'}成功!`, type: 'success' })
            this.$emit('success')
            this.visible = false
          } catch (e) {
            loading.close()
          } finally {
            loading.close()
          }
        } else {
          this.$message({ message: '请输入相关信息', type: 'warning' })
          return false
        }
      })
    }
  }
}
</script>

