<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="queryParams.model.submittedFileName"
        :placeholder="$t('table.attachment.submittedFileName')"
        class="filter-item search-item"
      />
      <el-date-picker
        v-model="queryParams.timeRange"
        :range-separator="null"
        class="filter-item search-item date-range-item"
        end-placeholder="结束日期"
        format="yyyy-MM-dd HH:mm:ss"
        start-placeholder="开始日期"
        type="daterange"
        value-format="yyyy-MM-dd HH:mm:ss"
      />
      <el-button class="filter-item" plain type="primary" @click="search">
        {{ $t("table.search") }}
      </el-button>
      <el-button class="filter-item" plain type="warning" @click="reset">
        {{ $t("table.reset") }}
      </el-button>
      <el-button
        v-has-permission="['file:add']" class="filter-item" plain
        type="danger"
        @click="upload"
      >
        {{ $t("table.upload") }}
      </el-button>
      <el-dropdown
        v-has-any-permission="['file:delete', 'file:download']"
        class="filter-item"
        trigger="click"
      >
        <el-button>
          {{ $t("table.more") }}
          <i class="el-icon-arrow-down el-icon--right"></i>
        </el-button>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item
            v-has-permission="['file:delete']"
            @click.native="batchDelete"
          >{{ $t("table.delete") }}</el-dropdown-item>
          <el-dropdown-item
            v-has-permission="['file:download']"
            @click.native="batchDownload"
          >{{ $t("table.download") }}</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>

    <el-table
      :key="tableKey"
      ref="table"
      v-loading="loading"
      :data="tableData.records"
      border
      fit
      row-key="id" style="width: 100%;" @filter-change="filterChange"
      @selection-change="onSelectChange"
      @sort-change="sortChange"
      @cell-click="cellClick"
    >
      <el-table-column align="center" type="selection" width="40px" :reserve-selection="true" />
      <el-table-column
        :label="$t('table.attachment.submittedFileName')"
        :show-overflow-tooltip="true"
        align="left"
        prop="submittedFileName"
      >
        <template slot-scope="scope">
          <div class="hover-effect" @click="onView(scope.row)">
            <i :class="scope.row.icon" class="button-list"></i>
            <span>{{ scope.row.submittedFileName }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('table.attachment.dataType')"
        :show-overflow-tooltip="true"
        align="center"
        prop="dataType"
        width="100px"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.dataType.desc }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('table.attachment.bizType')"
        :show-overflow-tooltip="true"
        align="center"
        width="120px"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.bizType }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('table.attachment.bizId')"
        :show-overflow-tooltip="true"
        align="center"
        width="180px"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.bizId }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('table.attachment.size')"
        align="center"
        :show-overflow-tooltip="true"
        width="120px"
      >
        <template slot-scope="scope">
          <span>{{ formatSize(scope.row) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('table.createTime')"
        align="center"
        prop="createTime"
        sortable="custom"
        width="170px"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('table.operation')"
        align="center" column-key="operation"
        class-name="small-padding fixed-width"
        width="100px"
      >
        <template slot-scope="{ row }">
          <i
            v-hasPermission="['file:download']"
            class="el-icon-download table-operation"
            style="color: #f50;"
            @click="singleDownload(row)"
          ></i>
          <i
            v-hasPermission="['file:delete']"
            class="el-icon-delete table-operation"
            style="color: #f50;"
            @click="singleDelete(row)"
          ></i>
          <el-link
            v-has-no-permission="['file:update', 'file:delete']"
            class="no-perm"
          >{{ $t("tips.noPermission") }}</el-link>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="tableData.total > 0"
      :limit.sync="queryParams.size"
      :page.sync="queryParams.current"
      :total="Number(tableData.total)"
      @pagination="fetch"
    />
    <attachment-edit
      ref="edit"
      :dialog-visible="dialog.isVisible"
      :type="dialog.type"
      @close="editClose"
      @success="editSuccess"
    />
    <el-dialog :visible.sync="dialogVisible">
      <img
        v-if="dialogImageUrl"
        :key="dialogImageUrl"
        :src="dialogImageUrl"
        alt="图片飞到火星了"
        height="500px"
        width="100%"
        @error="errorImage()"
      />
      <span v-else>图片飞到火星了~</span>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from "@/components/Pagination"
import AttachmentEdit from "./Edit"
import attachmentApi from "@/api/Attachment.js"
import { renderSize } from "@/utils/utils"
import { onlinePreview } from "@/settings"
import { downloadFile, initQueryParams } from '@/utils/commons'

export default {
  name: "AttachmentManage",
  components: { Pagination, AttachmentEdit },
  filters: {},
  data() {
    return {
      dialogVisible: false,
      dialogImageUrl: "",
      dialog: {
        isVisible: false,
        type: "add"
      },
      tableKey: 0,
      queryParams: initQueryParams({
        model: {}
      }),
      selection: [],
      loading: false,
      tableData: {
        records: [],
        total: 0
      }
    }
  },
  computed: {},
  mounted() {
    this.fetch()
  },
  methods: {
    errorImage() {
      const img = event.srcElement
      img.src = require("@/assets/404_images/404_image.jpeg")
      img.onerror = null // 防止闪图
    },
    formatSize(row) {
      return renderSize(row.size)
    },
    filterStatus(value, row) {
      return row.status === value
    },
    editClose() {
      this.dialog.isVisible = false
    },
    editSuccess() {
      this.search()
    },
    onSelectChange(selection) {
      this.selection = selection
    },
    search() {
      this.fetch({
        ...this.queryParams
      })
    },
    reset() {
      this.queryParams = initQueryParams({
        model: {}
      })
      this.$refs.table.clearSort()
      this.$refs.table.clearFilter()
      this.search()
    },
    singleDownload(row) {
      this.$refs.table.clearSelection()
      this.$refs.table.toggleRowSelection(row, true)
      this.batchDownload()
    },
    singleDelete(row) {
      this.$refs.table.clearSelection()
      this.$refs.table.toggleRowSelection(row, true)
      this.batchDelete()
    },
    batchDownload() {
      if (!this.selection.length) {
        this.$message({
          message: this.$t("tips.noDataSelected"),
          type: "warning"
        })
        return
      }
      this.$confirm("确认下载吗？", this.$t("common.tips"), {
        confirmButtonText: this.$t("common.confirm"),
        cancelButtonText: this.$t("common.cancel"),
        type: "warning"
      })
        .then(() => {
          const ids = []
          this.selection.forEach(u => {
            ids.push(u.id)
          })
          this.download(ids)
        })
        .catch(() => {
          this.clearSelections()
        })
    },
    batchDelete() {
      if (!this.selection.length) {
        this.$message({
          message: this.$t("tips.noDataSelected"),
          type: "warning"
        })
        return
      }
      this.$confirm(this.$t("tips.confirmDelete"), this.$t("common.tips"), {
        confirmButtonText: this.$t("common.confirm"),
        cancelButtonText: this.$t("common.cancel"),
        type: "warning"
      })
        .then(() => {
          const ids = []
          this.selection.forEach(u => {
            ids.push(u.id)
          })
          this.delete(ids)
        })
        .catch(() => {
          this.clearSelections()
        })
    },
    clearSelections() {
      this.$refs.table.clearSelection()
    },
    download(ids) {
      attachmentApi.download({ ids: ids }).then(response => {
        downloadFile(response)
        this.clearSelections()
      })
    },
    delete(ids) {
      attachmentApi.delete({ ids: ids }).then(response => {
        const res = response.data
        if (res.isSuccess) {
          this.$message({
            message: this.$t("tips.deleteSuccess"),
            type: "success"
          })
        }
        this.search()
      })
    },
    onView(row) {
      if (row.url) {
        window.open(onlinePreview + encodeURIComponent(row.url))
      }
    },
    upload() {
      this.dialog.type = "upload"
      this.dialog.isVisible = true
      this.$refs.edit.setAttachment(false)
    },
    edit(row) {
      this.$refs.edit.setAttachment(row)
      this.dialog.type = "edit"
      this.dialog.isVisible = true
    },
    fetch(params = {}) {
      this.loading = true
      if (this.queryParams.timeRange) {
        this.queryParams.map.createTime_st = this.queryParams.timeRange[0]
        this.queryParams.map.createTime_ed = this.queryParams.timeRange[1]
      }

      this.queryParams.current = params.current ? params.current : this.queryParams.current
      this.queryParams.size = params.size ? params.size : this.queryParams.size

      attachmentApi.page(this.queryParams).then(response => {
        const res = response.data
        if (res.isSuccess) {
          this.tableData = res.data
        }
      }).finally(() => this.loading = false)
    },
    sortChange(val) {
      this.queryParams.sort = val.prop
      this.queryParams.order = val.order
      if (this.queryParams.sort) {
        this.search()
      }
    },
    filterChange(filters) {
      for (const key in filters) {
        if (key.includes('.')) {
          const val = {}
          val[key.split('.')[1]] = filters[key][0]
          this.queryParams.model[key.split('.')[0]] = val
        } else {
          this.queryParams.model[key] = filters[key][0]
        }
      }
      this.search()
    },
    cellClick (row, column) {
      if (column['columnKey'] === "operation") {
        return
      }
      let flag = false
      this.selection.forEach((item) => {
        if (item.id === row.id) {
          flag = true
          this.$refs.table.toggleRowSelection(row)
        }
      })

      if (!flag) {
        this.$refs.table.toggleRowSelection(row, true)
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.file-breadcrumb {
  margin: 10px 0px 20px;
}
.page {
  text-align: center;
  margin-top: 5px;
}
.button-list {
  margin-right: 10px;
  font-size: 20px !important;
  color: #22a2ed;
  vertical-align: middle;
}
.title {
  color: #777;
  font-size: 2em;
  border-bottom: 1px solid #e5e5e5;
}
div{
  &.hover-effect {
    cursor: pointer;
    transition: background 0.3s;

    &:hover {
      background: rgba(0, 0, 0, 0.025);
    }
  }
}
</style>
