<template>
  <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="open" width="700px" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="奖品类型" prop="prizeType">
        <el-select v-model="form.prizeType" size="small">
          <el-option
              v-for="dict in prizeTypeList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="奖品名称" prop="prizeName">
        <el-input v-model="form.prizeName" placeholder="请输入奖品名称" />
      </el-form-item>
      <el-form-item label="奖品描述" prop="prizeDesc">
        <el-input v-model="form.prizeDesc" type="textarea" placeholder="请输入内容" />
      </el-form-item>
      <el-form-item label="奖品图片" prop="prizeImg">
        <image-upload2 v-model="form.prizeImg"/>
      </el-form-item>
      <el-form-item label="中奖率" prop="winProbability">
        <el-input-number v-model="form.winProbability" placeholder="请输入中奖率" min="0" max="1" />
      </el-form-item>
      <el-form-item label="保底抽数" prop="guaranteeDraws" v-if="form.prizeType == 3">
        <el-input v-model="form.guaranteeDraws" placeholder="请输入保底抽数" />
      </el-form-item>
      <el-form-item label="最低中奖抽数" prop="minWinDraws" v-if="form.prizeType == 3" >
        <el-input v-model="form.minWinDraws" placeholder="请输入最低中奖抽数" />
      </el-form-item>
      <el-form-item label="奖品价值" prop="prizePrice">
        <el-input v-model="form.prizePrice" placeholder="请输入奖品价值估算" />
      </el-form-item>
      <el-form-item label="自动兑奖" prop="autoGive">
        <el-select v-model="form.autoGive" size="small">
          <el-option
              v-for="dict in prizeAutoGiveList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="open = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {prizeAutoGiveList, prizeTypeList} from '@/constant/statusMap'
import {addPrizeInfo, getPrizeInfo, updatePrizeInfo} from "@/api/cai/prizeInfo";

export default {
  components: {
  },
  data () {
    return {
      prizeTypeList,prizeAutoGiveList,
      open: false,
      title: '',
      form:{
        id: undefined,
        prizeName: undefined,
        prizeDesc: undefined,
        prizeImg: undefined,
        winProbability: undefined,
        guaranteeDraws: undefined,
        minWinDraws: undefined,
        prizeType: undefined,
        prizePrice: undefined,
        autoGive: undefined,
      },
      // 表单校验
      rules: {
        prizeName: [
          { required: true, message: "奖品名称不能为空", trigger: "blur" }
        ],
        prizeDesc: [
          { required: true, message: "奖品描述不能为空", trigger: "blur" }
        ],
        prizeImg: [
          { required: true, message: "奖品图片不能为空", trigger: "blur" }
        ],
        winProbability: [
          { required: true, message: "中奖率不能为空", trigger: "blur" }
        ],
        guaranteeDraws: [
          { required: true, message: "保底抽数不能为空", trigger: "blur" }
        ],
        minWinDraws: [
          { required: true, message: "中奖抽数不能为空", trigger: "blur" }
        ],
        prizeType: [
          { required: true, message: "奖品类型不能为空", trigger: "change" }
        ],
        prizePrice: [
          { required: true, message: "奖品价值不能为空", trigger: "blur" }
        ],
        autoGive: [
          { required: true, message: "自动兑奖不能为空", trigger: "blur" }
        ],
      },
      buttonLoading: false,
    }
  },
  created() {
  },
  methods: {
    init (id) {
      this.form.id = id || undefined;
      this.title = (id ? "修改" : "新增") + "奖品库";
      this.open = true;
      this.$nextTick(() => {
        this.$refs['form'].resetFields();
        if(this.form.id){
          getPrizeInfo(id).then(response => {
            this.form = response.data;
          });
        }
      })
    },
    // 表单提交
    submitForm () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updatePrizeInfo(this.form).then(data => {
              this.$modal.msgSuccess("修改成功");
              this.$modal.buttonLoading = false;
              this.open = false
              this.$emit('refreshDataList')
            }).finally(() => {
              this.buttonLoading = false;
            });
          }else{
            addPrizeInfo(this.form).then(data => {
              this.$modal.msgSuccess("新增成功");
              this.buttonLoading = false;
              this.open = false
              this.$emit('refreshDataList')
            }).finally(() => {
              this.buttonLoading = false;
            });
          }

        }
      })
    },
  }
}
</script>
