<template>
  <div class="form-wrap">
    <div class="searchBox">
      <el-form
        ref="form"
        :model="form"
        :inline="true"
        label-width="100px">
        <el-form-item
          label="文件名称"
          prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item
          label="文件类型"
          prop="type">
          <el-select
            v-model="form.type">
            <el-option
              label="其他文件"
              :value="0"></el-option>
            <el-option
              label="软件"
              :value="1"></el-option>
            <el-option
              label="驱动"
              :value="2"></el-option>
            <el-option
              label="文档"
              :value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="文件地址"
          prop="fileUrl">
          <el-input v-model="form.fileUrl"></el-input>
        </el-form-item>
      </el-form>
      <div class="btn-wrap">
        <el-button
          type="primary"
          @click="getUploadFileList">搜索</el-button>
        <el-button
          type="primary"
          v-if="canAddUsers"
          @click="showDialog('add')">新增文件</el-button>
      </div>
    </div>
    <div class="tableBox">
      <!-- <div class="block-title">
        文件列表
      </div> -->

      <el-table
        v-loading="loading"
        row-class-name="is-center"
        :data="tableData">
        <el-table-column
          prop="name"
          label="文件名称"
          min-width="150">
        </el-table-column>
        <el-table-column
          prop="type"
          label="文件类型"
          min-width="100">
          <template #default="scope">
            <span>
              {{
                scope && scope.row && scope.row.type === 0 ? '其他文件' :
                scope && scope.row && scope.row.type === 1 ? '软件' :
                scope && scope.row && scope.row.type === 2 ? '驱动' :
                scope && scope.row && scope.row.type === 3 ? '文档' : ''
              }}
            </span>
          </template>
        </el-table-column>
        <el-table-column
          prop="fileUrl"
          label="下载地址"
          align="center"
          min-width="200">
          <template #default="scope">
            <el-link
              :href="(scope && scope.row && scope.row.fileUrl) || ''"
              target="_blank">下载</el-link>
          </template>
        </el-table-column>

        <el-table-column
          label="操作"
          width="120">
          <template #default="scope">
            <el-button
              type="text"
              @click="scope && scope.row && showDialog('edit', scope.row, scope.$index)">编辑</el-button>
            <el-button
              type="text"
              @click="scope && scope.row && deleteUploadFile(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 文件弹框 -->
    <el-dialog
      title="添加文件"
      width="860px"
      :visible.sync="addVisible">
      <AddDialog
        :project-list="projectList"
        @close-dialog="addVisible = false"
        @updateList="getUploadFileList"></AddDialog>
    </el-dialog>

    <el-dialog
      title="编辑文件"
      width="860px"
      :visible.sync="editVisible">
      <EditDialog
        :account-item="currentItem"
        :project-list="projectList"
        @close-dialog="editVisible = false"
        @updateList="getUploadFileList"></EditDialog>
    </el-dialog>

    <el-dialog
      title="提示"
      width="420px"
      :visible.sync="deleteConfirmVisible">
      <div style="padding: 10px 0 18px;">此操作将永久删除，是否继续？</div>
      <div class="dialog-btn-wrap">
        <el-button @click="deleteConfirmVisible = false">取消</el-button>
        <el-button type="primary" @click="confirmDeleteUploadFile">确定</el-button>
      </div>
    </el-dialog>

    <el-pagination
      background
      :hide-on-single-page="false"
      :page-size="10"
      :current-page="form.pageNum"
      @current-change="handlePage"
      layout="total, prev, pager, next"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
import { getUploadFileList, deleteUploadFile } from '@/api/platform'
import AddDialog from './file/AddDialog'
import EditDialog from './file/EditDialog'

export default {
  components: {
    AddDialog,
    EditDialog
  },
  computed: {
    canAddUsers () {
      return true
      // return this.$store.state.user.permissionList.includes('system_settings:add_users')
    }
  },

  created () {
    this.getUploadFileList()
  },
  data () {
    return {
      loading: false,
      addVisible: false,
      editVisible: false,
      tableData: [],
      projectList: [],
      total: 0,
      form: {
        name: '', // 文件名称
        type: 0, // 文件类型
        fileUrl: '', // 文件地址
        pageNum: 1,
        pageSize: 10
      },
      // 当前文件对象
      currentItem: {},
      deleteConfirmVisible: false,
      pendingDeleteId: ''
    }
  },
  methods: {
    showDialog (type, item, index) {
      const sourceItem = item || {}
      let fullItem = sourceItem
      if (typeof index === 'number' && this.tableData[index]) {
        fullItem = this.tableData[index]
      } else if (sourceItem.id) {
        fullItem = this.tableData.find(row => String(row.id) === String(sourceItem.id)) || sourceItem
      }
      this.currentItem = { ...fullItem }
      this[`${type}Visible`] = true
    },
    getUploadFileList (goFirst) {
      this.loading = true
      if (goFirst) {
        this.form.pageNum = 1
      }
      getUploadFileList(this.form).then(res => {
        this.loading = false
        this.tableData = res.data.records.map(item => {
          item.permissionList = (item.roleList || []).reduce((acc, role) => {
            return acc.concat(role.resourceList || [])
          }, [])
          return item
        }) || []
        this.total = res.data.total
      })
    },
    deleteUploadFile (id) {
      this.pendingDeleteId = id
      this.deleteConfirmVisible = true
    },
    confirmDeleteUploadFile () {
      if (!this.pendingDeleteId) {
        this.deleteConfirmVisible = false
        return
      }
      deleteUploadFile(this.pendingDeleteId).then(() => {
        this.$message.success('删除成功')
        this.deleteConfirmVisible = false
        this.pendingDeleteId = ''
        this.getUploadFileList(true)
      })
    },
    handlePage (pageNum) {
      this.form.pageNum = pageNum
      this.getUploadFileList()
    }
  }
}
</script>

<style scoped>

</style>
