<script lang="ts" setup>
import { Modal, message } from 'ant-design-vue/es';
import { onMounted, reactive } from 'vue';
import useAppStore from '@/store/modules/app';
import useI18n from '@/hooks/useI18n';
import { RESULT_CODE_SUCCESS } from '@/constants/result-constants';
import { changeValue } from '@/api/system/config';
import { validHttp } from '@/utils/regular-utils';
const appStore = useAppStore();
const { t } = useI18n();

type StateType = {
  edite: boolean;
  loading: boolean;
  url: string;
  flag: string;
};

let state: StateType = reactive({
  edite: false,
  loading: false,
  url: '',
  flag: '',
});

/**进入可编辑 */
function fnEdit(v: boolean) {
  state.edite = v;
  if (!v) {
    state.url = appStore.officialUrl;
    state.flag = appStore.officialUrl;
  }
}

/**提交保存 */
function fnSave() {
  if (state.url !== '#' && !validHttp(state.url)) {
    message.info(t('views.system.setting.sysOfficialUrlErrTip'), 3);
    return;
  }
  Modal.confirm({
    title: t('common.tipTitle'),
    content: t('views.system.setting.sysOfficialUrlTipContent'),
    onOk() {
      // 发送请求
      const hide = message.loading(t('common.loading'), 0);
      state.loading = true;
      changeValue({ key: 'sys.officialUrl', value: state.url }).then(res => {
        state.loading = false;
        hide();
        if (res.code === RESULT_CODE_SUCCESS) {
          message.success(t('views.system.setting.saveSuccess'), 3);
          appStore.officialUrl = state.url;
          fnEdit(false);
        } else {
          message.error(res.msg, 3);
        }
      });
    },
  });
}

onMounted(() => {
  state.url = appStore.officialUrl;
  state.flag = appStore.officialUrl;
});
</script>

<template>
  <a-row>
    <a-col :lg="12" :md="12" :xs="24" style="margin-bottom: 30px">
      <a-form layout="vertical" v-if="state.edite">
        <a-form-item>
          <a-input
            v-model:value="state.url"
            allow-clear
            :maxlength="255"
            :placeholder="t('common.inputPlease')"
          ></a-input>
        </a-form-item>

        <a-form-item>
          <a-button
            type="primary"
            :disabled="state.url === '' || state.url === state.flag"
            @click="fnSave"
          >
            {{ t('views.system.setting.saveSubmit') }}
          </a-button>
          <a-button style="margin-left: 10px" @click="fnEdit(false)">
            {{ t('common.cancel') }}
          </a-button>
        </a-form-item>
      </a-form>

      <template v-else>
        <a-form-item>
          <a-button type="text" v-if="state.flag === '#'">
            {{ t('views.system.setting.sysOfficialUrlNo') }}
          </a-button>
          <a-button type="link" :href="state.flag" target="_blank" v-else>
            <template #icon>
              <LinkOutlined />
            </template>
            {{ t('views.system.setting.sysOfficialUrlOpen') }}
          </a-button>
        </a-form-item>
        <a-button type="dashed" @click="fnEdit(true)">
          {{ t('common.editText') }}
        </a-button>
      </template>
    </a-col>
    <a-col :lg="12" :md="12" :xs="24">
      <a-typography>
        <a-typography-paragraph>
          {{ t('views.system.setting.sysOfficialUrlInstruction') }}
          <a-typography-text mark>http(s)://</a-typography-text>
          {{ t('views.system.setting.sysOfficialUrlInstruction1') }}
          <a-typography-text mark>#</a-typography-text>
        </a-typography-paragraph>
      </a-typography>
    </a-col>
  </a-row>
</template>

<style lang="less" scoped></style>
