<template>
  <div class='app-container'>
    <!-- 搜索 -->
    <el-form inline size="mini">
      <el-form-item label="创建时间：">
        <el-date-picker v-model="form.listDate" type="datetimerange" start-placeholder="开始日期" end-placeholder="结束日期"
          value-format="yyyy-MM-dd HH:mm:ss" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="getSearch">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="getRefresh">重置</el-button>
        <el-button type="primary" icon="el-icon-plus" plain @click="getDetail()"
          v-hasPermi="['content:video:add']">新增</el-button>
      </el-form-item>
    </el-form>
    <!-- 列表 -->
    <el-table :data="tableData" v-loading="loading">
      <el-table-column label="序号" type="index" align="center"></el-table-column>
      <el-table-column label="视频宣传名称" prop="name" align="center"></el-table-column>
      <el-table-column label="更新时间" prop="updateTime" align="center"></el-table-column>
      <el-table-column label="创建时间" prop="createTime" align="center"></el-table-column>
      <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="['content:video:edit']">编辑</el-button>
          <el-button type="text" v-clipboard:copy="getUrl(scope.row)" v-clipboard:success="copySuccess">复制链接</el-button>
          <el-button type="text" @click="getCode">下载二维码</el-button>
          <el-button type="delete" @click="getDelete(scope.row)" v-hasPermi="['content:video:delete']">删除</el-button>
          <vue-qr :text="getUrl(scope.row)" ref="qrcode" style="display: none;" />
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="form.pageNum" :limit.sync="form.pageSize"
      @pagination="getList" />
  </div>
</template>

<script>
import VueQr from 'vue-qr'
import { list, change } from '@/api/content/video'
export default {
  components: {
    VueQr
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 表单
      form: {
        pageNum: 1,
        pageSize: 10
      },
      // 总数据
      total: 0,
      // 列表
      tableData: []
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    // 列表
    getList() {
      this.loading = true
      list(this.form).then(res => {
        if (res.code === 0) {
          this.tableData = res.data.records
          this.total = res.data.total
          this.loading = false
        }
      })
    },

    // 搜索
    getSearch() {
      this.form.pageNum = 1
      this.getList()
    },

    // 重置
    getRefresh() {
      this.form = {
        pageNum: 1,
        pageSize: 10
      }
      this.getList()
    },

    // 新增 查看 编辑
    getDetail(id, boolean) {
      this.$router.push({
        path: `/content/video/detail`,
        query: {
          id: id,
          boolean: boolean
        }
      })
    },

    // H5路径
    getUrl(e) {
      return `${e.copyUrl}pages/devices/detail?clientType=${e.clientType}&id=${e.id}`
    },

    // 复制成功
    copySuccess() {
      this.$message.success('复制成功！')
    },

    // 下载二维码
    getCode() {
      const url = this.$refs.qrcode.$el.src
      this.$download.saveAs(url, '二维码.png')
    },

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