<template>
  <div>
    <QueryListPage ref="queryListPage" :pager-config="pagerConfig" :query-form="queryForm" :query-list="queryList" />
    <input id="competition-manage-url11" v-model="url" style="opacity: 0;">
  </div>
</template>

<script>
import { getPagingQueryList, putLicenseValid } from '@/api/pushMessage';

export default {
    name: 'CacheControl',
    components: {
    },
    data() {
        return {
            url: '',
            validList: [
                {label: '有效', value: true},
                {label: '无效', value: false}
            ],
            pagerConfig: {
                pageSize: 'pageSize',
                pageIndex: 'pageNum'
            },
            queryForm: {
                labelWidth: '80px',
                reset: true,
                queryObject: {
                    name: {
                        type: 'text',
                        label: '名称'
                    }
                }

            },
            queryList: {
                query: this.queryFunction,
                selectCheckShow: false,
                indexShow: true,
                columns: [
                    {
                        title: '名称',
                        prop: 'name'
                    },
                    {
                        title: '结束时间',
                        prop: 'overTime'
                    },
                    {
                        title: '授权码',
                        prop: 'license'
                    },
                    {
                        title: '秘钥',
                        prop: 'secretKey'
                    },
                    {
                        title: '有效',
                        prop: 'available',
                        type: 'tag',
                        columnValue: (row) => { return this.$convertField(row.available, this.validList, ['value', 'label']); },
                        tagType: (row) => { return row.available ? '' : 'danger'; }
                    },
                    {
                        type: 'button',
                        title: this.$t('global.operate'),
                        width: '250',
                        buttons: [
                            {
                                name: '复制授权码',
                                handleClick: this.copyLicense,
                                type: ''
                            },
                            {
                                name: '设置失效',
                                handleClick: this.handleValid,
                                type: 'danger'
                            }
                        ]
                    }
                ]
            },
            currentModel: {}
        };
    },
    created() {
    },
    methods: {
        // 设置失效
        handleValid(index, row) {
            this.$confirm('您确定设置此秘钥失效么？', this.$t('global.tips'), {
                confirmButtonText: this.$t('global.confirm'),
                cancelButtonText: this.$t('global.cancel'),
                type: 'warning'
            }).then(() => {
                putLicenseValid(row.id).then(response => {
                    this.$message.success(this.$t('tip.successfullyDelete'));
                    this.reloadTable();
                }).catch(() => {
                    this.$messageBox(this.$t('tip.failDelete'));
                });
            });
        },
        copyLicense(index, row) {
            this.url = `${row.license}`;
            const inputText = document.getElementById('competition-manage-url11');
            inputText.value = row.license;
            inputText.select(); // 选择对象
            document.execCommand('Copy'); // 执行浏览器复制命令
            this.$message.success('授权码已经复制到粘贴板');
        },
        queryFunction(params) {
            return getPagingQueryList(params);
        },
        reloadTable() {
            this.queryList.reload();
        }
    }
};
</script>
