<template>
    <div class="gains">
        <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: 10px">用车收益概况</div>
                <el-row :gutter="50" class="situation">
                    <el-col :span="6" v-for="(item, key) in bikeGains" :key="key">
                        <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" style="margin-top: 10px">总收益概况
                    <el-tooltip class="item" effect="dark" content="总收益概况=用车收益+充值" placement="bottom">
                        <i class="el-icon-info" style="font-size: 18px;"></i>
                    </el-tooltip>
                </div>
                <el-row :gutter="50" class="situation">
                    <el-col :span="6" v-for="(item, key) in allGains" :key="key">
                        <el-card class="card-wrp">
                            <p class="text">
                                {{ item.text }}
                                <el-tooltip class="item" effect="dark" content="实际收益=总收益+分成比例" placement="bottom">
                                    <i class="el-icon-info" v-show="key=='actualProfit'"></i>
                                </el-tooltip>
                            </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">近30日总收益增长趋势</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.profitDetailList"
                        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="revenueAdd" label="新增收益(元)" align="center"></el-table-column>
                    <el-table-column prop="totalRevenue" label="总收益(元)" align="center"></el-table-column>
                    <el-table-column prop="profitAdd" label="新增利润(元)" align="center"></el-table-column>
                    <el-table-column prop="totalProfit" 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">
                <gains-detailed></gains-detailed>
            </el-tab-pane>

            <el-tab-pane label="总收益明细" name="third">
                <all-detailed></all-detailed>
            </el-tab-pane>
        </el-tabs>
    </div>
</template>

<script>
    import CountTo from 'vue-count-to'
    import LineChart from "./components/gainsLineChart"
    import GainsDetailed from "./components/gainsDetailed"
    import AllDetailed from "./components/allDetailed"

    import { formatDate } from "../../utils"
    import api from "@/api/capital"
    export default {
        name: "gains",
        components: {
            CountTo,
            LineChart,
            GainsDetailed,
            AllDetailed
        },
        data() {
            return {
                activeName: 'first',
                chartData: {},
                listQuery: {
                    manageAreaId: '',
                    time: [
                        new Date(new Date(new Date() - 7*24*60*60*1000).toLocaleDateString()),
                        new Date(new Date().toLocaleDateString())
                    ],
                },
                listLoading: false,
                bikeGains: {
                    todayAdd: { text: '今日新增(元)', sum: 0.00 },
                    monAdd: { text: '本月新增(元)', sum: 0.00 },
                    totalRevenue: { text: '用车总收益(元)', sum: 0.00 }
                },
                allGains: {
                    todayAdd: { text: '今日新增收益(元)', sum: 0.00 },
                    monAdd: { text: '本月新增收益(元)', sum: 0.00 },
                    totalRevenue: { text: '总收益(元)', sum: 0.00 },
                    actualProfit: { text: '实际收益(元)', sum: 0.00 },
                },
                dateParams: {
                    startDate: formatDate(new Date(new Date() - 7*24*60*60*1000)),
                    endDate: formatDate(new Date()),
                    manageAreaId: ''
                },
                data: {},
                list: []
            }
        },
        methods: {
            getData() {
                delete this.listQuery.time
                api.revenueSum(this.listQuery).then(res => {
                    // console.log(res)
                    let data = res.results

                    for(let k in data.profitSum.useBikeProfit) {
                        this.bikeGains[k].sum = Number(data.profitSum.useBikeProfit[k])
                    }
                    for(let key in data.profitSum.profit) {
                        this.allGains[key].sum = Number(data.profitSum.profit[key])
                    }
                    // console.log(this.bikeGains, this.allGains)
                }).catch(err => {
                    console.log(err)
                })

                api.RevenueChartList(this.listQuery).then(res => {
                    // console.log(res)
                    let data = res.results

                    this.chartData = {
                        dateData: [],
                        profitData: [],
                        revenueData: []
                    }
                    data.revenueAndProfitTrendList.forEach((item, i) => {
                        this.chartData.dateData.push(item.createTime)
                        this.chartData.profitData.push(item.profitAdd)
                        this.chartData.revenueData.push(item.revenueAdd)
                    })
                    // console.log(this.chartData)
                    this.$refs.lineChart.initChart(this.chartData)
                }).catch(err => {
                    console.log(err)
                })
            },
            areaChange(id) {
                console.log(id)
                this.getData()
                this.dateParams.manageAreaId = id
                this.getTable()
            },
            getTable() {
                this.listLoading = true
                api.getProfitDetailList(this.dateParams).then(res => {
                    this.listLoading = false
                    console.log(res)
                    this.list = res.results
                }).catch(err => {
                    console.log(err)
                })
            },
            handleClick(tab, event) {
                console.log(tab, event);
            },
            curPage(val) {
                console.log(val)
                this.listQuery.pageNumber = val
                this.getList()
            },
            dateChange(val) {
                console.log(val)
                this.dateParams.startDate = formatDate(val[0])
                this.dateParams.endDate = formatDate(val[1])
            },
            exportExcel() {
                console.log('export Excel')
                this.listLoading = true
                import('@/vendor/Export2Excel').then(excel => {
                    console.log(excel.export_json_to_excel)
                    const tHeader = ['时间', '新增收益(元)', '总收益(元)', '新增利润(元)', '总利润(元)']
                    const filterVal = ['payTime', 'revenueAdd', 'totalRevenue', 'profitAdd', 'totalProfit']
                    const list = this.list.profitDetailList
                    console.log(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>
.gains {
    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: 20px;
    }
    .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>
