<template>
    <!-- 指标维护 -->
    <div class="indexMaintain">
        <div class="box">
            <div class="box-left" :style="listTopicTree.show ? null : { width: '22px' }">
                <div class="box-left-bt" style="overflow: hidden;">
                    <span v-show="listTopicTree.show">指标</span>
                    <div style="float: right;height: 100%;padding-right: 4px;">
                        <span
                            class="el-icon-d-arrow-right"
                            style="cursor: pointer; transition: transform 0.3s ease-out, -webkit-transform 0.3s ease-out;"
                            :style="listTopicTree.show ? { transform: 'rotate(180deg)'} : { transform: 'rotate(0deg)'} "
                            @click="listTopicTree.show = !listTopicTree.show;"
                        ></span>
                    </div>
                </div>
                <div class="box-left-tree">
                    <el-tree
                        :style="listTopicTree.show ? null : { 'margin-left': '22px' }"
                        :data="listTopicTree.data"
                        :props="listTopicTree.defaultProps"
                        :load="loadNode"
                        lazy
                        v-loading="listTopicTree.loading"
                    >
                        <span
                            class="custom-tree-node"
                            :class="{'tree-row-on': filterForm.node.id === data.id}"
                            style="width:100%; font-size:12px;"
                            slot-scope="{ node, data }"
                        >
                            <p
                                style="overflow:hidden; text-overflow:ellipsis; white-space:nowrap;"
                                :title="node.label"
                                @click="selectByTopicId(data, node)"
                            >{{ node.label }}</p>
                        </span>
                    </el-tree>
                </div>
            </div>
            <div class="box-right" :style="listTopicTree.show ? null : { 'margin-left': '22px' }">
                <div class="box-top" style="overflow: hidden;">
                    <el-form
                        style="border-bottom: 1px #ccc solid;overflow: hidden;"
                        :style="filterForm.show ? null : { height: '0px' }"
                        class="box-top-form"
                        size="mini"
                        label-width="85px"
                    >
                        <el-row>
                            <el-col :span="8">
                                <el-form-item label="当前节点代码">
                                    <el-input clearable
                                        v-model="filterForm.node.id"
                                        placeholder=""
                                        style="width: 100%;"
                                        readonly
                                    ></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item label="当前节点名称">
                                    <el-input clearable
                                        v-model.trim="filterForm.node.name"
                                        placeholder=""
                                        style="width: 100%;"
                                        readonly
                                    ></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item label="当前节点描述">
                                    <el-input clearable
                                        v-model.trim="filterForm.node.desc"
                                        placeholder=""
                                        style="width: 100%;"
                                        readonly
                                    ></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div
                        v-show="table.type === 1"
                    >
                        <el-form
                            style="border-bottom: 1px #ccc solid;overflow: hidden;padding-top: 7px;"
                            :style="filterForm.show ? null : { height: '0px', 'padding-top': '0px' }"
                            class="box-top-form"
                            size="mini"
                            label-width="85px"
                        >
                            <el-row>
                                <el-col :span="8">
                                    <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="8">
                                    <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-col :span="8">
                                    <el-form-item label="用途">
                                        <el-select
                                            filterable clearable
                                            v-model="filterForm.data.itemtype"
                                            placeholder=""
                                            multiple
                                            collapse-tags
                                            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-row>
                            <!-- <el-row>
                                <el-col :span="8">
                                    <el-form-item label="指标类型1">
                                        <el-select
                                            filterable clearable
                                            v-model="filterForm.data.type1"
                                            placeholder=""
                                            style="width: 100%;"
                                        >
                                            <el-option
                                                v-for="item of nameObj.type1.arr"
                                                :key="item.id"
                                                :value="item.id"
                                                :label="item.name"
                                            ></el-option>
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="8">
                                    <el-form-item label="指标类型2">
                                        <el-select
                                            filterable clearable
                                            v-model="filterForm.data.type2"
                                            placeholder=""
                                            style="width: 100%;"
                                        >
                                            <el-option
                                                v-for="item of nameObj.type2.arr"
                                                :key="item.id"
                                                :value="item.id"
                                                :label="item.name"
                                            ></el-option>
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="8">
                                    <el-form-item label="指标类型3">
                                        <el-select
                                            filterable clearable
                                            v-model="filterForm.data.type3"
                                            placeholder=""
                                            style="width: 100%;"
                                        >
                                            <el-option
                                                v-for="item of nameObj.type3.arr"
                                                :key="item.id"
                                                :value="item.id"
                                                :label="item.name"
                                            ></el-option>
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                            </el-row> -->
                            <el-row>
                                <!-- <el-col :span="8">
                                    <el-form-item label="工厂">
                                        <el-select
                                            filterable clearable
                                            v-model="filterForm.data.factoryid"
                                            placeholder=""
                                            style="width: 100%;"
                                        >
                                            <el-option
                                                v-for="item of nameObj.factoryid.arr"
                                                :key="item.id"
                                                :value="item.id"
                                                :label="item.name"
                                            ></el-option>
                                        </el-select>
                                    </el-form-item>
                                </el-col> -->
                                <!-- <el-col :span="8">
                                    <el-form-item label="指标类型4">
                                        <el-select
                                            filterable clearable
                                            v-model="filterForm.data.type4"
                                            placeholder=""
                                            style="width: 100%;"
                                        >
                                            <el-option
                                                v-for="item of nameObj.type4.arr"
                                                :key="item.id"
                                                :value="item.id"
                                                :label="item.name"
                                            ></el-option>
                                        </el-select>
                                    </el-form-item>
                                </el-col> -->
                                <el-col :span="8">
                                    <el-form-item label="能介">
                                        <el-select
                                            filterable clearable
                                            v-model="filterForm.data.energyid"
                                            placeholder=""
                                            style="width: 100%;"
                                        >
                                            <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="8">
                                    <el-form-item label="工序">
                                        <el-select
                                            filterable clearable
                                            v-model="filterForm.data.workprocid"
                                            placeholder=""
                                            multiple
                                            collapse-tags
                                            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-col :span="8">
                                    <el-form-item label="公式版本">
                                        <el-date-picker
                                            type="month"
                                            v-model="filterForm.data.version"
                                            placeholder="公式版本"
                                            style="width: 100%;"
                                            format="yyyyMM"
                                            value-format="yyyyMM00"
                                        ></el-date-picker>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                        </el-form>
                    </div>
                </div>
                <div class="box-top-gjl">
                    <el-button
                        class="button"
                        type="primary"
                        size="mini"
                        icon="el-icon-plus"
                        v-privilege="activeMenu + 'ADD'"
                        @click="but_add"
                    >新增</el-button>
                    <el-button v-show="table.type === 1"
                        class="button"
                        type="primary"
                        size="mini"
                        icon="el-icon-edit-outline"
                        v-privilege="activeMenu + 'PUT'"
                        @click="but_edit_plural()"
                        :loading="loading"
                    >修改</el-button>
                    <el-button v-show="table.type === 1"
                        class="button"
                        type="danger"
                        size="mini"
                        icon="el-icon-delete"
                        v-privilege="activeMenu + 'DELETE'"
                        @click="but_del_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="filterForm.useflag='';getTableData(1);"
                        >查询全部</el-button>
                        <el-button
                            class="button"
                            type="primary"
                            size="mini"
                            icon="el-icon-search"
                            v-privilege="activeMenu + 'QUERY'"
                            @click="filterForm.useflag='1';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['box_bottom']) - 60;
                                }
                            );"
                        >{{ filterForm.show ? '收起' : '展开' }}</el-button>
                    </div>
                </div>
                <div
                    ref="box_bottom"
                    class="box-bottom"
                >
                    <div v-show="table.type === 0">
                        <el-table
                            stripe
                            id="singleTable1"
                            :data="table.table1.data"
                            v-loading="tableLoading"
                            style="width: 100%;"
                            :height="singleTableHeight + 38"
                            border
                            size="mini"
                            highlight-current-row>
                            <el-table-column
                                sortable
                                prop="itemid"
                                label="项目编号"
                                min-width="100px"
                                :show-overflow-tooltip="true"
                            ></el-table-column>
                            <el-table-column
                                sortable
                                prop="indextype"
                                label="指标类型"
                                min-width="100px"
                                :show-overflow-tooltip="true"
                            >
                                <template slot-scope="scope">
                                    <span>{{ nameObj.indextype.obj[scope.row.indextype] ? nameObj.indextype.obj[scope.row.indextype] : scope.row.indextype }}</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="itemdesc"
                                label="项目描述"
                                min-width="100px"
                                :show-overflow-tooltip="true"
                            ></el-table-column>
                            <el-table-column
                                class-name="gn-TableDownloadExcel-none"
                                label="操作"
                                align="center"
                                width="140px"
                                fixed="right"
                            >
                                <template slot-scope="scope">
                                    <div>
                                        <el-button
                                            type="primary"
                                            size="mini"
                                            v-privilege="activeMenu + 'PUT'"
                                            @click="but_edit(scope.row)"
                                        >修改</el-button>
                                        <el-button
                                            type="danger"
                                            size="mini"
                                            v-privilege="activeMenu + 'DELETE'"
                                            @click="but_del(scope.row)"
                                        >删除</el-button>
                                    </div>
                                </template>
                            </el-table-column>
                        </el-table>
                    </div>
                    <div v-show="table.type === 1">
                        <el-form
                            size="mini"
                            ref="dialog_form_arr"
                            label-width="0px"
                            :model="tableFormDataObj"
                        >
                            <el-table
                                stripe
                                id="singleTable2"
                                :data="table.table2.data"
                                v-loading="tableLoading"
                                style="width: 100%;"
                                :height="singleTableHeight"
                                border
                                size="mini"
                                @current-change="handleCurrentChange"
                                @selection-change="handleSelectionChange"
                                highlight-current-row
                                :row-class-name="tableRowClassName"
                                @cell-click="cellClick">
                                <el-table-column
                                    type="selection"
                                    width="40"
                                    align="center"
                                    fixed="left"
                                ></el-table-column>
                                <el-table-column
                                    sortable
                                    prop="itemid"
                                    label="项目编号"
                                    width="163px"
                                    :show-overflow-tooltip="true"
                                ></el-table-column>
                                <el-table-column
                                    sortable
                                    prop="indextype"
                                    label="指标类型"
                                    width="70px"
                                    :show-overflow-tooltip="true"
                                >
                                    <template slot-scope="scope">
                                        <span>{{ nameObj.indextype.obj[scope.row.indextype] ? nameObj.indextype.obj[scope.row.indextype] : scope.row.indextype }}</span>
                                    </template>
                                </el-table-column>
                                <el-table-column
                                    sortable
                                    prop="itemtype"
                                    label="用途"
                                    width="50px"
                                    :show-overflow-tooltip="true"
                                >
                                    <template slot-scope="scope">
                                        <span>{{ nameObj.itemtype.obj[scope.row.itemtype] ? nameObj.itemtype.obj[scope.row.itemtype] : scope.row.itemtype }}</span>
                                    </template>
                                </el-table-column>
                                <el-table-column
                                    sortable
                                    prop="energyid"
                                    label="能介"
                                    width="75px"
                                    :show-overflow-tooltip="true"
                                >
                                    <template slot-scope="scope">
                                        <span>{{ nameObj.energyid.obj[scope.row.energyid] ? nameObj.energyid.obj[scope.row.energyid] : scope.row.energyid }}</span>
                                    </template>
                                    <!-- <template slot-scope="scope">
                                        <el-form-item
                                            v-if="scope.row.isSelection"
                                            :prop="scope.row.itemid + '.energyid'"
                                            :rules="dialog.editorBox.form2.rules.energyid"
                                        >
                                            <el-select
                                                filterable
                                                clearable
                                                style="width: 100%;"
                                                v-model="tableFormDataObj[scope.row.itemid].energyid"
                                                @change="getName(scope.row.itemid, scope.$index, scope.row)"
                                            >
                                                <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>
                                        <span v-else>{{ nameObj.energyid.obj[scope.row.energyid] ? nameObj.energyid.obj[scope.row.energyid] : scope.row.energyid }}</span>
                                    </template> -->
                                </el-table-column>
                                <el-table-column
                                    sortable
                                    prop="itemname"
                                    label="项目名称"
                                    width="296px"
                                    :show-overflow-tooltip="true"
                                >
                                    <template slot-scope="scope">
                                        <el-form-item
                                            v-if="scope.row.isSelection"
                                            :prop="scope.row.itemid + '.itemname'"
                                            :rules="dialog.editorBox.form2.rules.itemname"
                                        >
                                            <el-input @keydown.native="keyDown" :id="scope.$index + scope.column.id"
                                                clearable
                                                style="width: 100%;"
                                                v-model.trim="tableFormDataObj[scope.row.itemid].itemname"
                                            ></el-input>
                                        </el-form-item>
                                        <span v-else>{{scope.row.itemname}}</span>
                                    </template>
                                </el-table-column>
                                <el-table-column
                                    sortable
                                    prop="workprocid"
                                    label="工序"
                                    width="115px"
                                    :show-overflow-tooltip="true"
                                >
                                    <template slot-scope="scope">
                                        <el-form-item
                                            v-if="scope.row.isSelection"
                                            :prop="scope.row.itemid + '.workprocid'"
                                            :rules="dialog.editorBox.form2.rules.workprocid"
                                        >
                                            <el-select
                                                filterable
                                                clearable
                                                style="width: 100%;"
                                                v-model="tableFormDataObj[scope.row.itemid].workprocid"
                                                @change="getName(scope.row.itemid, scope.$index, scope.row)"
                                            >
                                                <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>
                                        <span v-else>{{ nameObj.workprocid.obj[scope.row.workprocid] ? nameObj.workprocid.obj[scope.row.workprocid] : scope.row.workprocid }}</span>
                                    </template>
                                </el-table-column>
                                <el-table-column
                                    sortable
                                    prop="factoryid"
                                    label="工厂"
                                    width="96px"
                                    :show-overflow-tooltip="true"
                                >
                                    <template slot-scope="scope">
                                        <el-form-item
                                            v-if="scope.row.isSelection"
                                            :prop="scope.row.itemid + '.factoryid'"
                                            :rules="dialog.editorBox.form2.rules.factoryid"
                                        >
                                            <el-select
                                                filterable
                                                clearable
                                                style="width: 100%;"
                                                v-model="tableFormDataObj[scope.row.itemid].factoryid"
                                                @change="getName(scope.row.itemid, scope.$index, scope.row)"
                                            >
                                                <el-option
                                                    v-for="item of nameObj.factoryid.arr"
                                                    :key="item.id"
                                                    :value="item.id"
                                                    :label="item.name"
                                                ></el-option>
                                            </el-select>
                                        </el-form-item>
                                        <span v-else>{{ nameObj.factoryid.obj[scope.row.factoryid] ? nameObj.factoryid.obj[scope.row.factoryid] : scope.row.factoryid }}</span>
                                    </template>
                                </el-table-column>
                                <el-table-column
                                    sortable
                                    prop="unitid"
                                    label="单位"
                                    width="124px"
                                    :show-overflow-tooltip="true"
                                >
                                    <template slot-scope="scope">
                                        <el-form-item
                                            v-if="scope.row.isSelection"
                                            :prop="scope.row.itemid + '.unitid'"
                                            :rules="dialog.editorBox.form2.rules.unitid"
                                        >
                                            <el-select
                                                filterable
                                                clearable
                                                style="width: 100%;"
                                                v-model="tableFormDataObj[scope.row.itemid].unitid"
                                            >
                                                <el-option
                                                    v-for="item of nameObj.unitid.arr"
                                                    :key="item.id"
                                                    :value="item.id"
                                                    :label="item.name"
                                                ></el-option>
                                            </el-select>
                                        </el-form-item>
                                        <span v-else>{{ nameObj.unitid.obj[scope.row.unitid] ? nameObj.unitid.obj[scope.row.unitid] : scope.row.unitid }}</span>
                                    </template>
                                </el-table-column>
                                <el-table-column
                                    sortable
                                    prop="mintimegranid"
                                    label="时间粒度"
                                    width="75px"
                                    :show-overflow-tooltip="true"
                                >
                                    <template slot-scope="scope">
                                        <el-form-item
                                            v-if="scope.row.isSelection"
                                            :prop="scope.row.itemid + '.mintimegranid'"
                                            :rules="dialog.editorBox.form2.rules.mintimegranid"
                                        >
                                            <el-select
                                                filterable
                                                clearable
                                                style="width: 100%;"
                                                v-model="tableFormDataObj[scope.row.itemid].mintimegranid"
                                            >
                                                <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>
                                        <span v-else>{{ nameObj.timegranid.obj[scope.row.mintimegranid] ? nameObj.timegranid.obj[scope.row.mintimegranid] : scope.row.mintimegranid }}</span>
                                    </template>
                                </el-table-column>
                                <el-table-column
                                    sortable
                                    prop="digits"
                                    label="精度"
                                    width="64px"
                                    :show-overflow-tooltip="true"
                                >
                                    <template slot-scope="scope">
                                        <el-form-item
                                            v-if="scope.row.isSelection"
                                            :prop="scope.row.itemid + '.digits'"
                                            :rules="dialog.editorBox.form2.rules.digits"
                                        >
                                            <el-input @keydown.native="keyDown" :id="scope.$index + scope.column.id"
                                                clearable style="width: 100%;"
                                                v-model.trim="tableFormDataObj[scope.row.itemid].digits"
                                            ></el-input>
                                        </el-form-item>
                                        <span v-else>{{scope.row.digits}}</span>
                                    </template>
                                </el-table-column>
                                <el-table-column
                                    sortable
                                    prop="warnvalue"
                                    label="超标警告(%)"
                                    width="90px"
                                    :show-overflow-tooltip="true"
                                >
                                    <template slot-scope="scope">
                                        <el-form-item
                                            v-if="scope.row.isSelection"
                                            :prop="scope.row.itemid + '.warnvalue'"
                                            :rules="dialog.editorBox.form2.rules.warnvalue"
                                        >
                                            <el-input @keydown.native="keyDown" :id="scope.$index + scope.column.id"
                                                clearable style="width: 100%;"
                                                v-model.trim="tableFormDataObj[scope.row.itemid].warnvalue"
                                            ></el-input>
                                        </el-form-item>
                                        <span v-else>{{scope.row.warnvalue}}</span>
                                    </template>
                                </el-table-column>
                                <el-table-column
                                    sortable
                                    prop="useflag"
                                    label="启用"
                                    width="56px"
                                    :show-overflow-tooltip="true"
                                >
                                    <template slot-scope="scope">
                                        <span :class="{color_yes:  scope.row.useflag === '1', color_no:  scope.row.useflag === '0'}">
                                            {{ scope.row.useflag === '0' ? '否' : scope.row.useflag === '1' ? '是' : '' }}
                                        </span>
                                    </template>
                                </el-table-column>
                                <el-table-column
                                    sortable
                                    prop="itemdesc"
                                    label="项目描述"
                                    min-width="75px"
                                    :show-overflow-tooltip="true"
                                >
                                    <template slot-scope="scope">
                                        <el-form-item
                                            v-if="scope.row.isSelection"
                                            :prop="scope.row.itemid + '.itemdesc'"
                                            :rules="dialog.editorBox.form2.rules.itemdesc"
                                        >
                                            <el-input @keydown.native="keyDown" :id="scope.$index + scope.column.id"
                                                clearable style="width: 100%;"
                                                v-model.trim="tableFormDataObj[scope.row.itemid].itemdesc"
                                            ></el-input>
                                        </el-form-item>
                                        <span v-else>{{scope.row.itemdesc}}</span>
                                    </template>
                                </el-table-column>
                                <el-table-column
                                    sortable
                                    prop="bz"
                                    label="备注"
                                    min-width="75px"
                                    :show-overflow-tooltip="true"
                                >
                                    <template slot-scope="scope">
                                        <el-form-item
                                            v-if="scope.row.isSelection"
                                            :prop="scope.row.itemid + '.bz'"
                                            :rules="dialog.editorBox.form2.rules.bz"
                                        >
                                            <el-input @keydown.native="keyDown" :id="scope.$index + scope.column.id"
                                                clearable style="width: 100%;"
                                                v-model.trim="tableFormDataObj[scope.row.itemid].bz"
                                            ></el-input>
                                        </el-form-item>
                                        <span v-else>{{scope.row.bz}}</span>
                                    </template>
                                </el-table-column>
                                <!-- <el-table-column
                                    sortable
                                    prop="type1"
                                    label="指标类型1"
                                    min-width="100px"
                                    :show-overflow-tooltip="true"
                                >
                                    <template slot-scope="scope">
                                        <span>{{ nameObj.type1.obj[scope.row.type1] ? nameObj.type1.obj[scope.row.type1] : scope.row.type1 }}</span>
                                    </template>
                                </el-table-column>
                                <el-table-column
                                    sortable
                                    prop="type2"
                                    label="指标类型2"
                                    min-width="100px"
                                    :show-overflow-tooltip="true"
                                >
                                    <template slot-scope="scope">
                                        <span>{{ nameObj.type2.obj[scope.row.type2] ? nameObj.type2.obj[scope.row.type2] : scope.row.type2 }}</span>
                                    </template>
                                </el-table-column>
                                <el-table-column
                                    sortable
                                    prop="type3"
                                    label="指标类型3"
                                    min-width="100px"
                                    :show-overflow-tooltip="true"
                                >
                                    <template slot-scope="scope">
                                        <span>{{ nameObj.type3.obj[scope.row.type3] ? nameObj.type3.obj[scope.row.type3] : scope.row.type3 }}</span>
                                    </template>
                                </el-table-column>
                                <el-table-column
                                    sortable
                                    prop="type4"
                                    label="指标类型4"
                                    min-width="100px"
                                    :show-overflow-tooltip="true"
                                >
                                    <template slot-scope="scope">
                                        <span>{{ nameObj.type4.obj[scope.row.type4] ? nameObj.type4.obj[scope.row.type4] : scope.row.type4 }}</span>
                                    </template>
                                </el-table-column> -->
                                <el-table-column
                                    class-name="gn-TableDownloadExcel-none"
                                    label="操作"
                                    align="center"
                                    width="132px"
                                    fixed="right"
                                >
                                    <template slot-scope="scope">
                                        <div>
                                            <el-button
                                                type="primary"
                                                size="mini"
                                                @click="but_equationEditing(scope.row)"
                                            >公式</el-button>
                                            <el-button
                                                v-if="scope.row.useflag === '1'"
                                                type="danger"
                                                size="mini"
                                                @click="upUseflag(scope.row, '0')"
                                            >禁用</el-button>
                                            <el-button
                                                v-if="scope.row.useflag !== '1'"
                                                type="primary"
                                                size="mini"
                                                @click="upUseflag(scope.row, '1')"
                                            >启用</el-button>
                                        </div>
                                    </template>
                                </el-table-column>
                            </el-table>
                        </el-form>
                        <el-pagination
                            layout="total, sizes, prev, pager, next, jumper"
                            :total="table.table2.total"
                            :page-sizes="[10, 20, 50, 100, 500, 1000]"
                            :page-size="table.table2.pageSize"
                            :current-page.sync="table.table2.pageNum"
                            @size-change="tableSizeChange"
                            @current-change="getTableData()"
                            style="text-align: right;margin-top: 10px;"
                        ></el-pagination>
                    </div>
                </div>
            </div>
        </div>
        <el-dialog
            :title="'数据 - ' + (dialog.editorBox.type === 'add' ? '新增' : '修改')"
            :visible.sync="dialog.editorBox.show"
            width="600px"
            :close-on-click-modal="false"
            :show-close="!loading"
        >
            <div>
                <el-form
                    v-if="table.type === 0"
                    ref="dialog_form1"
                    :model="dialog.editorBox.form1.data"
                    :rules="dialog.editorBox.form1.rules"
                    size="mini"
                    label-width="90px"
                >
                    <el-row>
                        <!-- <el-col :span="12">
                            <el-form-item label="项目编号" prop="itemid">
                                <el-input clearable
                                    style="width: 100%;"
                                    v-model="dialog.editorBox.form1.data.itemid"
                                ></el-input>
                            </el-form-item>
                        </el-col> -->
                        <el-col :span="12">
                            <el-form-item label="项目名称" prop="itemname">
                                <el-input clearable
                                    style="width: 100%;"
                                    v-model="dialog.editorBox.form1.data.itemname"
                                ></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="指标类型" prop="indextype">
                                <el-select
                                    filterable clearable
                                    style="width: 100%;"
                                    v-model="dialog.editorBox.form1.data.indextype"
                                    disabled
                                >
                                    <el-option
                                        v-for="item of nameObj.indextype.arr"
                                        :key="item.id"
                                        :value="item.id"
                                        :label="item.name"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="项目描述" prop="itemdesc">
                                <el-input clearable
                                    style="width: 100%;"
                                    v-model="dialog.editorBox.form1.data.itemdesc"
                                >
                                </el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
                <el-form
                    v-if="table.type === 1"
                    ref="dialog_form2"
                    :model="dialog.editorBox.form2.data"
                    :rules="dialog.editorBox.form2.rules"
                    size="mini"
                    label-width="90px"
                >
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="用途" prop="itemtype">
                                <el-select
                                    filterable clearable
                                    style="width: 100%;"
                                    v-model="dialog.editorBox.form2.data.itemtype"
                                    :disabled="dialog.editorBox.type !== 'add'"
                                >
                                    <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="12">
                            <el-form-item label="指标类型" prop="indextype">
                                <el-select
                                    filterable clearable
                                    style="width: 100%;"
                                    v-model="dialog.editorBox.form2.data.indextype"
                                    disabled
                                >
                                    <el-option
                                        v-for="item of nameObj.indextype.arr"
                                        :key="item.id"
                                        :value="item.id"
                                        :label="item.name"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="能介" prop="energyid">
                                <el-select
                                    filterable clearable
                                    style="width: 100%;"
                                    v-model="dialog.editorBox.form2.data.energyid"
                                    :disabled="dialog.editorBox.type !== 'add'"
                                >
                                    <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="12">
                            <el-form-item label="工序" prop="workprocid">
                                <el-select
                                    filterable clearable
                                    style="width: 100%;"
                                    v-model="dialog.editorBox.form2.data.workprocid"
                                    :disabled="dialog.editorBox.type !== 'add'"
                                >
                                    <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-row>
                        <el-col :span="12">
                            <el-form-item label="工厂" prop="factoryid">
                                <el-select
                                    filterable clearable
                                    style="width: 100%;"
                                    v-model="dialog.editorBox.form2.data.factoryid"
                                    :disabled="dialog.editorBox.type !== 'add'"
                                >
                                    <el-option
                                        v-for="item of nameObj.factoryid.arr"
                                        :key="item.id"
                                        :value="item.id"
                                        :label="item.name"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="时间粒度" prop="mintimegranid">
                                <el-select
                                    filterable clearable
                                    style="width: 100%;"
                                    v-model="dialog.editorBox.form2.data.mintimegranid"
                                >
                                    <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-row>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="数值精度" prop="digits">
                                <el-input clearable
                                    style="width: 100%;"
                                    v-model.trim="dialog.editorBox.form2.data.digits"
                                >
                                </el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="单位" prop="unitid">
                                <el-select
                                    filterable clearable
                                    style="width: 100%;"
                                    v-model="dialog.editorBox.form2.data.unitid"
                                >
                                    <el-option
                                        v-for="item of nameObj.unitid.arr"
                                        :key="item.id"
                                        :value="item.id"
                                        :label="item.name"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="项目描述" prop="itemdesc">
                                <el-input clearable
                                    style="width: 100%;"
                                    v-model.trim="dialog.editorBox.form2.data.itemdesc"
                                >
                                </el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="超标警告(%)" prop="warnvalue">
                                <el-input clearable maxlength="3"
                                    style="width: 100%;"
                                    v-model.trim="dialog.editorBox.form2.data.warnvalue"
                                >
                                </el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="备注" prop="bz">
                                <el-input clearable
                                    style="width: 100%;"
                                    v-model.trim="dialog.editorBox.form2.data.bz"
                                >
                                </el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item
                                label="属性"
                                prop="propertyid"
                            >
                                <el-select filterable clearable
                                    style="width: 100%;"
                                    v-model="dialog.editorBox.form2.data.propertyid"
                                >
                                    <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-row>
                        <el-col :span="12">
                            <el-form-item label="指标类型1" prop="type1">
                                <el-select
                                    filterable clearable
                                    style="width: 100%;"
                                    v-model="dialog.editorBox.form2.data.type1"
                                >
                                    <el-option
                                        v-for="item of nameObj.type1.arr"
                                        :key="item.id"
                                        :value="item.id"
                                        :label="item.name"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="指标类型2" prop="type2">
                                <el-select
                                    filterable clearable
                                    style="width: 100%;"
                                    v-model="dialog.editorBox.form2.data.type2"
                                >
                                    <el-option
                                        v-for="item of nameObj.type2.arr"
                                        :key="item.id"
                                        :value="item.id"
                                        :label="item.name"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="指标类型3" prop="type3">
                                <el-select
                                    filterable clearable
                                    style="width: 100%;"
                                    v-model="dialog.editorBox.form2.data.type3"
                                >
                                    <el-option
                                        v-for="item of nameObj.type3.arr"
                                        :key="item.id"
                                        :value="item.id"
                                        :label="item.name"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="指标类型4" prop="type4">
                                <el-select
                                    filterable clearable
                                    style="width: 100%;"
                                    v-model="dialog.editorBox.form2.data.type4"
                                >
                                    <el-option
                                        v-for="item of nameObj.type4.arr"
                                        :key="item.id"
                                        :value="item.id"
                                        :label="item.name"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row> -->
                </el-form>
            </div>
            <span
                slot="footer"
                class="dialog-footer"
            >
                <el-button
                    @click="dialog.editorBox.show = false"
                    :loading="loading"
                >取 消</el-button>
                <el-button
                    type="primary"
                    @click="dataSave"
                    :loading="loading"
                >保 存</el-button>
            </span>
        </el-dialog>
        <zj-formulaEditor :equationEditing="dialog.equationEditing"></zj-formulaEditor>
    </div>
