<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';
const appStore = useAppStore();
const { t } = useI18n();

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

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

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

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

onMounted(() => {
  state.copyright = appStore.copyright;
  state.flag = appStore.copyright;
});
</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.copyright"
            allow-clear
            :maxlength="128"
            :placeholder="t('common.inputPlease')"
          ></a-input>
        </a-form-item>

        <a-form-item>
          <a-button
            type="primary"
            :disabled="state.copyright === '' || state.copyright === 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>
        <div class="sys-copyright">{{ state.copyright }}</div>
        <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.sysCopyrightLimitation') }}
          <a-typography-text mark>128</a-typography-text>
          {{ t('views.system.setting.charMaxLen') }}
        </a-typography-paragraph>
      </a-typography>
    </a-col>
  </a-row>
</template>

<style lang="less" scoped>
.sys-copyright {
  color: inherit;
  opacity: 0.75;
  font-size: 14px;
  font-weight: 600;
  line-height: 1.4;
  margin-bottom: 24px;
}
</style>
