<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="itemName">
        <el-input v-model="formData.itemName" placeholder="请输入奖项名称" maxlength="30" show-word-limit />
      </el-form-item>
      <el-form-item label="关联抽奖活动" prop="lotteryId">
        <el-select v-model="formData.lotteryId" size="mini" placeholder="请选择关联抽奖活动" filterable>
          <el-option
            v-for="(item, index) in activityList" :key="item.id" :label="`${item.topic}（${item.id}）`"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="level" label="奖项等级">
        <el-input v-model="formData.level" placeholder="请输入奖项等级" style="width: 250px;"></el-input>
      </el-form-item>
      <el-form-item prop="percent" label="奖项概率">
        <el-input v-model="formData.percent" placeholder="请输入奖项概率" style="width: 250px;"></el-input>
      </el-form-item>
      <el-form-item label="关联抽奖奖品" prop="prizeId">
        <el-select v-model="formData.prizeId" size="mini" placeholder="请选择关联抽奖奖品" filterable>
          <el-option
            v-for="(item, index) in prizeList" :key="item.id" :label="`${item.prizeName}（${item.id}）`"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否默认" prop="defaultItem">
        <el-select v-model="formData.defaultItem" size="mini" placeholder="请选择是否默认">
          <el-option label="不是" :value="0" />
          <el-option label="是" :value="1" />
        </el-select>
      </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 { saveLotteryItem, updateLotteryItem, getByIdLotteryItem } from '@/api/lotteryManagement/LotteryAwards'
import { getPageLotteryActivities } from '@/api/lotteryManagement/lotteryActivities'
import { getPageLotteryPrize } from '@/api/lotteryManagement/LotteryPrizes'

export default {
  name: 'EditModal',
  components: {
  },
  data() {
    return {
      modalOptions: {
        closeOnClickModal: false,
        width: '820px',
        title: ''
      },
      visible: false,
      formData: {
        id: '',
        lotteryId: '',
        itemName: '',
        level: '',
        percent: '',
        prizeId: '',
        defaultItem: '',
        createTime: ''
      },
      formRules: {
        itemName: [
          { required: true, message: '请输入奖项名称' }
        ],
        lotteryId: [
          { required: true, message: '请选择关联活动' }
        ],
        level: [
          { required: false, message: '请输入奖项等级' },
          { pattern: /^(0|[1-9][0-9]*)$/, message: '请输入正整数' }
        ],
        percent: [
          { required: false, message: '请输入奖项概率' },
          { pattern: /^0\.\d{0,2}$|^[1-9]\d*\.\d{0,2}$|^[1-9]\d*$/, message: '数值有误' }
        ],
        defaultItem: [
          { required: true, message: '请选择是否默认' }
        ]
      },
      activityList: [],
      prizeList: []
    }
  },
  methods: {
    handleClose() {
      this.visible = false
    },
    async initList() {
      const res1 = await getPageLotteryActivities({ page: 1, pageSize: 9999 })
      this.activityList = res1.data.list
      const res2 = await getPageLotteryPrize({ page: 1, pageSize: 9999 })
      this.prizeList = res2.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 getByIdLotteryItem({ id })
        this.formData = Object.assign(this.$options.data().formData, res.data, {
          id: res.data.id || '',
          lotteryId: res.data.lotteryId || '',
          itemName: res.data.itemName || '',
          level: res.data.level,
          percent: res.data.percent,
          prizeId: res.data.prizeId || '',
          defaultItem: res.data.defaultItem,
          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 updateLotteryItem(params) : await saveLotteryItem(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>

