<template>
    <div class="adminCalpointData">
        <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="日期">
                                <zj-timegran-date
                                    v-model="filterForm.data.clock"
                                    style="width: 100%;"
                                    :type="'daterange'"
                                    :timegranId="filterForm.data.timegranid"
                                    :oneFoo="getTableData"
                                    :defaultOffset="-1"
                                ></zj-timegran-date>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="开始时间" prop="timestart">
                                <el-date-picker
                                    v-model="filterForm.data.timestart"
                                    type="datetime"
                                    placeholder="开始时间"
                                    style="width: 100%;"
                                    format="yyyy-MM-dd HH"
                                    value-format="yyyy-MM-dd HH"
                                ></el-date-picker>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="结束时间" prop="timeend">
                                <el-date-picker
                                    v-model="filterForm.data.timeend"
                                    type="datetime"
                                    placeholder="结束时间"
                                    style="width: 100%;"
                                    format="yyyy-MM-dd HH"
                                    value-format="yyyy-MM-dd HH"
                                ></el-date-picker>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="能介类型">
                                <el-select
                                    filterable
                                    clearable
                                    v-model="filterForm.data.energytypeid"
                                    placeholder="能介类型"
                                    style="width: 100%;"
                                    @change="filterForm.data.energyid = []"
                                >
                                    <el-option
                                        v-for="item of nameObj.energytypeid.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
                                    filterable
                                    clearable
                                    multiple
                                    collapse-tags
                                    v-model="filterForm.data.energyid"
                                    placeholder="能介"
                                    style="width: 100%;"
                                >
                                    <template v-for="item of nameObj.energyid.arr">
                                        <el-option
                                            v-if="item.energytypeid === filterForm.data.energytypeid || filterForm.data.energytypeid === ''"
                                            :key="item.id"
                                            :value="item.id"
                                            :label="item.name"
                                        ></el-option>
                                    </template>
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="6">
                            <el-form-item label="手抄或自动">
                                <el-select
                                    filterable
                                    clearable
                                    v-model="filterForm.data.itemtype"
                                    placeholder="手抄或自动"
                                    style="width: 100%;"
                                >
                                    <el-option
                                        v-for="item of nameObj.itemtype.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
                                    filterable
                                    clearable
                                    multiple
                                    collapse-tags
                                    v-model="filterForm.data.locateid"
                                    placeholder="位置"
                                    style="width: 100%;"
                                    :disabled="filterForm.disabled.locateid"
                                >
                                    <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-input
                                    clearable
                                    v-model.trim="filterForm.data.itemid"
                                    placeholder="计量点编号"
                                    style="width: 100%;"
                                    @keyup.enter.native="getTableData(1)"
                                ></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="计量点名称">
                                <el-input
                                    clearable
                                    v-model.trim="filterForm.data.itemname"
                                    placeholder="计量点名称"
                                    style="width: 100%;"
                                    @keyup.enter.native="getTableData(1)"
                                ></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="6">
                            <el-form-item label="站所">
                                <el-select
                                    filterable
                                    clearable
                                    multiple
                                    collapse-tags
                                    v-model="filterForm.data.networkid"
                                    placeholder="站所"
                                    style="width: 100%;"
                                >
                                    <el-option
                                        v-for="item of nameObj.networkid.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="TAG点别名">
                                <el-input
                                    clearable
                                    v-model.trim="filterForm.data.tagAlias"
                                    placeholder="TAG点别名"
                                    style="width: 100%;"
                                    @keyup.enter.native="getTableData(1)"
                                ></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="用能属性">
                                <el-select
                                    filterable clearable
                                    v-model="filterForm.data.propertyid"
                                    placeholder="用能属性"
                                    multiple
                                    collapse-tags
                                    style="width: 100%;"
                                >
                                    <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-row>
                </el-form>
                <div class="box-top-gjl">
                    <el-button
                        class="button"
                        type="primary"
                        size="mini"
                        icon="el-icon-edit-outline"
                        v-privilege="activeMenu + 'CREATE'"
                        @click="re_hour_data()"
                        :loading="loading"
                    >重新生成小时数据</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">
            </div>
        </div>
    </div>
