<template>
  <div id="index">
    <el-row>
      <el-col :span="24" class="index">
        <el-col :span="24" class="top">
          <topInfo :topTitle="pageTitle"></topInfo>
        </el-col>
        <el-col :span="24" class="add" style="text-align:right">
          <el-button size="mini" type="primary" @click="$router.push({ path: './detail' })" icon="el-icon-plus">添加房间</el-button>
        </el-col>
      </el-col>
      <el-col :span="24" class="info">
        <data-table
          :fields="fields"
          @edit="toEdit"
          @stat="stat"
          @delete="toDelete"
          @order="order"
          @qrBtn="qrBtn"
          :data="list"
          :opera="opera"
          :total="total"
          @query="search"
          :toFormat="toFormat"
        >
          <template #options="{item}">
            <template v-if="item.prop === 'anchorid'">
              <el-option v-for="(i, index) in clientList" :key="index" :label="i.name" :value="i.id"></el-option>
            </template>
          </template>
        </data-table>
      </el-col>
    </el-row>
    <el-dialog title="绑定" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
      <div>
        <img :src="qc" />
      </div>
    </el-dialog>
  </div>
</template>
<script>
import topInfo from '@/layout/public/top.vue';
import dataTable from '@/components/data-table.vue';
import { mapActions, mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: room } = createNamespacedHelpers('room');
const { mapActions: roomuser } = createNamespacedHelpers('roomuser');
import QRCode from 'qrcode';
export default {
  name: 'index',
  props: {},
  components: {
    topInfo,
    dataTable,
  },
  data: () => ({
    clientList: [],
    opera: [
      {
        label: '审核',
        icon: 'el-icon-s-order',
        method: 'order',
        display: item => {
          return item.status == '0' ? true : item.status == '1' ? true : false;
        },
      },
      {
        label: '编辑',
        icon: 'el-icon-edit',
        method: 'edit',
        display: item => {
          return item.status == '0' ? true : false;
        },
      },
      {
        label: '二维码',
        icon: 'el-icon-film',
        method: 'qrBtn',
      },
      {
        label: '统计',
        icon: 'el-icon-view',
        method: 'stat',
      },
      {
        label: '删除',
        icon: 'el-icon-delete',
        method: 'delete',
        confirm: true,
      },
    ],
    fields: [
      { label: '标题', prop: 'title' },
      { label: '房间号', prop: 'name', filter: 'index' },
      { label: '房间类型', prop: 'type', format: i => (i == '0' ? '直播' : i == '1' ? '会议' : '临时用户') },
      {
        label: '主播',
        prop: 'anchorid',
        filter: 'select',
        format: true,
      },
      { label: '房间状态', prop: 'status', format: i => (i == '0' ? '未开始' : i == '1' ? '开始' : '结束') },
    ],
    list: [],
    total: 0,
    dialogVisible: false,
    roomid: '',
    qc: '',
  }),
  created() {
    this.search();
    this.searchinfo();
  },
  methods: {
    ...room(['query', 'delete', 'update', 'fetch']),
    ...roomuser({ roomuserquery: 'query' }),
    async search({ skip = 0, limit = 10, ...info } = {}) {
      let res = await this.query({ skip, limit, ...info });
      this.$set(this, `list`, res.data);
      this.$set(this, `total`, res.total);
    },
    async searchinfo() {
      let res = await this.roomuserquery({ skip: 0, limit: 100000, role: 3 });
      this.$set(this, `clientList`, res.data);
    },
    toFormat(data) {
      let { model, value } = data;
      let res = '';
      if (model === 'anchorid') {
        let findRes = this.clientList.find(f => f.id === value);
        if (findRes) res = findRes.name;
      }
      return res;
    },
    toEdit({ data }) {
      this.$router.push({ path: './detail', query: { id: data.id } });
    },
    // 统计查看
    stat({ data }) {
      this.$router.push({ path: './statList', query: { id: data.id, name: data.name } });
      console.log(data);
    },
    async toDelete({ data }) {
      const res = await this.delete(data.id);
      this.$checkRes(res, '删除成功', '删除失败');
      this.search();
    },
    // 审核
    async order({ data }) {
      this.$router.push({ path: './detailStatus', query: { id: data.id } });
    },
    // 二维码
    qrBtn({ data }) {
      this.dialogVisible = true;
      this.$set(this, `roomid`, data.id);
      this.$nextTick(() => {
        this.creatQrCode();
      });
    },
    // 创建二维码
    async creatQrCode() {
      let url = `https://zb.liaoningdoupo.com/api/onlive/auth?type=1&qrcode=${this.roomid}`; // 需要转换为二维码的内容
      let el = document.getElementById('qrCodeUrl');
      this.qc = await QRCode.toDataURL(url, { height: 200, width: 200 });
    },
    // 关闭
    handleClose(done) {
      done();
    },
  },
  computed: {
    ...mapState(['user']),
    pageTitle() {
      return `${this.$route.meta.title}`;
    },
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
};
</script>
<style lang="less" scoped>
.add {
  height: 40px;
  line-height: 35px;
  padding: 0 15px;
}
/deep/.el-dialog__body {
  text-align: center;
}
</style>
