<template>
    <div id="finishInfo" class="card">
        <date @change="onChangeDate" :showRight="true" @rightAction="onRightAction"></date>
        <div class="info">
            <div class="item">
                <img class="item-icon" :src="status1" alt="" srcset="">
                <div class="item-txt">
                    <div class="item-txt-title title-blue">
                        <span>{{rate}}</span>
                        <span>%</span>
                    </div>
                    <div class="item-txt-name">完成率</div>
                </div>
            </div>
            <div class="item pointer" @click="onShowTableDialog(1)">
                <img class="item-icon" :src="status2" alt="" srcset="">
                <div class="item-txt">
                    <div class="item-txt-title title-red">
                        <span>{{finish}}</span>
                    </div>
                    <div class="item-txt-name">已完成</div>
                </div>
            </div>
            <div class="item pointer" @click="onShowTableDialog(2)">
                <img class="item-icon" :src="status3" alt="" srcset="">
                <div class="item-txt">
                    <div class="item-txt-title title-yellow">
                        <span>{{todo}}</span>
                    </div>
                    <div class="item-txt-name">待完成</div>
                </div>
            </div>
        </div>
        <div class="card-mt" :style="barStyle" id="finish-bar"></div>
    </div>
</template>

<script>
import Date from './Date.vue';

import { showFinish } from '../config/bar';
import { getJobStatus } from '@/api/home';
import homeMixin from '@/mixin/home';

export default {
    components: {
        Date,
    },
    mixins: [homeMixin],
    data () {
        return {
            status1: require('@/assets/home/status1.png'),
            status2: require('@/assets/home/status2.png'),
            status3: require('@/assets/home/status3.png'),
            dateRange: [],
            rate: 0,
            finish: 0,
            todo: 0,
            jobType: '',
            barHeight: 100,
        }
    },
    computed: {
        barStyle() {
            return {
                height: `${this.barHeight}px`,
                maxHeight: `${this.barHeight}px`,
            }
        }
    },
    methods: {
        onChangeDate(range) {
            this.dateRange = range;
            this.loadData();
            const vm = this;
            window.onresize = function() {
                vm.resize();
            };
            this.resize();
        },
        resize() {
            const e = document.querySelector('#finishInfo');
            let h = 140;
            if (document.body.clientWidth < 1600) {
                h = 150;
            }
            if (document.body.clientWidth < 1400) {
                h = 130;
            }
            this.barHeight = e.clientHeight - h;
        },
        onRightAction(data) {
            this.jobType = data || '';
            this.loadData();
        },
        loadData() {
            let start = '', end = '';
            if (this.dateRange && this.dateRange.length > 1) {
                start = this.dateRange[0];
                end = this.dateRange[1];
            }
            getJobStatus({
                start_date: start,
                end_date: end,
                enforce_org_id: this.orgId,
                job_type: this.jobType,
            }).then(res => {
                let done = 0, total = 0;
                let list = [], legend = [];
                if (res && res.data) {
                    let d = 0, t = 0;
                    res.data.map(item => {
                        d = parseInt(item.done_num);
                        done += d;
                        t = parseInt(item.total_num);
                        total += t;
                        if (t > 0) {
                            list.push(parseFloat((d / t * 100).toFixed(2)));
                        }
                        else {
                            list.push(0);
                        }
                        legend.push(item.enforce_org_name);
                    });
                    showFinish('finish-bar', {
                        legendData: legend,
                        seriesData: list,
                    });
                }
                this.finish = done;
                this.todo = total - done;
                this.rate = total > 0 ? parseFloat((done / total * 100).toFixed(2)) || 0 : 0;
            });
        },
        onShowTableDialog(type) {
            this.$emit('table-action', this.dateRange, type == 1);
        },
    }    
}
</script>

<style lang="scss" scoped>
.card {
    background: #FFFFFF;
    border: 1px solid #E9EAF8;
    border-radius: 4px;
    padding: 10px;
}
.info {
    margin-top: 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .item {
        display: flex;
        justify-content: center;
        align-items: center;
        &-icon {
            width: 46px;
        }
        &-txt {
            margin-left: 5px;
            &-title {
                font-size: 24px;
                font-family: Microsoft YaHei;
                font-weight: 400;
                &>span:nth-child(2) {
                    font-size: 14px;
                }
            }
            &-name {
                font-size: 14px;
                font-weight: 400;
                color: #333333;
            }
        }
    }
}
.title-blue {
    color: #3A8FEA;
}
.title-red {
    color: #F26789;
}
.title-yellow {
    color: #DDA121;
}@media screen and (min-width: 1608px) {
    .card {
        margin-left: 10px;
        margin-top: 10px;
    }
    .info {
        margin-top: 15px;
        .item-icon {
            width: 46px;
        }
        .item-txt-title {
            font-size: 24px;
        }
    }
}
@media screen and (max-width: 1600px) {
    .card {
        margin-left: 7px;
        margin-top: 7px;
    }
    .info {
        margin-top: 10px;
        .item-icon {
            width: 40px;
        }
        .item-txt-title {
            font-size: 22px;
        }
    }
}
@media screen and (max-width: 1300px) {
    .card {
        margin-left: 4px;
        margin-top: 4px;
    }
    .info {
        margin-top: 5px;
        .item-icon {
            width: 34px;
        }
        .item-txt-title {
            font-size: 20px;
        }
    }
}
@import '@/styles/home.scss';
</style>