<template>
  <el-dialog
    :title="title"
    :visible.sync="modal"
    width="500px"
    :close-on-click-modal="false"
    @close="
      res => {
        $emit('cancel');
      }
    "
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="等级名称" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="年费" prop="yearPrice">
        <el-input-number
          controls-position="right"
          v-model="form.yearPrice"
          class="w-80"
          :precision="2"
          :step="0.1"
          :max="999999"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="月费" prop="monthPrice">
        <el-input-number
          controls-position="right"
          v-model="form.monthPrice"
          class="w-80"
          :precision="2"
          :step="0.1"
          :max="999999"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="商品优惠折扣" prop="sale">
        <el-input-number
          controls-position="right"
          v-model="form.sale"
          class="w-80"
          :precision="2"
          :step="0.1"
          :max="999999"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="启用/停用">
        <el-radio-group v-model="form.isEnable" prop="isEnable">
          <el-radio :label="true">启用</el-radio>
          <el-radio :label="false">停用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input-number
          controls-position="right"
          v-model="form.sort"
          :precision="0"
          :min="1"
          :max="9999"
          label="序号"
        ></el-input-number>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="modal = false">取消</el-button>
      <el-button type="primary" @click="handleConfirm">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getItem, saveItem } from '@/api/member/level';

export default {
  name: 'MemberLeverItemModal',

  props: {
    id: {
      type: String,
      default: ''
    }
  },

  data() {
    return {
      modal: true,

      form: {
        name: '',
        yearPrice: 0,
        monthPrice: 0,
        sale: 0,
        isEnable: false,
        sort: 1
      },
      rules: {
        name: [{ required: true, message: '请输入等级名称', trigger: 'blur' }],
        yearPrice: [
          {
            type: 'number',
            required: true,
            message: '请输入正确的年费价格',
            trigger: 'change'
          }
        ],
        monthPrice: [
          {
            type: 'number',
            required: true,
            message: '请输入正确的月费价格',
            trigger: 'change'
          }
        ],
        sale: [
          {
            type: 'number',
            required: true,
            message: '请输入正确的商品优惠折扣',
            trigger: 'change'
          }
        ]
      }
    };
  },

  computed: {
    title() {
      if (this.id) {
        return '编辑会员等级';
      } else {
        return '新增会员等级';
      }
    }
  },

  watch: {
    id: {
      handler(id) {
        id && this.loadData();
      },
      immediate: true
    }
  },

  mounted() {},

  methods: {
    async loadData() {
      const { success, data, msg } = await getItem({
        id: this.id
      });
      if (success) {
        this.form = data;
      }
    },

    handleConfirm() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          const params = Object.assign({}, this.form);
          if (this.id) params.id = this.id;
          const { success, msg } = await saveItem(params);
          if (success) {
            this.$success('保存成功！');
            this.modal = false;
            this.$g_emit('member_lever_reload');
          }
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.w-80 {
  width: 80%;
}
</style>
