<template>
  <div class="app-container">
    <el-button type="primary" icon="el-icon-plus" size="mini" @click="getDetail()"
      v-hasPermi="['operation:waken:add']">新增换醒音</el-button>
    <!-- 列表 -->
    <el-table :data="tableData" v-loading="loading">
      <el-table-column type="index" label="序号" align="center" />
      <el-table-column label="唤醒音名称" align="center" prop="name" show-overflow-tooltip />
      <el-table-column label="唤醒音封面" align="center" width="100px">
        <template slot-scope="scope">
          <el-image :src="scope.row.pic" />
        </template>
      </el-table-column>
      <el-table-column label="关联设备" align="center" :formatter="devFormatter" show-overflow-tooltip />
      <el-table-column label="关联内容数" align="center" prop="num" />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="getDetail(scope.row.id, true)">查看</el-button>
          <el-button type="text" @click="getDetail(scope.row.id)" v-hasPermi="['operation:waken:edit']">
            编辑
          </el-button>
          <el-button type="delete" @click="getDelete(scope.row)" v-hasPermi="['operation:waken:delete']">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { list, devices, remove } from '@/api/operation/waken'
export default {
  data() {
    return {
      // 遮罩层
      loading: false,
      // 列表
      tableData: [],
      // 设备列表
      deviceOptions: []
    }
  },
  mounted() {
    this.getList()
    this.getDevices()
  },
  methods: {
    // 列表
    getList() {
      this.loading = true
      list().then(res => {
        if (res.code === 0) {
          this.tableData = res.data
          this.loading = false
        }
      })
    },

    // 设备列表
    getDevices() {
      this.$nextTick(() => {
        devices(1).then(res => {
          if (res.code === 0) {
            this.deviceOptions = res.data
          }
        })
      })
    },

    // 详情
    getDetail(id, boolean) {
      this.$router.push({
        path: `/operation/waken/detail`,
        query: {
          id: id,
          boolean: boolean
        }
      })
    },

    // 删除
    getDelete(row) {
      this.$confirm(`是否删除${row.name}?`, `提示`, {
        type: 'warning'
      }).then(() => {
        remove(row.id).then(res => {
          if (res.code === 0) {
            this.$message.success('删除成功！')
            this.getList()
          }
        })
      })
    },

    // 字典翻译
    devFormatter(row) {
      return this.deviceOptions.length > 0 ? row.deviceIds.split(',').map(i => this.deviceOptions.find(j => j.clientTypeId == i).name).join('，') : ''
    }
  }
}
</script>

<style lang="scss" scoped></style>