<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="name">
        <el-input v-model="form.name" placeholder="请输入标题" />
      </el-form-item>
      <el-form-item label="充值金额" prop="price">
        <el-input v-model="form.price" placeholder="请输入充值金额" />
      </el-form-item>
      <el-form-item label="支付宝云贝数量" prop="amount">
        <el-input v-model="form.amount" placeholder="请输入支付宝云贝数量" />
      </el-form-item>
      <el-form-item label="微信云贝数量" prop="wxAmount">
        <el-input v-model="form.wxAmount" placeholder="请输入微信云贝数量" />
      </el-form-item>
      <el-form-item label="说明" prop="remark">
        <el-input v-model="form.remark" placeholder="请输入说明" type="textarea" :rows="2"/>
      </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 { addGoods, getGoods, updateGoods } from '@/api/cai/goods'

export default {
  components: {
  },
  data () {
    return {
      open: false,
      title: '',
      form:{
        id: undefined,
        name: undefined,
        price: undefined,
        amount: undefined,
        wxAmount: undefined,
        remark: undefined,
      },
      // 表单校验
      rules: {
        amount: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
        wxAmount: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
        price: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
        name: [
          { 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){
          getGoods(id).then(response => {
            this.form = response.data;
          });
        }
      })
    },
    // 表单提交
    submitForm () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateGoods(this.form).then(data => {
              this.$modal.msgSuccess("修改成功");
              this.$modal.buttonLoading = false;
              this.open = false
              this.$emit('refreshDataList')
            }).finally(() => {
              this.buttonLoading = false;
            });
          }else{
            addGoods(this.form).then(data => {
              this.$modal.msgSuccess("新增成功");
              this.buttonLoading = false;
              this.open = false
              this.$emit('refreshDataList')
            }).finally(() => {
              this.buttonLoading = false;
            });
          }

        }
      })
    },
  }
}
</script>
