<!--
 * @Descripttion: 分销记录
 * @version: 1.0
 * @Author: zzx
 * @Date: 2024-11-09 16:08:16
 * @LastEditors: zzx
 * @LastEditTime: 2024-11-21 18:00:00
-->
<template>
    <div class="app-container">
        <el-card class="filter-container" shadow="hover">
            <div>
                <i class="el-icon-search"></i>
                <span>筛选搜索</span>
                <el-button style="float: right" type="primary" @click="handleSearchList()" size="small">
                    查询搜索
                </el-button>
                <el-button style="float: right; margin-right: 15px" @click="handleResetSearch()" size="small">
                    重置
                </el-button>
            </div>
            <div style="margin-top: 15px">
                <el-form :inline="true" :model="listQuery" size="mini" label-width="140px">
                    <el-form-item label="姓名：">
                        <el-input v-model="listQuery.userName" class="input-width" placeholder="请输入用户名称"
                            clearable></el-input>
                    </el-form-item>
                    <el-form-item label="手机号：">
                        <el-input v-model="listQuery.phone" class="input-width" placeholder="请输入手机号码"
                            clearable></el-input>
                    </el-form-item>
                    <el-form-item label="结算状态：">
                        <el-select v-model="listQuery.status" clearable placeholder="请选择">
                            <el-option v-for="item in options" :key="item.value" :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="提现时间：">
                        <el-date-picker v-model="txDate" size="mini" value-format="yyyy-MM-dd HH:mm:ss"
                            class="item-width-350 ml10" type="datetimerange" range-separator="至"
                            start-placeholder="开始日期" end-placeholder="结束日期">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item label="结算时间：">
                        <el-date-picker v-model="colseDate" size="mini" value-format="yyyy-MM-dd HH:mm:ss"
                            class="item-width-350 ml10" type="datetimerange" range-separator="至"
                            start-placeholder="开始日期" end-placeholder="结束日期">
                        </el-date-picker>
                    </el-form-item>
                </el-form>
            </div>
        </el-card>
        <el-card class="operate-container" shadow="hover">
            <div class="title">
                <div class="titleContent">
                    <i class="el-icon-tickets"></i>
                    <span>数据列表</span>
                </div>
                <div>
                    <el-button class="ml10" type="info" size="small" :disabled="exportDisabled" :loading="downloadLoading" icon="el-icon-printer"
                        @click="exportAllExcel">导出</el-button>
                </div>
            </div>
        </el-card>
        <div class="table-container">
            <el-table ref="adminTable" :data="list" style="width: 100%" v-loading="listLoading" border>
                <el-table-column label="分销员" align="center">
                    <template slot-scope="scope">
                    <span>{{ scope.row.userName || '--' }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="分销员手机号" align="center">
                    <template slot-scope="scope">
                    <span>{{ scope.row.phone || '--' }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="结算金额(直属用户)/元" align="center">
                    <template slot-scope="scope">
                    <span  class="pushRecords" @click="pushRecords(scope.row)">{{ scope.row.firstMoney || '--' }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="结算金额(下级分销)/元" align="center">
                    <template slot-scope="scope">
                    <span>{{ scope.row.secondMoney || '--' }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="结算金额(总额)/元" align="center">
                    <template slot-scope="scope">
                    <span>{{ scope.row.money || '--' }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="结算状态" align="center">
                    <template slot-scope="scope">
                        <el-tag type="success" v-if="scope.row.status === 2">已结算</el-tag>
                        <el-tag type="danger" v-else>待结算</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="提现时间" align="center" prop="withdrawTime"></el-table-column>
                <el-table-column label="结算时间" align="center" prop="settleTime"></el-table-column>
            </el-table>
        </div>
        <div class="pagination-container">
            <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
                layout="total, sizes,prev, pager, next,jumper" :current-page.sync="listQuery.pageNum"
                :page-size="listQuery.pageSize" :page-sizes="[10, 15, 20]" :total="total">
            </el-pagination>
        </div>
    </div>
</template>
<script>
import { financeList } from "@/api/distribution";
import { formatDate } from "@/utils/date";
import FileSaver from 'file-saver';
import XLSX from 'xlsx';
const defaultListQuery = {
    pageNum: 1,
    pageSize: 10,
    status: null,
    startSettleTime: "",
    endSettleTime: "",
    startWithdrawTime: "",
    endWithdrawTime: "",
};
export default {
    name: "finance",
    data() {
        return {
            colseDate: '',
            txDate: '',
            exportDisabled: false,
            downloadLoading: false,
            listQuery: Object.assign({}, defaultListQuery),
            list: null,
            total: null,
            listLoading: false,
            options: [{
                value: 1,
                label: "待结算",
            },
            {
                value: 2,
                label: "已结算",
            },
            ],
        };
    },
    watch: {
        txDate(val) {
            if (val) {
                this.listQuery.startWithdrawTime = Date.parse(val[0]);
                this.listQuery.endWithdrawTime = Date.parse(val[1]);
            } else {
                this.listQuery.startWithdrawTime = "";
                this.listQuery.endWithdrawTime = "";
            }
        },
        colseDate(val) {
            if (val) {
                this.listQuery.startSettleTime = Date.parse(val[0]);
                this.listQuery.endSettleTime = Date.parse(val[1]);

            } else {
                this.listQuery.startSettleTime = "";
                this.listQuery.endSettleTime = "";

            }
        },
    },
    created() {
        this.getList();
        if (this.$route) {
            this.listQuery.adminUserId = this.$route.query.adminUserId
        }
    },
    filters: {
        formatDateTime(time) {
            if (time == null || time === "") {
                return "N/A";
            }
            let date = new Date(time);
            return formatDate(date, "yyyy-MM-dd hh:mm:ss");
        },
    },
    methods: {
        // 跳转至记录列表
        pushRecords(row) {
            this.$router.push({
                path: "/distribution/records",
                query: {
                    adminUserId: row.adminUserId,
                    recordId: row.id,
                },
            });
        },
        // 导出
        exportAllExcel() {
            this.downloadLoading = true
            const allData = [];
            let query = JSON.parse(JSON.stringify(this.listQuery))
            query.pageSize = this.total
            query.page = 0
            financeList(query).then(res => {
                this.downloadLoading = false
                if (res.code == 200) {
                    allData.push(...res.data.list);
                    this.exportExcel(allData);
                }
            });
        },

        // 导出数据处理
        exportExcel(r) {
            const data = r.map((item) => {
                return {
                    "姓名": item.userName||'--',
                    "手机号": item.phone||'--',
                    "结算金额(直属用户)/元": item.firstMoney||'--',
                    "结算金额(下级分销)/元": item.secondMoney||'--',
                    "身份证号": item.card||'--',
                    "银行卡号":item.bankCard||'--',
                    "开户行": item.depositBank||'--',
                    "结算金额/元": item.money||'--',
                    "结算状态": item.status == 2 ? "已结算" : "待结算",
                    "提现时间": item.withdrawTime,
                    "结算时间": item.settleTime,
                }
            })
            const worksheet = XLSX.utils.json_to_sheet(data);
            const workbook = { Sheets: { data: worksheet }, SheetNames: ['data'] };
            const excelBuffer = XLSX.write(workbook, { bookType: 'xlsx', type: 'array' });
            const blob = new Blob([excelBuffer], { type: 'application/octet-stream' });
            FileSaver.saveAs(blob, '结算记录表.xlsx');
        },

        handleResetSearch() {
            this.colseDate='',
            this.txDate='',
            this.listQuery = Object.assign({}, defaultListQuery);
        },
        handleSearchList() {
            this.listQuery.pageNum = 1;
            this.getList();
        },
        handleSizeChange(val) {
            this.listQuery.pageNum = 1;
            this.listQuery.pageSize = val;
            this.getList();
        },
        handleCurrentChange(val) {
            this.listQuery.pageNum = val;
            this.getList();
        },
        getList() {
            this.listLoading = true;
            financeList(this.listQuery).then((response) => {
                this.listLoading = false;
                this.list = response.data.list;
                this.total = response.data.total;
                if(this.list.length<=0){
                    this.exportDisabled = true
                }else{
                    this.exportDisabled = false
                }
            });
        },
    },
};
</script>
<style scoped lang="scss">
.title {
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.pushRecords {
    color: #409EFF;
    cursor: pointer;
}
</style>
