<template>
    <el-dialog :visible.sync="visible" :title="title" width="60%" append-to-body :close-on-click-modal="false">
        <el-form :model="queryParams" ref="formRef" :inline="true" @submit.prevent.stop>
            <el-form-item label="商品名称">
                <el-input placeholder="请输入" v-model="queryParams.name"></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.code" closable>
                {{ tag.name }}
            </el-tag>
        </div>
        <el-table ref="tableRef" row-key="code" :data="list" @selection-change="handleCheckboxChange"
            v-loading="loading">
            <el-table-column type="selection" width="60"></el-table-column>
            <el-table-column label="商品名称" prop="name" align="center"></el-table-column>
            <el-table-column label="商品图片" align="center">
                <template #default="{ row }">
                    <el-image :src="row.imgsUrl" style="width: 30px;height: 30px;"></el-image>
                </template>
            </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, detailData } from '@/api/pointsMall';
export default {
    data() {
        return {
            visible: false,
            title: '选择',
            list: [],
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                nickName: ''
            },
            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.name = '';
            this.handleQuery()
        },
        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;
            detailData(data).then(res => {
                _this.selectDataList = [res.data]
            })
            const users = this.list.filter((item) => {
                return data == item.code;
            });
            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.visible = false
        },
        handleConfirm() {
            this.$emit('update:modelValue', this.selectDataList);
            this.$emit('confirmCallBack', this.selectDataList);
            this.handleClose()
        }
    }
}

</script>