<template>
    <el-dialog
        v-model="visible"
        :title="'活动参与记录'"
        width="80%"
        append-to-body
        destroy-on-close
    >
        <el-form
            :model="queryParams"
            ref="queryRef"
            :inline="true"
            v-show="showSearch"
            label-width="68px"
        >
            <el-form-item label="用户id" prop="userId">
                <el-input
                    v-model="queryParams.userId"
                    placeholder="请输入用户id"
                    clearable
                    @keyup.enter="handleQuery"
                />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                    type="warning"
                    plain
                    icon="Download"
                    @click="handleExport"
                    v-hasPermi="['service:imLiveActivityRecord:export']"
                >
                    导出
                </el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table
            v-loading="loading"
            :data="imLiveActivityRecordList"
            @selection-change="handleSelectionChange"
        >
            <!-- <el-table-column type="selection" width="55" align="center" /> -->
            <el-table-column label="活动ID" align="center" prop="activityId" />
            <el-table-column label="直播间ID" align="center" prop="roomId" />
            <el-table-column label="用户Id" align="center" prop="userId" />
            <el-table-column label="记录类型" align="center" prop="recordType">
                <template #default="scope">
                    {{ formatRecordType(scope.row.recordType) }}
                </template>
            </el-table-column>
            <el-table-column label="记录时间" align="center" prop="recordTime" width="180">
                <template #default="scope">
                    <span>{{ parseTime(scope.row.recordTime, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <!-- <el-table-column label="奖励类型" align="center" prop="rewardType" /> -->
            <el-table-column label="奖励值" align="center" prop="rewardValue" />
            <el-table-column label="状态" align="center" prop="status">
                <template #default="scope">
                    {{ formatStatus(scope.row.status) }}
                </template>
            </el-table-column>
            <el-table-column label="IP地址" align="center" prop="ipAddress" />
            <!-- <el-table-column label="设备信息" align="center" prop="deviceInfo" /> -->
            <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-button
                        link
                        type="primary"
                        icon="Edit"
                        @click="handleUpdate(scope.row)"
                        v-hasPermi="['service:imLiveActivityRecord:edit']"
                    >
                        修改
                    </el-button>
                    <el-button
                        link
                        type="primary"
                        icon="Delete"
                        @click="handleDelete(scope.row)"
                        v-hasPermi="['service:imLiveActivityRecord:remove']"
                    >
                        删除
                    </el-button>
                </template>
            </el-table-column> -->
        </el-table>

        <pagination
            v-show="total > 0"
            :total="total"
            v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize"
            @pagination="getList"
        />
    </el-dialog>
</template>

<script setup name="ImLiveActivityRecordModal">
import {
    listImLiveActivityRecord,
    getImLiveActivityRecord,
    delImLiveActivityRecord,
    addImLiveActivityRecord,
    updateImLiveActivityRecord,
} from '@/api/service/imLiveActivityRecord';

const { proxy } = getCurrentInstance();

// 定义props
const props = defineProps({
    // 活动ID
    activityId: {
        type: [Number, String],
        default: null,
    },
});

// 定义emit事件
const emit = defineEmits(['close']);

const imLiveActivityRecordList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref('');
const visible = ref(false);

const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        activityId: props.activityId, // 默认使用传入的活动ID
        roomId: null,
        userId: null,
        recordType: null,
        recordTime: null,
        rewardType: null,
        rewardValue: null,
        status: null,
        ipAddress: null,
        deviceInfo: null,
    },
});

const { queryParams, form } = toRefs(data);

// 记录类型映射
const recordTypeMap = {
    1: '参与',
    2: '完成',
    3: '获奖',
};

// 状态映射
const statusMap = {
    0: '参与',
    1: '获奖',
};

// 格式化记录类型
function formatRecordType(type) {
    return recordTypeMap[type] || type;
}

// 格式化状态
function formatStatus(status) {
    return statusMap[status] || status;
}

/** 打开弹窗 */
function openModal(activityId) {
    visible.value = true;
    // 重置查询参数，确保使用传入的activityId
    queryParams.value.activityId = activityId;
    getList();
}

/** 关闭弹窗 */
function closeModal() {
    visible.value = false;
    emit('close');
}

/** 查询活动参与记录列表 */
function getList() {
    loading.value = true;
    // 确保查询参数中包含activityId
    // if (props.activityId) {
    //     queryParams.value.activityId = props.activityId;
    // }

    listImLiveActivityRecord(queryParams.value).then(response => {
        imLiveActivityRecordList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}

// 取消按钮
function cancel() {
    open.value = false;
    reset();
}

// 表单重置
function reset() {
    form.value = {
        id: null,
        activityId: props.activityId || null, // 默认使用传入的activityId
        roomId: null,
        userId: null,
        recordType: null,
        recordTime: null,
        rewardType: null,
        rewardValue: null,
        status: null,
        ipAddress: null,
        deviceInfo: null,
        createTime: null,
        updateTime: null,
    };
    proxy.resetForm('imLiveActivityRecordRef');
}

/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
}

/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm('queryRef');
    // 重置时保留activityId
    queryParams.value.activityId = props.activityId;
    handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.id);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
}

/** 导出按钮操作 */
function handleExport() {
    proxy.download(
        'service/imLiveActivityRecord/export',
        {
            ...queryParams.value,
        },
        `imLiveActivityRecord_${new Date().getTime()}.xlsx`
    );
}

// 暴露方法给父组件
defineExpose({
    openModal,
    closeModal,
});
</script>

<style scoped lang="scss">
.el-dialog {
    position: relative;
    .pagination-container {
        position: relative !important;
    }
}
</style>