</template>

<script>
import { zCheckNumber1 } from '@/utils/validator.js'
import store from '@/store/index.js';
import timegranDate from '@/components/zg/timegranDate.vue'
import { formatDate, commonSummaries } from '@/utils/util.js';
export default {
    name: 'measuringLocationAdd',
    components: {
        'zj-timegran-date': timegranDate
    },
    data () {
        return {
            multipleSelection: [],
            tableFormDataObj: {},
            store,
            userInfo: {
                data: null
            },
            activeMenu: '',
            filterForm: {
                show: true,
                data: {
                    itemtype: '',
                    clock: '',
                    energytypeid: '',
                    energyid: '',
                    timegranid: '',
                    locateid: '',
                    itemid: '',
                    itemname: '',
                    networkid: '',
                    hierarchy: '',
                    tagAlias: '',
                    propertyid: ''
                },
                disabled: {
                    locateid: false
                },
                rules: {

                }
            },
            pageNum: 1,
            pageSize: 20,
            total: 0,
            singleTableHeight: 200,
            tableData: [],
            nameObj: {
                timegranid: {
                    obj: {},
                    arr: []
                },
                energytypeid: {
                    obj: {},
                    arr: []
                },
                energyid: {
                    obj: {},
                    arr: []
                },
                propertyid: {
                    obj: {},
                    arr: []
                },
                itemtype: {
                    obj: {
                        'AUTO': '自动上传',
                        'MANUAL': '手抄',
                        'VIRTUAL': '虚拟'
                    },
                    arr: [
                        {
                            id: 'AUTO',
                            name: '自动上传'
                        },
                        {
                            id: 'MANUAL',
                            name: '手抄'
                        },
                        {
                            id: 'VIRTUAL',
                            name: '虚拟'
                        }
                    ]
                },
                locateid: {
                    obj: {},
                    arr: []
                },
                networkid: {
                    obj: {},
                    arr: []
                },
                hierarchy: {
                    obj: {},
                    arr: []
                },
                unitid: {
                    obj: {},
                    arr: []
                }
            },
            loading: false,
            tableLoading: false,
            dialog: {
                editorBox: {
                    show: false,
                    type: '',
                    form: {
                        data: {
                            itemid: '',
                            correctvalue: '',
                            apportvalue: '',
                            bz: '',
                            updatereason: ''
                        },
                        rules: {
                            correctvalue: [
                                { required: true, message: '该项不能为空', trigger: 'change' },
                                { validator: zCheckNumber1, trigger: 'change' }
                            ],
                            apportvalue: [
                                { required: true, message: '该项不能为空', trigger: 'change' },
                                { validator: zCheckNumber1, trigger: 'change' }
                            ],
                            update_reason: [
                                { required: true, message: '该项不能为空', trigger: 'change' }
                            ]
                        }
                    }
                }
            }
        }
    },
    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.store.dispatch('getUserInfo').then((res) => {
            that.userInfo.data = res.data;
        });
        // 接收路由参数
        if (that.$route.query.locateid) {
            that.filterForm.disabled.locateid = true;
            that.filterForm.data.locateid = that.$route.query.locateid;
        }
        if (that.$route.query.timegranid) {
            that.filterForm.data.timegranid = that.$route.query.timegranid;
        }
        if (that.$route.query.itemtype) {
            that.filterForm.data.itemtype = that.$route.query.itemtype;
        }
        that.getEnergyTypeIdData();
        that.getEnergyIdData();
        that.getLocateIdData();
        that.getNetworkIdData();
        that.getHierarchyData();
        that.getPropertyIdData();
    },
    methods: {
        //  计算树区域高度
        getRoleHeight (dom) {
            return window.innerHeight - dom.offsetTop;
        },
        // 自定义的表格合计方法：只给指定列进行合计
        getSummaries (param) {
            const prop = ['actualvalue', 'correctvalue', 'apportvalue', 'sumvalue']; // 合计列绑定的prop
            return commonSummaries(param, prop);
        },
        // 获取能介类型
        getEnergyTypeIdData () {
            let that = this
            let url = 'pass/ems/v1/trmenergytypes/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
                            })
                            obj[item.id] = item.name;
                        }
                        that.nameObj.energytypeid.arr = arr;
                        that.nameObj.energytypeid.obj = obj;
                    } else {
                        that.$message.error(res.message);
                    }
                });
        },
        // 获取能介
        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);
                    }
                });
        },
        // 获取位置
        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);
                    }
                });
        },
        // 获取站所
        getNetworkIdData () {
            let that = this
            let url = 'pass/ems/v1/trmstations/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.networkid.arr = arr;
                        that.nameObj.networkid.obj = obj;
                    } else {
                        that.$message.error(res.message);
                    }
                });
        },
        // 获取计量层级
        getHierarchyData () {
            let that = this
            let url = 'pass/ems/v1/trmcalpoints/getRmCalpoint/';
            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.hierarchy.arr = arr;
                        that.nameObj.hierarchy.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);
                    }
                });
        },
        // 重置搜索
        tableDataCancel () {
            let that = this;
            for (let key in that.filterForm.data) {
                if (!that.filterForm.disabled || !that.filterForm.disabled[key]) {
                    if (that.filterForm.data[key] instanceof Array) {
                        that.filterForm.data[key] = [];
                    } else {
                        that.filterForm.data[key] = '';
                    }
                }
            }
        },
        // 重新生成小时数据
        re_hour_data () {
            let that = this;
            if (!that.filterForm.data.timestart || that.filterForm.data.timestart.length === 0) {
                that.$message.warning('请选择日期范围');
                return true;
            }
            if (!that.filterForm.data.timeend || !that.filterForm.data.timeend) {
                that.$message.warning('请选择开始日期与结束日期');
                return true;
            }
            let params = {
                itemtype: that.filterForm.data.itemtype,
                clock: that.filterForm.data.timestart,
                clocke: that.filterForm.data.timeend,
                energytypeid: that.filterForm.data.energytypeid,
                energyid: that.filterForm.data.energyid.toString(),
                locateid: that.filterForm.data.locateid.toString(),
                itemid: that.filterForm.data.itemid,
                itemname: that.filterForm.data.itemname,
                networkid: that.filterForm.data.networkid.toString(),
                tagAlias: that.filterForm.data.tagAlias,
                propertyid: that.filterForm.data.propertyid.toString()
            };
            that.$confirm('是否重新生成小时数据？日期跨度不易太长！否则运行时间较长', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                state: 'warning'
            }).then(() => {
                that.loading = true;
                let url = 'pass/ems/v1/trmcalpoints/reHourData'
                that.axios.get(url, {
                    params: params
                })
                    .then(function (res) {
                        if (res.code === '0') {
                            if (res.message && res.message !== '操作成功') {
                                that.$message.warning('重新生成数据成功, 其中【' + res.message + '】');
                            } else {
                                that.$message({
                                    message: '重新生成数据成功',
                                    type: 'success'
                                });
                            }
                            //that.getTableData();
                        } else {
                            that.$message.error(res.message);
                        }
                        that.loading = false;
                    }).catch(function () {
                        that.loading = false;
                    });
            }).catch(() => {
            });
        }
    }
}
</script>

<style lang="less">
.adminCalpointData {
    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;
                // border-bottom: 1px solid #ccc;
                // margin-bottom: 6px;
            }
        }
        .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;
            }
        }
    }
    .attribute-box {
        .item {
            border: #ccc 1px solid;
            padding: 5px;
            margin-bottom: 6px;
            .bt {
                font-size: 14px;
                font-weight: 600;
                color: #000;
                padding-left: 10px;
                border-left: #008fe0 5px solid;
            }
        }
    }
}
</style>
