<template>
  <div class="upload-file">
    <el-upload
      class="upload-demo"
      action="#"
      :http-request="handlRequest"
      :before-remove="beforeRemove"
      :on-remove="handleRemove"
      :on-preview="handleItem"
      multiple
      :before-upload="beforeUpload"
      :file-list="fileList"
      :show-file-list="!showLib"
      :disabled="disabled"

    >
      <slot>
        <el-button :disabled="disabled" type="primary" icon="el-icon-plus" size="mini">点击上传</el-button>
      </slot>
    </el-upload>
    <el-button type="primary" class="lib" @click="handleOpenLib" v-if="showLib"
      >文档库</el-button
    >
    <div class="imgs-box" v-if="fileList.length && showLib">
      <p class="imgs-p">
        <span> {{ fileList[0].name }}</span>
        <el-link @click="delFileList" type="primary" class="link">删除</el-link>
      </p>
    </div>
    <!--图文档弹窗 -->
    <el-dialog
      title="图文档"
      append-to-body
      :visible.sync="documentVisible"
      width="72%"
    >
      <el-form label-width="100px">
        <el-row :gutter="12">
          <el-col :span="8">
            <el-form-item label-width="70px" label="文档名称">
              <el-input v-model="documentForm.name"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-button type="primary" @click="reload">搜索</el-button>
          </el-col>
        </el-row>
      </el-form>
      <ele-pro-table
        ref="table"
        :current.sync="selectItem"
        :columns="columns"
        highlight-current-row
        :datasource="datasource"
        :initLoad="false"
        height="500px"
        class="dict-table"
        tool-class="ele-toolbar-actions"
      >
      </ele-pro-table>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="documentVisible = false"
          >关 闭</el-button
        >
        <el-button size="small" @click="submitDocument" type="primary"
          >确 认</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    uploadFile,
    removeFile,
    getFileList,
    getFile
  } from '@/api/system/file/index.js';
  import { getImageUrl, getImagePath } from '@/utils/file';
  export default {
    props: {
      value: {
        type: [Array,String],
        default: () => []
      },
      // 所属模块
      module: {
        type: String,
        required: true
      },
      // 文档库
      showLib: {
        type: Boolean,
        default: false
      },
      // 限制数量
      limit: {
        type: Number,
        default: -1
      },
      // 限制大小 mb
      size: {
        type: Number,
        default: 10
      },
      disabled:{
        default:false,
        type:Boolean
      }
    },
    data () {
      return {
        documentVisible: false,
        selectItem: null,
        documentForm: {
          name: ''
        },
        columns: [
          {
            label: '序号',
            type: 'index',
            width: 55,
            align: 'center'
          },
          {
            label: '文档名称',
            prop: 'name',
            minWidth: '180',
            showOverflowTooltip: true
          },
          {
            label: '文档类型',
            prop: 'type'
          },
          {
            label: '系统'
          },
          {
            label: '储存路径',
            prop: 'storePath',
            minWidth: '180',
            showOverflowTooltip: true
          },
          {
            label: '模块名',
            prop: 'module'
          },
          {
            label: '上传时间',
            prop: 'createTime'
          }
        ]
      };
    },
    computed: {
      fileList: {
        set (val) {
          // console.log(val);
          this.$emit(
            'input',
            val.map((item) => ({
              ...item,
              url: getImagePath(item.url)
            }))
          );
        },
        get () {
          // console.log(this.value, 2);
          if(!Array.isArray(this.value)) return []
          const arr =
            (this.value &&
              this.value.map((item) => ({
                ...item,
                url: getImageUrl(item.url)
              }))) ||
            [];
          return arr;
        }
      }
    },

    methods: {
      //点击查看图片
      handleItem(file){
   
       getFile({ objectName: file.storePath }, file.name);
      },
      delFileList () {
        this.$emit('input', []);
      },
      handleOpenLib () {
        this.documentVisible = true;
        this.$nextTick(() => {
          this.reload();
        });
      },
      //图文档勾选
      submitDocument () {
        this.$emit('input', [
          { url: this.selectItem.storePath, ...this.selectItem }
        ]);
        this.documentVisible = false;
      },
      datasource ({ page, limit }) {
        return getFileList({
          ...this.documentForm,
          pageNum: page,
          size: limit
        });
      },
      reload () {
        this.$refs.table.reload();
      },
      beforeRemove (file) {
        if (file.id) {
          return removeFile({
            fileId: file.id
          }).then(() => {
            return true;
          });
        } else {
          return true;
        }
      },
      handleRemove (file, fileList) {
        this.fileList = fileList;
      },
      beforeUpload (file) {
        if (file.size / 1024 / 1024 > this.size) {
          this.$message.error(`大小不能超过 ${this.size}MB`);
          return false;
        }

        if (this.limit > 0 && this.fileList.length === this.limit) {
          this.$message.error(`最多上传 ${this.limit}个文件`);
          return false;
        }
        return uploadFile({
          module: this.module,
          multiPartFile: file
        }).then((res) => {
          if (res.data) {
            this.$emit('input', [
              ...(this.value || []),
              { ...file, url: res.data.storePath, ...res.data }
            ]);
          }
          return res.data;
        });
      },
      handlRequest () {
        return Promise.resolve();
      }
    }
  };
</script>

<style lang="scss" scoped>
  .upload-file {
    display: flex;
    justify-content: flex-start;
    align-items: center;

    .lib {
      margin-left: 12px;
    }

    .imgs-box {
      margin-left: 10px;
      flex: 1;
    }
    .imgs-box .imgs-p {
      height: 30px;
      background: #f0f3f3;
      line-height: 30px;
      min-width: 480px;
      margin-bottom: 5px;
      padding: 0 10px;
      display: flex;
      justify-content: space-between;
    }
  }
</style>
