<template>
    <app-layout>
        <view class="header">
            <view class="date-group">
                <view class="date">
                    <picker mode="date" :value="startDate" @change="startDateChange">
                        <view>{{startDate}}</view>
                    </picker>
                </view>
                <text>至</text>
                <view class="date">
                    <picker mode="date" :value="endDate" @change="endDateChange">
                        <view>{{endDate}}</view>
                    </picker>
                </view>
            </view>
            <view @click="search" :class="getTheme+' '+getTheme+`-m-back`"  class="summary-btn" >统计</view>
        </view>
        <view class="tabs">
            <view  :class="tabIndex==index?'active '+`${getTheme} ${getTheme}-m-back`:`${getTheme} ${getTheme}-m-back`"
                   class="tab"  v-for="(item,index) in tabs" :key="index" @click="changeTab(index)">
                {{item.name}}
            </view>
        </view>
        <view class="content">
            <view class="list-item" v-for="(item,index) in tabs[tabIndex]['listItem']" :key="index">
                <text>{{item.name}}</text>
                <text>{{item.value}}</text>
            </view>
        </view>
        <view class="desc" @click="showDesc = !showDesc">
            <text>{{tabs[tabIndex].name}}说明</text>
            <image src="../../../static/image/icon/arrow-right.png"></image>
        </view>
        <view v-if="showDesc" class="show-desc">
            <view class="main">
                <image class="close" src="../../../static/image/icon/close.png" @click="showDesc = !showDesc"></image>
                <view class="title">{{tabs[tabIndex].name}}说明</view>
                <view class="desc-con">{{tabs[tabIndex].desc}}</view>
            </view>
        </view>
    </app-layout>
</template>


<script>
    import { mapState,mapGetters } from "vuex";
    export default {
        data(){
            return {
                showDesc:'',
                tabIndex: 0,
                date: this.getDate({
                    format: true
                }),
                startDate:this.getDate('start'),
                endDate:this.getDate('end'),
                tabs:[
                    {
                        name: '人数',
                        value: 1,
                        desc:'新增顾客是指自己发展客户和团队代理发展顾客；新增代理是指自己团队的代理数；团队人数是指截止结束日期团队代理的总数。',
                        listItem:[
                            {
                                key: 'newCustomer',
                                name: '新增顾客',
                                value: 0,
                            },
                            {
                                key: 'newShare',
                                name: '新增代理',
                                value: 0,
                            },
                            {
                                key: 'myTeam',
                                name: '团队人数',
                                value: 0,
                            }
                        ],
                    },
                    {
                        name: '业绩',
                        value: 2,
                        desc:'个人业绩是指代理自己购买产生的业绩；团队业绩是指除代理自己购买外，团队其他人购买产生的业绩；总共业绩是指截止筛选结束日，团队的总业绩。',
                        listItem:[
                            {
                                key: 'selfPerformance',
                                name: '个人业绩',
                                value: 0,
                            },
                            {
                                key: 'teamPerformance',
                                name: '团队业绩',
                                value: 0,
                            },
                            {
                                key: 'totalPerformance',
                                name: '总共业绩',
                                value: 0,
                            }
                        ],
                    },
                    {
                        name: '销量',
                        value: 3,
                        desc:'个人销量是指代理自己购买产生的销量；团队销量是指除代理自己购买外，团队其他人购买产生的销量；总共销量是指截止筛选结束日，团队的总销量。\n',
                        listItem:[
                            {
                                key: 'selfSale',
                                name: '个人销量',
                                value: 0,
                            },
                            {
                                key: 'teamSale',
                                name: '团队销量',
                                value: 0,
                            },
                            {
                                key: 'totalSale',
                                name: '总共销量',
                                value: 0,
                            }
                        ],
                    },
                    {
                        name: '返利',
                        value: 4,
                        desc: '个人返利是指代理自己购买产生的返利；团队返利是指除代理自己购买外，团队其他人购买产生的返利；总共返利是指截止筛选结束日，团队的总返利。\n',
                        listItem:[
                            {
                                key: 'selfCash',
                                name: '个人返利',
                                value: 0,
                            },
                            {
                                key: 'teamCash',
                                name: '团队返利',
                                value: 0,
                            },
                            {
                                key: 'totalCash',
                                name: '总共返利',
                                value: 0,
                            }
                        ],
                    }

                ]
            }
        },
        methods:{
            changeTab(index){
                this.tabIndex = index
            },
            search(){
                this.$showLoading();
                this.$request({
                    url: this.$api.share.summary,
                    data: {
                        startDate: this.startDate,
                        endDate: this.endDate
                    }
                }).then(response => {
                    uni.hideLoading();
                    this.$hideLoading();
                    if(response.code == 1){
                        const data = response.data
                        this.tabs.map(item => {
                            for (const index in item['listItem']) {
                                item['listItem'][index]['value'] = data[item['listItem'][index]['key']].toString()
                            }
                        })
                    }
                }).catch(() => {
                    uni.hideLoading();
                    this.$hideLoading();
                })
            },
            startDateChange(e){
                this.startDate = e.detail.value;
            },
            endDateChange(e){
                this.endDate = e.detail.value;
            },
            getDate(type) {
                const date = new Date();

                let year = date.getFullYear();
                let month = date.getMonth() + 1;
                let day = date.getDate();

                if (type === 'start') {
                    const startDate = new Date(date.getTime() - 30 * 24 * 60 * 60 * 1000);
                    year = startDate.getFullYear();
                    month = startDate.getMonth() + 1;
                    day = startDate.getDate();
                } else if (type === 'end') {
                    //return `${year}-${month}-${day}`;
                }
                month = month > 9 ? month : '0' + month;
                day = day > 9 ? day : '0' + day;

                return `${year}-${month}-${day}`;
            }
        },
        onLoad(){
            this.search()
        },
        computed: {
            ...mapGetters('mallConfig', {
                getTheme: 'getTheme',
            })
        }

    }
