<template>
  <el-container>
    <el-main>
      <el-form :model="form.model" :rules="form.rules" ref="formRef" label-width="100px">
        <el-form-item label="等级名称" prop="name">
          <el-input v-model="form.model.name" placeholder="请填写等级名称"></el-input>
        </el-form-item>
        <el-form-item label="等级图标" prop="icon">
          <el-input v-model="form.model.icon" placeholder="请填写图标地址"></el-input>
        </el-form-item>
        <el-form-item label="升级条件" prop="upgrade_amount">
          <el-input-number
            v-model="form.model.upgrade_amount"
            :min="0"
            :precision="2"
            placeholder="消费金额"
          />
          <span style="margin-left: 10px">元</span>
        </el-form-item>
        <el-form-item label="折扣率" prop="discount">
          <el-input-number v-model="form.model.discount" :min="1" :max="100" placeholder="折扣率" />
          <span style="margin-left: 10px">%</span>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input
            v-model="form.model.description"
            type="textarea"
            :rows="3"
            placeholder="请填写等级描述"
          ></el-input>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.model.sort" placeholder="请填写排序"></el-input>
        </el-form-item>
      </el-form>
    </el-main>
    <el-footer class="sa-footer--submit">
      <el-button v-if="modal.params.type == 'add'" type="primary" @click="confirm">保存</el-button>
      <el-button v-if="modal.params.type == 'edit'" type="primary" @click="confirm">更新</el-button>
    </el-footer>
  </el-container>
</template>
<script setup>
  import { cloneDeep } from 'lodash';
  import { onMounted, reactive, ref, unref } from 'vue';
  import { api } from '../user.service';
  const emit = defineEmits(['modalCallBack']);
  const props = defineProps({
    modal: {
      type: Object,
    },
  });
  // 添加 编辑 form
  let formRef = ref(null);
  const form = reactive({
    model: {
      name: '',
      icon: '',
      upgrade_amount: 0,
      discount: 100,
      description: '',
      sort: '0',
    },
    rules: {
      name: [{ required: true, message: '请填写等级名称', trigger: 'blur' }],
      upgrade_amount: [{ required: true, message: '请填写升级条件', trigger: 'blur' }],
      discount: [{ required: true, message: '请填写折扣率', trigger: 'blur' }],
      sort: [{ required: true, message: '请填写排序', trigger: 'blur' }],
    },
  });
  const loading = ref(false);
  // 获取详情
  async function getDetail(id) {
    loading.value = true;
    const { code, data } = await api.level.detail(id);
    code == '200' && (form.model = data);
    loading.value = false;
  }
  // 表单关闭时提交
  async function confirm() {
    unref(formRef).validate(async (valid) => {
      if (!valid) return;
      let submitForm = cloneDeep(form.model);
      const { code } =
        props.modal.params.type == 'add'
          ? await api.level.add(submitForm)
          : await api.level.edit(props.modal.params.id, submitForm);
      if (code == '200') {
        emit('modalCallBack', { event: 'confirm' });
      }
    });
  }
  async function init() {
    if (props.modal.params.id) {
      await getDetail(props.modal.params.id);
    }
  }
  onMounted(() => {
    init();
  });
</script>