</template>

<script>
import formulaEditor from '@/components/zg/formulaEditor.vue'
import { zCheckNumber2, chekPercent } from '@/utils/validator.js'
import { formatDate } from '@/utils/util.js';
export default {
    name: 'indexMaintain',
    components: {
        'zj-formulaEditor': formulaEditor
    },
    data () {
        return {
            rowIndex: '',
            columnId: '',
            focusTarget: null,
            seqArr: [],
            editSetFlag: '',
            multipleSelection: [],
            tableFormDataObj: {},
            activeMenu: '',
            filterForm: {
                useflag: '1',
                show: true,
                node: {
                    id: '',
                    name: '',
                    desc: '',
                    level: '',
                    indextype: '',
                    nodeObj: null
                },
                data: {
                    itemid: '',
                    itemname: '',
                    itemtype: [],
                    type1: '',
                    type2: '',
                    type3: '',
                    type4: '',
                    energyid: '',
                    workprocid: [],
                    factoryid: '',
                    version: formatDate(new Date(), 'yyyyMM00')
                },
                rules: {
                }
            },
            listTopicTree: {
                show: true,
                loading: false,
                data: [],
                defaultProps: {
                    children: 'children',
                    label: 'name'
                }
            },
            table: {
                type: 0, // 0:节点,1:项目
                table1: {
                    data: [
                    ],
                    tableLoading: false
                },
                table2: {
                    pageNum: 1,
                    pageSize: 20,
                    total: 0,
                    data: [
                    ],
                    tableLoading: false
                }
            },
            nameObj: {
                itemtype: {
                    obj: {},
                    arr: []
                },
                propertyid: {
                    obj: {},
                    arr: []
                },
                type1: {
                    obj: {},
                    arr: []
                },
                type2: {
                    obj: {},
                    arr: []
                },
                type3: {
                    obj: {},
                    arr: []
                },
                type4: {
                    obj: {},
                    arr: []
                },
                energyid: {
                    obj: {},
                    arr: []
                },
                workprocid: {
                    obj: {},
                    arr: []
                },
                factoryid: {
                    obj: {},
                    arr: []
                },
                indextype: {
                    obj: {},
                    arr: []
                },
                unitid: {
                    obj: {},
                    arr: []
                },
                timegranid: {
                    obj: {
                        'DAY': '日',
                        'MONTH': '月'
                    },
                    arr: [{id: 'DAY', name: '日'}, {id: 'MONTH', name: '月'}]
                }
            },
            singleTableHeight: 100,
            loading: false,
            tableLoading: false,
            dialog: {
                editorBox: {
                    show: false,
                    type: '',
                    addRow: null,
                    form1: {
                        data: {
                            itemid: '',
                            itemname: '',
                            indextype: '',
                            seqno: '',
                            itemdesc: ''
                        },
                        rules: {
                            itemname: [
                                { required: true, message: '该项不能为空', trigger: 'change' }
                            ],
                            indextype: [
                                { required: true, message: '该项不能为空', trigger: 'change' }
                            ],
                            seqno: [
                                { validator: zCheckNumber2, trigger: 'change' }
                            ]
                        }
                    },
                    form2: {
                        data: {
                            itemid: '',
                            itemname: '',
                            itemtype: '',
                            indextype: '',
                            energyid: '',
                            workprocid: '',
                            factoryid: '',
                            unitid: '',
                            digits: '',
                            seqno: '',
                            itemdesc: '',
                            mintimegranid: 'MONTH',
                            warnvalue: '',
                            propertyid: '',
                            type1: '',
                            type2: '',
                            type3: '',
                            type4: ''
                        },
                        rules: {
                            itemname: [
                                { required: true, message: '该项不能为空', trigger: 'change' }
                            ],
                            itemtype: [
                                { required: true, message: '该项不能为空', trigger: 'change' }
                            ],
                            indextype: [
                                { required: true, message: '该项不能为空', trigger: 'change' }
                            ],
                            factoryid: [
                                { required: true, message: '该项不能为空', trigger: 'change' }
                            ],
                            energyid: [
                                { required: true, message: '该项不能为空', trigger: 'change' }
                            ],
                            workprocid: [
                                { required: true, message: '该项不能为空', trigger: 'change' }
                            ],
                            mintimegranid: [
                                { required: true, message: '该项不能为空', trigger: 'change' }
                            ],
                            digits: [
                                { validator: zCheckNumber2, trigger: 'change' }
                            ],
                            warnvalue: [
                                { validator: chekPercent, trigger: 'change' }
                            ],
                            unitid: [],
                            itemdesc: [],
                            bz: [],
                            useflag: [
                                { required: true, message: '该项不能为空', trigger: 'change' }
                            ]
                        }
                    }
                },
                equationEditing: {
                    show: false,
                    form: {
                        data: {
                            clock: '',
                            code: ''
                        }
                    }
                }
            }
        }
    },
    watch: {
        'table.type': function (newVal, oldVal) {
            let that = this;
            window.PEDataObj.tableArr[0].id = newVal === 0 ? 'singleTable1' : 'singleTable2';
        }
    },
    created () {
        this.activeMenu = window.localStorage.getItem('activeMenu');
    },
    mounted () {
        let that = this;
        window.PEDataObj = {
            // 将数据绑定到window上，供main页面使用
            vm: that,
            // tableArr：用于导出成Excel的表格的信息
            tableArr: [
                {
                    name: '',
                    id: 'singleTable1'
                }
            ]
        };
        that.$nextTick(() => {
            // 立即获取的height有一定偏差，通过setTimeout延迟来解决
            setTimeout(() => {
                that.singleTableHeight = that.getRoleHeight(that.$refs['box_bottom']) - 60;
            }, 1);
        });
        that.getEnergyIdData();
        that.getNodeData();
        that.getFactoryIdData();
        that.getUnitIdData();
        that.getType();
        that.getPropertyIdData();
        that.getIndexItemTypeData();
    },
    methods: {
        // 获取指标用途类型
        getIndexItemTypeData () {
            let that = this
            let url = 'pass/ems/v1/trmindexitemtypes/?pageSize=500&pageNum=1';
            that.axios.get(url)
                .then(function (res) {
                    if (res.code === '0') {
                        let arr = [];
                        let obj = {};
                        for (let item of res.data.list) {
                            arr.push({
                                id: item.id,
                                name: item.name
                            })
                            obj[item.id] = item.name;
                        }
                        that.nameObj.itemtype.arr = arr;
                        that.nameObj.itemtype.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);
                    }
                });
        },
        //  计算树区域高度
        getRoleHeight (dom) {
            return window.innerHeight - dom.offsetTop;
        },
        // 用于表格特殊列的过滤方法（:sort-by="function(row, index) { return sortFoo() }"）
        sortFoo (row, index, objName, name) {
            name = name || objName;
            let s = this.nameObj[objName].obj[row[name]] ? this.nameObj[objName].obj[row[name]] : row[name];
            return s;
        },
        // 获取能介
        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
                            })
                            obj[item.id] = item.name;
                        }
                        that.nameObj.energyid.arr = arr;
                        that.nameObj.energyid.obj = obj;
                    } else {
                        that.$message.error(res.message);
                    }
                });
        },
        // 获取工序列表
        getNodeData () {
            let that = this
            let url = 'pass/ems/v1/trmworkprocs/getIdAndName?isreal=1';
            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);
                    }
                });
        },
        // 获取工厂
        getFactoryIdData () {
            let that = this
            let url = 'pass/ems/v1/trmfactorys/getIAndName';
            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.factoryid.arr = arr;
                        that.nameObj.factoryid.obj = obj;
                    } else {
                        that.$message.error(res.message);
                    }
                });
        },
        // 获取标准计量单位
        getUnitIdData () {
            let that = this
            let url = 'pass/ems/v1/trmunits/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;
                            if (item.name === '吨') {
                                that.nameObj.unitid.defaults = that.nameObj.unitid.defaults ? that.nameObj.unitid.defaults : item.id;
                            }
                        }
                        that.nameObj.unitid.arr = arr;
                        that.nameObj.unitid.obj = obj;
                    } else {
                        that.$message.error(res.message);
                    }
                });
        },
        // 分类获取所有指标类型
        getType () {
            let that = this
            let url = 'pass/ems/v1/trmindextypes/?pageSize=1000000';
            that.axios.get(url)
                .then(function (res) {
                    if (res.code === '0') {
                        let arr = [],
                            arr1 = [],
                            arr2 = [],
                            arr3 = [],
                            arr4 = [];
                        let obj = {},
                            obj1 = {},
                            obj2 = {},
                            obj3 = {},
                            obj4 = {};
                        for (let item of res.data.list) {
                            if (item.indexcol === 'INDEXTYPE') {
                                arr.push({
                                    id: item.indexid,
                                    name: item.indexname
                                })
                                obj[item.indexid] = item.indexname;
                            } else if (item.indexcol === 'TYPE1') {
                                arr1.push({
                                    id: item.indexid,
                                    name: item.indexname
                                })
                                obj1[item.indexid] = item.indexname;
                            } else if (item.indexcol === 'TYPE2') {
                                arr2.push({
                                    id: item.indexid,
                                    name: item.indexname
                                })
                                obj2[item.indexid] = item.indexname;
                            } else if (item.indexcol === 'TYPE3') {
                                arr3.push({
                                    id: item.indexid,
                                    name: item.indexname
                                })
                                obj3[item.indexid] = item.indexname;
                            } else if (item.indexcol === 'TYPE4') {
                                arr4.push({
                                    id: item.indexid,
                                    name: item.indexname
                                })
                                obj4[item.indexid] = item.indexname;
                            }
                        }
                        that.nameObj.indextype.arr = arr;
                        that.nameObj.indextype.obj = obj;
                        that.nameObj.type1.arr = arr1;
                        that.nameObj.type1.obj = obj1;
                        that.nameObj.type2.arr = arr2;
                        that.nameObj.type2.obj = obj2;
                        that.nameObj.type3.arr = arr3;
                        that.nameObj.type3.obj = obj3;
                        that.nameObj.type4.arr = arr4;
                        that.nameObj.type4.obj = obj4;
                    } else {
                        that.$message.error(res.message);
                    }
                });
        },
        selectByTopicId (data, node) {
            let that = this;
            // console.log(node);
            // console.log(data);
            let type = that.table.type;
            that.filterForm.node.id = data.id;
            that.filterForm.node.name = data.name;
            that.filterForm.node.desc = data.desc;
            that.filterForm.node.level = data.level;
            that.filterForm.node.indextype = data.indextype;
            that.filterForm.node.nodeObj = node;
            if (data.level && data.level === 3) {
                type = 1;
            } else {
                type = 0;
            }
            that.getTableData(1);
            if (type !== that.table.type) {
                that.table.type = type;
                that.$nextTick(() => {
                    that.singleTableHeight = that.getRoleHeight(that.$refs['box_bottom']) - 60;
                });
            }
        },
        upNode () {
            let that = this;
            let node = that.filterForm.node.nodeObj;
            node.loaded = false;
            node.expand();
        },
        // 懒加载节点数
        loadNode (node, resolve) {
            let that = this;
            // console.log(node)
            if (node.level === 0) {
                let url = 'pass/ems/v1/trmindextypes/getIdAndName';
                that.axios.get(url)
                    .then(function (res) {
                        if (res.code === '0') {
                            let arr = [];
                            for (let item of res.data) {
                                arr.push({
                                    id: item.indexid,
                                    name: item.indexname,
                                    desc: item.indexdesc,
                                    level: node.level + 1,
                                    indextype: item.indexid
                                })
                            }
                            return resolve(arr);
                        } else {
                            that.$message.error(res.message);
                        }
                    });
            }
            if (node.level === 1 || node.level === 2) {
                let url = 'pass/ems/v1/trmindexitems/getTrees?pageSize=1000000',
                    params = {};
                if (node.level === 1) {
                    params = {
                        indextype: node.data.id,
                        itemflag: 1
                    }
                } else {
                    params = {
                        parentid: node.data.id
                    }
                }
                that.axios.get(url, {
                    params: params
                })
                    .then(function (res) {
                        if (res.code === '0') {
                            let arr = [];
                            for (let item of res.data.list) {
                                arr.push({
                                    id: item.ITEMID,
                                    name: item.ITEMNAME,
                                    desc: item.ITEMDESC,
                                    level: node.level + 1,
                                    indextype: item.INDEXTYPE
                                })
                            }
                            return resolve(arr);
                        } else {
                            that.$message.error(res.message);
                        }
                    });
            }
            if (node.level === 3) {
                return resolve([]);
            }
        },
        // 获取表格中的数据
        getTableData (pageNum) {
            let that = this;
            if (that.filterForm.node.level === 3) {
                let params = {
                    parentid: that.filterForm.node.id,
                    itemid: that.filterForm.data.itemid,
                    itemname: that.filterForm.data.itemname,
                    itemtype: that.filterForm.data.itemtype.join(','),
                    type1: that.filterForm.data.type1,
                    type2: that.filterForm.data.type2,
                    type3: that.filterForm.data.type3,
                    type4: that.filterForm.data.type4,
                    energyid: that.filterForm.data.energyid,
                    workprocid: that.filterForm.data.workprocid.join(','),
                    factoryid: that.filterForm.data.factoryid,
                    version: that.filterForm.data.version,
                    useflag: that.filterForm.useflag
                };
                that.table.table2.pageNum = pageNum || that.table.table2.pageNum;
                that.table.table2.tableLoading = true;
                let url = 'pass/ems/v1/trmindexitems/like/?pageNum=' + that.table.table2.pageNum + '&pageSize=' + that.table.table2.pageSize;
                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({
                                    seq: i,
                                    itemid: item.itemid,
                                    itemname: item.itemname,
                                    itemtype: item.itemtype,
                                    indextype: item.indextype,
                                    energyid: item.energyid,
                                    workprocid: item.workprocid,
                                    factoryid: item.factoryid,
                                    unitid: item.unitid,
                                    digits: item.digits,
                                    seqno: item.seqno,
                                    itemdesc: item.itemdesc,
                                    type1: item.type1,
                                    type2: item.type2,
                                    type3: item.type3,
                                    type4: item.type4,
                                    bz: item.bz,
                                    mintimegranid: item.mintimegranid,
                                    warnvalue: item.warnvalue,
                                    useflag: item.useflag,
                                    propertyid: item.useflag
                                })
                            }
                            that.table.table2.data = arr;
                            that.table.table2.total = res.data.total;
                        } else {
                            that.$message.error(res.message);
                        }
                        that.table.table2.tableLoading = false;
                    }).catch(function () {
                        that.table.table2.tableLoading = false;
                    });
            } else {
                let url = 'pass/ems/v1/trmindexitems/getTrees?pageSize=1000000',
                    params = {};
                if (that.filterForm.node.level === 1) {
                    params = {
                        indextype: that.filterForm.node.id,
                        itemflag: 1
                    }
                } else {
                    params = {
                        parentid: that.filterForm.node.id
                    }
                }
                that.table.table1.tableLoading = true;
                that.axios.get(url, {
                    params: params
                })
                    .then(function (res) {
                        if (res.code === '0') {
                            let arr = [];
                            for (let item of res.data.list) {
                                arr.push({
                                    itemid: item.ITEMID,
                                    itemname: item.ITEMNAME,
                                    indextype: item.INDEXTYPE,
                                    seqno: item.SEQNO,
                                    itemdesc: item.ITEMDESC
                                })
                            }
                            that.table.table1.data = arr;
                        } else {
                            that.$message.error(res.message);
                        }
                        that.table.table1.tableLoading = false;
                    }).catch(function () {
                        that.table.table1.tableLoading = false;
                    });
            }
        },
        // 改变表格显示条数
        tableSizeChange (val) {
            let that = this;
            that.table.table2.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);
        },
        but_add () {
            let that = this;
            if (that.filterForm.node.nodeObj) {
                let formData = that.dialog.editorBox.form1.data;
                let formName = 'dialog_form1';
                if (that.table.type === 1) {
                    formData = that.dialog.editorBox.form2.data;
                    formName = 'dialog_form2';
                }
                for (let key in formData) {
                    formData[key] = ''; // 清空输入项
                }
                if (that.table.type === 1) {
                    if (that.dialog.editorBox.addRow) {
                        for (let key in formData) {
                            if (that.dialog.editorBox.addRow[key] || that.dialog.editorBox.addRow[key] === 0) {
                                formData[key] = that.dialog.editorBox.addRow[key];
                            } else {
                                formData[key] = '';
                            }
                        }
                    }
                }
                // that.dialog.editorBox.form2.data.unitid = that.nameObj.unitid.defaults ? that.nameObj.unitid.defaults : '';
                formData.indextype = that.filterForm.node.indextype;
                setTimeout(() => {
                    that.$refs[formName].clearValidate();
                }, 100);
                that.dialog.editorBox.type = 'add';
                that.dialog.editorBox.show = true;
            } else {
                that.$message.error('请先选择左侧的指标');
            }
        },
        but_edit (row) {
            let that = this;
            let formData = that.dialog.editorBox.form1.data;
            if (that.table.type === 1) {
                formData = that.dialog.editorBox.form2.data;
            }
            for (let key in formData) {
                formData[key] = row[key]; // 取出表格选取数据
            }
            that.dialog.editorBox.type = 'edit';
            that.dialog.editorBox.show = true;
        },
        but_del (row) {
            let that = this;
            that.$confirm('是否删除该条数据?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                state: 'warning'
            }).then(() => {
                that.axios.delete('pass/ems/v1/trmindexitems/' + row.itemid)
                    .then(function (res) {
                        if (res.code === '0') {
                            that.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            that.upNode();
                            that.getTableData();
                        } else {
                            that.$message.error(res.message);
                        }
                    }).catch(function () {
                    });
            }).catch(() => {
            });
        },
        dataSave () {
            let that = this;
            let formName = 'dialog_form1';
            let SubmitData = {
                _id: that.dialog.editorBox.form1.data.itemid,
                itemname: that.dialog.editorBox.form1.data.itemname,
                indextype: that.dialog.editorBox.form1.data.indextype,
                itemdesc: that.dialog.editorBox.form1.data.itemdesc,
                parentid: that.filterForm.node.level === 2 ? that.filterForm.node.id : null
                // itemflag: that.filterForm.node.level
            };
            if (that.table.type === 1) {
                formName = 'dialog_form2';
                SubmitData = {
                    mintimegranid: that.dialog.editorBox.form2.data.mintimegranid,
                    itemname: that.dialog.editorBox.form2.data.itemname,
                    itemtype: that.dialog.editorBox.form2.data.itemtype,
                    unitid: that.dialog.editorBox.form2.data.unitid,
                    digits: that.dialog.editorBox.form2.data.digits,
                    warnvalue: that.dialog.editorBox.form2.data.warnvalue,
                    bz: that.dialog.editorBox.form2.data.bz,
                    itemdesc: that.dialog.editorBox.form2.data.itemdesc,
                    type1: that.dialog.editorBox.form2.data.type1,
                    type2: that.dialog.editorBox.form2.data.type2,
                    type3: that.dialog.editorBox.form2.data.type3,
                    type4: that.dialog.editorBox.form2.data.type4,
                    propertyid: that.dialog.editorBox.form2.data.propertyid,
                    parentid: that.filterForm.node.id,
                    yongtu: that.nameObj.itemtype.obj[that.dialog.editorBox.form2.data.itemtype],
                    trmindextype: {
                        indexid: that.dialog.editorBox.form2.data.indextype,
                        indexname: that.nameObj.indextype.obj[that.dialog.editorBox.form2.data.indextype]
                    },
                    trmfactory: {
                        id: that.dialog.editorBox.form2.data.factoryid,
                        name: that.nameObj.factoryid.obj[that.dialog.editorBox.form2.data.factoryid]
                    },
                    trmenergy: {
                        id: that.dialog.editorBox.form2.data.energyid,
                        name: that.nameObj.energyid.obj[that.dialog.editorBox.form2.data.energyid]
                    },
                    trmworkproc: {
                        id: that.dialog.editorBox.form2.data.workprocid,
                        name: that.nameObj.workprocid.obj[that.dialog.editorBox.form2.data.workprocid]
                    },
                    itemflag: that.filterForm.node.level
                }
            }
            that.$refs[formName].validate((valid) => {
                if (valid) {
                    if (that.dialog.editorBox.type === 'add') {
                        that.loading = true;
                        let url = that.table.type === 1 ? 'pass/ems/v1/trmindexitems/add3' : 'pass/ems/v1/trmindexitems/';
                        that.axios.post(url, SubmitData, {
                            contentType: 'application/json'
                        })
                            .then(function (res) {
                                if (res.code === '0') {
                                    that.$message({
                                        message: '新增成功',
                                        type: 'success'
                                    });
                                    that.upNode();
                                    that.getTableData();
                                    that.dialog.editorBox.show = false;
                                } else {
                                    that.$message.error(res.message);
                                }
                                that.loading = false;
                            }).catch(function () {
                                that.loading = false;
                            });
                    } else {
                        that.loading = true;
                        that.axios.put('pass/ems/v1/trmindexitems/' + SubmitData._id, SubmitData)
                            .then(function (res) {
                                if (res.code === '0') {
                                    that.$message({
                                        message: '修改成功',
                                        type: 'success'
                                    });
                                    that.upNode();
                                    that.getTableData();
                                    that.dialog.editorBox.show = false;
                                } else {
                                    that.$message.error(res.message);
                                }
                                that.loading = false;
                            }).catch(function () {
                                that.loading = false;
                            });
                    }
                }
            });
        },
        but_equationEditing (row) {
            let that = this;
            that.dialog.equationEditing.form.data.clock = that.filterForm.data.version;
            that.dialog.equationEditing.form.data.code = row.itemid;
            that.dialog.equationEditing.show = true;
        },
        // 选择添加时的模板
        handleCurrentChange (row) {
            let that = this;
            that.dialog.editorBox.addRow = row;
        },
        // 表格中多选按钮的相关方法
        handleSelectionChange (val) {
            let that = this;
            let tableFormDataObj = {};
            let arr = [];
            for (let item of val) {
                arr.push(item.seq);
            }
            this.seqArr = arr.sort(sortNumber);
            for (let item of that.table.table2.data) {
                item.isSelection = false;
            }
            for (let item of val) {
                let obj = {};
                let xId = item.itemid;
                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({
                                itemid: that.tableFormDataObj[key].itemid,
                                itemname: that.tableFormDataObj[key].itemname,
                                energyid: that.tableFormDataObj[key].energyid,
                                workprocid: that.tableFormDataObj[key].workprocid,
                                factoryid: that.tableFormDataObj[key].factoryid,
                                unitid: that.tableFormDataObj[key].unitid,
                                digits: that.tableFormDataObj[key].digits,
                                warnvalue: that.tableFormDataObj[key].warnvalue,
                                itemdesc: that.tableFormDataObj[key].itemdesc,
                                bz: that.tableFormDataObj[key].bz,
                                mintimegranid: that.tableFormDataObj[key].mintimegranid,
                                useflag: that.tableFormDataObj[key].useflag
                            });
                        }
                        that.loading = true;
                        that.axios.put('pass/ems/v1/trmindexitems/batchupdate', SubmitData)
                            .then(function (res) {
                                if (res.code === '0') {
                                    let msg = res.message
                                    if (!msg || msg === '操作成功') {
                                        that.$message({
                                            message: '修改成功',
                                            type: 'success'
                                        });
                                    } else {
                                        that.$message.warning('修改成功，注意：' + msg);
                                    }
                                    that.getTableData();
                                } 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 === 'itemname') {
                            this.$message.error('请填写项目名称');
                        } else if (col && col === 'itemtype') {
                            this.$message.error('请填写用途');
                        } else if (col && col === 'indextype') {
                            this.$message.error('请填写指标类型');
                        } else if (col && col === 'factoryid') {
                            this.$message.error('请填写工厂');
                        } else if (col && col === 'energyid') {
                            this.$message.error('请填写能介');
                        } else if (col && col === 'workprocid') {
                            this.$message.error('请填写工序');
                        } else if (col && col === 'mintimegranid') {
                            this.$message.error('请填写时间粒度');
                        } else if (col && col === 'digits') {
                            this.$message.error('精度为非负整数');
                        } else if (col && col === 'warnvalue') {
                            this.$message.error('超标警告数值为0-100');
                        } else {
                            this.$message.error('数据不完整或者格式错误');
                        }
                    }
                });
            } else {
                this.$message.error('至少要选择一条数据');
            }
        },
        // 批量删除
        but_del_plural () {
            let that = this;
            if (that.multipleSelection.length > 0) {
                let ids = [];
                for (let item of that.multipleSelection) {
                    ids.push(item.itemid);
                }
                that.$confirm('是否删除已选数据?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    state: 'warning'
                }).then(() => {
                    that.axios.delete('pass/ems/v1/trmindexitems/' + ids.toString())
                        .then(function (res) {
                            if (res.code === '0') {
                                that.$message({
                                    message: '删除成功',
                                    type: 'success'
                                });
                                that.getTableData();
                            } else {
                                that.$message.error(res.message);
                            }
                        }).catch(function () {
                        });
                }).catch(() => {
                });
            } else {
                this.$message.error('至少要选择一条数据');
            }
        },
        tableRowClassName ({row, rowIndex}) {
            // 把每一行的索引放进row
            row.index = rowIndex;
            return row.useflag !== '1' ? 'rowDisable' : '';
        },
        cellClick (row, column, cell, event) {
            let _this = this;
            this.rowIndex = row.index;
            this.columnId = column.id;
            _this.editSetFlag = row.index + column.id;
            _this.focusTarget = event.target;
            while (_this.focusTarget.tagName !== 'TD') {
                _this.focusTarget = _this.focusTarget.parentElement;
            }
            _this.$nextTick(() => {
                let target = this.focusTarget;
                $(target).find('input').focus();
            });
        },
        keyDown (event1) {
            let tdTarget = event1.target;
            let _this = this;
            while (tdTarget.tagName !== 'TD') {
                tdTarget = tdTarget.parentElement;
            }
            // 如果按下键盘下键或者回车键
            if (event.keyCode === 40 || event.keyCode === 13) {
                let index = parseInt(this.rowIndex);
                if (index !== this.seqArr[this.seqArr.length - 1]) {
                    for (let i = 0; i < this.seqArr.length; i++) {
                        if (index === this.seqArr[i]) {
                        index = this.seqArr[i + 1];
                        break;
                        }
                    }
                }
                this.editSetFlag = index + this.columnId;
                let id = '#' + this.editSetFlag;
                this.$nextTick(() => {
                    $(id).click();
                });
            } else if (event.keyCode === 38) { // 键盘上键
                let index = parseInt(this.rowIndex);
                if (index !== this.seqArr[0]) {
                    for (let i = 0; i < this.seqArr.length; i++) {
                        if (index === this.seqArr[i]) {
                        index = this.seqArr[i - 1];
                        break;
                        }
                    }
                }
                this.editSetFlag = index + this.columnId;
                let id = '#' + this.editSetFlag;
                this.$nextTick(() => {
                    $(id).click();
                });
            } else if (event.keyCode === 37) { // 键盘左键
                $(tdTarget).prev().click();
                $(tdTarget).prev().find('input').select();
            } else if (event.keyCode === 39) { // 键盘右键
                $(tdTarget).next().click();
                $(tdTarget).next().find('input').select();
            }
        },
        getName (itemid, rowIndex, row) {
            let that = this;
            let fname = that.nameObj.factoryid.obj[that.tableFormDataObj[itemid].factoryid] ? that.nameObj.factoryid.obj[that.tableFormDataObj[itemid].factoryid] : '';
            let wname = that.nameObj.workprocid.obj[that.tableFormDataObj[itemid].workprocid] ? that.nameObj.workprocid.obj[that.tableFormDataObj[itemid].workprocid] : '';
            let ename = that.nameObj.energyid.obj[row.energyid] ? that.nameObj.energyid.obj[row.energyid] : ''; // 能介
            let yname = that.nameObj.itemtype.obj[row.itemtype] ? that.nameObj.itemtype.obj[row.itemtype] : ''; // 用途名称
            let iname = that.nameObj.indextype.obj[row.indextype] ? that.nameObj.indextype.obj[row.indextype] : ''; // 指标类型
            let itname = fname + wname + ename + yname + iname;
            that.tableFormDataObj[itemid].itemname = itname;
        },
        upUseflag (row, useflag = 0) {
            let that = this;
            let SubmitData = [{
                itemid: row.itemid,
                workprocid: row.workprocid,
                factoryid: row.factoryid,
                useflag: useflag,
                mintimegranid: row.mintimegranid
            }];
            that.$confirm('是否' + (useflag === '1' ? '启用' : '禁用') + '?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                that.axios.put('pass/ems/v1/trmindexitems/batchupdate', SubmitData)
                    .then(function (res) {
                        if (res.code === '0') {
                            that.$message({
                                message: useflag === '1' ? '启用成功' : '禁用成功',
                                type: 'success'
                            });
                            that.getTableData();
                            that.dialog.editorBox.show = false;
                        } else {
                            that.$message.error(res.message);
                        }
                    }).catch(function () {
                    });
            }).catch(() => {
            })
        }
    }
}
function sortNumber (a, b) {
  return a - b
}
</script>

