<template>
  <at-table
    @load="loadData"
    :data="data"
    :page-result="pageResult"
    ref="AtTable"
    @select-all="handleSelect"
    @select="handleSelect"
    @row-dblclick="handleRowDBClick"
  >
    <template v-slot:searchInput>
      <el-form
        ref="searchForm"
        :model="searchForm"
        label-width="80px"
        class="search-top"
        @submit.native.prevent
      >
        <el-input
          v-model="searchForm.name"
          style="width: 100%"
          placeholder="请输入文件名称"
          clearable
          @keyup.enter.native="findinfo"
        ></el-input>
        <el-select
          v-model="searchForm.type"
          placeholder="请选择文件类型"
          clearable
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
        <el-input
          v-model="searchForm.createName"
          style="width: 100%"
          placeholder="请输入上传人"
          clearable
          @keyup.enter.native="findinfo"
        ></el-input>
        <el-button
          size="small"
          @click="findinfo"
          type="primary"
          class="el-icon-search search-btn"
          >查询</el-button
        >
        <el-button
          size="small"
          @click="handleReset"
          type="primary"
          plain
          class="el-icon-refresh"
          >重置</el-button
        >
      </el-form>
    </template>
    <template v-slot:operationBtn>
      <el-button
        size="small"
        type="primary"
        @click="handleAdd"
        icon="el-icon-plus"
        >添加</el-button
      >
      <ht-delete-button
        :url="deleteUrl"
        :requestMethod="'POST'"
        :htTable="$refs.AtTable"
        >删除</ht-delete-button
      >
    </template>
    <template>
      <el-table-column type="selection" width="55" align="center">
      </el-table-column>
      <el-table-column type="index" label="序号" width="55" align="center">
      </el-table-column>
      <el-table-column
        prop="name"
        label="文件名称"
        align="center"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="type"
        label="文件类型"
        align="center"
        :show-overflow-tooltip="true"
      >
        <template v-slot="{row}">
          <span v-if="row.type == 1">图片</span>
          <span v-if="row.type == 2">文档</span>
          <span v-if="row.type == 3">压缩文件</span>
          <span v-if="row.type == 4">视频</span>
          <span v-if="row.type == 5">其它</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="fileSize"
        label="文件大小"
        align="center"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="createName"
        label="上传人"
        align="center"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="上传时间"
        align="center"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <ht-table-column label="操作" width="200">
        <template v-slot="{row}">
          <el-dropdown
            size="mini"
            split-button
            @command="handleCommand"
            @click="handleCommand({row, command: 'preview'})"
          >
            <span> <i class="el-icon-view"></i>查看</span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                icon="el-icon-download"
                :command="{row: row, command: 'download'}"
                >下载</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </ht-table-column>
    </template>
    <el-dialog
      title="预览"
      class="dialog"
      append-to-body
      :close-on-click-modal="false"
      :visible.sync="dialogVisible"
      v-if="dialogVisible"
      destroy-on-close
      @close="closeDialog"
      width="100%"
    >
      <div>
        <iframe
          :src="url"
          frameborder="0"
          style="z-index: 1000; height: 800px; width: 100%"
        ></iframe>
      </div>
    </el-dialog>
  </at-table>
