<style scoped>
</style>
<template id="app-attachment">
  <div class="app-attachment" style="z-index:99999">
    <el-dialog class="app-attachment-dialog" style="width:1000px;height:800px;margin:0 auto;"
      :title="title ? title : '选择文件'" :visible.sync="dialogVisible" @opened="dialogOpened"
      :close-on-click-modal="false">
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="本地文件" name="first">
          <el-upload class="avatar-uploader" action="" :accept="accept" :show-file-list="false"
            :http-request="UploadAttachment" :multiple="uploadMultple">
            <div class="app-upload" flex="main:center cross:center" style="width: 100px; height: 100px;">
              <i class="el-icon-upload"></i>
            </div>
          </el-upload>
        </el-tab-pane>
        <el-tab-pane label="云盘文件" name="second">
          <table style="width:100%">
            <tr>
              <th width="8%">
                选择
              </th>
              <th width="50%">
                文件名
              </th>
              <th width="20%">
                大小
              </th>
              <th width="20%">
                创建时间
              </th>
            </tr>
            <tbody>
              <tr v-for="(item,index) in fileDataList">
                <td>
                  <el-checkbox v-model="item.IsCheck" />
                </td>
                <td>
                  {{item.FileName}}
                </td>
                <td>
                  {{item.FileSize}}
                </td>
                <td>
                  {{item.CreateTimeStr}}
                </td>
              </tr>
            </tbody>
          </table>
        </el-tab-pane>
      </el-tabs>
      <div style="margin-top:20px;text-align:right">
        <el-button size="small" type="primary" @click="SelectAttachment">选定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import {
    UploadSelfFile
  } from '../../api/common/common'
  export default {
    props: {
      //上传单选还是多选
      uploadMultple: {
        type: Boolean,
        value: false,
      },
      //文件单选还是多选
      chooseMultple: {
        type: Boolean,
        value: false,
      },
      //上传类型(1-图片,2-附件，3-音频)
      uploadType: {
        type: String,
        default: '1',
      },
      //是否显示弹窗
      openDialog: {
        type: Boolean,
        default: false,
      },
    },
    computed: {
      accept: {
        get() {
          if (this.uploadType === '1') {
            return 'image/*';
          }
          if (this.uploadType === '2') {
            return '*/*';
          }
          if (this.uploadType === '3') {
            return 'audio/*';
          }
          return '*/*';
        },
      },
    },
    watch: {
      openDialog: {
        handler(newValue) {
          this.dialogVisible = newValue;
        },
        immediate: true
      },
      uploadType: {
        handler(newValue) {
          if (newValue) {
            switch (newValue) {
              case "1":
                this.title = "图片";
                break;
              case "2":
                this.title = "附件";
                break;
              case "3":
                this.title = "音频";
                break;
            }
          }

        },
        immediate: true
      },
      dialogVisible(newVal, oldVal) {
        if (!newVal) {
          this.$emit("closed");
        }
      },
    },
    data() {
      return {
        dialogVisible: false,
        title: "",
        activeName: "first",
        fileDataList: [{
          IsCheck: false,
          FileName: "测试文件",
          FileUrl: "http://192.168.1.214:8130/EduSystem/Test/Upload/assistIcon/20201228032704905.jpg",
          FileSize: "100",
          CreateTimeStr: "2021-01-04"
        }],
        chooseFileArray: []
      };
    },
    created() {
      this.chooseFileArray = [];
    },
    mounted() {
      this.chooseFileArray = [];
    },
    methods: {
      //选择文件
      SelectAttachment() {
        this.$emit("selected", this.chooseFileArray);
      },
      //上传附件
      UploadAttachment(files) {
        UploadSelfFile('Attachment', files.file, res => {
          if (res.Code == 1) {
            this.chooseFileArray.push({
              fileName: res.FileName,
              fileUrl: res.FileUrl
            })
            this.$emit("selected", this.chooseFileArray);
          }
        })
      },
      dialogOpened() {

      },
      handleClick() {

      },
    },
  };

</script>