<style lang="less">
.indexMaintain {
    min-width: 700px;
    height: 100%;
    .box {
        height: 100%;
        padding: 15px 15px 0 15px;
        .box-left {
            float: left;
            width: 200px;
            height: calc(100% - 3px);
            overflow: auto;
            .box-left-bt {
                line-height: 35px;
                font-size: 16px;
                border-bottom: 1px solid #ccc;
            }
            .box-left-tree {
                height: calc(100% - 50px);
                overflow: auto;
                padding: 7px 7px 0 0;
                .tree-row-on {
                    color: #3279e8;
                    font-weight: 600;
                }
            }
        }
        .box-right {
            // float: left;
            // width: calc(100% - 200px);
            margin-left: 200px;
            border-left: 1px solid #ccc;
            .box-top {
                .box-top-form {
                    padding-left: 7px;
                    .el-form-item {
                        margin-bottom: 7px;
                    }
                }
                margin-bottom: 6px;
            }
            .box-top-gjl {
                overflow: hidden;
                padding-left: 7px;
                margin-bottom: 6px;
            }
            .box-bottom {
                padding-left: 7px;
            }
            .rowDisable td {
                background-color: #dfdfdf !important;
            }
        }
        .el-table__body .el-form-item--mini.el-form-item {
            margin: 0px;
            .error {
                overflow: hidden;
                color: #f56c6c;
                font-size: 12px;
                line-height: 1;
            }
        }
        .el-table__body input {
            font-weight: normal;
            padding-left: 8px;
            padding-right: 25px;
        }
        .color_no {
            color: #7a7a7a;
        }
        .color_yes {
            color: #1a9f17;
        }
    }
}
</style>
