<template>
  <a-card :bordered="false">
    <a-form
      ref="formRef"
      :model="form"
      :rules="rules"
      :label-col="styleResponsive ? { md: 3, sm: 5, xs: 24 } : { flex: '90px' }"
      :wrapper-col="styleResponsive ? { md: 9, sm: 19, xs: 24 } : { flex: '1' }"
    >
      <a-form-item label="开发者ID(AppID)" name="appId">
        <a-input
          allow-clear
          :maxlength="50"
          placeholder="请输入开发者ID"
          v-model:value="form.appId"
        />
      </a-form-item>
      <a-form-item label="开发者秘钥(AppSecret)" name="appSecret">
        <a-input-password
          :maxlength="100"
          placeholder="请输入开发者秘钥AppSecret"
          v-model:value="form.appSecret"
        />
      </a-form-item>
      <a-form-item label="原始ID" name="originalId">
        <a-input
          allow-clear
          :maxlength="20"
          placeholder="请输入原始ID"
          v-model:value="form.originalId"
        />
      </a-form-item>
      <a-form-item label="微信号" name="wxOfficialAccount">
        <a-input
          allow-clear
          :maxlength="20"
          placeholder="请输入微信号"
          v-model:value="form.wxOfficialAccount"
        />
      </a-form-item>
      <div style="margin-bottom: 22px; width: 750px">
        <a-divider>网页授权域名</a-divider>
      </div>
      <a-form-item label="网页授权域名" name="authorize">
        <a-input-group compact>
          <a-input
            :value="`https://server.websoft.top`"
            placeholder="请输入网页授权域名"
            style="width: calc(100% - 50px)"
          />
          <a-tooltip title="复制">
            <a-button @click="onCopyText(`https://server.websoft.top`)">
              <template #icon><CopyOutlined /></template>
            </a-button>
          </a-tooltip>
        </a-input-group>

        <a-button
          type="primary"
          class="ele-btn-icon"
          style="margin-top: 10px"
          @click="save"
        >
          <span>保存</span>
        </a-button>
      </a-form-item>
    </a-form>
  </a-card>
</template>

<script lang="ts" setup>
  import { reactive, ref, watch } from 'vue';
  import { message } from 'ant-design-vue';
  import { Setting } from '@/api/system/setting/model';
  import { useThemeStore } from '@/store/modules/theme';
  import { storeToRefs } from 'pinia';
  import { FormInstance } from 'ant-design-vue/es/form';
  import useFormData from '@/utils/use-form-data';
  import { addSetting, updateSettingByKey } from '@/api/system/setting';
  import { copyText } from '@/utils/common';
  import { CopyOutlined } from '@ant-design/icons-vue';

  const props = defineProps<{
    value?: string;
    // 修改回显的数据
    data?: Setting | null;
  }>();

  // const emit = defineEmits<{
  //   (e: 'done', value): void;
  // }>();

  // 保存字段信息(设定好key和描述,content里的字段是随意加的会自动转为json保存到数据库)
  // const settingId = ref(null);
  // const settingKey = ref('');
  const settingKey = ref('wx-official');
  // 是否开启响应式布局
  const themeStore = useThemeStore();
  const { styleResponsive } = storeToRefs(themeStore);
  // 提交状态
  const loading = ref(false);
  // 是否是修改
  const isUpdate = ref(false);
  //
  const formRef = ref<FormInstance | null>(null);
  // 表单数据
  const { form, resetFields, assignFields } = useFormData<Setting>({
    settingId: undefined,
    settingKey: settingKey.value,
    appId: '',
    appSecret: '',
    wxOfficialAccount: '',
    originalId: '',
    tenantId: localStorage.getItem('TenantId')
  });

  // 表单验证规则
  const rules = reactive({
    chatKey: [
      {
        required: true,
        message: '请输入KEY',
        type: 'string',
        trigger: 'blur'
      }
    ]
  });

  /* 保存编辑 */
  const save = () => {
    if (!formRef.value) {
      return;
    }
    formRef.value
      .validate()
      .then(() => {
        loading.value = true;
        form.settingKey = settingKey.value;
        const appForm = {
          ...form,
          content: JSON.stringify(form)
        };
        const saveOrUpdate = isUpdate.value ? updateSettingByKey : addSetting;
        saveOrUpdate(appForm)
          .then(() => {
            message.success('保存成功');
          })
          .catch((e) => {
            message.error(e.message);
          });
      })
      .catch(() => {});
  };

  const onCopyText = (text) => {
    copyText(text);
  };

  watch(
    () => props.data,
    (data) => {
      const activeMatch = props.value === settingKey.value;
      if (!data || typeof data !== 'object') {
        if (!activeMatch) return;
        isUpdate.value = false;
        resetFields();
        form.settingId = undefined;
        form.settingKey = settingKey.value;
        return;
      }

      const normalized: any = Array.isArray(data)
        ? data.find((d) => d?.settingKey === settingKey.value) ?? data[0]
        : (data as any).data && typeof (data as any).data === 'object'
        ? (data as any).data
        : data;

      let parsedContent: any | undefined;
      const rawContent = (normalized as any).content;
      if (rawContent) {
        if (typeof rawContent === 'string') {
          try {
            parsedContent = JSON.parse(rawContent);
          } catch {
            parsedContent = undefined;
          }
        } else if (typeof rawContent === 'object') {
          parsedContent = rawContent;
        }
      }
      const contentOrRow = parsedContent ?? normalized;
      const incomingKey =
        (contentOrRow as any).settingKey ?? (normalized as any).settingKey;
      if (!activeMatch && incomingKey !== settingKey.value) return;

      isUpdate.value = true;
      assignFields(contentOrRow);
      form.settingId = (normalized as any).settingId;
      form.settingKey = settingKey.value;
    },
    { immediate: true }
  );
</script>
