<!-- 角色编辑弹窗 -->
<template>
  <ele-modal
    :width="460"
    :visible="visible"
    :confirm-loading="loading"
    :title="isUpdate ? '修改版本' : '更新版本'"
    :body-style="{ paddingBottom: '8px' }"
    @update:visible="updateVisible"
    @ok="save"
  >
    <a-form
      ref="formRef"
      :model="form"
      :rules="rules"
      :label-col="styleResponsive ? { md: 5, sm: 5, xs: 24 } : { flex: '90px' }"
      :wrapper-col="
        styleResponsive ? { md: 19, sm: 19, xs: 24 } : { flex: '1' }
      "
    >
      <a-form-item label="安装包" name="iosDownloadUrl">
        <a-upload :show-upload-list="false" :customRequest="onUpload">
          <a-button type="primary" class="ele-btn-icon">
            <template #icon>
              <upload-outlined />
            </template>
            <span>上传</span>
          </a-button>
        </a-upload>
      </a-form-item>
      <a-form-item label="版本名" name="versionName">
        <a-input placeholder="1.0.0" v-model:value="form.versionName" />
      </a-form-item>
      <a-form-item label="版本号" name="versionCode">
        <a-input placeholder="100" v-model:value="form.versionCode" />
      </a-form-item>
      <a-form-item label="更新日志">
        <a-textarea
          :rows="4"
          :maxlength="200"
          placeholder="请输入更新日志"
          v-model:value="form.updateInfo"
        />
      </a-form-item>
    </a-form>
  </ele-modal>
</template>

<script lang="ts" setup>
  import { ref, reactive, watch } from 'vue';
  import { message } from 'ant-design-vue/es';
  import type { FormInstance, Rule } from 'ant-design-vue/es/form';
  import { storeToRefs } from 'pinia';
  import { useThemeStore } from '@/store/modules/theme';
  import useFormData from '@/utils/use-form-data';
  import { UploadOutlined } from '@ant-design/icons-vue';
  import { messageLoading } from 'ele-admin-pro';
  import { uploadFileLocal } from '@/api/system/file';
  import { Version } from '@/api/system/version/model';
  import { addVersion, updateVersion } from '@/api/system/version';

  // 是否开启响应式布局
  const themeStore = useThemeStore();
  const { styleResponsive } = storeToRefs(themeStore);

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

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

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

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

  // 提交状态
  const loading = ref(false);

  // 表单数据
  const { form, resetFields, assignFields } = useFormData<Version>({
    id: undefined,
    versionName: '',
    versionCode: '',
    updateInfo: '',
    iosDownloadUrl: '',
    androidDownloadUrl: '',
    vueDownloadUrl: '',
    comments: ''
  });

  // 表单验证规则
  const rules = reactive<Record<string, Rule[]>>({
    iosDownloadUrl: [
      {
        required: true,
        message: '请上传安装包',
        type: 'string',
        trigger: 'blur'
      }
    ],
    versionName: [
      {
        required: true,
        message: '请输入版本名',
        type: 'string',
        trigger: 'blur'
      }
    ],
    versionCode: [
      {
        required: true,
        message: '请输入版本名',
        type: 'string',
        trigger: 'blur'
      }
    ]
  });

  /* 保存编辑 */
  const save = () => {
    if (!formRef.value) {
      return;
    }
    formRef.value
      .validate()
      .then(() => {
        loading.value = true;
        const saveOrUpdate = isUpdate.value ? updateVersion : addVersion;
        console.log(form);
        saveOrUpdate(form)
          .then((msg) => {
            loading.value = false;
            message.success(msg);
            updateVisible(false);
            emit('done');
          })
          .catch((e) => {
            loading.value = false;
            message.error(e.message);
          });
      })
      .catch(() => {});
  };

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

  /* 上传 */
  const onUpload = ({ file }) => {
    if (file.size / 1024 / 1024 > 100) {
      message.error('大小不能超过 100MB');
      return false;
    }
    const hide = messageLoading({
      content: '上传中..',
      duration: 0,
      mask: true
    });
    uploadFileLocal(file, 5)
      .then((res) => {
        console.log(res);
        hide();
        form.androidDownloadUrl = res.downloadUrl;
        form.iosDownloadUrl = res.downloadUrl;
        form.vueDownloadUrl = res.downloadUrl;
        message.success('上传成功');
      })
      .catch((e) => {
        hide();
        message.error(e.message);
      });
    return false;
  };

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