<template>
  <div>
    <n-spin :show="show" description="请稍候...">
      <n-form :label-width="100" :model="formValue" :rules="rules" ref="formRef">
        <n-form-item label="当前服务器时间" path="date">
          <n-date-picker
            v-model:formatted-value="formValue.date"
            value-format="yyyy-MM-dd HH:mm:ss"
            format="yyyy-MM-dd HH:mm:ss"
            type="datetime"
          />

          <template #feedback>
            时间格式：2006-01-02 15:04:05 修改时间仅用于测试，测试完毕务必进行还原</template
          >
        </n-form-item>
        <br />

        <div>
          <n-space>
            <n-popconfirm
              @positive-click="formSubmit"
              @negative-click="handleNegativeClick"
              positive-text="确认修改"
            >
              <template #trigger>
                <n-button type="primary">保存</n-button>
              </template>
              确定要修改？每次修改服务器时间请告诉所有使用本服务器的人，游戏内出现和时间相关的功能都会受到影响，请知晓
            </n-popconfirm>
            <n-button @click="flushSubmit">刷新</n-button>
          </n-space>
        </div>
      </n-form>
    </n-spin>
  </div>
</template>

<script lang="ts" setup>
  import { onMounted, ref } from 'vue';
  import { useMessage } from 'naive-ui';
  import { EditServerDate, GetServerDate } from '@/api/account/list';

  const show = ref(false);
  const formRef: any = ref(null);
  const message = useMessage();
  const rules = {};
  const formValue = ref({
    date: null,
  });

  function formSubmit() {
    formRef.value.validate((errors) => {
      if (!errors) {
        EditServerDate(formValue.value).then((_res) => {
          message.success('更新成功');
          load();
        });
      } else {
        message.error('验证失败，请填写完整信息');
      }
    });
  }

  function flushSubmit() {
    load();
  }

  onMounted(() => {
    load();
  });

  function load() {
    show.value = true;
    new Promise((_resolve, _reject) => {
      GetServerDate()
        .then((res) => {
          console.log('res:' + JSON.stringify(res));
          formValue.value = res;
        })
        .finally(() => {
          show.value = false;
        });
    });
  }

  function handleNegativeClick() {}
</script>
