<template>
    <div class="deposit">
        <el-tabs v-model="activeName" @tab-click="handleClick">
            <el-tab-pane label="押金分析" name="first">
                <div class="manageArea">
                    <span>区域：</span>
                    <el-select v-model="listQuery.manageAreaId" size="small" placeholder="请选择" class="filter-item" style="width: 160px" @change="areaChange">
                        <el-option v-for="item in $root.regionOptions" :key="item.id" :label="item.name" :value="item.id"/>
                    </el-select>
                </div>

                <div class="title" style="margin-top: 20px">押金概况</div>
                <el-row :gutter="50" class="situation">
                    <el-col :span="6" v-for="(item, i) in deposit" :key="i">
                        <el-card class="card-wrp">
                            <p class="text">{{ item.text }}</p>
                            <p class="sum"><count-to :start-val="0.00" :end-val="item.sum" :decimals=2 :duration="2000" /></p>
                        </el-card>
                    </el-col>
                </el-row>

                <div class="title">押金增长趋势</div>
                <line-chart ref="lineChart" style="margin-bottom: 30px;" />

                <div class="title">每日押金增长明细</div>
                <div class="filter-container">
                    <span>时间：</span>
                    <el-date-picker
                            size="small"
                            v-model="listQuery.time"
                            type="daterange"
                            range-separator="至"
                            start-placeholder="开始日期"
                            end-placeholder="结束日期"
                            format="yyyy-MM-dd"
                            value-format="yyyy-MM-dd"
                            @change="dateChange">
                    </el-date-picker>
                    <el-button size="small" type="primary" @click="getTable">搜索</el-button>
                    <el-button size="small" type="primary" icon="el-icon-download" class="export" @click="exportExcel">导出 Excel</el-button>
                </div>

                <el-table
                        v-loading="listLoading"
                        :data="list"
                        element-loading-text="Loading"
                        :header-cell-style="{background:'rgba(60,179,113, .1)'}"
                        stripe
                        fit>
                    <el-table-column prop="payTime" label="时间" align="center"></el-table-column>
                    <el-table-column prop="todayAddDeposit" label="新增押金(元)" align="center"></el-table-column>
                    <el-table-column prop="payNumber" label="交纳人数" align="center"></el-table-column>
                    <el-table-column prop="todayReturnDeposit" label="退还押金(元)" align="center"></el-table-column>
                    <!--<el-table-column label="本月累计押金(元)" align="center">-->
                        <!--<template slot-scope="scope">-->
                            <!--{{ scope.row.depositSumOfMouthStr }}-->
                        <!--</template>-->
                    <!--</el-table-column>-->
                    <el-table-column prop="depositSumStr" label="总押金(元)" align="center"></el-table-column>
                </el-table>

                <!--<div class="pagination-container" v-show="!listLoading">-->
                    <!--<span>共<span>{{ list.length }}</span>条记录</span>-->
                    <!--<el-pagination background layout="prev, pager, next" :page-size="10" :total="list.length" @current-change="curPage" style="float: right;"></el-pagination>-->
                <!--</div>-->
            </el-tab-pane>

            <el-tab-pane label="押金明细" name="second">
                <deposit-detailed></deposit-detailed>
            </el-tab-pane>
        </el-tabs>
    </div>
</template>

<script>
import CountTo from 'vue-count-to'
import LineChart from "./components/depositLineChart"
import DepositDetailed from "./components/depositDetailed"

