<template>
    <!-- 吨钢综合能耗影响因素分析 -->
    <div class="steelFactorsAffecting">
        <div class="box">
            <div class="box-top">
                <el-form
                    style="overflow: hidden;"
                    :style="filterForm.show ? null : { height: '0px' }"
                    size="mini"
                    label-width="70px"
                >
                    <el-row>
                        <el-col :span="6">
                            <el-form-item label="时间粒度">
                                <el-select
                                    filterable clearable
                                    v-model="filterForm.data.timegranid"
                                    placeholder=""
                                    style="width: 100%;"
                                >
                                    <el-option
                                        v-for="item of nameObj.timegranid.arr"
                                        :key="item.id"
                                        :value="item.id"
                                        :label="item.name"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="比较时间">
                                <zj-timegran-date
                                    v-model="filterForm.data.clock"
                                    style="width: 100%;"
                                    :timegranId="filterForm.data.timegranid"
                                    :timegranObj="nameObj.timegranid.obj"
                                    :defaultOffset="-1"
                                ></zj-timegran-date>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="被比时间">
                                <zj-timegran-date
                                    v-model="filterForm.data.compareClock"
                                    style="width: 100%;"
                                    :timegranId="filterForm.data.timegranid"
                                    :timegranObj="nameObj.timegranid.obj"
                                    :defaultOffset="-2"
                                ></zj-timegran-date>
                            </el-form-item>
                        </el-col>
                        <!-- <el-col :span="6">
                            <el-form-item label="工序">
                                <el-select
                                    filterable clearable
                                    v-model="filterForm.data.workprocid"
                                    multiple
                                    collapse-tags
                                    placeholder=""
                                    style="width: 100%;"
                                >
                                    <el-option
                                        v-for="item of nameObj.workprocid.arr"
                                        :key="item.id"
                                        :value="item.id"
                                        :label="item.name"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col> -->
                    </el-row>
                </el-form>
                <div class="box-top-gjl">
                    <!-- <el-button
                        class="button"
                        type="primary"
                        size="mini"
                        icon="el-icon-download"
                    >导出</el-button> -->
                    <div style="float:right; text-align: right;">
                        <el-button
                            class="button"
                            type="primary"
                            size="mini"
                            icon="el-icon-search"
                            v-privilege="activeMenu + 'QUERY'"
                            @click="getTableData(1)"
                        >查询</el-button>
                        <el-button
                            class="button"
                            type="primary"
                            size="mini"
                            icon="el-icon-refresh"
                            v-privilege="activeMenu + 'QUERY'"
                            @click="tableDataCancel();"
                        >重置</el-button>
                        <el-button
                            size="mini"
                            :icon="filterForm.show ? 'el-icon-caret-top' : 'el-icon-caret-bottom'"
                            :title="filterForm.show ? '收起搜索区' : '展开搜索区'"
                            @click="filterForm.show = !filterForm.show;
                                $nextTick(() => {
                                    singleTableHeight = getRoleHeight($refs['singleTable'].$el) - 45;
                                }
                            );"
                        >{{ filterForm.show ? '收起' : '展开' }}</el-button>
                    </div>
                </div>
            </div>
            <div class="box-bottom">
                <el-table
                    stripe
                    id="singleTable"
                    ref="singleTable"
                    :data="tableData"
                    v-loading="tableLoading"
                    style="width: 100%;"
                    :height="singleTableHeight"
                    border
                    size="mini"
                    :span-method="arraySpanMethod"
                    highlight-current-row
                    :row-class-name="tableRowClassName"
                >
                    <el-table-column label="序号" type="index" align="center" width="50"></el-table-column>
                    <el-table-column
                        sortable
                        prop="WORKPROCNAME"
                        label="工序名称"
                        min-width="100px"
                        :show-overflow-tooltip="true"
                    ></el-table-column>
                    <el-table-column :label="colList[0]" :key="colList[0]" :show-overflow-tooltip="true">
                        <el-table-column prop="GBXS" label="钢比系数(t/t钢)" min-width="100px"></el-table-column>
                        <el-table-column prop="APPORTVALUE" label="工序能耗(kgce/t)" min-width="100px"></el-table-column>
                    </el-table-column>
                    <el-table-column :label="colList[1]" :key="colList[1]" :show-overflow-tooltip="true">
                        <el-table-column prop="COMPGBXS" label="钢比系数(t/t钢)" min-width="100px"></el-table-column>
                        <el-table-column prop="COMPAPPORTVALUE" label="工序能耗(kgce/t)" min-width="100px"></el-table-column>
                    </el-table-column>
                    <el-table-column
                        sortable
                        prop="SUBSTEALMODULUS"
                        label="钢比系数差值"
                        min-width="100px"
                        :show-overflow-tooltip="true"
                    ></el-table-column>
                    <el-table-column
                        sortable
                        prop="SUBWORKPROCCONSUMPTION"
                        label="工序能耗差值"
                        min-width="100px"
                        :show-overflow-tooltip="true"
                    ></el-table-column>
                    <el-table-column label="对吨钢综合能耗的影响">
                        <el-table-column
                            sortable
                            prop="INFLSTEALMODULUS"
                            label="产量结构影响分析"
                            min-width="100px"
                            :show-overflow-tooltip="true"
                        ></el-table-column>
                        <el-table-column
                            sortable
                            prop="INFLWORKPROCCONSUMPTION"
                            label="工序能耗影响分析"
                            min-width="100px"
                            :show-overflow-tooltip="true"
                        ></el-table-column>
                    </el-table-column>
                </el-table>
                <!-- <el-pagination
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total"
                    :page-sizes="[10, 20, 50, 100, 500, 1000]"
                    :page-size="pageSize"
                    :current-page.sync="pageNum"
                    @size-change="tableSizeChange"
                    @current-change="getTableData()"
                    style="text-align: right;margin-top: 10px;"
                ></el-pagination> -->
            </div>
        </div>
    </div>
