<template>
  <div class="app-container">
    <el-card class="box-card" style="margin-bottom:30px">
      <el-form label-width="80px" ref="queryForm" :inline="true" style="display: flex;">
        <el-row :gutter="24" class="" style="margin-right: 0;flex: 1;">
          <el-col :span="24">
            <el-form-item class="searchRadio" label="类型：" style="margin-bottom: 0;">
              <el-tag @click="selectType('1')" :class="type == '1' ? 'current-tag' : ''">文档</el-tag>
              <el-tag @click="selectType('2')" :class="type == '2' ? 'current-tag' : ''">图片</el-tag>
            </el-form-item>
          </el-col>
        </el-row>

        <el-button v-hasPermi="['front:evaluatingAccurateTeachingRecording:uploadToResource']" type="primary" icon="el-icon-upload2" @click="openUpload">本地上传至文件库</el-button>
      </el-form>
    </el-card>

    <el-row :gutter="24">
      <el-table v-loading="loading" :data="resourcesList">
        <el-table-column label="序号" align="center" type="index" width="60" />
        <el-table-column label="文件" prop="materialName">
          <template slot-scope="scope">
            <el-image v-if="scope.row.picSuffix=='jpg'||scope.row.picSuffix=='png'||scope.row.picSuffix=='jpeg'" style="width: 100px; height: 100px" :src="scope.row.materialUrl" :title="scope.row.materialName" :preview-src-list="[scope.row.materialUrl]">
            </el-image>
            <div v-else>{{scope.row.materialName}}</div>
          </template>
        </el-table-column>
        <el-table-column label="发布人" prop="user">
          <template slot-scope="scope">
            {{scope.row.user.userName}}
          </template>
        </el-table-column>
        <el-table-column label="时间" prop="createTime" />
        <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button v-if="scope.row.picSuffix!='jpg'&& scope.row.picSuffix!='png'&&scope.row.picSuffix!='jpeg'" size="mini" type="text" icon="el-icon-edit" @click="handleListPreview(scope.row)">预览
            </el-button>
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改
            </el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['system:resources:remove']">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
    </el-row>

    <!-- 添加或修改个人资源库对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="30%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" v-if="open">
        <el-form-item label="资源名称" prop="materialName">
          <el-input v-model="form.materialName" placeholder="请输入资源名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="上传" v-if="dialogUploadVisible" :visible.sync="dialogUploadVisible" width="60%" v-loading="dialogUploading">
      <el-form label-width="80px">
        <el-form-item label="文件上传">
          <file-upload :type="5" @input="uploadChanged"></file-upload>
        </el-form-item>
        <el-form-item label="资源名称" prop="materialName">
          <el-input v-model="form.materialName" placeholder="请输入资源名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="uploadConfirm">确 定</el-button>
        <el-button @click="dialogUploadVisible=false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  uploadFile2OSS
} from '@/api/webApi/common.js'
import {
  list,
  add,
  update,
  remove
} from '@/api/webApi/materials.js'
import {
  getUserProfile
} from "@/api/system/user";
// 导入组件
import ElImageViewer from 'element-ui/packages/image/src/image-viewer'
export default {
  data() {
    return {
      type: '1',

      // 遮罩层
      loading: false,

      dialogUploading: false,
      dialogUploadVisible: false,
      uploadFileList: [],

      // 列表数据
      resourcesList: [],

      // 弹出层标题
      title: "",
      fileName: '',
      // 是否显示弹出层
      open: false,

      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      fileType: '1',
      user: {},
      form: {
        materialName: ''
      },
      // 表单校验
      rules: {
        materialName: [{
          required: true,
          message: '文件名称不能为空',
          trigger: 'blur'
        }],
      },
    };
  },
  created() {
    this.getUser()
    this.getList()
  },
  methods: {
    getUser() {
      getUserProfile().then(response => {
        this.user = response.data;
      });
    },
    getList() {
      this.loading = true
      list({
        type: this.type
      }).then(response => {
        this.resourcesList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    openUpload() {
      this.uploadFileList = [];
      this.form.materialName = '';
      this.dialogUploadVisible = true;
    },
    uploadChanged(fileList) {
      this.uploadFileList = fileList
      this.form.materialName = this.uploadFileList.length ? this.uploadFileList[0].name : ""
    },
    uploadConfirm() {
      if (!this.uploadFileList.length) {
        this.msgError('请先上传文件')
        return;
      }
      this.dialogUploading = true
      const file = this.uploadFileList[0]
      let Ajax = {
        'type': file.isImage ? 2 : 1, //图片2，文件1
        "fileSize": file.fileSize,
        "materialName": this.form.materialName,
        "materialUrl": file.url,
        "customerId": this.user.userId
      }
      add(Ajax).then(res => {
        this.msgSuccess('提交成功')
        this.dialogUploading = true
        this.dialogUploadVisible = false
        this.getList()
      }).catch(_=> this.dialogUploading = false)
    },
    selectType(type) {
      this.type = type
      this.getList()
    },
    closeViewer() {
      this.showViewer = false
    },
    /** 预览按钮操作 */
    handleListPreview(row, index) {
      if (row.materialUrl.indexOf('.pdf') != -1) {
        window.open('/pdfView/index.html?src=' + row.materialUrl)
      } else if (row.materialUrl.indexOf('.docx') != -1 || row.materialUrl.indexOf('.xlsm') != -1 || row.materialUrl
        .indexOf('.pptx') != -1 || row.materialUrl.indexOf('.doc') != -1 || row.materialUrl.indexOf('.ppt') != -1 ||
        row.materialUrl.indexOf('.xls') != -1 || row.materialUrl.indexOf('.xlsx') != -1) {
        window.open('https://view.officeapps.live.com/op/view.aspx?src=' + row.materialUrl)
      }
      console.log(row)
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.form = {}
      this.form = Object.assign({}, row);
      this.open = true;
      this.title = "修改文件名称";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          update(this.form).then(response => {
            this.msgSuccess('修改成功')
            this.open = false
            this.getList()
          })
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除文件名为"' + row.materialName + '"的数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return remove(row.materialId)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.fileName = '';
    },
  }
};
</script>

<style scoped>
.el-tag {
  cursor: pointer;
  margin-right: 10px;
  border-radius: 14px;
  padding: 0 20px;
  background-color: #ffffff;
  border-color: rgba(0, 0, 0, 0.65);
  color: rgba(0, 0, 0, 0.65);
}

.current-tag {
  background-color: #47c6a2;
  border-color: #47c6a2;
  color: #ffffff;
}
</style>
