<template>
  <div class="edit-popup">
    <popup
      ref="popupRef"
      :title="popupTitle"
      :async="true"
      width="550px"
      @confirm="handleSubmit"
      @close="handleClose"
    >
      <el-form
        ref="formRef"
        :model="formData"
        label-width="120px"
        :rules="formRules"
      >
        <el-form-item label="标题" prop="title">
          <el-input
            v-model="formData.title"
            clearable
            placeholder="请输入标题"
          />
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input
            v-model="formData.content"
            clearable
            placeholder="请输入内容"
            type="textarea"
            autosize
          />
        </el-form-item>
        <el-form-item label="APP类型" prop="type">
          <el-radio-group v-model="formData.type" placeholder="请选择APP类型">
            <el-radio
              v-for="(item, index) in dictData.app_type"
              :key="index"
              :label="parseInt(item.value)"
            >
              {{ item.name }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="版本" prop="version">
          <el-input
            v-model="formData.version"
            clearable
            placeholder="请输入版本,例如1.0.1"
          />
        </el-form-item>
        <el-form-item label="更新包文件" prop="dow_url">
          <el-upload
            class="upload-demo"
            style="width: 100%"
            drag
            :headers="{ Token: userStore.token }"
            :action="base_url + '/upload/file'"
            :limit="1"
            :on-success="handleFile"
            :on-exceed="handleExceed"
            ref="upload"
          >
            <el-icon class="el-icon--upload"><upload-filled /></el-icon>
            <div class="el-upload__text">文件拖入或点击上传</div>
            <template #tip>
              <div class="el-upload__tip">请上传APK/IPA/WGT文件</div>
            </template>
          </el-upload>
        </el-form-item>
        <el-form-item label="是否强制更新" prop="force">
          <el-radio-group
            v-model="formData.force"
            placeholder="请选择是否强制更新"
          >
            <el-radio
              v-for="(item, index) in dictData.app_force_quiet"
              :key="index"
              :label="parseInt(item.value)"
            >
              {{ item.name }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否静默更新" prop="quiet">
          <el-radio-group
            v-model="formData.quiet"
            placeholder="请选择是否静默更新"
          >
            <el-radio
              v-for="(item, index) in dictData.app_force_quiet"
              :key="index"
              :label="parseInt(item.value)"
            >
              {{ item.name }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
    </popup>
  </div>
</template>

<script lang="ts" setup name="appUpdateEdit">
import type { FormInstance } from "element-plus";
import { genFileId } from "element-plus";
import Popup from "@/components/popup/index.vue";
import {
  apiAppUpdateAdd,
  apiAppUpdateEdit,
  apiAppUpdateDetail,
} from "@/api/app_update";
import { timeFormat } from "@/utils/util";
import type { PropType } from "vue";
import { inject } from "vue";
const base_url: any = inject("base_url");
defineProps({
  dictData: {
    type: Object as PropType<Record<string, any[]>>,
    default: () => ({}),
  },
});
const emit = defineEmits(["success", "close"]);
const formRef = shallowRef<FormInstance>();
const popupRef = shallowRef<InstanceType<typeof Popup>>();
const mode = ref("add");

import useUserStore from "@/stores/modules/user";
const userStore = useUserStore();

const handleFile = (e: any) => {
  formData.dow_url = e.data.uri;
};

const upload = ref(null);
const handleExceed = (files: any) => {
  upload.value!.clearFiles();
  const file = files[0] as UploadRawFile;
  file.uid = genFileId();
  upload.value!.handleStart(file);
  upload.value!.submit();
};

// 弹窗标题
const popupTitle = computed(() => {
  return mode.value == "edit" ? "编辑app更新" : "新增app更新";
});

// 表单数据
const formData = reactive({
  id: "",
  title: "",
  content: "",
  type: "",
  version: "",
  dow_url: "",
  force: "",
  quiet: "",
});

// 表单验证
const formRules = reactive<any>({});

// 获取详情
const setFormData = async (data: Record<any, any>) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      //@ts-ignore
      formData[key] = data[key];
    }
  }
};

const getDetail = async (row: Record<string, any>) => {
  const data = await apiAppUpdateDetail({
    id: row.id,
  });
  setFormData(data);
};

// 提交按钮
const handleSubmit = async () => {
  await formRef.value?.validate();
  const data = { ...formData };
  mode.value == "edit"
    ? await apiAppUpdateEdit(data)
    : await apiAppUpdateAdd(data);
  popupRef.value?.close();
  emit("success");
};

//打开弹窗
const open = (type = "add") => {
  mode.value = type;
  popupRef.value?.open();
};

// 关闭回调
const handleClose = () => {
  emit("close");
};

defineExpose({
  open,
  setFormData,
  getDetail,
});
</script>
