<template>
  <el-dialog
    :title="__('')"
    :visible.sync="dialogFormVisible"
    width="60%"
    @close="close"
  >
    <vab-query-form>
      <vab-query-form-left-panel :span="12" />
      <vab-query-form-right-panel :span="12">
        <el-button type="primary" @click="exportCsv">
          {{ __('导出') }}
        </el-button>
      </vab-query-form-right-panel>
    </vab-query-form>
    <el-table
      ref="table"
      v-loading="listLoading"
      border
      :data="list"
      :height="tableConfig.height"
      @selection-change="setSelectRows"
    >
      <el-table-column align="center" show-overflow-tooltip type="selection" />
      <el-table-column
        align="center"
        :label="__('序号')"
        prop="ucb_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('用户编号')"
        prop="user_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('用户昵称')"
        prop="user_nickname"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('手机号')"
        prop="user_mobile"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('扫码时间')"
        prop="ucb_scan_time"
        show-overflow-tooltip
      />
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
  </el-dialog>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import {
    getListChannelBring,
    channelBringExportFile,
  } from '@/api/user/channel'

  export default {
    name: 'TopicImport',
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 480,
        },
        selectRows: '',
        listLoading: false,
        list: [],
        dialogFormVisible: false,
        ucc_id: '',
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        queryForm: {
          pageNum: 1,
          pageSize: 10,
        },
      }
    },
    created() {},

    methods: {
      __,

      showUserImp(row) {
        this.ucc_id = row.ucc_id
        this.getChannelBringList()
        this.dialogFormVisible = true
      },
      setSelectRows(val) {
        this.selectRows = val
      },
      close() {
        this.dialogFormVisible = false
      },
      exportCsv() {
        const rows = this.$refs.table.selection
        if (!rows || rows.length <= 0) {
          this.$baseMessage('没有选中的数据！', 'error')
          return
        }
        let ucb_ids = rows.map((item) => item.ucb_id)
        channelBringExportFile({ ucb_ids: ucb_ids.toString() }).then((data) => {
          const blob = new Blob([data])
          const downloadElement = document.createElement('a')
          const href = window.URL.createObjectURL(blob) //创建下载的链接
          downloadElement.href = href
          downloadElement.download = decodeURIComponent('渠道引流用户信息.csv') //下载后文件名
          document.body.appendChild(downloadElement)
          downloadElement.click() //点击下载
          document.body.removeChild(downloadElement) //下载完成移除元素
          window.URL.revokeObjectURL(href) //释放掉blob对象
        })
        this.getChannelBringList()
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.getChannelBringList()
      },

      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.getChannelBringList()
      },
      async getChannelBringList() {
        this.listLoading = true
        this.queryForm.ucc_id = this.ucc_id
        const { data } = await getListChannelBring(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
    },
  }
</script>
