<template>
  <div class="app-container">
        <div>
          <el-select
            v-model="listQuery.type"
            placeholder="文件类型"
            clearable
            style="width: 200px"
            class="filter-item"
            @change="handleFilter"
          >
            <el-option
              v-for="item in enabledOptions"
              :key="item.key"
              :label="item.display_name"
              :value="item.key"
            />
          </el-select>
          <el-input
            v-model="listQuery.search"
            placeholder="文件名"
            style="width: 300px;"
            class="filter-item"
            @keyup.enter.native="handleFilter"
          />
          <el-button
            class="filter-item"
            type="primary"
            icon="el-icon-search"
            @click="handleFilter"
            size="small"
          >搜索</el-button>
          <el-button
            class="filter-item"
            type="primary"
            icon="el-icon-refresh-left"
            @click="resetFilter"
            size="small"
          >重置</el-button>
          <el-button
            class="filter-item"
            type="danger"
            icon="el-icon-delete"
            @click="handleClear"
            size="small"
          >一键清空</el-button>
          <el-button
            class="filter-item"
            type="warning"
            icon="el-icon-edit"
            @click="handleReplaceUrl"
            size="small"
          >一键修改IP/URL</el-button>
        </div>
        <el-table
          v-loading="listLoading"
          :data="fileList.results"
          style="width: 100%;margin-top:10px;"
          highlight-current-row
          row-key="id"
          height="100"
          stripe
          border
          v-el-height-adaptive-table="{bottomOffset: 50}"
        >
          <el-table-column type="index" width="50" />
          <el-table-column align="center" label="名称">
            <template slot-scope="scope">
                <el-link type="primary" :href="scope.row.file" target="_blank">{{ scope.row.name }}</el-link>
            </template>
          </el-table-column>
          <el-table-column align="header-center" label="类型">
            <template slot-scope="scope">{{ scope.row.type }}</template>
          </el-table-column>
          <el-table-column align="header-center" label="格式">
            <template slot-scope="scope">{{ scope.row.mime }}</template>
          </el-table-column>
          <el-table-column align="header-center" label="大小(B)">
            <template slot-scope="scope">{{ scope.row.size }}</template>
          </el-table-column>
          <el-table-column align="header-center" label="地址">
            <template slot-scope="scope">{{ scope.row.path }}</template>
          </el-table-column>
          <el-table-column label="上传日期">
            <template slot-scope="scope">
              <span>{{ scope.row.create_time }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="100" class-name="small-padding fixed-width">
            <template slot-scope="{row,$index}">
              <el-button size="mini" type="danger" @click="handleDelete(row,$index)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <el-dialog title="一键修改IP/URL" :visible.sync="dialogFormVisible" width="30%">
          <el-form :model="temp" label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">
            <el-form-item label="原IP/URL" prop="old_url">
              <el-input v-model="temp.old_url" placeholder="请输入原IP/URL" />
            </el-form-item>
            <el-form-item label="新IP/URL" prop="new_url">
              <el-input v-model="temp.new_url" placeholder="请输入新IP/URL" />
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取消</el-button>
            <el-button type="primary" @click="replaceUrlData">确定</el-button>
          </div>
        </el-dialog>

        <pagination
          v-show="fileList.count>0"
          :total="fileList.count"
          :page.sync="listQuery.page"
          :limit.sync="listQuery.page_size"
          @pagination="getList"
        />
  </div>
</template>
<script>
import { getFileList, deleteFile, clearFiles, replaceUrl } from "@/api/file"
import Pagination from "@/components/Pagination"
export default {
  components: { Pagination },
  data() {
    return {
      fileList: {count:0},
      listLoading: true,
      listQuery: {
        page: 1,
        page_size: 20
      },
      dialogFormVisible: false,
      temp: {
        old_url: '',
        new_url: ''
      },
      enabledOptions: [
        { key: "文档", display_name: "文档" },
        { key: "图片", display_name: "图片" },
        { key: "音频", display_name: "音频" },
        { key: "视频", display_name: "视频" },
        { key: "其它", display_name: "其它" }
      ],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.listLoading = true;
      getFileList(this.listQuery).then(response => {
        if (response.data) {
          this.fileList = response.data
        }
        this.listLoading = false;
      });
    },
    resetFilter() {
      this.listQuery = {
        page: 1,
        page_size: 20
      };
      this.getList();
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    handleDelete(row, index) {
      this.$confirm('确认删除该文件?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteFile(row.id).then(() => {
          this.$notify({
            title: 'Success',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
          this.fileList.results.splice(index, 1)
          this.fileList.count -= 1
        })
      })
    },
    handleClear() {
      this.$confirm('确认清空所有文件? 此操作不可恢复!', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        clearFiles().then(() => {
          this.$notify({
            title: 'Success',
            message: '清空成功',
            type: 'success',
            duration: 2000
          })
          this.getList()
        })
      })
    },
    handleReplaceUrl() {
      this.temp = {
        old_url: '',
        new_url: ''
      }
      this.dialogFormVisible = true
    },
    replaceUrlData() {
      if (!this.temp.old_url || !this.temp.new_url) {
        this.$message.error('请填写完整')
        return
      }
      this.$confirm('确认修改? 此操作不可恢复!', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        replaceUrl(this.temp).then(response => {
          this.$notify({
            title: 'Success',
            message: response.data.message || '修改成功',
            type: 'success',
            duration: 2000
          })
          this.dialogFormVisible = false
          this.getList()
        })
      })
    }
  }
};
</script>
