<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true">
                <el-form-item>
                    <el-button type="text" @click="backToList" icon="el-icon-back" class="back">返回</el-button>
                </el-form-item>
                <el-form-item class="divLine"></el-form-item>
                <el-form-item>
                    <span class="workName">{{detailName}}</span>
                </el-form-item>
                <el-form-item style="float:right;">
                    <span style="font-size:18px;">项目成本：<span style="color:#20a0ff;">{{cost}}元</span></span>
                </el-form-item>
            </el-form>
        </el-col>

        <div id="container" :style="'height:' + tableHeight + 'px'"></div> 
    </section>
</template>

<script>
    import util from "../../common/js/util";

    export default {
        data() {
            return {
                detailId: this.$route.params.id,
                detailName: this.$route.params.name,

                user: JSON.parse(sessionStorage.getItem("user")),

                cost: 0,

                tableHeight: 0,
                
                echart: null,
            };
        },
        methods: {
            //返回
            backToList() {
                this.$router.go(-1);
            },

            //获取项目列表
            getList() {
                this.listLoading = true;
                this.http.post(this.port.project.projectCost, {
                    id: this.detailId,
                },
                res => {
                    this.listLoading = false;
                    var _this = this;
                    if (res.code == "ok") {
                        var xList = [],yList = [] , list = res.data.costList;
                        this.cost = res.data.totalMoneyCost;
                        for(var i in list) {
                            xList.push(list[i].name);
                            yList.push({
                                "value": list[i].cost,
                                "cost": list[i].costMoney
                            });
                        }

                        var myChart = echarts.init(document.getElementById("container"));
                        _this.myChart = myChart;
                        var option = {
                            // 工具箱
                            toolbox: {
                                show: true,
                                feature:{
                                    saveAsImage:{
                                        show:true
                                    },
                                    restore:{
                                        show:true
                                    },
                                    dataView:{
                                        show:true
                                    },
                                    dataZoom:{
                                        show:true
                                    },
                                    magicType:{
                                        type:['line','bar']
                                    }
                                }
                            },
                            tooltip:{
                                trigger:'axis',
                                formatter: function (params,ticket,callback) {
                                    var res = params[0].name + "<br/>工作时长（h）"+" : " + params[0].data.value 
                                    + "h <br/>工作成本（元）"+" : " + params[0].data.cost + "元";
                                    return res;
                                }
                            },
                            xAxis: {
                                data: xList,
                                axisLabel: {
                                    interval:0,rotate:20
                                }
                            },
                            yAxis: [{
                                type : 'value',
                                axisLabel: {
                                    formatter:'{value} (h)'
                                }
                            }],
                            series: [{
                                name: '工作时长（h）',
                                type: 'bar',
                                barMaxWidth: 30,
                                data: yList,
                            }]
                        };
                        myChart.setOption(option);
                    } else {
                        this.$message({
                        message: res.msg,
                        type: "error"
                        });
                    }
                },
                error => {
                    this.listLoading = false;
                    this.$message({
                        message: error,
                        type: "error"
                    });
                });
            },
        },
        created() {
            let height = window.innerHeight;
            this.tableHeight = height - 145;
            const that = this;
            window.onresize = function temp() {
                that.tableHeight = window.innerHeight - 145;
            };
        },
        mounted() {
            this.getList();
            var _this = this;
            window.addEventListener("resize", function() {
                _this.myChart.resize();
            });
        }
    };
</script>

<style lang="scss" scoped>
    .toolbar {
        .el-form-item {
            font-size: 14px;
            vertical-align: middle;
        }
        .back {
            font-size: 16px;
            cursor: pointer;
        }
        .divLine {
            width: 2px;
            background: #c3c3c3;
            height: 100%;
        }
        .workName {
            color: #333;
            font-size: 18px;
        }
        .workHours {
            color: #20a0ff;
            font-size: 18px;
        }
    }

    #container {
        float: left;
        width: 100%;
    }
</style>

<style lang="scss">
</style>