<!-- 用户编辑弹窗 -->
<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="type">
        <a-radio-group v-model:value="form.type">
          <a-radio :value="0">续费</a-radio>
          <a-radio :value="1">新购</a-radio>
        </a-radio-group>
      </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 { ShopOrder } from '@/api/shop/shopOrder/model';
  import useFormData from '@/utils/use-form-data';
  import { addOrder, updateOrder } from '@/api/shop/shopOrder';
  import { message } from 'ant-design-vue/es';
  import {DictData} from "@/api/system/dict-data/model";

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

  const props = defineProps<{
    // 弹窗是否打开
    visible: boolean;
    appId: number | null;
    // 修改回显的数据
    data?: ShopOrder | 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<ShopOrder>({
    orderNo: undefined,
    type: 0,
    price: undefined,
    payType: undefined,
    comments: undefined
  });

  // 表单验证规则
  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
        };
        const saveOrUpdate = isUpdate.value ? updateOrder : addOrder;
        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>
