<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="用户ID" prop="buyerUserId">
        <el-input v-model="formData.buyerUserId" placeholder="请输入用户ID" style="width: 250px;"></el-input>
      </el-form-item>
      <el-form-item label="关联奖项" prop="itemId">
        <el-select v-model="formData.itemId" size="mini" placeholder="请选择关联奖项" filterable>
          <el-option
            v-for="(item, index) in awardsList" :key="item.id" :label="`${item.itemName}（${item.id}）`"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="prizeName" label="奖品名称">
        <el-input v-model="formData.prizeName" placeholder="请输入奖品名称" style="width: 250px;"></el-input>
      </el-form-item>
      <el-form-item prop="accountIp" label="IP">
        <el-input v-model="formData.accountIp" placeholder="请输入IP" style="width: 250px;"></el-input>
      </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 { saveLotteryRecord, updateLotteryRecord, getByIdLotteryRecord } from '@/api/lotteryManagement/LotteryRecords'
import { getPageLotteryItem } from '@/api/lotteryManagement/LotteryAwards'

export default {
  name: 'EditModal',
  components: {
  },
  data() {
    return {
      modalOptions: {
        closeOnClickModal: false,
        width: '820px',
        title: ''
      },
      visible: false,
      formData: {
        id: '',
        buyerUserId: '',
        itemId: '',
        prizeName: '',
        accountIp: '',
        createTime: ''
      },
      formRules: {
        buyerUserId: [
          { required: true, message: '请输入用户ID' },
          { pattern: /^(0|[1-9][0-9]*)$/, message: '请输入正整数' }
        ],
        itemId: [
          { required: true, message: '请选择关联奖项' }
        ]
      },
      awardsList: []
    }
  },
  methods: {
    handleClose() {
      this.visible = false
    },
    async initList() {
      const res1 = await getPageLotteryItem({ page: 1, pageSize: 9999 })
      this.awardsList = res1.data.list
    },
    handleOpen(params = {}) {
      this.modalOptions.title = params.id ? '编辑抽奖记录' : '添加抽奖记录'
      this.formData = Object.assign(this.$options.data().formData, params)
      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 getByIdLotteryRecord({ id })
        this.formData = Object.assign(this.$options.data().formData, res.data, {
          id: res.data.id || '',
          buyerUserId: res.data.buyerUserId || '',
          itemId: res.data.itemId || '',
          prizeName: res.data.prizeName || '',
          accountIp: res.data.accountIp || '',
          createTime: res.data.createTime || ''
        })
        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 { ...otps } = this.formData
            const params = {
              ...otps
            }
            this.formData.id ? await updateLotteryRecord(params) : await saveLotteryRecord(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>
