<script setup>
import { ref, reactive } from 'vue'
import { Search } from '@element-plus/icons-vue'
import {apiOrderConfig} from '@/api/order'
import { apiSystemCommon } from '@/api/system'

const tableColumns = [
    {
        label:'支付单号',
        key:'TransactionID',
        sortable:false
    },
    {
        label:'订单编号',
        key:'ProductOrderID',
        sortable:false
    },
    {
        label:'姓名',
        key:'RealName',
        sortable:false,
        widthsty:80,
    },
    {
        label:'手机号',
        key:'Mobile',
        sortable:false
    },
    {
        label:'商品名称',
        key:'ProductName',
        sortable:false
    },{
        label:'支付金额',
        key:'Amount',
        sortable:false,
        widthsty:100,
    },{
        label:'支付状态',
        key:'PaymentStatus',
        sortable:false,
        widthsty:100,
    },{
        label:'支付渠道',
        key:'PaymentWay',
        sortable:false,
        widthsty:90,
    },{
        label:'支付账号',
        key:'ReadCount',
        sortable:false
    },{
        label:'收款方',
        key:'MerchantID',
        sortable:false,
        widthsty:120,
    },{
        label:'完成支付时间',
        key:'DealTime',
        sortable:false
    },{
        label:'创建时间',
        key:'CreatedTime',
        sortable:false
    }
]
const orderStatusList = ref([
    {
        value: 'pending',
        label: '待支付'
    }, {
        value: 'done',
        label: '已支付'
    }, {
        value: 'failed',
        label: '已关闭'
    }
])

const tableQuery = reactive({
    keyWord:'',
    currentPage:1,
    pageSize:10,
    totals:0,
    sortType:'',
    paymentDate:'',
    paymentWay:'',
    createdDate:'',
    orderStatus:''
})

const tableData = ref([])
const show = ref(false)
const showUserDialog = ref(false)
function getTableData(){
    apiOrderConfig.getTradeOrderList({
        KeyWord:tableQuery.keyWord,
        CurrentIndex:tableQuery.currentPage,
        PageSize:tableQuery.pageSize,
        SortType:tableQuery.sortType,
        PaymentDate:tableQuery.paymentDate,
        PaymentWay:tableQuery.paymentWay,
        CreatedDate:tableQuery.createdDate,
        OrderStatus:tableQuery.orderStatus,
        IsRefund:false,
    }).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('')
const value1 = ref(['',''])
function getLableList(){
    apiSystemCommon.getSysConfig({
        ConfigKey:'paymentWay'
    }).then(res=>{
        if(res.Ret!==200) return
        labelOptions.value = res.Data.paymentWay.map(item => {
            const key = Object.keys(item)[0]; // 获取支付方式的中文名称
            const value = item[key]; // 获取支付方式的英文名称
            return {
                value: value, // 将英文名称设置为 value
                label: key // 将中文名称设置为 label
            };
        })
    })
}

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.TemplateUserId + '';
}
function handleSelectChange() {
    getTableData();
}
function userDetails(row) {
    showUserDialog.value = true;
}

/* 下载数据 */
async function downloadExcel() {
    const res = await apiOrderConfig.getExportTradeOrder(
        {
            KeyWord:tableQuery.keyWord,
            SortType:tableQuery.sortType,
            PaymentDate:tableQuery.paymentDate,
            PaymentWay:tableQuery.paymentWay,
            CreatedDate:tableQuery.createdDate,
            OrderStatus:tableQuery.orderStatus,
            IsRefund:false,
        }
    )
    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="top-box">
                <div class="search-box">
                    <el-select clearable v-model="tableQuery.orderStatus" @change="handleSelectChange()" placeholder="支付状态" style="width: 150px; margin-right: 20px;">
                        <el-option
                        v-for="item in orderStatusList"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value">
                        </el-option>
                    </el-select>
                    <el-select v-model="tableQuery.paymentWay" clearable @change="handleSelectChange()" placeholder="支付渠道" style="width: 150px; margin-right: 20px;">
                        <el-option
                            v-for="item in labelOptions"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                        </el-option>
                    </el-select>
                    <el-date-picker
                        v-model="tableQuery.createdDate"
                        type="date"
                        placeholder="付款时间"
                        format="YYYY-MM-DD"
                        style="margin-right: 20px;width: 150px;"
                        value-format="YYYY-MM-DD"
                        @change="handlePageChange(1)"
                    />
                    <el-date-picker
                        v-model="tableQuery.paymentDate"
                        type="date"
                        placeholder="下单时间"
                        style="margin-right: 20px;width: 150px;"
                        format="YYYY-MM-DD"
                        value-format="YYYY-MM-DD"
                        @change="handlePageChange(1)"
                    />
                </div>
                <div class="search-box">
                    <el-button type="primary" style="margin-right: 20px;" @click="downloadExcel" v-permission="'payment:export'">导出表格</el-button>
                    <el-input 
                        v-model="tableQuery.keyWord"
                        :prefix-icon="Search" clearable
                        style="width:400px"
                        placeholder="订单编号/姓名/手机号/商品名称" 
                        @input="handlePageChange(1)"
                        />
                </div>
            </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" :width="column.widthsty"
                        :min-width="column.minwidthsty">
                        <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)">
                                {{ 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: 20px;"
                />
            </div>
        </div>
    </el-card>
</template>

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