import { formatDate } from '@/utils/index'
import api from "@/api/capital"
export default {
    name: "deposit",
    components: {
        CountTo,
        LineChart,
        DepositDetailed
    },
    data() {
        let dayMs = 6 * 24*60*60*1000
        return {
            activeName: 'first',
            chartData: {
                dateData: [],
                numData: []
            },
            listQuery: {
                manageAreaId: '',
                time: [
                    new Date(new Date(new Date() - dayMs).toLocaleDateString()),
                    new Date(new Date().toLocaleDateString())
                ],
            },
            listLoading: false,
            deposit: {
                todayAddDeposit: { text: '今日新增押金(元)', sum: 0.00 },
                todayReturnDeposit: { text: '今日退还押金(元)', sum: 0.00 },
                depositSumOfMouth: { text: '本月累计押金(元)', sum: 0.00 },
                depositSum: { text: '总押金(元)', sum: 0.00 }
            },
            dateParams: {
                startDate: formatDate(new Date(new Date() - dayMs)),
                endDate: formatDate(new Date()),
                manageAreaId: ''
            },
            data: {},
            list: [],
        }
    },
    methods: {
        handleClick: function(tab, event) {
            // console.log(tab, event);
        },
        getData() {
            delete this.listQuery.time
            api.depositSum(this.listQuery).then((res) => {
                console.log('sum：', res)
                let data = res.results
                for(let key in data.depositSum) {
                    this.deposit[key].sum = Number(data.depositSum[key])
                }
            }).catch((error) => {
                console.log(error)
            })

            api.depositChartList(this.listQuery).then((res) => {
                console.log('chart：', res)
                let data = res.results
                this.chartData = {
                    dateData: [],
                    numData: []
                }
                data.depositTrendList.forEach((item, i) => {
                    this.chartData.dateData.push(item.createTime)
                    this.chartData.numData.push(item.totalDeposit)
                })

                this.$refs.lineChart.initChart(this.chartData)
            }).catch((error) => {
                console.log(error)
            })
        },
        areaChange(id) {
            console.log(id)
            this.getData()
            this.dateParams.manageAreaId = id
            this.getTable()
        },
        getTable() {
            api.depositTrendList(this.dateParams).then((res) => {
                console.log('table：', res)
                this.list = res.results.depositTrendDetailList
            }).catch((error) => {
                console.log(error)
            })
        },
        // curPage(val) {
        //     console.log(val)
        //     this.listQuery.pageNumber = val
        //     this.getList()
        // },
        dateChange(date) {
            console.log(date)
            this.dateParams.startDate = date[0]
            this.dateParams.endDate = date[1]
            this.getData()
        },
        exportExcel() {
            console.log('export Excel')
            this.listLoading = true
            import('@/vendor/Export2Excel').then(excel => {
                console.log(excel.export_json_to_excel)
                const tHeader = ['本月累计押金', '总押金', '交纳人数', '交易时间', '新增押金', '退还押金']
                const filterVal = ['depositSumOfMouthStr', 'depositSumStr', 'payNumber', 'payTime', 'yesterdayAddDeposit', 'yesterdayReturnDeposit']
                const list = this.list
                const data = this.formatJson(filterVal, list)
                excel.export_json_to_excel({
                    header: tHeader,
                    data,
                    filename: '押金增长明细(' + this.dateParams.startDate + '至' + this.dateParams.endDate + ')',
                    autoWidth: true,
                    bookType: 'xlsx'
                })
                this.listLoading = false
            })
        },
        formatJson(filterVal, jsonData) {
            return jsonData.map(v => filterVal.map(j => v[j]))
        }
    },
    created() {
        this.getData()
        this.getTable()
    }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.deposit {
    position: relative;
    padding: 5px 16px 0;
    margin-bottom: 30px;
    .situation {
        /*padding: 0 20px;*/
        margin-bottom: 50px;
        .card-wrp {
            text-align: center;
            background-color: rgba(127,255,212, .1);
            .text {
                font-size: 20px;
            }
            .sum {
                font-size: 2rem;
                letter-spacing: 3px;
                margin: 20px 0 10px;
            }
        }
    }

    .title {
        margin-bottom: 10px;
    }
    .export {
        float: right;
    }
    .filter-container {
        padding: 0px 0 10px 12px;
    }
    .pagination-container {
        margin: 20px 0 30px;
    }

    .manageArea {
        position: absolute;
        top: 0px;
        right: 0px;
        z-index: 10;
    }
}
</style>
