<template>
  <div class="page-select">
    <el-select v-model="mValue" v-bind="$attrs" v-on="$listeners">
      <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id" />
    </el-select>
    <div class="preview mt-10">
      <el-button type="text" :disabled="!mValue" @click="createQrCode(mValue)">预览</el-button>
    </div>
    <el-dialog v-if="showQrCode" :visible.sync="showQrCode" append-to-body width="400px" title="落地页预览">
      <QrCode ref="QrCode" :is-show="0" @closePopover="showQrCode = false" />
    </el-dialog>
  </div>
</template>

<script>
import { getPageList } from '@/api/page.js'
import QrCode from '@/components/QrCode'
import { urlResize } from '@/api/videoLive.js'

export default {
  components: {
    QrCode
  },
  name: 'PageSelect',
  props: {
    value: {
      type: [String, Number],
      default: ''
    }
  },
  data() {
    return {
      options: [],
      showQrCode: false
    }
  },
  computed: {
    mValue: {
      get() {
        return this.value
      },
      set(val) {
        this.$emit('update:value', val)
      }
    }
  },
  mounted() {
    getPageList({
      current: 1,
      size: 9999,
      status: 1
    }).then(res => {
      this.options = res.data.list
    })
  },
  methods: {
    async createQrCode(id) {
      this.showQrCode = true
      const ret = await urlResize({
        url: `/page?id=${id}&preview=1`
      })
      if (ret.code === 0) {
        this.$refs.QrCode.createCode(ret.data)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.page-select {
  width: 100%;
  display: flex;

  .el-select {
    flex: 1;
  }

  .preview {
    margin-left: 10px;
  }
}
</style>
