<template>
  <q-splitter :model-value="70" class="w-full h-full">
    <template #before>
      <w-tree-grid ref="parameterTreeGridRef" title="系统参数树" label-key="code" label-i18n @update:selected="parameterSelected" />
    </template>
    <template #after>
      <div class="row">
        <div class="col-12">
          <q-input v-model="code" label="代码" outlined class="p-1" />
        </div>
      </div>
      <div class="row">
        <div class="col-12">
          <q-input v-model="value" label="值" outlined class="p-1" />
        </div>
      </div>
      <div class="row">
        <div class="col-12 flex justify-center q-gutter-md pt-5">
          <q-btn label="保存" color="primary" outlined class="p-1" style="width: 100px" @click="save" />
        </div>
      </div>
      <div class="row" style="height: 200px"></div>
    </template>
  </q-splitter>
</template>
<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { useI18n } from 'vue-i18n';
import { Environment, axios } from 'platform-core';

const { t } = useI18n();

const parameterTreeGridRef = ref();
const id = ref('');
const code = ref('');
const value = ref('');

const parameterSelected = (target) => {
  if (target) {
    const node = parameterTreeGridRef.value.getNodeById(target);
    id.value = node.id;
    code.value = node.code;
    value.value = node.value;
  } else {
    id.value = '';
    code.value = '';
    value.value = '';
  }
};

const save = () => {
  const node = parameterTreeGridRef.value.getNodeById(id.value);
  const data = {
    id: node.id,
    code: node.code,
    value: value.value,
    parent: node.parentId,
    corporationCode: node.corporationCode,
    dataComeFrom: node.dataComeFrom,
    creator: node.creator,
    createDate: node.createDate,
    lastModifier: node.lastModifier,
    lastModifyDate: node.lastModifyDate,
  };
  axios.put(Environment.apiContextPath('/api/system/parameter/') + id.value, data).then((response) => {});
};

onMounted(() => {
  axios.get(Environment.apiContextPath('/api/system/parameter?pageable=false&sortBy=code')).then((response) => {
    parameterTreeGridRef.value.setNodes(response.data.content);
  });
});
</script>
