<template>
  <div class="container">
    <!-- 添加模块 -->
    <div class="center">
      <el-form
        ref="form"
        :model="companylist"
        :inline="true"
        label-width="150px"
        :rules="rules"
      >
        <el-form-item label="文件名称" prop="companyFileName">
          <el-input v-model="companylist.companyFileName"></el-input>
        </el-form-item>
        <el-form-item label="文件描述">
          <el-input
            type="textarea"
            :rows="4"
            placeholder="请输入内容"
            v-model="companylist.fileDetails"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="文件说明">
          <el-input
            type="textarea"
            :rows="4"
            placeholder="请输入内容"
            v-model="companylist.fileRemarks"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="上传公司文件">
          <el-upload
            class="upload-demo"
            drag
            ref="Upload"
            :action="action"
            :auto-upload="false"
            :data="companylist"
            multiple
          >
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">
              将文件拖到此处，或<em>点击上传</em>
            </div>
            <div class="el-upload__tip" slot="tip">文件大小不超过500kb</div>
          </el-upload>
        </el-form-item>
        <br />
        <div class="items">
          <el-form-item>
            <el-button
              type="success"
              icon="el-icon-upload2"
              @click="submitForm()"
              >提交</el-button
            >
          </el-form-item>
          <el-form-item>
            <el-button type="info" @click="returnback()">返回</el-button>
          </el-form-item>
        </div>
      </el-form>
    </div>
    <div></div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      //添加文件的数组
      companylist: {
        companyFileName: null, //文件名字
        companyId: null,
        fileDetails: "", //文件描述
        fileRemarks: "", //文件备注
        userId: null,
      },
      // 表单验证
      rules: {
        companyFileName: [
          { required: true, message: "请填写文件名称", trigger: "change" },
        ],
      },
    };
  },
  created() {
    this.companylist.companyId = this.$store.getters.companyId;
    this.companylist.userId = this.$store.getters.info.userId;
    this.action = process.env.VUE_APP_BASE_API + "/file/companyFile";
  },
  methods: {
    submitForm() {
      //判断是否有文件
      var file = this.$refs.Upload.uploadFiles;
      if (file != "") {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.$refs.Upload.submit();
            this.$message.success("上传成功");
            this.returnback();
          }
        });
      } else {
        this.$message.error("请添加附件，在进行提交");
      }
    },
    //返回
    returnback() {
      this.$store.dispatch("tagsView/delView", this.$route);
      this.$router.push("/selfFileList");
    },
  },
};
</script>

<style scoped>
.center {
  width: 800px;
  padding: 40px 10px;
}
.items {
  padding: 0 100px;
}
</style>