<!-- 用户编辑弹窗 -->
<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="domain">
        <a-input
          allow-clear
          placeholder="http://10093.wsdns.cn"
          v-model:value="form.domain"
        />
      </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
          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 { AppUrl } from '@/api/oa/app/url/model';
  import useFormData from '@/utils/use-form-data';
  import { decrypt } from '@/utils/common';
  import { addAppUrl, updateAppUrl } from '@/api/oa/app/url';
  import { message } from 'ant-design-vue/es';

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

  const props = defineProps<{
    // 弹窗是否打开
    visible: boolean;
    appId: number | null;
    // 修改回显的数据
    data?: AppUrl | 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<AppUrl>({
    appUrlId: undefined,
    appId: undefined,
    name: '',
    domain: '',
    account: '',
    password: '',
    comments: '',
    status: 0,
    sortNumber: 0
  });

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

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

  /* 保存编辑 */
  const save = () => {
    if (!formRef.value) {
      return;
    }
    formRef.value
      .validate()
      .then(() => {
        loading.value = true;
        const data = {
          ...form,
          appId: props.appId
        };
        const saveOrUpdate = isUpdate.value ? updateAppUrl : addAppUrl;
        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);
          isUpdate.value = true;
        } else {
          isUpdate.value = false;
        }
      } else {
        resetFields();
      }
    }
  );
</script>
