<template>
    <el-dialog :visible.sync="visible" :title="title" width="60%" append-to-body>
        <el-form :model="queryParams" ref="formRef" :inline="true" @submit.prevent.stop>
            <el-form-item label="姓名">
                <el-input placeholder="请输入" v-model="queryParams.nickName"></el-input>
            </el-form-item>
            <el-form-item label="用户名">
                <el-input placeholder="请输入" v-model="queryParams.userName"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="handleQuery">搜索</el-button>
                <el-button @click="handleReset">重置</el-button>
            </el-form-item>
        </el-form>
        <div v-if="selectDataList.length > 0" class="mb20">
            <el-tag v-for="tag in selectDataList" :key="tag.userName" closable>
                {{ tag.nickName }}
            </el-tag>
        </div>
        <el-table ref="tableRef" row-key="userName" :data="list" @selection-change="handleCheckboxChange"
            v-loading="loading">
            <el-table-column type="selection" width="60"></el-table-column>
            <el-table-column label="姓名" prop="nickName" align="center"></el-table-column>
            <el-table-column label="用户名" prop="userName" align="center"></el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />
        <div slot="footer" class="dialog-footer">
            <el-button @click="handleClose">取 消</el-button>
            <el-button type="primary" @click="handleConfirm">确定</el-button>
        </div>
    </el-dialog>
</template>

<script>
import { listData, pointsDetail } from '@/api/pointsManage';
export default {
    data() {
        return {
            visible: false,
            title: '选择',
            list: [],
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                nickName: '',
                userName: ''
            },
            total: 0,
            selectDataList: [],
            loading: false
        }
    },
    props: {
        multiple: {
            type: Boolean,
            default: false
        },
        modelValue: {
            type: [Object, Array],
            default: undefined
        },
        data: {
            type: [String, Number, Array],
            default: undefined
        },
    },
    methods: {
        openModal(data) {
            this.visible = true;
            this.getList(data)

        },
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList()
        },
        handleReset() {
            this.queryParams = {
                pageNum: 1,
                pageSize: 10,
                nickName: '',
                userName: ''
            }
            this.getList()
        },
        getList(data) {
            this.loading = true;
            listData(this.queryParams).then(res => {
                this.list = res.rows;
                this.total = res.total
                if (data) {
                    this.getDetail(data)
                }
            }).finally(() => {
                this.loading = false
            })
        },
        getDetail(data) {
            let _this = this;
            pointsDetail(data).then(res => {
                _this.selectDataList = [res.data]
            })
            const users = this.list.filter((item) => {
                return data == item.userName;
            });
            if (users.length > 0) {
                users.forEach(async (item) => {
                    await _this.$nextTick(() => {
                        this.$refs.tableRef.toggleRowSelection(item, true);
                    });
                })
            }
        },
        handleCheckboxChange(selection) {
            if (!this.multiple) {
                if (selection.length > 1) {
                    this.$refs.tableRef.clearSelection();
                    this.$refs.tableRef.toggleRowSelection(selection[selection.length - 1]);
                    this.selectDataList = [selection[selection.length - 1]]
                } else {
                    this.selectDataList = selection
                }
            } else {
                this.selectDataList = selection
            }
        },
        handleClose() {
            // this.selectDataList = [];
            // this.list = [];
            // this.total = 0;
            this.visible = false
        },
        handleConfirm() {
            this.$emit('update:modelValue', this.selectDataList);
            this.$emit('confirmCallBack', this.selectDataList);
            this.handleClose()
        }
    }
}

</script>