<script setup lang="ts">
defineOptions({
  name: "Upload"
});
import { ref } from "vue";
const fileList = ref([
  {
    name: "food.jpeg",
    url: "https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg"
  },
  {
    name: "food2.jpeg",
    url: "https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg"
  }
]);

const form = ref({
  name: "",
  file: null
});
</script>

<template>
  <div>
    <el-card class="box-card">
      <template v-slot:header>
        <div class="clearfix">
          <span>上传组件</span>
        </div>
      </template>
      <el-upload
        class="upload-demo"
        drag
        action="https://jsonplaceholder.typicode.com/posts/"
        :on-preview="
          file => {
            console.log('preview', file);
          }
        "
        :on-remove="
          (file, fileList) => {
            console.log('remove', file, fileList);
          }
        "
        :before-remove="
          (file, fileList) => {
            return $confirm(`确定移除 ${file.name}？`);
          }
        "
        :file-list="fileList"
      >
        <i class="el-icon-upload" />
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <template v-slot:tip>
          <div class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
        </template>
      </el-upload>
    </el-card>

    <el-card class="box-card mt-5">
      <template v-slot:header>
        <div class="clearfix">
          <span>表单中的上传组件</span>
        </div>
      </template>
      <el-form :model="{ name: '', file: null }" label-width="80px">
        <el-form-item label="姓名">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="上传文件">
          <el-upload
            action="https://jsonplaceholder.typicode.com/posts/"
            :on-preview="
              file => {
                console.log('preview', file);
              }
            "
            :on-remove="
              (file, fileList) => {
                console.log('remove', file, fileList);
              }
            "
            :before-remove="
              (file, fileList) => {
                return $confirm(`确定移除 ${file.name}？`);
              }
            "
            :file-list="fileList"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <template v-slot:tip>
              <div class="el-upload__tip">
                只能上传jpg/png文件，且不超过500kb
              </div>
            </template>
          </el-upload>
        </el-form-item>
        <el-form-item>
          <el-button type="primary">提交</el-button>
          <el-button>重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<style scoped lang="scss"></style>
