<script setup>
import { ref, reactive } from 'vue'
import { Search } from '@element-plus/icons-vue'
import {apiCustomerUser} from '@/api/customer'
import { apiMediaCommon } from '@/api/media'
import ReadDialog from './components/ReadDialog.vue'

const tableColumns = [
    {
        label:'手机号',
        key:'Mobile',
        sortable:false
    },{
        label:'最近一次阅读时间',
        key:'LastReadTime',
        sortable:true
    },{
        label:'累计阅读次数',
        key:'ReadCount',
        sortable:true
    },{
        label:'注册时间',
        key:'CreatedTime',
        sortable:true
    }
]

const tableQuery = reactive({
    keyWord:'',
    currentPage:1,
    pageSize:10,
    totals:0,
    sortParam:'',
    sortType:''
})

const tableData = ref([])
const show = ref(false)
function getTableData(){
    apiCustomerUser.getTempCustomList({
        Keyword:tableQuery.keyWord,
        CurrentIndex:tableQuery.currentPage,
        PageSize:tableQuery.pageSize,
        SortParam:tableQuery.sortParam,
        SortType:tableQuery.sortType
    }).then(res=>{
        if(res.Ret!==200) return 

        tableData.value = res.Data.List||[]
        tableQuery.totals = res.Data.Paging.Totals||0
    })
}
getTableData()
getLableList()
const labelOptions = ref([])
const userId = ref('')
function getLableList(){
    apiMediaCommon.getPermissionList().then(res=>{
        if(res.Ret!==200) return
        labelOptions.value = res.Data.List||[]
    })
}

function handlePageChange(page){
    tableQuery.currentPage = page
    getTableData()
}
function handleSortChange({order,prop}){
    // ascending 
    const propMap = {
        0:'CreatedTime',
        1:'ReadCount',
        2:'LastReadTime',
    }
    tableQuery.sortParam = propMap[prop]||2
    tableQuery.sortType = order==='ascending'?1:0
    getTableData()
}

// 查看用户详情
function Details(row) {
    if (row.ReadCount <= 0) return;
    show.value = true;
    userId.value = row.Id + '';
}

/* 下载数据 */
async function downloadExcel() {
    const res = await apiCustomerUser.getTemporaryExport(
        {
            Keyword:tableQuery.keyWord,
            SortParam:tableQuery.sortParam,
            SortType:tableQuery.sortType,
        }
    )
    const blob = new Blob([res], {
        type: "application/vnd.ms-excel;charset=utf-8",
    });
    let fileName = res.fileName;

    const elink = document.createElement("a");
    elink.download = fileName; //命名下载名称
    elink.style.display = "none";
    elink.href = URL.createObjectURL(blob);
    document.body.appendChild(elink);
    elink.click(); // 点击下载
    URL.revokeObjectURL(elink.href); // 释放URL 对象
    document.body.removeChild(elink); // 释放标
}
</script>

<template>
    <el-card class="box-card">
        <div class="temp-user-list-wrap">
            <div class="search-box">
                <el-button type="primary" style="margin-right: 20px;"  @click="downloadExcel" v-permission="'tempUser:export'">导出表格</el-button>
                <el-input 
                    v-model="tableQuery.keyWord"
                    :prefix-icon="Search" clearable
                    style="width:400px"
                    placeholder="请输入手机号" 
                    @input="handlePageChange(1)"
                    />
            </div>
            <div class="table-box">
                <el-table stripe border :data="tableData" @sort-change="handleSortChange">
                    <el-table-column 
                        v-for="column in tableColumns" :key="column.key"
                        :prop="column.key" :label="column.label" :sortable="column.sortable">
                        <template #default="scope" v-if="column.key === 'AccountStatus'">
                            <el-tag :type="scope.row[column.key]=== 'Open' ?'success':'info'">{{scope.row[column.key]=== 'Open'?'已开户':'未开户' }}</el-tag>
                        </template>
                        <template #default="scope" v-else-if="column.key === 'ReadCount'">
                            <span class="ReadCount" @click="Details(scope.row)" v-permission="'tempUser:readCount'">
                                {{ scope.row[column.key] }}
                            </span>
                        </template>
                        <template #default="scope" v-else>
                            {{scope.row[column.key]}}
                        </template>
                    </el-table-column>
                </el-table>
                <el-pagination
                    background
                    layout="total,prev,pager,next,jumper"
                    :current-page="tableQuery.currentPage"
                    :page-size="tableQuery.pageSize"
                    :total="tableQuery.totals"
                    @current-change="handlePageChange"
                    style=" justify-content: flex-end; margin-top: 50px;"
                />
            </div>
        </div>
        <ReadDialog v-model:show="show" :labelOptions="labelOptions" :userId="userId"></ReadDialog>
    </el-card>
</template>

<style scoped lang="scss">
.temp-user-list-wrap{
    height: calc(100vh - 208px); //layout padding 30*2 headHeight 48
    /* display: flex;
    flex-direction: column; */
    .search-box{
        text-align: right;
        margin: 20px 0 40px 0;
    }
    .table-box{
       .ReadCount {
        color: rgba(8, 108, 224, 1);
        cursor: pointer;
       }
    }
}
</style>
