<template>
        <div :style="'padding:10px;background:#f7f7f7;min-height:'+tableHeight+'px;'">
        <div style="margin: 0 auto;width:1120px;">
            <el-row :gutter="10">
            <el-col :span="24">    
            <div class="box" style="height:650px;">
                <div >
                    <div class="lableTxt">项目利润快照</div>
                    <el-divider></el-divider>
                    <!--利润率列表-->
                    <el-table :data="list" highlight-current-row v-loading="listLoading" max-height="300" style="width: 100%;">
                        <el-table-column prop="indate" label="校准日期"  ></el-table-column>
                        <el-table-column prop="profitA"  >
                            <template slot="header">
                               <span>利润率A</span>
                               <el-popover placement="top" width="250" trigger="hover" content="利润率A = (项目金额 - 总成本)/项目金额">
                                   <i class="el-icon-question" slot="reference" />
                               </el-popover>
                            </template>
                            <template slot-scope="scope">
                                {{scope.row.profitA}}%
                            </template>
                        </el-table-column>
                        <el-table-column prop="profitB" label="利润率B"  >
                            <template slot="header">
                               <span>利润率B</span>
                               <el-popover placement="top" width="350" trigger="hover" content="利润率B = (项目金额 - 总成本 - 预留金额1)/项目金额">
                                   <i class="el-icon-question" slot="reference" />
                               </el-popover>
                            </template>
                            <template slot-scope="scope">
                                {{scope.row.profitB}}%
                            </template>
                        </el-table-column>
                        <el-table-column prop="profitC" label="利润率C"  >
                            <template slot="header">
                               <span>利润率C</span>
                               <el-popover placement="top" width="430" trigger="hover" content="利润率C = (项目金额 - 总成本 - 预留金额1 - 预留金额2)/项目金额">
                                   <i class="el-icon-question" slot="reference" />
                               </el-popover>
                            </template>
                            <template slot-scope="scope">
                                {{scope.row.profitC}}%
                            </template>
                        </el-table-column>
                        <el-table-column prop="contractAmount" label="项目金额" ></el-table-column>
                        <el-table-column  label="基线成本" >
                            <el-table-column prop="baseMan" label="人员成本">
                                <template slot-scope="scope">
                                ￥{{scope.row.baseMan | numberToCurrency}}
                                </template>
                            </el-table-column>
                            <el-table-column prop="baseFee" label="费用">
                                <template slot-scope="scope">
                                ￥{{scope.row.baseFee | numberToCurrency}}
                                </template>
                            </el-table-column>
                            <el-table-column prop="baseOutsourcing" label="外包费用">
                                <template slot-scope="scope">
                                ￥{{scope.row.baseOutsourcing | numberToCurrency}}
                                </template>
                            </el-table-column>
                            <el-table-column prop="baseRisk1" label="风险预留金额1" width="150">
                                <template slot-scope="scope">
                                ￥{{scope.row.baseRisk1 | numberToCurrency}}
                                </template>
                            </el-table-column>
                            <el-table-column prop="baseRisk2" label="风险预留金额2" width="150">
                                <template slot-scope="scope">
                                ￥{{scope.row.baseRisk2 | numberToCurrency}}
                                </template>
                            </el-table-column>
                        </el-table-column>
                    </el-table>

                    <el-divider ></el-divider>
                    <!--图形显示 -->
                    <div id="chartPanel" style="height: 300px;width: 1100px;"></div>
                </div>
            </div>
            </el-col>
            </el-row>
        </div>
        
    </div>
</template>
<style scoped>
#executorPanel {
        display: inline-block;
        width: 100%; 
    }  
.el-divider--horizontal {
    margin: 10px 0;
    height: 0.5px;
}
.box {
    background:#fff;border: 1px solid #eeeeee;border-radius:5px;padding:10px;
    height:303.7px;margin-top:10px;
}
.info span {
    color:#303133;
}
.gray_label {
    color:#999 !important;
}

.el-row {
    margin-top:10px;
}
.lableTxt {
    color:#666;
}

