<template>
    <!-- 能源实时监控标准维护 -->
    <div class="nyjkbz">
        <div class="box">
            <div class="box-top">
                <el-form
                    style="overflow: hidden;"
                    :style="filterForm.show ? null : { height: '0px' }"
                    size="mini"
                >
                    <el-row>
                        <el-col :span="6">
                            <el-form-item label="监测因子" label-width="70px">
                                <el-input
                                    clearable
                                    v-model.trim="filterForm.data.comments"
                                    placeholder="监测因子"
                                    style="width: 100%;"
                                    @keyup.enter.native="getTableData()"
                                ></el-input>
                            </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 + 'PUT'"
                        @click="but_edit_plural()"
                        :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()"
                        >查询</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-form
                    size="mini"
                    ref="dialog_form_arr"
                    label-width="0px"
                    :model="tableFormDataObj"
                >
                    <el-table
                        stripe
                        id="singleTable"
                        ref="singleTable"
                        :data="tableData"
                        v-loading="tableLoading"
                        style="width: 100%;"
                        :height="singleTableHeight"
                        border
                        size="mini"
                        highlight-current-row
                        @selection-change="handleSelectionChange"
                        :span-method="arraySpanMethod"
                        :cell-style="setColor"
                    >
                        <el-table-column
                            type="selection"
                            width="40"
                            align="center"
                            fixed="left"
                        ></el-table-column>
                        <el-table-column
                            prop="TABLE_NAME"
                            label="能源"
                            width="120px"
                            align="center"
                            :show-overflow-tooltip="true">
                            <template slot-scope="scope">
                                <span>{{ nameObj.nyObj[scope.row.TABLE_NAME] }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column
                            prop="COMMENTS"
                            label="监测因子"
                            width="400px"
                            :show-overflow-tooltip="true"
                        ></el-table-column>
                        <el-table-column
                            prop="FLAG"
                            label="启用报警"
                            width="110px"
                            :show-overflow-tooltip="true"
                        >
                            <template slot-scope="scope">
                                <el-form-item
                                    v-if="scope.row.isSelection"
                                    :prop="scope.row.col + '.FLAG'"
                                    :rules="dialog.editorBox.form.rules.FLAG"
                                >
                                    <el-select
                                        filterable
                                        clearable
                                        style="width: 100%;"
                                        v-model="tableFormDataObj[scope.row.col].FLAG"
                                    >
                                        <el-option
                                            v-for="item of nameObj.flag.arr"
                                            :key="item.id"
                                            :value="item.id"
                                            :label="item.name"
                                        ></el-option>
                                    </el-select>
                                </el-form-item>
                                <span v-else>{{ nameObj.flag.obj[scope.row.FLAG] ? nameObj.flag.obj[scope.row.FLAG] : scope.row.FLAG }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column
                            prop="ST"
                            label="报警开始时间"
                            width="196px"
                            :show-overflow-tooltip="true"
                        > <template slot-scope="scope">
                                <el-form-item
                                    v-if="scope.row.isSelection"
                                    :prop="scope.row.col + '.ST'"
                                    :rules="dialog.editorBox.form.rules.ST"
                                >
                                    <el-date-picker
                                        v-model="tableFormDataObj[scope.row.col].ST"
                                        type="datetime"
                                        style="width: 100%;"
                                        format="yyyy-MM-dd HH:mm:ss"
                                        value-format="yyyy-MM-dd HH:mm:ss">
                                    </el-date-picker>
                                </el-form-item>
                                <span v-else>{{scope.row.ST}}</span>
                            </template>
                        </el-table-column>
                        <el-table-column label="标准">
                            <el-table-column prop="VALD" label="最小值" min-width="90px" :show-overflow-tooltip="true">
                                <template slot-scope="scope">
                                    <el-form-item
                                        v-if="scope.row.isSelection"
                                        :prop="scope.row.col + '.VALD'"
                                        :rules="dialog.editorBox.form.rules.VALD"
                                    >
                                        <el-input placeholder="请填写数值"
                                            clearable
                                            style="width: 100%;"
                                            v-model.trim="tableFormDataObj[scope.row.col].VALD"
                                        ></el-input>
                                    </el-form-item>
                                    <span v-else>{{scope.row.VALD}}</span>
                                </template>
                            </el-table-column>
                            <el-table-column prop="VALU" label="最大值" min-width="90px" :show-overflow-tooltip="true">
                                <template slot-scope="scope">
                                    <el-form-item
                                        v-if="scope.row.isSelection"
                                        :prop="scope.row.col + '.VALU'"
                                        :rules="dialog.editorBox.form.rules.VALU"
                                    >
                                        <el-input placeholder="请填写数值"
                                            clearable
                                            style="width: 100%;"
                                            v-model.trim="tableFormDataObj[scope.row.col].VALU"
                                        ></el-input>
                                    </el-form-item>
                                    <span v-else>{{scope.row.VALU}}</span>
                                </template>
                            </el-table-column>
                        </el-table-column>
                    </el-table>
                </el-form>
                <!-- <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 { formatDate } from '@/utils/util.js';
import { zCheckNumber1 } from '~/utils/validator.js'
export default {
    name: 'nyjkbz',
    data () {
        return {
            multipleSelection: [],
            tableFormDataObj: {},
            activeMenu: '',
            filterForm: {
                show: true,
                data: {
                    comments: ''
                }
            },
            pageNum: 1,
            pageSize: 20,
            total: 0,
            singleTableHeight: 100,
            tableData: [],
            nameObj: {
                nyObj: {
                    'COREDAS_AR': '氩气',
                    'COREDAS_BFG': '高炉煤气',
                    'COREDAS_CLD': '转炉煤气',
                    'COREDAS_COG': '焦炉煤气',
                    'COREDAS_N2': '氮气',
                    'COREDAS_O2': '氧气'
                },
                flag: {
                    obj: {
                        '0': '否',
                        '1': '是'
                    },
                    arr: [
                        {
                            id: '0',
                            name: '否'
                        },
                        {
                            id: '1',
                            name: '是'
                        }
                    ]
                }
            },
            loading: false,
            tableLoading: false,
            dialog: {
                editorBox: {
                    show: false,
                    form: {
                        rules: {
                            FLAG: [
                                { required: true, message: '该项不能为空', trigger: 'change' }
                            ],
                            VALD: [
                                { validator: zCheckNumber1, trigger: 'change' }
                            ],
                            VALU: [
                                { validator: zCheckNumber1, trigger: 'change' }
                            ]
                        }
                    }
                },
                equationEditing: {
                    show: false,
                    form: {
                        data: {
                            clock: '',
                            code: ''
                        }
                    }
                }
            }
        }
    },
    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.getTableData();
    },
    methods: {
        //  计算树区域高度
        getRoleHeight (dom) {
            return window.innerHeight - dom.offsetTop;
        },
        // 获取表格中的数据
        getTableData (pageNum) {
            let that = this,
                params = {
                    comments: that.filterForm.data.comments
                };
            that.pageNum = pageNum || that.pageNum;
            that.tableLoading = true;
            let url = 'pass/ems/v1/coredasabnormalinfos/getCoredasStd';
            that.axios.get(url, {
                params: params
            })
                .then(function (res) {
                    if (res.code === '0') {
                        that.tableData = res.data.data;
                        // that.total = res.data.total;
                    } 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);
        },
        // 表格中多选按钮的相关方法
        handleSelectionChange (val) {
            let that = this;
            let tableFormDataObj = {};
            for (let item of that.tableData) {
                item.isSelection = false;
            }
            for (let item of val) {
                let obj = {};
                let xId = item.col;
                if (that.tableFormDataObj[xId]) {
                    tableFormDataObj[xId] = that.tableFormDataObj[xId];
                } else {
                    for (let key in item) {
                        obj[key] = item[key];
                    }
                    tableFormDataObj[xId] = obj;
                }
                item.isSelection = true;
            }
            that.tableFormDataObj = tableFormDataObj;
            that.multipleSelection = val;
        },
        // 批量修改
        but_edit_plural () {
            let that = this;
            if (that.multipleSelection.length > 0) {
                that.$refs['dialog_form_arr'].validate((valid, obj) => {
                    if (valid) {
                        let SubmitData = [];
                        for (let key in that.tableFormDataObj) {
                            SubmitData.push({
                                col: that.tableFormDataObj[key].col,
                                TABLE_NAME: that.tableFormDataObj[key].TABLE_NAME,
                                ST: that.tableFormDataObj[key].ST,
                                FLAG: that.tableFormDataObj[key].FLAG,
                                VALD: that.tableFormDataObj[key].VALD,
                                VALU: that.tableFormDataObj[key].VALU
                            });
                        }
                        that.loading = true;
                        that.axios.put('pass/ems/v1/coredasabnormalinfos/', SubmitData).then(function (res) {
                            if (res.code === '0') {
                                that.$message({
                                    message: '修改成功',
                                    type: 'success'
                                });
                                that.getTableData();
                                that.dialog.editorBox.show = false;
                            } else {
                                that.$message.error(res.message);
                            }
                            that.loading = false;
                        }).catch(function () {
                            that.loading = false;
                        });
                    } else {
                        let col = Object.keys(obj)[0].split('.')[1];
                        if (col && col === 'VALD') {
                            this.$message.error('最小值请填写数字');
                        } else if (col && col === 'VALU') {
                            this.$message.error('最大值请填写数字');
                        } else if (col && col === 'FLAG') {
                            this.$message.error('是否启用报警不能为空');
                        } else {
                            this.$message.error('数据不完整或者格式错误');
                        }
                    }
                });
            } else {
                this.$message.error('至少要选择一条数据');
            }
        },
        // 表格合并单元格
        arraySpanMethod ({ row, column, rowIndex, columnIndex }) {
            let tableData = this.$refs.singleTable.tableData
            let rowsPan = 1;
            let thisColomnData = row[column['property']];
            // 能源列 合并
            if (column['property'] === 'TABLE_NAME' && thisColomnData) {
                if (rowIndex > 0 && thisColomnData === tableData[rowIndex - 1][column['property']]) {
                    return {
                        rowspan: 0,
                        colspan: 0
                    };
                }
                while (tableData[rowIndex + rowsPan] && thisColomnData === tableData[rowIndex + rowsPan][column['property']]) {
                    rowsPan += 1
                }
                return {
                    rowspan: rowsPan,
                    colspan: 1
                };
            }
        },
        setColor ({row, column, rowIndex, columnIndex}) {
            if (column['property'] === 'TABLE_NAME' || columnIndex === 0) {
                return 'background-color:#fff'
            }
        }
    }
}
</script>

<style lang="less">
.nyjkbz {
    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;
            }
        }
        .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: #7a7a7a;
    }
    .color_yes {
        color: #1a9f17;
    }
}
</style>
