<!-- 用户编辑弹窗 -->
<template>
  <ele-modal
    :width="1000"
    :visible="visible"
    :maskClosable="false"
    :maxable="maxable"
    :title="isUpdate ? '编辑域名' : '添加域名'"
    :body-style="{ paddingBottom: '28px' }"
    @update:visible="updateVisible"
    @ok="save"
  >
    <a-form
      ref="formRef"
      :model="form"
      :rules="rules"
      :label-col="{ md: { span: 2 }, sm: { span: 4 }, xs: { span: 24 } }"
      :wrapper-col="{ md: { span: 21 }, sm: { span: 22 }, xs: { span: 24 } }"
    >
<!--      <a-form-item label="客户端" name="name">-->
<!--        <a-input allow-clear placeholder="PC端" v-model:value="form.name" />-->
<!--      </a-form-item>-->
<!--      <a-form-item label="类型" name="name">-->
<!--        <div class="w-full flex justify-between">-->
<!--          <DictSelect-->
<!--            dict-code="ProjectUrlType"-->
<!--            :width="200"-->
<!--            :show-search="true"-->
<!--            placeholder="请选择类型"-->
<!--            v-model:value="form.name"-->
<!--            @done="chooseType"-->
<!--          />-->
<!--        </div>-->
<!--      </a-form-item>-->
      <a-form-item label="类型" name="name">
        <div class="w-full flex justify-between">
          <a-input
            allow-clear
            :maxlength="20"
            placeholder="请输入链接名称"
            v-model:value="form.name"
          />
        </div>
      </a-form-item>
      <a-form-item label="访问域名" name="domain">
        <a-input
          v-model:value="form.domain"
          placeholder="https://site.websoft.top"
        />
      </a-form-item>
      <a-form-item label="账号" name="account">
        <a-input allow-clear placeholder="demo" v-model:value="form.account" />
      </a-form-item>
      <a-form-item label="密码" name="password">
        <a-input-password
          allow-clear
          placeholder="123456"
          v-model:value="form.password"
        />
      </a-form-item>
      <a-form-item label="描述" name="comments">
        <a-textarea
          :rows="4"
          :maxlength="200"
          placeholder="备注信息"
          v-model:value="form.comments"
        />
      </a-form-item>
    </a-form>
  </ele-modal>
</template>

<script lang="ts" setup>
  import { ref, reactive, watch } from 'vue';
  import { FormInstance } from 'ant-design-vue/es/form';
  import { ProjectUrl } from '@/api/project/projectUrl/model';
  import useFormData from '@/utils/use-form-data';
  import { addProjectUrl, updateProjectUrl } from '@/api/project/projectUrl';
  import { message } from 'ant-design-vue/es';
  import {decrypt, encrypt} from "@/utils/common";
  import DictSelect from "@/components/DictSelect/index.vue";
  import {DictData} from "@/api/system/dict-data/model";

  // 是否是修改
  const isUpdate = ref(false);

  const props = defineProps<{
    // 弹窗是否打开
    visible: boolean;
    appId: number | null;
    // 修改回显的数据
    data?: ProjectUrl | null;
  }>();

  const emit = defineEmits<{
    (e: 'done'): void;
    (e: 'update:visible', visible: boolean): void;
  }>();

  // 提交状态
  const loading = ref(false);
  // 是否显示最大化切换按钮
  const maxable = ref(true);

  const formRef = ref<FormInstance | null>(null);

  const { form, resetFields, assignFields } = useFormData<ProjectUrl>({
    appUrlId: undefined,
    appId: undefined,
    name: undefined,
    domain: '',
    account: '',
    password: '',
    comments: '',
    status: 0,
    sortNumber: 0
  });

  // 表单验证规则
  const rules = reactive({
    name: [
      {
        required: true,
        message: '请输入名称'
      }
    ],
    domain: [
      {
        required: true,
        message: '请输入域名'
      }
    ]
  });

  /* 更新visible */
  const updateVisible = (value: boolean) => {
    emit('update:visible', value);
  };

  const chooseType = (item: DictData) => {
    form.name = item.dictDataCode;
    form.domain = item.comments;
  };

  /* 保存编辑 */
  const save = () => {
    if (!formRef.value) {
      return;
    }
    formRef.value
      .validate()
      .then(() => {
        loading.value = true;
        const data = {
          ...form,
          appId: props.appId,
          password: encrypt(form.password)
        };
        const saveOrUpdate = isUpdate.value ? updateProjectUrl : addProjectUrl;
        saveOrUpdate(data)
          .then((msg) => {
            loading.value = false;
            message.success(msg);
            updateVisible(false);
            emit('done');
          })
          .catch((e) => {
            loading.value = false;
            message.error(e.message);
          });
      })
      .catch(() => {});
  };

  watch(
    () => props.visible,
    (visible) => {
      if (visible) {
        if (props.data) {
          assignFields(props.data);
          if(props.data.password){
            form.password = decrypt(props.data.password)
          }
          isUpdate.value = true;
        } else {
          isUpdate.value = false;
        }
      } else {
        resetFields();
      }
    }
  );
</script>