</style>
<script>
    import util from "../../common/js/util";
    export default {
        data() {
            return {
                yList3:[],
                yList2:[],
                yList1:[],
                xList:[],
                list:[],
                costChart:null,
                stagesChart: null,
                executorChart: null,
                pVisible:false,
                taskSum:{},
                users:[],
                importanceList:[{id:1,label:'一般'},{id:2,label:'紧急'},{id:3,label:'重要'},{id:4,label:'重要且紧急'}],
                //1-一般,2-紧急,3-重要,4-重要且紧急
                levelTxt:["全部","一般","紧急","重要","重要且紧急"],
                //1-进行中,2-已完成,3-已撤销
                statusTxt: ["全部","进行中","已完成","已撤销"],
                addFolderDialog: false,
                upLoading:false,
                user: JSON.parse(sessionStorage.getItem("user")),
                addLoading: false,
                curProjectId:null,
                title: "",
                
            };
        },
        filters: {
            numberToCurrency(value) {
                if (!value) return '0.00'
                value = value.toFixed(2)
                const intPart = Math.trunc(value)
                const intPartFormat = intPart.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,')
                let floatPart = '.00'
                const valueArray = value.toString().split('.')
                if (valueArray.length === 2) { // 有小数部分
                floatPart = valueArray[1].toString() // 取得小数部分
                return intPartFormat + '.' + floatPart
                }
                return intPartFormat + floatPart
            }
        },
        methods: {
            getProfitSnapshot() {
                let _this = this;
                this.http.post('/earning-snapshot/list', {projectId: this.curProjectId},
                res => {
                    if (res.code == "ok") {
                        var list = JSON.parse(JSON.stringify(res.data)).reverse();
                        _this.list = res.data;
                        for (var m=0;m<list.length; m++) {
                            this.xList.push(list[m].indate);
                            this.yList1.push(list[m].profitA);
                            this.yList2.push(list[m].profitB);
                            this.yList3.push(list[m].profitC);
                        }
                        //图表展示
                        var myChart = echarts.init(document.getElementById("chartPanel"));
                        myChart.setOption({
                            title: {
                                textStyle: {
                                    color: "#666666",
                                    fontSize: 18,
                                    fontWeight: 'normal',
                                 },
                              text: list.length == 0?"暂无数据":"利润率趋势图",
                              left: list.length == 0?"center":"left",
                              top: list.length == 0?"center":"left"
                            },
                            toolbox: {
                                show: true,
                                feature:{
                                    saveAsImage:{
                                        show:true
                                    },
                                }
                            },
                            tooltip:{
                               trigger: 'axis'
                            },
                            legend: {
                                data: ['利润率A', '利润率B', '利润率C']
                            },
                            grid: {
                                left: '3%',
                                right: '4%',
                                bottom: '3%',
                                containLabel: true
                            },
                            xAxis: {
                                type: 'category',
                                boundaryGap: false,
                                data: this.xList
                            },
                            yAxis: {
                                type: 'value'
                            },
                            series : [
                                {
                                    name: '利润率A',
                                    type: 'line',
                                    stack: '百分比',
                                    data: this.yList1
                                },
                                {
                                    name: '利润率B',
                                    type: 'line',
                                    stack: '百分比',
                                    data: this.yList2
                                },
                                {
                                    name: '利润率C',
                                    type: 'line',
                                    stack: '百分比',
                                    data: this.yList3
                                },
                            ]
                        })
                    } else {
                        this.$message({
                            message: res.msg,
                            type: "error"
                        });
                    }
                },
                error => {
                    this.$message({
                        message: error,
                        type: "error"
                    });
                });
            },
            getStagesPanel(){
                let _this = this;
                this.http.post('/task/getStagesPanel', {projectId: this.curProjectId},
                res => {
                    if (res.code == "ok") {
                        var list = res.data;
                        var myChart = echarts.init(document.getElementById("stagesPanel"));
                        _this.stagesChart = myChart;
                        myChart.setOption({
                            title: {
                                show:list.length == 0,
                                textStyle: {
                                    color: "#666666",
                                    fontSize: 18,
                                    fontWeight: 'normal',
                                 },
                              text: list.length == 0?"暂无数据":"任务列表统计",
                              left: "center",
                              top: "center"
                            },
                            toolbox: {
                                show: true,
                                feature:{
                                    saveAsImage:{
                                        show:true
                                    },
                                }
                            },
                            tooltip:{
                                trigger:'item',
                                formatter: "{b}<br/>任务数:{c} ({d}%)",
                            },
                            series : [
                                {
                                    name: '任务列表',
                                    type: 'pie',
                                    radius: '55%',
                                    data:list
                                }
                            ]
                        })
                    } else {
                        this.$message({
                            message: res.msg,
                            type: "error"
                        });
                    }
                },
                error => {
                    this.$message({
                        message: error,
                        type: "error"
                    });
                });
            },
            getExecutorPanel(){
                let _this = this;
                this.http.post('/task/getExecutorPanel', {projectId: this.curProjectId},
                res => {
                    if (res.code == "ok") {
                        var xList = [], yList = [], list = res.data;
                        for(var i in list) {
                            xList.push(list[i].executorName);
                            yList.push({
                                "value": list[i].taskCount,
                                "id": list[i].executorId
                            });
                        }
                        var myChart = echarts.init(document.getElementById("executorPanel"));
                        _this.executorChart = myChart;
                        var option = {
                            title: {
                                show:list.length == 0,
                                textStyle: {
                                    color: "#666666",
                                    fontSize: 18,
                                    fontWeight: 'normal',
                                 },
                              text: list.length == 0?"暂无数据":"执行人分配图",
                              left: "center",
                              top: "center"
                            },
                            toolbox: {
                                show: true,
                                feature:{
                                    saveAsImage:{
                                        show:true
                                    },
                                    restore:{
                                        show:true
                                    },
                                    magicType:{
                                        type:['line','bar']
                                    },
                                }
                            },
                            tooltip:{
                                trigger:'axis',
                                formatter: function (params,ticket,callback) {
                                    var res = params[0].name + ""+" : " + params[0].data.value 
                                    + "个";
                                    _this.params = params;
                                    return res;
                                }
                            },
                            xAxis: {
                                data: xList,
                                axisLabel: {
                                    interval:0,rotate:20
                                }
                            },
                            yAxis: [{
                                type : 'value',
                                axisLabel: {
                                    formatter:'{value} '
                                }
                            }],
                            series: [{
                                name: '任务数量（个）',
                                type: 'bar',
                                barMaxWidth: 30,
                                data: yList,
                            }]
                        };
                        myChart.setOption(option);
                        console.log('===这是完成');
                    } else {
                        this.$message({
                            message: res.msg,
                            type: "error"
                        });
                    }
                },
                error => {
                    this.$message({
                        message: error,
                        type: "error"
                    });
                });
            },
            getProjectTaskSum() {
                this.http.post('/project/taskSum', {
                    id: this.curProjectId
                },
                res => {
                    if (res.code == "ok") {
                        this.taskSum = res.data;
                    } else {
                        this.$message({
                        message: res.msg,
                        type: "error"
                        });
                    }
                },
                error => {
                    this.$message({
                        message: error,
                        type: "error"
                    });
                });
            },
            refreshPage() {
                this.curProjectId = parseInt(this.$route.params.id);
                this.getProfitSnapshot();
            }
        },
        created() {
            console.log('created===');
            let height = window.innerHeight;
            this.tableHeight = height - 160;
            const that = this;
            window.onresize = function temp() {
                that.tableHeight = window.innerHeight - 160;
            };
        },
        mounted() {
            console.log('=========图表mounted===');
            this.curProjectId = parseInt(this.$route.params.id);
            var _this = this;
            window.addEventListener("resize", function() {
                _this.executorChart.resize();
                _this.stagesChart.resize();
                _this.costChart.resize();
            });
            this.getProfitSnapshot(); // 调用图表方法
        }
    };
</script>

<style scoped>
</style>