</script>

<style scoped lang="scss">
    $defaultSpace:30rpx;
    $borderColor:#ccc;
    $defaultColor:#FF3A30;
    .header{
        display: flex;
        align-items: center;
        background: white;
        padding: $defaultSpace/3 $defaultSpace;
        .date-group{
            display: flex;
            flex: 1;
            border-radius: 10rpx;
            border: 1rpx solid $borderColor;
            height: 80rpx;
            align-items: center;
            .date{
                display: flex;
                flex: 1;
                align-items: center;
                justify-content: center;
            }
            text{
                padding: 0 20rpx;
            }
        }
        .summary-btn{
            padding: 0 $defaultSpace;
            color: #ffffff;
            border-radius: 10rpx;
            height: 80rpx;
            margin-left: 20rpx;
            display: flex;
            align-items: center;
            justify-content: center;
        }
    }
    .tabs{
        display: flex;
        align-items: center;
        justify-content: center;
        margin: $defaultSpace $defaultSpace;
        border-radius: 20rpx;
        overflow: hidden;
        background: white;
        .tab{
            color: #333333;
            border-left: 1rpx solid $borderColor;
            display: flex;
            flex: 1;
            align-items: center;
            justify-content: center;
            padding: $defaultSpace 0;
            &.active{
                color: #ffffff;
            }
        }
    }
    .content{
        background: white;
        margin-top: $defaultSpace;
        .list-item{
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding: $defaultSpace;
            border-bottom: 1px solid $borderColor;
            color: #666666;
        }
    }
    .desc{
        padding: $defaultSpace;
        display: flex;
        align-items: center;
        justify-content: space-between;
        background: white;
        margin-top: $defaultSpace;
        color: #666666;
        image{
            width: 20rpx;
            height: 30rpx;
        }
    }
    .show-desc{
        position: fixed;
        background: rgba(0,0,0,.3);
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
        .main{
            width: 90%;
            height: 80%;
            border-radius: $defaultSpace;
            background: white;
            position: relative;
            .close{
                position: absolute;
                right: $defaultSpace;
                top: $defaultSpace;
                border-radius: 50%;
                border: 2px solid $borderColor;
                width: 40rpx;
                height: 40rpx;
            }
            .title{
                color: #666666;
                padding: $defaultSpace 20rpx;
            }
            .desc-con{
                padding: $defaultSpace 20rpx;
            }
        }
    }
</style>