<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="memberType">
        <el-select v-model="form.memberType" size="small" disabled>
          <el-option
              v-for="dict in memberTypeList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="form.name" disabled />
      </el-form-item>
      <el-form-item label="价格" prop="price">
        <el-input v-model="form.price" placeholder="价格" />
      </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 {memberTypeList} from "@/constant/statusMap";
import {getMemberPrice, updateMemberPrice} from "@/api/cai/memberPrice";

export default {
  components: {
  },
  data () {
    return {
      memberTypeList,
      open: false,
      title: '',
      form:{
        id: undefined,
        name: undefined,
        price: undefined,
        memberType: undefined,
      },
      // 表单校验
      rules: {
        memberType: [
          { 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 = "修改充值配置";
      this.open = true;
      this.$nextTick(() => {
        this.$refs['form'].resetFields();
        if(this.form.id){
          getMemberPrice(id).then(response => {
            this.form = response.data;
          });
        }
      })
    },
    // 表单提交
    submitForm () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          updateMemberPrice({
            id: this.form.id,
            price: this.form.price
          }).then(data => {
            this.$modal.msgSuccess("修改成功");
            this.$modal.buttonLoading = false;
            this.open = false
            this.$emit('refreshDataList')
          }).finally(() => {
            this.buttonLoading = false;
          });
        }
      })
    },
  }
}
</script>
