<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="120px">
      <el-form-item label="礼物图片" prop="img">
        <image-upload2 v-model="form.img"/>
      </el-form-item>
      <el-form-item label="动态文件" prop="svga">
        <file-upload2 v-model="form.svga" :file-size="50" :file-type="['svga']" :limit="1"/>
      </el-form-item>
      <el-form-item label="礼物名称" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="礼物价格" prop="price">
        <el-input v-model="form.price" />
      </el-form-item>
      <el-form-item label="描述" prop="desc">
        <el-input v-model="form.desc" />
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model="form.sort" />
      </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 {addGift, getGift, updateGift} from "@/api/cai/gift";
import FileUpload2 from "@/components/FileUpload2/index.vue";

export default {
  components: {
    FileUpload2
  },
  data () {
    return {
      systemName: process.env.VUE_APP_SYSTEM_HOME,
      open: false,
      title: '',
      form:{
        id: undefined,
        img: undefined,
        name: undefined,
        desc: undefined,
        svga: undefined,
        price: undefined,
        sort: undefined,
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
        price: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
        sort: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
      },
      buttonLoading: false,
    }
  },
  created() {
  },
  methods: {
    init (row) {
      let id = row ? row.id : undefined;
      this.form.id = id || undefined;
      this.title = id ? "修改礼物信息" : "新增礼物信息";
      this.open = true;
      this.$nextTick(() => {
        this.$refs['form'].resetFields();
        if(this.form.id){
          getGift(id).then(response => {
            this.form = response.data;
          });
        }
      })
    },
    // 表单提交
    submitForm () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateGift({
              id: this.form.id,
              name: this.form.name,
              price: this.form.price,
              desc: this.form.desc,
              svga: this.form.svga,
              sort: this.form.sort,
            }).then(data => {
              this.$modal.msgSuccess("修改成功");
              this.buttonLoading = false;
              this.open = false
              this.$emit('refreshDataList')
            }).finally(() => {
              this.buttonLoading = false;
            });
          }else{
            addGift(this.form).then(data => {
              this.$modal.msgSuccess("修改成功");
              this.buttonLoading = false;
              this.open = false
              this.$emit('refreshDataList')
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      })
    },
  }
}
</script>
