<template>
  <el-dialog :title="title" width="800px" top="20px" :visible.sync="dialogVisible" @close="diaClose"
    :close-on-click-modal="true">
    <div style="max-height: 70vh; overflow-y: scroll">
      <el-form ref="table" :rules="rules" :model="form" label-width="100px">
        <el-form-item label="奖励名称" required>
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="奖励类型" required>
          <el-radio-group v-model="form.type">
            <el-radio :label="1">金币</el-radio>
            <el-radio :label="2">现金</el-radio>
            <el-radio :label="3">虚拟物品</el-radio>
            <el-radio :label="9">大转盘抽奖次数</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="奖励图标">
          <el-upload class="avatar-uploader" v-model="form.img" :action="$http.adornUrl('alioss/upload')"
            :show-file-list="false" :on-success="uploadSuccess">
            <div class="upload-file-box">
              <img v-if="form.img" :src="form.img" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon iconss"></i>
            </div>
          </el-upload>
        </el-form-item>
        <el-form-item label="数量" required>
          <el-input-number :min="0" :step="1" :step-strictly="form.type == 9 ? true : false"
            v-model="form.number"></el-input-number>
        </el-form-item>
        <el-form-item label="剩余数量" required>
          <el-input-number :min="0" :step="1" v-model="form.surplusNumber"></el-input-number>
        </el-form-item>
        <el-form-item label="总数量" required>
          <el-input-number :min="0" :step="1" v-model="form.totalNumber"></el-input-number>
        </el-form-item>
      </el-form>
    </div>

    <div class="dialog-footer">
      <el-button @click="diaClose">取 消</el-button>
      <el-button type="primary" @click="confirm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import $api from "@/api/gift.js";

export default {
  data() {
    return {
      dialogVisible: false,
      title: "",
      rules: {
        title: [{ required: true, message: "请输入任务名称", trigger: "blur" }],
      },
      renwu: {},
      form: {
        id: "",
        name: "",
        number: "",
        type: "",
        img: '',
        surplusNumber: '',
        totalNumber: '',

      },
    };
  },
  methods: {
    uploadSuccess(file) {
      this.form.img = file.data;
    },
    open(item, renwu) {
      console.log(item);
      this.dialogVisible = true;
      this.renwu = renwu;
      Object.assign(this.form, item);
      this.title = item ? "修改奖励" : "添加奖励";
    },
    diaClose() {
      this.dialogVisible = false;
      this.form = {
        id: "",
        name: "",
        number: "",
        type: "",
        img: "",
      };
      this.renwu = {}
    },
    async confirm() {
      if (!this.form.name) {
        return this.$message.error("请输入奖励名称");
      }
      if (!this.form.type) {
        return this.$message.error("请选择奖励类型");
      }
      if (this.form.number <= 0) {
        return this.$message.error("奖励数量不能小于0");
      }

      this.submit();
    },
    async submit() {
      let res = { data: { code: 1 } };
      const submitForm = {
        taskId: this.renwu.id,
        ...this.form,
      };
      if (this.form.id) {
        res = await $api.update(submitForm);
      } else {
        res = await $api.add(submitForm);
      }
      console.log(res);
      const { data } = res;
      if (data.code == 0) {
        this.$message.success(this.form.id ? "修改成功" : "添加成功");
        this.$emit("refresh");
        this.diaClose();
      } else {
        this.$message.error(data.msg);
      }
    },
  },
};
</script>
<style scoped lang="scss">
.el-form-item__label {
  text-align: left;
}

::v-deep .el-form-item__label {
  text-align: left;
}

.dialog-footer {
  padding-top: 20px;
}

.upload-file-box {
  border-radius: 6px;
  width: 148px;
  height: 148px;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
  border: 1px solid #c0c4cc;

  img {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
}
</style>