<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="站点名称" name="siteName">
        <a-input
          allow-clear
          :maxlength="30"
          placeholder="请输入站点名称"
          v-model:value="form.siteName"
        />
      </a-form-item>
      <a-form-item label="站点描述" name="remarks">
        <a-textarea
          :rows="4"
          :maxlength="200"
          placeholder="请输入站点描述"
          v-model:value="form.remarks"
        />
      </a-form-item>
      <a-form-item label="SEO" name="keyword">
        <a-select
          v-model:value="keyword"
          mode="tags"
          style="width: 100%"
          :token-separators="[',']"
          placeholder="请输入网站关键字"
          :options="options"
          @change="handleKeyword"
        ></a-select>
      </a-form-item>
      <a-form-item label="ICP备案" name="icp">
        <a-input
          allow-clear
          :maxlength="18"
          placeholder="请输入ICP备案号"
          v-model:value="form.icp"
        />
      </a-form-item>
      <a-form-item label="版权信息" name="copyright">
        <a-input
          allow-clear
          :maxlength="50"
          placeholder="请输入版权信息"
          v-model:value="form.copyright"
        />
      </a-form-item>
      <a-form-item label="公司名称" name="company">
        <a-input
          allow-clear
          :maxlength="30"
          placeholder="请输入公司名称"
          v-model:value="form.company"
        />
      </a-form-item>
      <a-form-item label="办公地址" name="address">
        <a-input
          allow-clear
          :maxlength="50"
          placeholder="请输入公司办公地址"
          v-model:value="form.address"
        />
      </a-form-item>
      <a-form-item label="服务热线" name="phone">
        <a-input
          allow-clear
          :maxlength="50"
          placeholder="请输入服务热线电话"
          v-model:value="form.phone"
        />
      </a-form-item>
      <a-form-item label="电子邮箱" name="email">
        <a-input
          allow-clear
          :maxlength="50"
          placeholder="请输入电子邮箱"
          v-model:value="form.email"
        />
      </a-form-item>
      <a-form-item label="网站域名" name="domain">
        <a-input v-model:value="form.domain" placeholder="domain.com">
          <template #addonBefore>
            <a-select v-model:value="prefix" style="width: 90px">
              <a-select-option value="http://">http</a-select-option>
              <a-select-option value="https://">https</a-select-option>
            </a-select>
          </template>
        </a-input>
      </a-form-item>
      <a-form-item label="技术支持" name="support">
        <a-input
          allow-clear
          :maxlength="50"
          placeholder="请输入技术支持"
          v-model:value="form.support"
        />
      </a-form-item>
      <a-form-item label="ico文件" name="logo">
        <SelectFile
          :placeholder="'请选择ico文件'"
          :accept="'image/x-icon,image/svg'"
          :data="logo"
          :limit="1"
          @done="chooseImage"
          @del="onDeleteImage"
        />
        <a-space direction="vertical">
          <a-button
            type="primary"
            class="ele-btn-icon"
            style="margin-top: 10px;"
            @click="save"
          >
            <span>保存</span>
          </a-button>
        </a-space>
      </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, listSetting, updateSettingByKey } from "@/api/system/setting";
  import { ItemType } from "ele-admin-pro/es/ele-image-upload/types";
  import { FileRecord } from "@/api/system/file/model";

  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('basic');
  // 是否开启响应式布局
  const themeStore = useThemeStore();
  const { styleResponsive } = storeToRefs(themeStore);
  // 编辑器内容，双向绑定
  const logo = ref<any>([]);
  const prefix = ref('https://');
  const suffix = ref('.com');
  const keyword = ref([]);
  const tenantId = localStorage.getItem('TenantId');
  // 提交状态
  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,
    siteName: '',
    icp: '',
    copyright: '',
    keyword: '',
    remarks: '',
    fullName: '',
    company: '',
    address: '',
    domain: '',
    email: '',
    phone: '',
    support: '',
    logo: '',
    tenantId: undefined
  });

  // 表单验证规则
  const rules = reactive({
    siteName: [
      {
        required: true,
        message: '请输入系统名称',
        type: 'string',
        trigger: 'blur'
      }
    ],
    keyword: [
      {
        required: true,
        message: '请输入网站关键词',
        type: 'string',
        trigger: 'blur'
      }
    ],
    remarks: [
      {
        required: true,
        message: '请输入站点描述',
        type: 'string',
        trigger: 'blur'
      }
    ],
    icp: [
      {
        required: true,
        message: '请输入ICP备案号',
        type: 'string',
        trigger: 'blur'
      }
    ],
    copyright: [
      {
        required: true,
        message: '请输入版权信息',
        type: 'string',
        trigger: 'blur'
      }
    ],


  });

  // 选择图片
  const chooseImage = (data: FileRecord) => {
    logo.value.push({ uid: data.id, url: data.path, status: 'done' });
    form.logo = data.path;
  };

  // 删除图片
  const onDeleteImage = (index: number) => {
    logo.value.splice(index, 1);
    form.logo = '';
  };

  const handleKeyword = (keyword) => {
    keyword.value = keyword;
    form.keyword = JSON.stringify(keyword);
  }

  /* 保存编辑 */
  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((msg) => {
            message.success('保存成功');
          })
          .catch((e) => {
            message.error(e.message);
          });
      })
      .catch(() => {});
  };

  watch(
    () => props.data,
    (data) => {
      const activeMatch = props.value === settingKey.value;
      if (!data || typeof data !== 'object') {
        if (!activeMatch) return;
        isUpdate.value = false;
        resetFields();
        logo.value = [];
        keyword.value = [];
        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);

      // 头像/关键词回显（兼容 content 与平铺字段两种返回）
      logo.value = [];
      const logoPath = (contentOrRow as any).logo;
      if (logoPath) {
        logo.value.push({ uid: 1, url: logoPath, status: '' });
      }
      const rawKeyword = (contentOrRow as any).keyword;
      if (rawKeyword) {
        try {
          keyword.value = typeof rawKeyword === 'string' ? JSON.parse(rawKeyword) : rawKeyword;
        } catch {
          keyword.value = [];
        }
      }

      form.settingId = (normalized as any).settingId;
      form.settingKey = settingKey.value;
    },
    { immediate: true }
  );

</script>
