<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 slot-scope="scope">
            <span>
              {{
                scope.row.type === 0 ? '其他文件' :
                scope.row.type === 1 ? '软件' :
                scope.row.type === 2 ? '驱动' :
                scope.row.type === 3 ? '文档' : ''
              }}
            </span>
          </template>
        </el-table-column>
        <el-table-column
          prop="fileUrl"
          label="下载地址"
          align="center"
          min-width="200">
          <template slot-scope="scope">
            <el-link
              :href="scope.row.fileUrl"
              target="_blank">下载</el-link>
          </template>
        </el-table-column>

        <el-table-column
          fixed="right"
          label="操作"
          width="120">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="showDialog('edit',scope.row)">编辑</el-button>
            <el-button
              type="text"
              @click="deleteUploadFile(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 文件弹框 -->
    <ElDialog
      title="添加文件"
      width="860px"
      v-model="addVisible">
      <AddDialog
        :project-list="projectList"
        @updateList="getUploadFileList"></AddDialog>
    </ElDialog>

    <ElDialog
      title="编辑文件"
      width="860px"
      v-model="editVisible">
      <EditDialog
        :account-item="currentItem"
        :project-list="projectList"
        @updateList="getUploadFileList"></EditDialog>
    </ElDialog>

    <el-pagination
      background
      :page-size="10"
      @current-change="handlePage"
      layout="prev, pager, next"
      :total="total">
    </el-pagination>
  </div>
</template>

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

export default {
  components: {
    ElDialog,
    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: {}
    }
  },
  methods: {
    showDialog (type, item) {
      this[`${type}Visible`] = true
      this.currentItem = item
    },
    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.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteUploadFile(id).then(res => {
          this.$message.success('删除成功')
          this.getUploadFileList(true)
        })
      })
    },
    handlePage (pageNum) {
      this.form.pageNum = pageNum
      this.getUploadFileList()
    }
  }
}
</script>

<style scoped>

</style>
