<template>
  <div>
    <n-spin :show="show" description="请稍候...">
      <n-form :label-width="80" :model="formValue" ref="formRef">
        <n-form-item label="选择版本" path="version" v-if="status === 1">
          <n-select
            v-model:value="formValue.version"
            :options="versionOptions"
            @update:value="handleUpdateValue"
          />
        </n-form-item>

        <n-form-item label="新增版本" path="version" v-if="status === 2">
          <n-input v-model:value="formValue.version" placeholder="" />
          <template #feedback>
            新增成功后默认为提审服务器，请和客户端提审版本号保持一致，否则修改不生效。例如：v1.6.9或v1.6.9.1
          </template>
        </n-form-item>
        <br />

        <n-form-item label="切换服务器" path="index" v-if="status === 1">
          <n-select v-model:value="formValue.index" :options="serverOptions" />
        </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 type="info" v-if="status === 1" @click="saveStatus(2)">新增版本</n-button>
            <n-button type="info" v-if="status === 2" @click="saveStatus(1)">选择版本</n-button>
            <n-button quaternary type="error">非发版期间请勿操作保存！</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 { GetVersionIndex, ReleaseVersion, VersionList } from '@/api/account/list';

  const show = ref(false);
  const formRef: any = ref(null);
  const message = useMessage();
  const formValue = ref<any>({
    index: null,
    version: '',
  });

  const status = ref(1);

  const serverOptions = [
    // {
    //   value: 0,
    //   label: '测试服(gravecs.mg.xhgame.com)',
    // },
    {
      value: 1,
      label: '提审服',
    },
    {
      value: 2,
      label: '正式服',
    },
  ];

  const versionOptions = ref([]);

  function saveStatus(st) {
    status.value = st;
    formValue.value.version = null;
    if (st === 2) {
      formValue.value.index = 1; // 新增默认提审服
    }

    if (st === 1) {
      formValue.value.index = null; // 更新重置选项
    }
  }

  function formSubmit() {
    formRef.value.validate((errors) => {
      if (!errors) {
        ReleaseVersion(formValue.value).then((_res) => {
          message.success('发布成功，请重新登录客户端确认是否生效');
          load();
        });
      } else {
        message.error('验证失败，请填写完整信息');
      }
    });
  }

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

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

  function handleNegativeClick() {}

  function handleUpdateValue(value) {
    show.value = true;
    new Promise((_resolve, _reject) => {
      GetVersionIndex({ version: value })
        .then((res) => {
          console.log('res:' + JSON.stringify(res));
          formValue.value.index = res.index;
        })
        .finally(() => {
          show.value = false;
        });
    });
  }
</script>
