<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="code">
          <el-input v-model="form.model.code" placeholder="请填写模板编码"></el-input>
        </el-form-item>
        <el-form-item label="短信类型" prop="type">
          <el-select v-model="form.model.type" placeholder="请选择短信类型">
            <el-option label="验证码" value="verify"></el-option>
            <el-option label="通知" value="notice"></el-option>
            <el-option label="营销" value="marketing"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="模板内容" prop="content">
          <el-input
            v-model="form.model.content"
            type="textarea"
            :rows="4"
            placeholder="请填写短信模板内容，使用{变量名}表示变量"
          ></el-input>
          <div class="form-tip">
            <small>提示：使用 {code} 表示验证码，{name} 表示用户名等变量</small>
          </div>
        </el-form-item>
        <el-form-item label="变量说明" prop="variables">
          <el-input
            v-model="form.model.variables"
            type="textarea"
            :rows="3"
            placeholder="请说明模板中使用的变量，如：{code}=验证码，{name}=用户名"
          ></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.model.status" placeholder="请选择状态">
            <el-option label="启用" value="active"></el-option>
            <el-option label="禁用" value="disabled"></el-option>
          </el-select>
        </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 '../content.service';
  const emit = defineEmits(['modalCallBack']);
  const props = defineProps({
    modal: {
      type: Object,
    },
  });
  // 添加 编辑 form
  let formRef = ref(null);
  const form = reactive({
    model: {
      name: '',
      code: '',
      type: 'verify',
      content: '',
      variables: '',
      status: 'active',
      sort: '0',
    },
    rules: {
      name: [{ required: true, message: '请填写模板名称', trigger: 'blur' }],
      code: [{ required: true, message: '请填写模板编码', trigger: 'blur' }],
      type: [{ required: true, message: '请选择短信类型', trigger: 'change' }],
      content: [{ required: true, message: '请填写模板内容', trigger: 'blur' }],
      status: [{ required: true, message: '请选择状态', trigger: 'change' }],
      sort: [{ required: true, message: '请填写排序', trigger: 'blur' }],
    },
  });
  const loading = ref(false);
  // 获取详情
  async function getDetail(id) {
    loading.value = true;
    const { code, data } = await api.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.add(submitForm)
          : await api.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>
<style lang="scss" scoped>
  .form-tip {
    margin-top: 5px;
    color: #909399;
  }
</style>
