<template>
<!-- CorDas数据查询 -->
    <div class="getCorDas">
        <div class="box">
            <div class="box-top">
                <el-form
                    style="overflow: hidden;"
                    :style="filterForm.show ? null : { height: '0px' }"
                    size="mini"
                    label-width="80px"
                >
                    <el-row>
                        <el-col :span="6">
                            <el-form-item label="位置">
                                <el-select
                                    filterable clearable
                                    v-model="filterForm.data.locateid"
                                    placeholder="位置"
                                    multiple
                                    collapse-tags
                                    style="width: 100%;"
                                >
                                    <el-option
                                        v-for="item of nameObj.locateid.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="能介">
                                <el-select
                                    clearable
                                    filterable
                                    multiple
                                    collapse-tags
                                    v-model="filterForm.data.energyid"
                                    placeholder="能介"
                                    style="width: 100%;"
                                    @keyup.enter.native="getTableData(1)"
                                >
                                    <el-option
                                        v-for="item of nameObj.energyid.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="用能属性">
                                <el-select
                                    clearable
                                    filterable
                                    multiple
                                    collapse-tags
                                    v-model="filterForm.data.propertyid"
                                    placeholder="用能属性"
                                    style="width: 100%;"
                                    @keyup.enter.native="getTableData(1)"
                                >
                                    <el-option
                                        v-for="item of nameObj.propertyid.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="计量点名称">
                                <el-input
                                    clearable
                                    v-model="filterForm.data.itemname"
                                    placeholder="计量点名称"
                                    style="width: 100%;"
                                    @keyup.enter.native="getTableData(1)"
                                ></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
                <div class="box-top-gjl">
                    <span style="font-size:14px;">数据获取时间：{{ CorDasTime }}</span>
                    <!-- v-if="userInfo.data && userInfo.data.userId === 'admin'" -->
                    <el-checkbox
                        v-show="tableData.length > 0"
                        v-model="autoGetData"
                        style="margin-left: 7px;"
                    >自动更新数据</el-checkbox>
                    <div style="float:right; text-align: right;">
                        <el-button
                            class="button"
                            type="primary"
                            size="mini"
                            icon="el-icon-search"
                            @click="getTableData(1)"
                        >查询</el-button>
                        <el-button
                            class="button"
                            type="primary"
                            size="mini"
                            icon="el-icon-refresh"
                            @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"
                    highlight-current-row
                >
                    <el-table-column
                        type="index"
                        label="序号"
                        align="center"
                        width="45">
                    </el-table-column>
                    <el-table-column
                        sortable
                        prop="itemid"
                        label="计量点编号"
                        width="110px"
                        :show-overflow-tooltip="true"
                    ></el-table-column>
                    <el-table-column
                        sortable
                        prop="locateid"
                        label="位置"
                        width="110px"
                        :show-overflow-tooltip="true"
                    >
                        <template slot-scope="scope">
                            <span>{{ nameObj.locateid.obj[scope.row.locateid] ? nameObj.locateid.obj[scope.row.locateid] : scope.row.locateid }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                        sortable
                        prop="energyid"
                        label="能介"
                        width="95px"
                        :show-overflow-tooltip="true"
                    >
                        <template slot-scope="scope">
                            <span>{{ nameObj.energyid.obj[scope.row.energyid] ? nameObj.energyid.obj[scope.row.energyid].name : scope.row.energyid }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                        sortable
                        prop="propertyid"
                        label="属性"
                        width="65px"
                        :show-overflow-tooltip="true"
                    >
                        <template slot-scope="scope">
                            <span>{{ nameObj.propertyid.obj[scope.row.propertyid] ? nameObj.propertyid.obj[scope.row.propertyid] : scope.row.propertyid }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                        sortable
                        prop="itemname"
                        label="计量点名称"
                        min-width="100px"
                        :show-overflow-tooltip="true"
                    ></el-table-column>
                    <el-table-column
                        sortable
                        prop="value"
                        label="CorDas数据值"
                        width="110px"
                        :show-overflow-tooltip="true"
                    >
                        <template slot-scope="scope">
                            <span :class="{ color_yes: scope.row.ischange, color_no: scope.row.noChangeSize > 60 }">{{ scope.row.value }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                        sortable
                        prop="sourceType"
                        label="CorDas数据位置"
                        min-width="100px"
                        :show-overflow-tooltip="true"
                    ></el-table-column>
                    <el-table-column
                        sortable
                        prop="btype2"
                        label="10分钟表数据位置"
                        min-width="100px"
                        :show-overflow-tooltip="true"
                    ></el-table-column>
                    <el-table-column
                        sortable
                        prop="tagTable"
                        label="小时表数据位置"
                        min-width="100px"
                        :show-overflow-tooltip="true"
                    >
                        <template slot-scope="scope">
                            <span>{{ (scope.row.tagTable && scope.row.tagCol) ? (scope.row.tagTable + '.' + scope.row.tagCol) : '' }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                        sortable
                        prop="bdesc1"
                        label="das地址"
                        min-width="100px"
                        :show-overflow-tooltip="true"
                    ></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'
import {formatDate, commonSummaries} from '@/utils/util.js';
import store from '@/store/index.js';
export default {
    name: 'getCorDas',
    components: {
        'zj-timegran-date': timegranDate
    },
    data () {
        return {
            store,
            userInfo: {
                data: null
            },
            autoGetData: false,
            autoTimeId: null,
            autoSize: 0,
            activeMenu: '',
            operateType: '0',
            filterForm: {
                show: true,
                data: {
                    locateid: '', // 位置
                    energyid: '',
                    propertyid: '',
                    itemname: ''
                },
                rules: {

                }
            },
            CorDasTime: '',
            pageNum: 1,
            pageSize: 2000,
            total: 0,
            singleTableHeight: 100,
            tableData: [],
            multipleSelection: [],
            loading: false,
            tableLoading: false,
            nameObj: {
                energyid: {
                    obj: {},
                    arr: []
                },
                propertyid: {
                    obj: {},
                    arr: []
                },
                locateid: {
                    obj: {},
                    arr: []
                }
            }
        }
    },
    watch: {
        'autoGetData': function (newVal, oldVal) {
           let that = this;
            window.clearInterval(that.autoTimeId);
            that.autoSize = 0;
            if (that.autoGetData) {
                console.log('on')
                that.autoTimeId = setInterval(function () {
                    if (that.autoSize < 100) {
                        that.getCorDasData();
                        that.autoSize++;
                    } else {
                        that.autoGetData = false;
                    }
                }, 2000);
            } else {
                console.log('off')
            }
        }
    },
    created () {
        this.activeMenu = window.localStorage.getItem('activeMenu');
    },
    mounted () {
        let that = this;
        window.PEDataObj = {
            // 将数据绑定到window上，供main页面使用
            vm: that,
            // tableArr：用于导出成Excel的表格的信息
            tableArr: [
                {
                    name: '',
                    id: 'singleTable'
                }
            ]
        };
        that.store.dispatch('getUserInfo').then((res) => {
            that.userInfo.data = res.data;
        });
        that.$nextTick(() => {
            // 立即获取的height有一定偏差，通过setTimeout延迟来解决
            setTimeout(() => {
                that.singleTableHeight = that.getRoleHeight(that.$refs['singleTable'].$el) - 15;
            }, 1);
        });
        that.getEnergyIdData();
        that.getPropertyIdData();
        that.getLocateIdData();
        that.getTableData(1);
    },
    methods: {
        //  计算树区域高度
        getRoleHeight (dom) {
            return window.innerHeight - dom.offsetTop;
        },
        // 自定义的表格合计方法：只给指定列进行合计
        getSummaries (param) {
            const prop = ['apportvalue']; // 合计列绑定的prop
            return commonSummaries(param, prop);
        },
        // 获取能介
        getEnergyIdData () {
            let that = this;
            let url = 'pass/ems/v1/trmenergys/selectNameAndId/';
            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,
                                energytypeid: item.energytypeid,
                                unitid: item.unitid
                            })
                            obj[item.id] = {
                                name: item.name,
                                unitid: item.unitid
                            }
                        }
                        that.nameObj.energyid.arr = arr;
                        that.nameObj.energyid.obj = obj;
                    } else {
                        that.$message.error(res.message);
                    }
                });
        },
        // 获取属性
        getPropertyIdData () {
            let that = this
            let url = 'pass/ems/v1/trmenergypropertys/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.propertyid.arr = arr;
                        that.nameObj.propertyid.obj = obj;
                    } else {
                        that.$message.error(res.message);
                    }
                });
        },
        // 获取位置
        getLocateIdData () {
            let that = this
            let url = 'pass/ems/v1/trmlocations/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.locateid.arr = arr;
                        that.nameObj.locateid.obj = obj;
                    } else {
                        that.$message.error(res.message);
                    }
                });
        },
        // 获取表格中的数据
        getTableData (pageNum) {
            let that = this,
                params = {
                    energytypeid: 'W,P',
                    useflag: '1',
                    itemtype: 'AUTO',
                    locateid: that.filterForm.data.locateid.join(','),
                    energyid: that.filterForm.data.energyid.join(','),
                    propertyid: that.filterForm.data.propertyid.join(','),
                    itemname: that.filterForm.data.itemname
                };
            that.pageNum = pageNum || that.pageNum;
            that.autoGetData = false;
            that.tableLoading = true;
            let url = 'pass/ems/v1/trmcalpoints/like/?pageNum=' + that.pageNum + '&pageSize=10000';
            // let url = 'pass/ems/v1/cordasconfigurations/like/?pageNum=' + that.pageNum + '&pageSize=10000';
            that.axios.get(url, {
                params: params
            })
                .then(function (res) {
                    if (res.code === '0') {
                        let arr = [];
                        for (let i = 0; i < res.data.list.length; i++) {
                            let item = res.data.list[i];
                            arr.push({
                                itemid: item.itemid, // 计量点编号
                                locateid: item.locateid, // 位置
                                energyid: item.energyid, // 能介
                                propertyid: item.propertyid, // 用能属性
                                itemname: item.itemname, // 计量点名称
                                bdesc1: item.bdesc1, // das地址
                                sourceType: item.sourceType, // CorDas数据位置
                                btype2: item.btype2, // 10分钟表数据位置
                                tagTable: item.tagTable, // 小时表
                                tagCol: item.tagCol, // 小时表字段
                                returnName: 'DATA_TAB_' + i
                            });
                        }
                        that.tableData = arr;
                        that.getCorDasData();
                        // that.total = res.data.total;
                    } else {
                        that.$message.error(res.message);
                    }
                    that.tableLoading = false;
                }).catch(function () {
                    that.tableLoading = false;
                });
        },
        // 获取CorDas的实时数据
        getCorDasData () {
            let that = this,
                cordasSql = '';
            for (let item of that.tableData) {
                if (cordasSql !== '') {
                    cordasSql += ', ';
                }
                cordasSql += item.sourceType + ' ' + item.returnName;
            }
            let url = 'pass/ems/v1/emscordass/gettabledata';
            that.axios.post(url, {
                cordasDataNames: cordasSql
            }, {
                contentType: 'application/json'
            })
                .then(function (res) {
                    if (res.code === '0') {
                        let arr = [];
                        for (let item of that.tableData) {
                            if (item.value && item.value !== res.data[0][item.returnName]) {
                                item.ischange = true;
                                item.noChangeSize = 0;
                            } else {
                                item.ischange = false;
                                item.noChangeSize ? item.noChangeSize++ : item.noChangeSize = 1;
                            }
                            item.value = res.data[0][item.returnName];
                            arr.push(item);
                        }
                        that.tableData = arr;
                        that.CorDasTime = formatDate(res.data[0].TIM.replace(/-/g, '/'), 'yyyy/MM/dd HH:mm:ss');
                    } else {
                        that.$message.error(res.message);
                    }
                }).catch(function () {
                });
        },
        // 改变表格显示条数
        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);
        }
    }
}
</script>

<style lang="less">
.getCorDas {
    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;
                padding: 7px 0;
                border-top: 1px solid #ccc;
            }
        }
        .rowDisable td {
            color: #6bb025 !important;
        }
        .el-table .el-table__row {
            height: 35px;
        }
        .el-table__body .el-form-item--mini.el-form-item {
            margin: 0px;
            .error {
                overflow: hidden;
                color: #f56c6c;
                font-size: 12px;
                line-height: 1;
            }
        }
    }
    .color_no {
        color: red;
    }
    .color_yes {
        color: #1a9f17;
    }
    .cnspicuous .el-checkbox__input.is-disabled .el-checkbox__inner {
        background-color: #dcdfe6;
        border-color: #babbbf;
    }
}
</style>
