<template>
  <div class="app-container">
    <el-form label-width="80px" :model="requestBody">
      <el-form-item label="加密key">
        <el-input v-model="requestBody.key"></el-input>
      </el-form-item>
      <el-form-item label="order明文" >
        <el-input type="textarea" v-model="requestBody.order"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onEncrypt">加密</el-button>
        <el-button type="primary" @click="onFormatJson">JSON格式化</el-button>
      </el-form-item>
      <el-form-item label="返回报文">
        <el-input type="textarea" v-model="requestBody.resp"></el-input>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { encrypt } from "@/api/sandbox/encrypt";

export default {
  data() {
    return {
      requestBody: {
        key: "",
        order: "",
        resp: ""
      }
    };
  },
  methods: {
    onEncrypt() {
      this.requestBody.resp = "";
      encrypt(this.requestBody).then(res => {
        if (res.code === 200) {
          this.requestBody.resp = res.data;
        }
      });
    },
    onFormatJson() {
      try {
        const formattedJson = JSON.stringify(JSON.parse(this.requestBody.order), null, 2);
        this.requestBody.order = formattedJson;
      } catch (error) {
        // Handle JSON parse error
        console.error("Invalid JSON");
        this.$modal.msgError("JSON格式化失败，请检查格式！");

      }
    }
  }
};
</script>
