<style scoped>
.user-panel {
  margin: 10px auto;
}
.avatar-uploader .el-upload {
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
      border-color: #409EFF;
    }
    .avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 178px;
      height: 178px;
      line-height: 178px;
      text-align: center;
    }
    .avatar {
      width: 178px;
      height: 178px;
      display: block;
    }
</style>
<template>
  <el-dialog 
      :visible.sync="showDialog"
      :title="title"
      :modal-append-to-body="true"
      style="text-align:left;"
      :close-on-click-modal="true"
      @close="closeDialog"
      :append-to-body="true"
      width="580px"
  >
    <div class="user-panel" v-loading="loading">
      <el-form ref="form" :model="formModel" :rules="ruleValidate" :label-width="'100px'">
        <el-form-item label="奖项名称" prop="name">
          <el-input v-model="formModel.name" placeholder="请输入奖项名称" style="width:400px"></el-input>
        </el-form-item>
        <el-form-item label="奖品名称" prop="prizeName">
          <el-input v-model="formModel.prizeName" placeholder="请输入奖品名称" style="width:400px"></el-input>
        </el-form-item>
        <el-form-item label="奖品数量" prop="num">
          <el-input-number v-model="formModel.num" :step="1" :min="0" style="width:400px"></el-input-number>
        </el-form-item>
        <el-form-item label="照片" prop="image">
              <el-upload
                  style="width:300px"
                  class="avatar-uploader"
                  name="photoFile"
                  :action="uploadUrl"
                  :headers="headers"
                  :data="uploadData"
                  :show-file-list="false"
                  :on-success="handleAvatarSuccess"
                  :before-upload="beforeAvatarUpload">
                  <img v-if="fileUrl" :src="fileUrl" class="avatar">
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
          </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="handleSubmit" :loading="submitting">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import Constant from "@/constant";
import awardInfoApi from "@/api/base/awardInfo";
import { getToken } from "@/utils/auth"; // get token from cookie


export default {
    props: ["businessKey","activityId", "title"],
  data() {
    return {
      formModel: {
        id: ""
      },
      ruleValidate: {
        name: [{ required: true, message: "奖项名称不能为空", trigger: "blur" }],
        prizeName: [{ required: true, message: "奖品名称不能为空", trigger: "blur" }],
        num: [{ required: true, message: "数量不能为空", trigger: "blur" }],
      },
      dataDictionaryList: [],
      showDialog: true,
      loading: false,
      submitting: false,
      //上传地址
      uploadUrl: Constant.serverUrl + "/uploadPicture",
      uploadData: {
          subFolder: "awardInfo",
      },
      fileUrl: "",
      headers: {
          Authorization: getToken(),
      },
    };
  },
  methods: {
    closeDialog() {
      this.$emit("close", false);
    },
    handleSubmit() {
      var self = this;
      this.$refs["form"].validate(valid => {
        if (valid) {
          (function() {
            var id = self.formModel.id;

            self.formModel.activityId = self.activityId;

            if (id == null || id.length == 0) {
              return awardInfoApi.add(self.formModel);
            } else {
              return awardInfoApi.update(self.formModel);
            }
          })().then(function(response) {
            var jsonData = response.data;

            if (jsonData.result) {
              self.$message({
                message: "保存成功!",
                type: "success"
              });

              self.$emit("close", true);
            } else {
              self.$message({
                message: jsonData.message + "",
                type: "warning"
              });

              self.$emit("close", false);
            }
          });
        }
      });
    },
    handleAvatarSuccess(res, file) {
        var self = this;
        self.formModel.image = res.data;

        self.fileUrl =
          res.data + "?x-oss-process=image/resize,m_lfit,h_300,w_300";
    },
    beforeAvatarUpload(file) {
        const isLt2M = file.size / 1024 / 1024 < 2;
        if (!isLt2M) {
            this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isLt2M;
    },
  },
  mounted: function ()  {
    var self = this;
    self.loading = true;

    (function() {
      if (self.businessKey.length == 0) {
        return awardInfoApi.create();
      } else {
        return awardInfoApi.edit(self.businessKey);
      }
    })()
      .then(response => {
        var jsonData = response.data;
        self.loading = false;

        if (jsonData.result) {
          self.formModel = jsonData.data;

            let image = self.formModel.image;
            if (image != null) {
                self.fileUrl = image 
            }
        } else {
          self.$message.error(jsonData.message + "");
        }
      })
      .catch(error => {
        self.$message.error(error + "");
      });
  }
};
</script>