</template>
<script>
import req from '@/request.js'
export default {
  name: 'safetyEnvironmental',
  data() {
    return {
      loading: false,
      searchForm: {
        name: '',
        type: '',
        createName: '',
      }, //顶部搜索参数
      data: [], //表格数据
      pageResult: {
        page: 1,
        pageSize: 20,
        total: 0,
      },
      dialogVisible: false,
      options: [
        {
          value: '1',
          label: '图片',
        },
        {
          value: '2',
          label: '文档',
        },
        {
          value: '3',
          label: '压缩文件',
        },
        {
          value: '4',
          label: '视频',
        },
        {
          value: '5',
          label: '其他',
        },
      ],
    }
  },
  computed: {
    deleteUrl: function () {
      return window.context.cqxy + '/biz/universal/accessory/v1/delete'
    },
  },
  created() {
    this.$bus.$off('fileAdded')
    this.$bus.$off('fileSuccess')
  },
  mounted() {
    // 文件选择后的回调
    this.$bus.$on('fileAdded', () => {
      console.log('文件已选择')
    })
    // 文件上传成功的回调
    this.$bus.$on('fileSuccess', (file) => {
      console.log(file)
      // file.type = 0
      let params = [
        {
          sourceId: 'safetyEnvironmental',
          projectId: '',
          ...file,
          group: 'safetyEnvironmental',
        },
      ]
      this.$http
        .post('${cqxy}/biz/universal/accessory/v1/save', params)
        .then((res) => {
          if (res.data.state) {
            this.$message.success(res.data.message)
          } else {
            this.$message.error(res.data.message)
          }
          this.$refs.AtTable.load()
        })
    })
  },
  methods: {
    //重置
    handleReset() {
      this.searchForm.name = ''
      this.searchForm.type = ''
      this.searchForm.createName = ''
      this.$refs['searchForm'].resetFields()
      this.$refs.AtTable.load()
    },
    handleAdd() {
      this.$bus.$emit('openUploader')
    },
    handleRowDBClick() {},
    handleSelect(selection) {},
    loadData(param, cb) {
      param.querys = [
        {
          group: 'main',
          operation: 'EQUAL',
          parentGroup: '',
          property: 'SOURCE_ID_',
          relation: 'AND',
          value: 'safetyEnvironmental',
        },
      ]
      this.$http
        .post('${cqxy}/biz/universal/accessory/v1/getPageBySourceId', param)
        .then(
          (resp) => {
            console.log(resp)
            this.data = resp.data.value.rows
            // this.pageResult.total = resp.data.value.total
            // let response = resp.data.value
            // this.data = response.rows
            this.pageResult = {
              page: resp.data.value.page,
              pageSize: resp.data.value.pageSize,
              total: resp.data.value.total,
            }
          },
          (error) => {
            reject(error)
          }
        )
        .finally(() => {
          if (cb) {
            cb()
          }
        })
    },
    findinfo() {
      let query = [
        {
          group: 'main',
          operation: 'EQUAL',
          parentGroup: '',
          property: 'SOURCE_ID_',
          relation: 'AND',
          value: 'safetyEnvironmental',
        },
      ]
      let search = {
        name_: this.searchForm.name,
        type_: this.searchForm.type,
        create_name_: this.searchForm.createName,
      }
      for (let i in search) {
        if (search[i]) {
          query.push({
            group: 'advance',
            operation: 'LIKE',
            property: i,
            relation: 'AND',
            value: search[i],
          })
        }
      }
      let data = {
        pageBean: {
          page: 1,
          pageSize: 20,
          showTotal: true,
        },
        querys: query,
      }
      this.$http
        .post('${cqxy}/biz/universal/accessory/v1/getPageBySourceId', data)
        .then((resp) => {
          let response = resp.data.value
          this.data = response.rows
          this.pageResult = {
            page: response.page,
            pageSize: response.pageSize,
            total: response.total,
          }
        })
    },
    handleCommand(params) {
      console.log(params)
      if (params.command == 'preview') {
        let type = params.row.suffix
        if (
          type.indexOf('xsl') !== -1 ||
          type.indexOf('xslx') !== -1 ||
          type.indexOf('xls') !== -1 ||
          type.indexOf('docx') !== -1 ||
          type.indexOf('doc') !== -1 ||
          type.indexOf('pdf') !== -1 ||
          type.indexOf('jpg') !== -1 ||
          type.indexOf('png') !== -1 ||
          type.indexOf('jpeg') !== -1 ||
          type.indexOf('pptx') !== -1 ||
          type.indexOf('ppt') !== -1
        ) {
          this.dialogVisible = true
          // this.url = encodeURIComponent(row.url)
          this.url = params.row.url
        } else {
          this.$message('当前文件暂不支持预览，请下载查看')
        }
      } else {
        let ids = params.row.id
        let bizPath = 'default'
        let formData = new FormData()
        formData.append('bizPath', bizPath)
        formData.append('ids', ids)
        req
          .downloadPostCopy(
            '${cqxy}/biz/universal/accessory/v1/minIoAllDownFile',
            formData
          )
          .then((res) => {
            console.log(res)
          })
      }
    },
    beforeDestroy() {
      this.$bus.$off('fileAdded')
      this.$bus.$off('fileSuccess')
    },
    closeDialog() {
      this.dialogVisible = false
    },
  },
}
</script>
<style lang="scss" scoped>
@import '@/assets/css/common.scss';
</style>

