<template>
  <el-container>
    <el-header height="220">
      <h3>我的节点</h3>
      <el-button type="text">添加节点</el-button>
      <el-button type="text" @click="onShowKeyDialog">凭证列表</el-button>
    </el-header>
    <el-main>
      <el-table
        :data="dataList"
        border
        height="480"
        style="width: 100%"
      >
        <el-table-column
          fixed="left"
          label="No"
          type="index"
        />
        <el-table-column
          prop="storeNodeAddr"
          label="节点地址"
        />
        <el-table-column
          prop="userNodeDomain"
          label="节点域名"
        />
        <el-table-column
          prop="referer"
          label="Referer"
        />
        <el-table-column
          prop="secretKey"
          label="SecretKey"
        />
        <el-table-column
          fixed="right"
          label="操作"
          width="280"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button>
            <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)"
            >详情</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-main>

    <!-- 修改视频可见范围对话框 -->
    <el-dialog
      append-to-body
      :visible.sync="showKeyDialog"
      center
    >
      <el-table
        :data="tableList"
        border
        height="480"
        style="width: 100%"
      >
        <el-table-column
          fixed="left"
          label="No"
          type="index"
        />
        <el-table-column
          prop="accessKeyId"
          label="AccessKeyId"
        />
        <el-table-column
          prop="accessKeySecret"
          label="AccessKeySecret"
        />
        <el-table-column
          fixed="right"
          label="操作"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)"
            >复制</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleReset(scope.$index, scope.row)"
            >重置</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </el-container>
</template>

<script>
import { getUserKeyList, getUserNodeList, resetUserKey } from '@/api/oss'

export default {
  name: 'AdminUserNode',
  data() {
    return {
      queryInfo: {
        path: null
      },
      // 屏幕宽度, 为了控制分页条的大小
      screenWidth: document.body.clientWidth,
      currentPage: 1,
      pageSize: 12,
      totalSize: 0,
      dataList: [],
      nextId: 0,
      tableList: [],
      // **********************************************************************
      showKeyDialog: false,
      form: {
        videoId: null,
        scope: 1
      }
    }
  },
  created() {
    document.title = '我的节点'
    this.getData()
  },
  methods: {
    getData() {
      this.dataList = []
      getUserNodeList().then(resp => {
        if (resp.code === 0) {
          this.dataList = resp.data
        } else {
          this.$message.error(resp.msg)
        }
      })
    },
    onRefresh() {
      this.getData()
    },
    handleCurrentChange(pageNumber) {
      this.currentPage = pageNumber
      this.getData()
      // 回到顶部
      scrollTo(0, 0)
    },
    handleEdit(index, row) {
      this.$message.info('handleEdit')
    },
    handleDelete(index, row) {
      this.$confirm('确定要删除 ' + row.title + '?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message.info('handleDelete')
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    },
    handleReset(index, row) {
      this.$confirm('确定要重置 ' + row.accessKeyId + '?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        resetUserKey().then(resp => {
          this.$message.info(resp.msg)
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    },
    onShowKeyDialog() {
      this.tableList = []
      getUserKeyList().then(resp => {
        if (resp.code === 0) {
          this.tableList = resp.data
          this.showKeyDialog = true
        } else {
          this.$message.error(resp.msg)
        }
      })
    },
    handleClose() {
    }
  }
}
</script>

<style>
</style>