</template>

<script>
import timegranDate from '@/components/zg/timegranDate.vue'
export default {
    name: 'steelFactorsAffecting',
    components: {
        'zj-timegran-date': timegranDate
    },
    data () {
        return {
            colList: ['比较日期', '被比日期'], // 时间['2020-05', '2020-06']
            activeMenu: '',
            filterForm: {
                show: true,
                data: {
                    timegranid: 'MONTH',
                    clock: '',
                    compareClock: '',
                    workprocid: ''
                },
                rules: {

                }
            },
            pageNum: 1,
            pageSize: 20,
            total: 0,
            singleTableHeight: 100,
            tableData: [
                // {
                //     clockStealmodulus: '',
                //     clockWorkprocconsumption: '',
                //     compareStealmodulus: '',
                //     compareWorkprocconsumption: '',
                //     subStealmodulus: '',
                //     subWorkprocconsumption: '',
                //     inflStealmodulus: '',
                //     inflWorkprocconsumption: ''
                // }
            ],
            nameObj: {
                timegranid: {
                    obj: {
                        'MONTH': {
                            name: '月',
                            format: 'yyyy-MM'
                        },
                        'YEAR': {
                            name: '年',
                            format: 'yyyy'
                        }
                    },
                    arr: [
                        {
                            id: 'MONTH',
                            name: '月'
                        },
                        {
                            id: 'YEAR',
                            name: '年'
                        }
                    ]
                },
                workprocid: {
                    obj: {},
                    arr: []
                }
            },
            loading: false,
            tableLoading: false
        }
    },
    created () {
        this.activeMenu = window.localStorage.getItem('activeMenu');
    },
    mounted () {
        let that = this;
        window.PEDataObj = {
            // 将数据绑定到window上，供main页面使用
            vm: that,
            // tableArr：用于导出成Excel的表格的信息
            tableArr: [
                {
                    name: '',
                    id: 'singleTable'
                }
            ]
        };
        that.$nextTick(() => {
            // 立即获取的height有一定偏差，通过setTimeout延迟来解决
            setTimeout(() => {
                that.singleTableHeight = that.getRoleHeight(that.$refs['singleTable'].$el) - 45;
            }, 1);
        });
        that.getNodeData();
    },
    methods: {
        //  计算树区域高度
        getRoleHeight (dom) {
            return window.innerHeight - dom.offsetTop;
        },
        // 获取工序列表
        getNodeData () {
            let that = this
            let url = 'pass/ems/v1/trmworkprocs/getIdAndName';
            that.axios.get(url)
                .then(function (res) {
                    if (res.code === '0') {
                        let arr = [];
                        let obj = {};
                        for (let item of res.data) {
                            arr.push({
                                id: item.id,
                                name: item.name
                            })
                            obj[item.id] = item.name;
                        }
                        that.nameObj.workprocid.arr = arr;
                        that.nameObj.workprocid.obj = obj;
                    } else {
                        that.$message.error(res.message);
                    }
                });
        },
        // 获取表格中的数据
        getTableData (pageNum) {
            let that = this;
            if (!that.filterForm.data.timegranid) {
                that.$message.warning('请选择时间粒度');
                return true;
            }
            if (!that.filterForm.data.clock) {
                that.$message.warning('请选择比较时间');
                return true;
            }
            if (!that.filterForm.data.compareClock) {
                that.$message.warning('请选择被比时间');
                return true;
            }
            that.colList = [];
            let params = {
                timegranid: that.filterForm.data.timegranid,
                clock: that.filterForm.data.clock,
                compareclock: that.filterForm.data.compareClock,
                workprocid: that.filterForm.data.workprocid.toString()
            };
            that.pageNum = pageNum || that.pageNum;
            that.tableLoading = true;
            let url = 'pass/ems/v1/trmindexitems/getDate632ForPage?pageNum=' + that.pageNum + '&pageSize=' + that.pageSize;
            that.axios.get(url, {
                params: params
            })
                .then(function (res) {
                    if (res.code === '0') {
                        let arr = [];
                        that.colList.push(that.filterForm.data.clock, that.filterForm.data.compareClock);
                        let lt = {}
                        let gxnh1 = 0, gxnh2 = 0;
                        let infl1 = 0, infl2 = 0;
                        lt.WORKPROCNAME = '吨钢综合能耗';
                        for (let item of res.data) {
                            if (item.GBXS && item.APPORTVALUE) {
                                gxnh1 += item.GBXS * item.APPORTVALUE;
                            }
                            if (item.COMPGBXS && item.COMPAPPORTVALUE) {
                                gxnh2 += item.COMPGBXS * item.COMPAPPORTVALUE;
                                // gxnh2 = gxnh2.toFixed(6);
                            }
                            if (item.INFLSTEALMODULUS) {
                                infl1 += item.INFLSTEALMODULUS
                            }
                            if (item.INFLWORKPROCCONSUMPTION) {
                                infl2 += item.INFLWORKPROCCONSUMPTION
                            }
                            arr.push(item);
                        }
                        lt.APPORTVALUE = gxnh1.toFixed(6);
                        lt.GBXS = gxnh1.toFixed(6);
                        lt.COMPAPPORTVALUE = gxnh2.toFixed(6);
                        lt.COMPGBXS = gxnh2.toFixed(6);
                        lt.INFLSTEALMODULUS = infl1.toFixed(6);
                        lt.INFLWORKPROCCONSUMPTION = infl2.toFixed(6);
                        lt.LAST = '1'
                        arr.push(lt);
                        that.tableData = arr;
                        // that.total = res.data.total;
                        // that.dialog.analyzeChart.chart.option.xAxis.data = xAxis;
                        // that.dialog.analyzeChart.chart.option.series.data = series;
                        // that.$refs['graphical'].setGraphical();
                    } else {
                        that.$message.error(res.message);
                    }
                    that.tableLoading = false;
                }).catch(function () {
                    that.tableLoading = false;
                });
        },
        // 改变表格显示条数
        tableSizeChange (val) {
            let that = this;
            that.pageSize = val;
            that.getTableData(1);
        },
        // 重置搜索
        tableDataCancel () {
            let that = this;
            for (let key in that.filterForm.data) {
                if (that.filterForm.data[key] instanceof Array) {
                    that.filterForm.data[key] = [];
                } else {
                    that.filterForm.data[key] = '';
                }
            }
            that.getTableData(1);
        },
        // 表格合并单元格
        arraySpanMethod ({ row, column, rowIndex, columnIndex }) {
            let last = row['LAST'];
            // 污染源类别列的 行合并
            if (last) {
                if (column['property'] === 'GBXS' || column['property'] === 'COMPGBXS') {
                    return {
                        rowspan: 1,
                        colspan: 2
                    };
                } else if (column['property'] === 'APPORTVALUE' || column['property'] === 'COMPAPPORTVALUE') {
                    return {
                        rowspan: 0,
                        colspan: 0
                    };
                }
            }
        },
        tableRowClassName ({row, rowIndex}) {
            return row.LAST === '1' ? 'lastRow' : '';
        }
    }
}
</script>

<style lang="less">
.steelFactorsAffecting {
    min-width: 700px;
    height: 100%;
    .box {
        height: 100%;
        padding: 15px 15px 0 15px;
        .box-top {
            .el-form-item {
                margin-bottom: 7px;
            }
            .box-top-gjl {
                overflow: hidden;
                // margin-top: -7px;
                padding: 7px 0;
                border-top: 1px solid #ccc;
                // border-bottom: 1px solid #ccc;
                // margin-bottom: 6px;
            }
        }
        .lastRow td {
            font-weight: 700;
            // text-align: center;
        }
    }
}
</style>
