<template>
  <div class="user-resource-container">
    <vab-query-form>
      <vab-query-form-right-panel :span="24">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.ucc_code"
              clearable
              :placeholder="__('请输入页面地址')"
            />
          </el-form-item>
          <el-form-item>
            <el-select
              v-model="queryForm.user_channel_id"
              clearable
              :placeholder="__('请选择渠道名称')"
            >
              <el-option
                v-for="item in channels"
                :key="item.user_channel_id"
                :label="item.user_channel_name"
                :value="item.user_channel_id"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              {{ __('查询') }}
            </el-button>
            <el-button icon="el-icon-refresh" type="primary" @click="queryData">
              {{ __('刷新') }}
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      border
      :data="list"
      :height="tableConfig.height"
    >
      <el-table-column align="center" :label="__('操作')" width="120">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">
            {{ __('编辑') }}
          </el-button>
          <el-button type="text" @click="handleDelete(row)">
            {{ __('删除') }}
          </el-button>
          <el-button
            icon="el-icon-my-qr-code"
            type="text"
            @click="generateQrCode(row)"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('编号')"
        prop="ucc_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('页面地址')"
        prop="ucc_code"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('渠道名称')"
        prop="user_channel_name"
        show-overflow-tooltip
      />
      <el-table-column align="center" :label="__('是否启用')" prop="ucc_enable">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.ucc_enable"

            :active-value="1"

            :inactive-value="0"
            @change="sChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('渠道引流人数')"
        prop="bring_num"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <el-button type="text" @click="handleUserImp(scope.row)">
            {{ scope.row.bring_num }}
          </el-button>
        </template>
      </el-table-column>
    </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 :title="title" :visible.sync="dialogFormVisible" width="370px">
      <el-button>
        <el-image
          v-show="qrcode"
          :preview-src-list="[qrcode]"
          :src="qrcode"
          style="width: 300px; height: 300px"
        />
      </el-button>
    </el-dialog>
    <edit ref="edit" @fetch-data="fetchData" />
    <userInfo-import ref="userInfoImport" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import {
    doDeleteChannelCode,
    getListChannelCode,
    doEditChannelCode,
    poster,
  } from '@/api/user/channel'
  import Edit from './components/UserResourceEdit'
  import userInfoImport from './components/UserInfoImport'
  import { getListChannel } from '@/api/user/channel'

  export default {
    name: 'UserResource',
    components: { Edit, userInfoImport },
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        title: this.__('预览小程序码'),
        dialogFormVisible: false,
        list: [],
        channels: [],
        qrcode: '',
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNum: 1,
          pageSize: 20,
        },
      }
    },
    created() {
      this.fetchData()
      this.getChannelList()
    },
    methods: {
      __,
      handleEdit(row) {
        if (row.ucc_id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleUserImp(row) {
        this.$refs['userInfoImport'].showUserImp(row)
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },

      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      async sChange(row) {
        const param = {
          ucc_id: row.ucc_id,
          ucc_enable: row.ucc_enable,
        }
        const { msg, status } = await doEditChannelCode(param)
        if (200 == status) {
          this.$baseMessage(msg, 'success')
        } else {
          this.$baseMessage(msg, 'error')
        }
        await this.fetchData()
      },

      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      handleDelete(row) {
        if (row.ucc_id) {
          this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
            const { msg, status } = await doDeleteChannelCode({
              ucc_id: row.ucc_id,
            })
            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
            await this.fetchData()
          })
        }
      },
      async generateQrCode(row) {
        this.listLoading = true
        const { data, msg, status } = await poster({
          ucc_id: row.ucc_id,
          ucc_code: row.ucc_code,
        })
        if (status == 200) {
          this.qrcode = data
          this.dialogFormVisible = true
        } else {
          this.$baseMessage(msg, 'error')
        }
        this.listLoading = false
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getListChannelCode(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
      async getChannelList() {
        const params = {}
        params.user_channel_enable = 1
        await getListChannel(params)
          .then((res) => {
            this.channels = res.data.items
          })
          .catch((err) => {
            console.log(err)
          })
      },
    },
  }
</script>
<style scoped>
  ::v-deep .el-icon-my-qr-code {
    background: url('~@/assets/qr-code-icon.svg') center no-repeat;
    font-size: 30px;
    width: 30px;
    height: 30px;
    background-size: cover;
    display: inline-block;
    margin-top: -10px;
  }
  ::v-deep .el-icon-my-qr-code:before {
    fontent: '\e75f';
  }
</style>
