<template>
    <!-- 外供用电地点维护 -->
    <div class="electricityLocation">
        <div class="box">
            <div class="box-top">
                <el-form
                    style="overflow: hidden;"
                    :style="filterForm.show ? null : { height: '0px' }"
                    size="mini" label-width="85px"
                >
                    <el-row>
                        <el-col :span="6">
                            <el-form-item label="工序-站所">
                                <el-select
                                    filterable clearable
                                    v-model="filterForm.data.networkid"
                                    placeholder="工序-站所"
                                    multiple
                                    collapse-tags
                                    style="width: 100%;"
                                    :disabled="filterForm.disabled.workshop"
                                >
                                    <el-option
                                        v-for="item of nameObj.networkid.arr"
                                        :key="item.id"
                                        :value="item.id"
                                        :label="item.name"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="位置">
                                <el-select
                                    filterable clearable
                                    v-model="filterForm.data.locateid"
                                    placeholder="位置"
                                    multiple
                                    collapse-tags
                                    style="width: 100%;"
                                    :disabled="filterForm.disabled.location"
                                >
                                    <el-option
                                        v-for="item of nameObj.locateid.arr"
                                        :key="item.id"
                                        :value="item.id"
                                        :label="item.name"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="工序" label-width="60px">
                                <el-select
                                    filterable
                                    clearable
                                    multiple
                                    collapse-tags
                                    v-model="filterForm.data.workprocid"
                                    placeholder="工序"
                                    style="width: 100%;"
                                >
                                    <el-option
                                        v-for="item of nameObj.workprocid.arr"
                                        :key="item.id"
                                        :value="item.id"
                                        :label="item.name"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="是否为外单位" label-width="100px">
                                <el-select
                                    filterable
                                    clearable
                                    v-model="filterForm.data.type"
                                    placeholder="是否为外单位"
                                    style="width: 100%;"
                                >
                                    <el-option
                                        v-for="item of nameObj.type.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="6">
                            <el-form-item label="计量点名称" label-width="85px">
                                <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="6">
                            <el-form-item label="电度表编号" label-width="85px">
                                <el-input
                                    clearable
                                    v-model.trim="filterForm.data.bcode"
                                    placeholder="电度表编号"
                                    style="width: 100%;"
                                    @keyup.enter.native="getTableData(1)"
                                ></el-input>
                            </el-form-item>
                        </el-col>
                          <el-col :span="6">
                            <el-form-item label="4G计量点编号" label-width="85px">
                                <el-input
                                    clearable
                                    v-model.trim="filterForm.data.fourgitem"
                                    placeholder="4G计量点编号"
                                    style="width: 90%;"
                                    @keyup.enter.native="getTableData(1)"
                                ></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
                <div class="box-top-gjl">
                    <el-button
                        class="button"
                        type="primary"
                        size="mini"
                        icon="el-icon-plus"
                        v-privilege="activeMenu + 'ADD'"
                        @click="but_add"
                    >新增</el-button>
                    <el-button
                        class="button"
                        type="primary"
                        size="mini"
                        icon="el-icon-edit-outline"
                        v-privilege="activeMenu + 'PUT'"
                        @click="but_edit_plural()"
                        :loading="loading"
                    >修改</el-button>
                    <el-button
                        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="getTableData(1)"
                        >查询</el-button>
                        <el-button
                            class="button"
                            type="primary"
                            size="mini"
                            icon="el-icon-refresh"
                            v-privilege="activeMenu + 'QUERY'"
                            @click="tableDataCancel();"
                        >重置</el-button>
                        <el-button
                            size="mini"
                            :icon="filterForm.show ? 'el-icon-caret-top' : 'el-icon-caret-bottom'"
                            :title="filterForm.show ? '收起搜索区' : '展开搜索区'"
                            @click="filterForm.show = !filterForm.show;
                                $nextTick(() => {
                                    singleTableHeight = getRoleHeight($refs['singleTable'].$el) - 45;
                                }
                            );"
                        >{{ filterForm.show ? '收起' : '展开' }}</el-button>
                    </div>
                </div>
            </div>
            <div class="box-bottom">
                <el-form
                    size="mini"
                    ref="dialog_form_arr"
                    label-width="0px"
                    :model="tableFormDataObj"
                >
                    <el-table
                        stripe
                        id="singleTable"
                        ref="singleTable"
                        :data="tableData"
                        v-loading="tableLoading"
                        style="width: 100%;"
                        :height="singleTableHeight"
                        border
                        size="mini"
                        highlight-current-row
                        @current-change="handleCurrentChange"
                        @selection-change="handleSelectionChange"
                        :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="id"
                            label="序号"
                            width="50px"
                            :show-overflow-tooltip="true"
                        ></el-table-column>
                        <el-table-column
                            sortable
                            prop="workshop"
                            label="工序-站所"
                            width="100px"
                            :show-overflow-tooltip="true"
                        > <template slot-scope="scope">
                                <el-form-item
                                    v-if="scope.row.isSelection"
                                    :prop="scope.row.id + '.workshop'"
                                    :rules="dialog.editorBox.form.rules.workshop"
                                >
                                    <el-select
                                        filterable
                                        clearable
                                        v-model="tableFormDataObj[scope.row.id].workshop"
                                        placeholder="工序-站所"
                                        style="width: 100%;"
                                    >
                                        <el-option
                                            v-for="item of nameObj.networkid.arr"
                                            :key="item.id"
                                            :value="item.id"
                                            :label="item.name"
                                        ></el-option>
                                    </el-select>
                                </el-form-item>
                                <span v-else>{{ nameObj.networkid.obj[scope.row.workshop] ? nameObj.networkid.obj[scope.row.workshop] : scope.row.workshop }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column
                            sortable
                            prop="location"
                            label="车间机组-位置"
                            width="105px"
                            :show-overflow-tooltip="true"
                        > <template slot-scope="scope">
                                <el-form-item
                                    v-if="scope.row.isSelection"
                                    :prop="scope.row.id + '.location'"
                                    :rules="dialog.editorBox.form.rules.location"
                                >
                                    <el-select
                                        filterable
                                        clearable
                                        v-model="tableFormDataObj[scope.row.id].location"
                                        placeholder="车间机组-位置"
                                        style="width: 100%;"
                                    >
                                        <el-option
                                            v-for="item of nameObj.locateid.arr"
                                            :key="item.id"
                                            :value="item.id"
                                            :label="item.name"
                                        ></el-option>
                                    </el-select>
                                </el-form-item>
                                <span v-else>{{ nameObj.locateid.obj[scope.row.location] ? nameObj.locateid.obj[scope.row.location] : scope.row.location }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column
                            sortable
                            prop="workprocid"
                            label="用电地点-工序"
                            width="160px"
                            :show-overflow-tooltip="true"
                        > <template slot-scope="scope">
                            <el-form-item
                                    v-if="scope.row.isSelection"
                                    :prop="scope.row.id + '.workprocid'"
                                    :rules="dialog.editorBox.form.rules.workprocid"
                                >
                                    <el-select
                                        filterable
                                        clearable
                                        style="width: 100%;"
                                        v-model="tableFormDataObj[scope.row.id].workprocid"
                                    >
                                        <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="itemname"
                            label="计量点名称"
                            min-width="140px"
                            :show-overflow-tooltip="true"
                        > <template slot-scope="scope">
                                <el-form-item
                                    v-if="scope.row.isSelection"
                                    :prop="scope.row.id + '.itemname'"
                                    :rules="dialog.editorBox.form.rules.itemname"
                                >
                                    <el-input @keydown.native="keyDown" :id="scope.$index + scope.column.id"
                                        clearable
                                        style="width: 100%;"
                                        v-model.trim="tableFormDataObj[scope.row.id].itemname"
                                    ></el-input>
                                </el-form-item>
                                <span v-else>{{scope.row.itemname}}</span>
                            </template>
                        </el-table-column>
                        <el-table-column
                            sortable
                            prop="fourgitem"
                            label="4G计量点编号"
                            min-width="140px"
                            :show-overflow-tooltip="true"
                        > <template slot-scope="scope">
                                <el-form-item
                                    v-if="scope.row.isSelection"
                                    :prop="scope.row.id + '.fourgitem'"
                                    :rules="dialog.editorBox.form.rules.fourgitem"
                                >
                                    <el-input @keydown.native="keyDown" :id="scope.$index + scope.column.id"
                                        clearable
                                        style="width: 100%;"
                                        v-model.trim="tableFormDataObj[scope.row.id].fourgitem"
                                    ></el-input>
                                </el-form-item>
                                <span v-else>{{scope.row.fourgitem}}</span>
                            </template>
                        </el-table-column>
                        <el-table-column
                            sortable
                            prop="bcode"
                            label="电度表编号"
                            width="155px"
                            :show-overflow-tooltip="true"
                        > <template slot-scope="scope">
                                <el-form-item
                                    v-if="scope.row.isSelection"
                                    :prop="scope.row.id + '.bcode'"
                                    :rules="dialog.editorBox.form.rules.bcode"
                                >
                                    <el-input @keydown.native="keyDown" :id="scope.$index + scope.column.id"
                                        clearable
                                        style="width: 100%;"
                                        v-model.trim="tableFormDataObj[scope.row.id].bcode"
                                    ></el-input>
                                </el-form-item>
                                <span v-else>{{scope.row.bcode}}</span>
                            </template>
                        </el-table-column>
                        <el-table-column
                            sortable
                            prop="mods"
                            label="倍率"
                            width="75px"
                            :show-overflow-tooltip="true"
                        > <template slot-scope="scope">
                                <el-form-item
                                    v-if="scope.row.isSelection"
                                    :prop="scope.row.id + '.mods'"
                                    :rules="dialog.editorBox.form.rules.mods"
                                >
                                    <el-input @keydown.native="keyDown" :id="scope.$index + scope.column.id"
                                        clearable
                                        style="width: 100%;"
                                        v-model.trim="tableFormDataObj[scope.row.id].mods"
                                    ></el-input>
                                </el-form-item>
                                <span v-else>{{scope.row.mods}}</span>
                            </template>
                        </el-table-column>
                        <el-table-column
                            sortable
                            prop="type"
                            label="外单位"
                            width="80px"
                            :show-overflow-tooltip="true"
                        > <template slot-scope="scope">
                                <el-form-item
                                    v-if="scope.row.isSelection"
                                    :prop="scope.row.id + '.type'"
                                    :rules="dialog.editorBox.form.rules.type"
                                >
                                    <el-select
                                        filterable
                                        clearable
                                        style="width: 100%;"
                                        v-model="tableFormDataObj[scope.row.id].type"
                                    >
                                        <el-option
                                            v-for="item of nameObj.type.arr"
                                            :key="item.id"
                                            :value="item.id"
                                            :label="item.name"
                                        ></el-option>
                                    </el-select>
                                </el-form-item>
                                <span v-else>{{ nameObj.type.obj[scope.row.type] ? nameObj.type.obj[scope.row.type] : scope.row.type }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column
                            sortable
                            prop="itemid"
                            label="计量点编号"
                            width="110px"
                            :show-overflow-tooltip="true"
                        ></el-table-column>
                        <el-table-column
                            sortable
                            prop="state"
                            label="状态"
                            width="70px"
                            :show-overflow-tooltip="true"
                        > <template slot-scope="scope">
                                <el-form-item
                                    v-if="scope.row.isSelection"
                                    :prop="scope.row.id + '.state'"
                                    :rules="dialog.editorBox.form.rules.state"
                                >
                                    <el-select
                                        filterable
                                        clearable
                                        style="width: 100%;"
                                        v-model="tableFormDataObj[scope.row.id].state"
                                    >
                                        <el-option
                                            v-for="item of nameObj.state.arr"
                                            :key="item.id"
                                            :value="item.id"
                                            :label="item.name"
                                        ></el-option>
                                    </el-select>
                                </el-form-item>
                                <span v-else>{{ nameObj.state.obj[scope.row.state] ? nameObj.state.obj[scope.row.state] : scope.row.state }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column
                            sortable
                            prop="seqno"
                            label="顺序号"
                            width="80px"
                            :show-overflow-tooltip="true"
                        > <template slot-scope="scope">
                                <el-form-item
                                    v-if="scope.row.isSelection"
                                    :prop="scope.row.id + '.seqno'"
                                    :rules="dialog.editorBox.form.rules.seqno"
                                >
                                    <el-input @keydown.native="keyDown" :id="scope.$index + scope.column.id"
                                        clearable
                                        style="width: 100%;"
                                        v-model.trim="tableFormDataObj[scope.row.id].seqno"
                                    ></el-input>
                                </el-form-item>
                                <span v-else>{{scope.row.seqno}}</span>
                            </template>
                        </el-table-column>
                        <el-table-column
                            sortable
                            prop="note"
                            label="备注"
                            min-width="110px"
                            :show-overflow-tooltip="true"
                        > <template slot-scope="scope">
                                <el-form-item
                                    v-if="scope.row.isSelection"
                                    :prop="scope.row.id + '.note'"
                                    :rules="dialog.editorBox.form.rules.note"
                                >
                                    <el-input @keydown.native="keyDown" :id="scope.$index + scope.column.id"
                                        clearable
                                        style="width: 100%;"
                                        v-model.trim="tableFormDataObj[scope.row.id].note"
                                    ></el-input>
                                </el-form-item>
                                <span v-else>{{scope.row.note}}</span>
                            </template>
                        </el-table-column>
                        <el-table-column
                            sortable
                            prop="createman"
                            label="创建人"
                            width="70px"
                            :show-overflow-tooltip="true"
                        ></el-table-column>
                        <el-table-column
                            sortable
                            prop="createtime"
                            label="创建时间"
                            width="125px"
                            :show-overflow-tooltip="true"
                        ></el-table-column>
                        <el-table-column
                            sortable
                            prop="updateman"
                            label="修改人"
                            width="70px"
                            :show-overflow-tooltip="true"
                        ></el-table-column>
                        <el-table-column
                            sortable
                            prop="updatetime"
                            label="修改时间"
                            width="120px"
                            :show-overflow-tooltip="true"
                        ></el-table-column>
                    </el-table>
                </el-form>
                <el-pagination
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total"
                    :page-sizes="[10, 20, 50, 100, 500, 1000]"
                    :page-size="pageSize"
                    :current-page.sync="pageNum"
                    @size-change="tableSizeChange"
                    @current-change="getTableData()"
                    style="text-align: right;margin-top: 10px;"
                ></el-pagination>
            </div>
        </div>
        <el-dialog
            :title="'外供用电地点信息 - ' + (dialog.editorBox.type === 'add' ? '新增' : '修改')"
            :visible.sync="dialog.editorBox.show"
            width="660px"
            :close-on-click-modal="false"
            :show-close="!loading"
        >
            <div>
                <el-form
                    ref="dialog_form"
                    :model="dialog.editorBox.form.data"
                    :rules="dialog.editorBox.form.rules"
                    size="mini"
                    label-width="105px"
                >
                    <el-row>
                        <el-col :span="12">
                            <el-form-item
                                label="工序-站所"
                                prop="workshop"
                            > <el-select
                                    filterable
                                    clearable
                                    style="width: 100%;"
                                    v-model="dialog.editorBox.form.data.workshop"
                                >
                                    <el-option
                                        v-for="item of nameObj.networkid.arr"
                                        :key="item.id"
                                        :value="item.id"
                                        :label="item.name"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item
                                label="车间机组-位置"
                                prop="location"
                                label-width="110px"
                            >
                                <el-select
                                    filterable
                                    clearable
                                    v-model="dialog.editorBox.form.data.location"
                                    placeholder="车间机组-位置"
                                    style="width: 100%;"
                                >
                                    <el-option
                                        v-for="item of nameObj.locateid.arr"
                                        :key="item.id"
                                        :value="item.id"
                                        :label="item.name"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item
                                label="计量点名称"
                                prop="itemname"
                            > <el-input
                                    clearable
                                    style="width: 100%;"
                                    v-model.trim="dialog.editorBox.form.data.itemname"
                                ></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item
                                label="用电地点-工序"
                                prop="workprocid"
                                label-width="110px"
                            > <el-select
                                    filterable
                                    clearable
                                    style="width: 100%;"
                                    v-model="dialog.editorBox.form.data.workprocid"
                                >
                                    <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="mods"
                            > <el-input
                                    clearable
                                    style="width: 100%;"
                                    v-model.trim="dialog.editorBox.form.data.mods"
                                ></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item
                                label="电度表编号"
                                prop="bcode"
                                label-width="110px"
                            > <el-input
                                    clearable
                                    style="width: 100%;"
                                    v-model.trim="dialog.editorBox.form.data.bcode"
                                ></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item
                                label="顺序号"
                                prop="seqno"
                            > <el-input
                                    clearable
                                    style="width: 100%;"
                                    v-model.trim="dialog.editorBox.form.data.seqno"
                                ></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item
                                label="是否为外单位"
                                prop="type"
                                label-width="110px"
                            > <el-select
                                    filterable
                                    clearable
                                    style="width: 100%;"
                                    v-model="dialog.editorBox.form.data.type"
                                >
                                    <el-option
                                        v-for="item of nameObj.type.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="note"
                            >
                                <el-input
                                    clearable
                                    style="width: 100%;"
                                    v-model.trim="dialog.editorBox.form.data.note"
                                ></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item
                                label="4G计量点编号"
                                prop="fourgitem"
                            >
                                <el-input
                                    clearable
                                    style="width: 100%;"
                                    v-model.trim="dialog.editorBox.form.data.fourgitem"
                                ></el-input>
                            </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>
    </div>
</template>

<script>
import { formatDate } from '@/utils/util.js';
import { zCheckNumber1, zCheckNumber2 } from '@/utils/validator.js'
export default {
    name: 'electricityLocation2',
    data () {
        return {
            workShopsName: {},
            workshop: [],
            rowIndex: '',
            columnId: '',
            focusTarget: null,
            seqArr: [],
            editSetFlag: '',
            multipleSelection: [],
            tableFormDataObj: {},
            activeMenu: '',
            filterForm: {
                show: true,
                data: {
                    workprocid: [],
                    id: '',
                    name: '',
                    itemname: '',
                    locateid: [],
                    itemid: '',
                    type: '',
                    bcode: '',
                    fourgitem: '',
                    networkid: [],
                    version: formatDate(new Date(), 'yyyyMM00')
                },
                rules: {
                },
                disabled: {
                    workshop: false,
                    location: false
                }
            },
            pageNum: 1,
            pageSize: 100,
            total: 0,
            singleTableHeight: 100,
            tableData: [],
            nameObj: {
                workprocid: {
                    obj: {},
                    arr: []
                },
                locateid: {
                    obj: {},
                    arr: []
                },
                networkid: {
                    obj: {},
                    arr: []
                },
                type: {
                    obj: {
                        '0': '否',
                        '1': '是'
                    },
                    arr: [
                        {
                            id: '0',
                            name: '否'
                        },
                        {
                            id: '1',
                            name: '是'
                        }
                    ]
                },
                state: {
                    obj: {
                        '0': '禁用',
                        '1': '启用'
                    },
                    arr: [
                        {
                            id: '0',
                            name: '禁用'
                        },
                        {
                            id: '1',
                            name: '启用'
                        }
                    ]
                }
            },
            loading: false,
            tableLoading: false,
            dialog: {
                editorBox: {
                    show: false,
                    type: '',
                    addRow: null,
                    form: {
                        data: {
                            workprocid: '', // 用电地点-工序
                            location: '', // 车间机组-位置
                            workshop: '', // 工序-站所
                            type: '',
                            state: '',
                            note: '',
                            itemname: '', // 计量点名称
                            bcode: '', // 电度表编号
                            seqno: '', // 顺序号
                            mods: '1', // 倍率
                            fourgitem: ''// 4G计量点编号
                        },
                        rules: {
                            workprocid: [
                                { required: true, message: '该项不能为空', trigger: 'change' }
                            ],
                            bcode: [
                                { required: true, message: '该项不能为空', trigger: 'change' }
                            ],
                            location: [
                                { required: true, message: '该项不能为空', trigger: 'change' }
                            ],
                            workshop: [
                                { required: true, message: '该项不能为空', trigger: 'change' }
                            ],
                            seqno: [
                                { validator: zCheckNumber2, trigger: 'change' }
                            ],
                            mods: [
                                { validator: zCheckNumber1, trigger: 'change' }
                            ],
                            itemname: [
                                { required: true, message: '该项不能为空', trigger: 'change' }
                            ],
                            fourgitem: [
                                { required: true, message: '该项不能为空', trigger: 'change' }
                            ]
                        }
                    }
                },
                equationEditing: {
                    show: false,
                    form: {
                        data: {
                            clock: '',
                            code: ''
                        }
                    }
                }
            }
        }
    },
    created () {
        this.activeMenu = window.localStorage.getItem('activeMenu');
    },
    mounted () {
        let that = this;
        window.PEDataObj = {
            // 将数据绑定到window上，供main页面使用
            vm: that,
            // tableArr：用于导出成Excel的表格的信息
            tableArr: [
                {
                    name: '',
                    id: 'singleTable'
                }
            ]
        };
        that.$nextTick(() => {
            // 立即获取的height有一定偏差，通过setTimeout延迟来解决
            setTimeout(() => {
                that.singleTableHeight = that.getRoleHeight(that.$refs['singleTable'].$el) - 45;
            }, 1);
        });
        // 接收路由参数
        if (that.$route.query.workshop) {
            that.filterForm.disabled.workshop = true;
            that.filterForm.data.networkid = that.$route.query.workshop.split(',');
        }
        if (that.$route.query.location) {
            that.filterForm.disabled.location = true;
            that.filterForm.data.locateid = that.$route.query.location.split(',');
        }
        that.getworkprocidData();
        that.getLocateIdData();
        that.getNetworkIdData();
        that.getTableData();
    },
    methods: {
        // 获取位置
        getLocateIdData () {
            let that = this
            let url = 'pass/ems/v1/trmlocations/getidandname/';
            that.axios.get(url)
                .then(function (res) {
                    if (res.code === '0') {
                        let arr = [];
                        let obj = {};
                        for (let item of res.data) {
                            if (that.filterForm.disabled.location) {
                                if (that.filterForm.data.locateid.indexOf(item.id) > -1) {
                                    arr.push({
                                        id: item.id,
                                        name: item.name
                                    })
                                }
                            } else {
                                arr.push({
                                    id: item.id,
                                    name: item.name
                                })
                            }
                            obj[item.id] = item.name;
                        }
                        that.nameObj.locateid.arr = arr;
                        that.nameObj.locateid.obj = obj;
                    } else {
                        that.$message.error(res.message);
                    }
                });
        },
        // 获取站所
        getNetworkIdData () {
            let that = this
            let url = 'pass/ems/v1/trmstations/getidandname/';
            that.axios.get(url)
                .then(function (res) {
                    if (res.code === '0') {
                        let arr = [];
                        let obj = {};
                        for (let item of res.data) {
                            if (that.filterForm.disabled.workshop) {
                                if (that.filterForm.data.networkid.indexOf(item.id) > -1) {
                                    arr.push({
                                        id: item.id,
                                        name: item.name
                                    })
                                }
                            } else {
                                arr.push({
                                    id: item.id,
                                    name: item.name
                                })
                            }
                            obj[item.id] = item.name;
                        }
                        that.nameObj.networkid.arr = arr;
                        that.nameObj.networkid.obj = obj;
                    } else {
                        that.$message.error(res.message);
                    }
                });
        },
        //  计算树区域高度
        getRoleHeight (dom) {
            return window.innerHeight - dom.offsetTop;
        },
        // 获取工序
        getworkprocidData () {
            let that = this
            let url = 'pass/ems/v1/trmworkprocs/getIdAndName?issettle=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);
                    }
                });
        },
        // 获取表格中的数据
        getTableData (pageNum) {
            let that = this,
                params = {
                    workprocid: that.filterForm.data.workprocid.join(','), // 用电地点-工序
                    workshop: that.filterForm.data.networkid.join(','), // 工序-站所
                    location: that.filterForm.data.locateid.join(','), // 车间机组-位置
                    type: that.filterForm.data.type,
                    bcode: that.filterForm.data.bcode,
                    itemname: that.filterForm.data.itemname,
                    fourgitem: that.filterForm.data.fourgitem
                };
            that.pageNum = pageNum || that.pageNum;
            that.tableLoading = true;
            let url = 'pass/ems/v1/trmelectricitylocations/?pageNum=' + that.pageNum + '&pageSize=' + that.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,
                                id: item.id,
                                workprocid: item.workprocid,
                                location: item.location,
                                type: item.type,
                                state: item.state,
                                note: item.note,
                                createman: item.createman,
                                createtime: item.createtime,
                                updateman: item.updateman,
                                updatetime: item.updatetime,
                                mods: item.mods,
                                itemname: item.itemname,
                                bcode: item.bcode,
                                seqno: item.seqno,
                                workshop: item.workshop,
                                itemid: item.itemid,
                                fourgitem: item.fourgitem
                            })
                        }
                        that.tableData = arr;
                        that.total = res.data.total;
                    } else {
                        that.$message.error(res.message);
                    }
                    that.tableLoading = false;
                }).catch(function () {
                    that.tableLoading = false;
                });
        },
        // 改变表格显示条数
        tableSizeChange (val) {
            let that = this;
            that.pageSize = val;
            that.getTableData(1);
        },
        // 重置搜索
        tableDataCancel () {
            let that = this;
            for (let key in that.filterForm.data) {
                if (that.filterForm.data[key] instanceof Array) {
                    that.filterForm.data[key] = [];
                } else {
                    that.filterForm.data[key] = '';
                }
            }
            that.getTableData(1);
        },
        // 选择添加时的模板
        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.tableData) {
                item.isSelection = false;
            }
            for (let item of val) {
                let obj = {};
                let xId = item.id;
                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_add () {
            let that = this;
            for (let key in that.dialog.editorBox.form.data) {
                that.dialog.editorBox.form.data[key] = '';
            }
            if (that.dialog.editorBox.addRow) {
                for (let key in that.dialog.editorBox.form.data) {
                    if (that.dialog.editorBox.addRow[key] || that.dialog.editorBox.addRow[key] === 0) {
                        that.dialog.editorBox.form.data[key] = that.dialog.editorBox.addRow[key];
                    } else {
                        that.dialog.editorBox.form.data[key] = '';
                    }
                }
            }
            that.dialog.editorBox.type = 'add';
            that.dialog.editorBox.show = true;
            setTimeout(() => {
                that.$refs['dialog_form'].clearValidate();
            }, 100);
        },
        // 批量修改
        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({
                                workprocid: that.tableFormDataObj[key].workprocid,
                                location: that.tableFormDataObj[key].location,
                                type: that.tableFormDataObj[key].type,
                                state: that.tableFormDataObj[key].state,
                                note: that.tableFormDataObj[key].note,
                                itemname: that.tableFormDataObj[key].itemname,
                                bcode: that.tableFormDataObj[key].bcode,
                                seqno: that.tableFormDataObj[key].seqno,
                                mods: that.tableFormDataObj[key].mods,
                                id: that.tableFormDataObj[key].id,
                                workshop: that.tableFormDataObj[key].workshop,
                                itemid: that.tableFormDataObj[key].itemid,
                                fourgitem: that.tableFormDataObj[key].fourgitem
                            });
                        }
                        that.loading = true;
                        that.axios.put('pass/ems/v1/trmelectricitylocations/', SubmitData)
                            .then(function (res) {
                                if (res.code === '0') {
                                    that.$message({
                                        message: '修改成功',
                                        type: 'success'
                                    });
                                    that.getTableData();
                                    that.dialog.editorBox.show = false;
                                } else {
                                    that.$message.error(res.message);
                                }
                                that.loading = false;
                            }).catch(function () {
                                that.loading = false;
                            });
                    } else {
                        let col = Object.keys(obj)[0].split('.')[1];
                        if (col && col === 'bcode') {
                            this.$message.error('请填写电度表编号');
                        } else if (col && col === 'seqno') {
                            this.$message.error('顺序号请填写数字');
                        } else if (col && col === 'mods') {
                            this.$message.error('倍率请填写数字');
                        } else if (col && col === 'itemname') {
                            this.$message.error('请填写计量点名称');
                        } else {
                            this.$message.error('数据不完整或者格式错误');
                        }
                    }
                });
            } else {
                this.$message.error('至少要选择一条数据');
            }
        },
        // 批量删除
        but_del_plural () {
            let that = this;
            if (that.multipleSelection.length > 0) {
                let SubmitData = [];
                for (let item of that.multipleSelection) {
                    SubmitData.push({
                        workprocid: item.workprocid,
                        location: item.location,
                        id: item.id,
                        itemid: item.itemid
                    });
                }
                console.log(SubmitData)
                that.$confirm('是否删除已选数据?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    state: 'warning'
                }).then(() => {
                    that.axios.delete('pass/ems/v1/trmelectricitylocations/', {
                        data: SubmitData,
                        contentType: 'application/json;charset=UTF-8'
                    })
                        .then(function (res) {
                            if (res.code === '0') {
                                that.$message({
                                    message: '删除成功',
                                    type: 'success'
                                });
                                that.getTableData();
                            } else {
                                if (res.code === '51') {
                                }
                                that.$message.error(res.message);
                            }
                        }).catch(function () {
                        });
                }).catch(() => {
                });
            } else {
                this.$message.error('至少要选择一条数据');
            }
        },
        dataSave () {
            let that = this;
            let SubmitData = {
                workprocid: that.dialog.editorBox.form.data.workprocid,
                location: that.dialog.editorBox.form.data.location,
                type: that.dialog.editorBox.form.data.type,
                state: '1',
                note: that.dialog.editorBox.form.data.note,
                itemname: that.dialog.editorBox.form.data.itemname,
                bcode: that.dialog.editorBox.form.data.bcode,
                seqno: that.dialog.editorBox.form.data.seqno,
                mods: that.dialog.editorBox.form.data.mods,
                workshop: that.dialog.editorBox.form.data.workshop,
                fourgitem: that.dialog.editorBox.form.data.fourgitem
            };
            that.$refs['dialog_form'].validate((valid) => {
                if (valid) {
                    if (that.dialog.editorBox.type === 'add') {
                        let foorData = new FormData();
                        for (let key in SubmitData) {
                            foorData.append(key, SubmitData[key]);
                        }
                        that.loading = true;
                        that.axios.post('pass/ems/v1/trmelectricitylocations/', foorData, {
                            headers: {
                                'Content-Type': 'multipart/form-data'
                            }
                        })
                            .then(function (res) {
                                if (res.code === '0') {
                                    that.$message({
                                        message: '新增成功',
                                        type: 'success'
                                    });
                                    that.getTableData();
                                    that.dialog.editorBox.show = false;
                                } else {
                                    that.$message.error(res.message);
                                }
                                that.loading = false;
                            }).catch(function () {
                                that.loading = false;
                            });
                    } else {
                        that.loading = true;
                        that.axios.put('pass/ems/v1/trmelectricitylocations/' + SubmitData.id, SubmitData)
                            .then(function (res) {
                                if (res.code === '0') {
                                    that.$message({
                                        message: '修改成功',
                                        type: 'success'
                                    });
                                    that.getTableData();
                                    that.dialog.editorBox.show = false;
                                } else {
                                    that.$message.error(res.message);
                                }
                                that.loading = false;
                            }).catch(function () {
                                that.loading = false;
                            });
                    }
                }
            });
        },
        but_equationEditing (row) {
            let that = this;
            that.dialog.equationEditing.form.data.clock = that.filterForm.data.version;
            that.dialog.equationEditing.form.data.code = row.id;
            that.dialog.equationEditing.show = true;
        },
        // but_edit (row) {
        //     let that = this;
        //     for (let key in that.dialog.editorBox.form.data) {
        //         if (typeof row[key] !== 'undefined') {
        //             that.dialog.editorBox.form.data[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/trmelectricitylocations/' + row.id)
        //             .then(function (res) {
        //                 if (res.code === '0') {
        //                     that.$message({
        //                         message: '删除成功',
        //                         type: 'success'
        //                     });
        //                     that.getTableData();
        //                 } else {
        //                     that.$message.error(res.message);
        //                 }
        //             }).catch(function () {
        //             });
        //     }).catch(() => {
        //     });
        // },
        keyDown (event1, disabledFlag) {
            let tdTarget = event1.target;
            let _this = this;
            while (tdTarget.tagName !== 'TD') {
                tdTarget = tdTarget.parentElement;
            }
            if (event.keyCode) {
                _this.codeT = event.keyCode;
            }
            // console.log('code', _this.codeT);
            if (disabledFlag && (_this.codeT === 37 || !_this.codeT)) {
                $(tdTarget).prev()[0].click();
                $(tdTarget).prev()[0].find('input').select();
            } else if (disabledFlag && (_this.codeT === 39 || _this.codeT === 13 || !_this.codeT)) {
                $(tdTarget).next()[0].click();
                $(tdTarget).next()[0].find('input').select();
            }
            // 如果按下键盘下键或者回车键
            if (event.keyCode === 40 || event.keyCode === 13) {
                let index = parseInt(this.rowIndex);
                // index++;
                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();
            }
        },
        tableRowClassName ({row, rowIndex}) {
            // 把每一行的索引放进row
            row.index = rowIndex;
        },
        cellClick (row, column, cell, event) {
            let _this = this;
            this.rowIndex = row.index;
            this.columnId = column.id;
            let disabledFlag = false;
            if (!row[column.property] && row[column.property] !== null && row[column.property] !== '') {
                // _this.keyDown(event, true);
            }
            _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();
            });
        }
    }
}
function sortNumber (a, b) {
  return a - b
}
</script>

<style lang="less">
.electricityLocation {
    min-width: 700px;
    height: 100%;
    .box {
        height: 100%;
        padding: 15px 15px 0 15px;
        .box-top {
            .el-form-item {
                margin-bottom: 7px;
            }
            .box-top-gjl {
                overflow: hidden;
                // margin-top: -7px;
                padding: 7px 0;
                border-top: 1px solid #ccc;
                // border-bottom: 1px solid #ccc;
                // margin-bottom: 6px;
            }
        }
        .el-table .el-table__row {
            height: 35px;
        }
        .el-input--suffix .el-input__inner {
            padding: 0 20px 0 8px;
        }
        .el-select .el-input--suffix .el-input__inner {
            padding: 0 25px 0 10px;
        }
        .el-table__body .el-form-item--mini.el-form-item {
            margin: 0px;
            .error {
                overflow: hidden;
                color: #f56c6c;
                font-size: 12px;
                line-height: 1;
            }
        }
    }
    .color_no {
        color: #7a7a7a;
    }
    .color_yes {
        color: #1a9f17;
    }
}
</style>
