<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="key">
          <el-input
            v-model="form.model.key"
            placeholder="请填写设置键，如：order_timeout"
          ></el-input>
        </el-form-item>
        <el-form-item label="数据类型" prop="type">
          <el-select
            v-model="form.model.type"
            placeholder="请选择数据类型"
            @change="handleTypeChange"
          >
            <el-option label="字符串" value="string"></el-option>
            <el-option label="数字" value="number"></el-option>
            <el-option label="布尔值" value="boolean"></el-option>
            <el-option label="JSON" value="json"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设置值" prop="value">
          <el-input
            v-if="form.model.type === 'string'"
            v-model="form.model.value"
            placeholder="请填写设置值"
          ></el-input>
          <el-input-number
            v-else-if="form.model.type === 'number'"
            v-model="form.model.value"
            placeholder="请填写数字"
            style="width: 100%"
          />
          <el-select
            v-else-if="form.model.type === 'boolean'"
            v-model="form.model.value"
            placeholder="请选择布尔值"
          >
            <el-option label="是" value="true"></el-option>
            <el-option label="否" value="false"></el-option>
          </el-select>
          <el-input
            v-else-if="form.model.type === 'json'"
            v-model="form.model.value"
            type="textarea"
            :rows="4"
            placeholder="请填写JSON格式数据"
          ></el-input>
        </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="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 '../order.service';
  const emit = defineEmits(['modalCallBack']);
  const props = defineProps({
    modal: {
      type: Object,
    },
  });
  // 添加 编辑 form
  let formRef = ref(null);
  const form = reactive({
    model: {
      name: '',
      key: '',
      value: '',
      type: 'string',
      description: '',
      status: 'active',
      sort: '0',
    },
    rules: {
      name: [{ required: true, message: '请填写设置名称', trigger: 'blur' }],
      key: [{ required: true, message: '请填写设置键', trigger: 'blur' }],
      value: [{ required: true, message: '请填写设置值', trigger: 'blur' }],
      type: [{ required: true, message: '请选择数据类型', trigger: 'change' }],
      status: [{ required: true, message: '请选择状态', trigger: 'change' }],
      sort: [{ required: true, message: '请填写排序', trigger: 'blur' }],
    },
  });
  const loading = ref(false);

  // 处理类型变化
  function handleTypeChange(type) {
    // 重置值
    if (type === 'number') {
      form.model.value = 0;
    } else if (type === 'boolean') {
      form.model.value = 'true';
    } else {
      form.model.value = '';
    }
  }

  // 获取详情
  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>
