<script lang="ts">
export default {
    name: "record",
};
</script>

<script setup lang="ts">
import { onMounted, reactive, ref, toRefs } from "vue";
import { ElForm, ElTable } from "element-plus";
import { Search, Refresh, ArrowUp, ArrowDown } from "@element-plus/icons-vue";
// 组件引用
import SecondRecord from "./components/SecondRecord.vue";
import { inviteRecord } from "@/api/invitation/index";
import { RecordQueryParam, RecordItem } from "@/types/api/invitation/index";
const dataTableRef = ref(ElTable);
const queryFormRef = ref(ElForm);

const state = reactive({
    loading: true,
    searchBox: {
        title: "展开搜索",
        expand: false,
    },
    queryParams: {
        pageNo: 1,
        pageSize: 15,
        realName: "",
        mobilePhone: "",
        email: "",
        id: "",
    } as RecordQueryParam,
    recordList: [] as RecordItem[],
    total: 0,
});

const { loading, searchBox, queryParams, recordList, total } = toRefs(state);

function handleQuery() {
    state.loading = true;
    inviteRecord(state.queryParams).then(({ data }) => {
        state.recordList = data.content;
        state.total = data.totalElements;
        state.loading = false;
    });
}

/**
 * 排序
 */
function handleSortChange(column: any) {
    var sortingType = column.order;
    state.queryParams.property = column.prop;
    if (sortingType === "ascending") {
        state.queryParams.direction = "1";
    } else if (sortingType === "descending") {
        state.queryParams.direction = "0";
    } else {
        state.queryParams.property = null;
        state.queryParams.direction = null;
    }
    handleQuery();
}

/**
 * 查询重置
 */
function resetQuery() {
    queryFormRef.value.resetFields();
    handleQuery();
}

/**
 * 展开/收起搜索
 */
const closeSearch = () => {
    state.searchBox.expand = !state.searchBox.expand;
    state.searchBox.title = !state.searchBox.expand ? "展开搜索" : "收起搜索";
};

onMounted(() => {
    handleQuery();
});
</script>

<template>
    <div class="app-container">
        <!-- 搜索表单 -->
        <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item prop="id">
                <el-tooltip
                    content="请输入会员名称搜索"
                    placement="bottom"
                    effect="light"
                >
                    <el-input
                        v-model="queryParams.id"
                        placeholder="请输入会员ID"
                        style="width: 300px"
                        clearable
                        @keyup.enter="handleQuery"
                    />
                </el-tooltip>
            </el-form-item>
            <el-form-item prop="mobilePhone" v-show="searchBox.expand">
                <el-tooltip
                    content="请输入手机号搜索"
                    placement="bottom"
                    effect="light"
                >
                    <el-input
                        v-model="queryParams.mobilePhone"
                        placeholder="请输入手机号搜索"
                        style="width: 300px"
                        clearable
                        @keyup.enter="handleQuery"
                    />
                </el-tooltip>
            </el-form-item>
            <el-form-item prop="email" v-show="searchBox.expand">
                <el-tooltip
                    content="请输入邮箱搜索"
                    placement="bottom"
                    effect="light"
                >
                    <el-input
                        v-model="queryParams.email"
                        placeholder="请输入邮箱搜索"
                        style="width: 300px"
                        clearable
                        @keyup.enter="handleQuery"
                    />
                </el-tooltip>
            </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-button
                    link
                    @click="closeSearch"
                    style="border: 0"
                    size="small"
                >
                    {{ searchBox.title }}
                    <template v-if="searchBox.expand"
                        ><el-icon class="el-icon--right"><ArrowUp /></el-icon
                    ></template>
                    <template v-else
                        ><el-icon class="el-icon--right"><ArrowDown /></el-icon
                    ></template>
                </el-button>
            </el-form-item>
        </el-form>

        <!-- 数据表格 -->
        <el-table
            ref="dataTableRef"
            v-loading="loading"
            :data="recordList"
            @sort-change="handleSortChange"
            highlight-current-row
            border
        >
            <el-table-column type="expand">
                <template #default="scope">
                    <SecondRecord
                        :id="scope.row.id"
                        width="90%"
                        height="auto"
                    />
                </template>
            </el-table-column>
            <el-table-column
                label="编号"
                prop="id"
                align="center"
                sortable="custom"
            />
            <el-table-column
                label="用户名"
                prop="username"
                min-width="160"
                align="center"
            />
            <el-table-column
                label="邀请码"
                prop="promotionCode"
                min-width="160"
                align="center"
            />
            <el-table-column
                label="一级邀请"
                prop="firstLevel"
                align="center"
            />
            <el-table-column
                label="二级邀请"
                prop="secondLevel"
                align="center"
            />
            <el-table-column
                label="三级邀请"
                prop="thirdLevel"
                align="center"
                sortable="custom"
            />
            <el-table-column
                label="获得积分"
                prop="generalizeTotal"
                align="center"
                sortable="custom"
            />
        </el-table>

        <!-- 分页工具条 -->
        <pagination
            v-if="total > 0"
            :total="total"
            v-model:page="queryParams.pageNo"
            v-model:limit="queryParams.pageSize"
            @pagination="handleQuery"
        />
    </div>
</template>

<style lang="scss" scoped>
.dropdown-text {
    margin-left: 12px;
    height: 24px;
    line-height: 24px;
    font-size: 12px;
}
</style>
