<script setup lang="ts">
import {onMounted, reactive, ref} from "vue";
import * as $api from "@/api/platform/index";
import {message} from "@/utils/message";
import type {FormInstance} from "element-plus";
import {cloneDeep} from "@pureadmin/utils";

defineOptions({name: "templateEdit"});
const isDetail = ref(false);
const formRef = ref<FormInstance>();
// 初始化数据
const initData = {
  serviceId: undefined,
  privateKey: undefined,
  publicKey: undefined,
  status: 1
};

// 表单校验规则
const formRules = {
  appId: [{required: true, message: "appId不能为空", trigger: "blur"}],
  privateKey: [{required: true, message: "密钥不能为空", trigger: "blur"}],
  publicKey: [{required: true, message: "公钥不能为空", trigger: "blur"}]
};

// 页面数据
const pageData: any = reactive({
  dialogVisible: false,
  title: "",
  formLoading: false,
  mode: "add",
  isUpdate: false,
  // 表单数据
  formData: initData,
  formRules: formRules
});
// 暴露给父级调用
const emits = defineEmits(["ok", "close"]);

const _save = () => {
  pageData.formLoading = true;
  const _data = cloneDeep(pageData.formData);
  $api
    .save(_data)
    .then((res: any) => {
      if (res.success) {
        _confirm();
      } else {
        message(res.message, {type: "warning"});
      }
    })
    .finally(() => {
      pageData.formLoading = false;
    });
};
const _update = () => {
  pageData.formLoading = true;
  const _data = cloneDeep(pageData.formData);
  $api
    .update(pageData.formData.id, _data)
    .then((res: any) => {
      if (res.success) {
        _confirm();
      } else {
        message(res.message, {type: "warning"});
      }
    })
    .finally(() => {
      pageData.formLoading = false;
    });
};
const _confirm = () => {
  message("操作成功", {type: "success"});
};

// 确定并关闭弹窗
const handleConfirm = () => {
  formRef.value!.validate((isValid: boolean) => {
    if (isValid) {
      const {id} = pageData.formData;
      if (id) {
        _update();
      } else {
        _save();
      }
    } else {
      message("表单校验失败", {type: "warning"});
    }
  });
};
onMounted(() => {
  $api.info().then((res: any) => {
    pageData.formData = res.result;
    console.log(pageData.formData);
  });
});
</script>

<template>
  <el-card>
    <el-form
      ref="formRef"
      :model="pageData.formData"
      style="width: 50%; margin: 20px"
      label-width="auto"
      :rules="pageData.formRules"
    >
      <el-form-item label="应用appId" prop="appId">
        <el-input
          v-model="pageData.formData.appId"
          clearable
          placeholder="请输入应用appId"
          size="small"
          :readonly="isDetail"
        />
      </el-form-item>
      <el-form-item label="应用密钥" prop="privateKey">
        <el-input
          :rows="13"
          type="textarea"
          v-model="pageData.formData.privateKey"
          clearable
          placeholder="请输入应用密钥"
          size="small"
          :readonly="isDetail"
        />
      </el-form-item>
      <el-form-item label="服务商公钥" prop="publicKey">
        <el-input
          :rows="5"
          type="textarea"
          v-model="pageData.formData.publicKey"
          clearable
          placeholder="请输入服务商公钥"
          size="small"
          :readonly="isDetail"
        />
      </el-form-item>
      <div class="w-full text-center">
        <el-button type="primary" @click="handleConfirm"
        >保存
        </el-button
        >
      </div>
    </el-form>
  </el-card>
</template>
