<template>
  <div class="app-container">
    <div class="level-title">
      <h1>消防点位检查表</h1>
    </div>
    <div>
      <el-form ref="form" :model="form" :rules="rules" label-width="auto">
        <el-row>
          <el-col :span="10">
            <el-form-item label="消防点位检查表名称：" prop="FIRE_CHECK_STANDARD_NAME">
              <el-input v-model="form.FIRE_CHECK_STANDARD_NAME" placeholder="请输入内容"/>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="检查表类型：" prop="FIRE_CHECK_TYPE">
              <el-select v-model="form.FIRE_CHECK_TYPE" placeholder="请选择" @change = "getCheckTypeName">
                <el-option v-for="item in periodList" :key="item.DICTIONARIES_ID" :label="item.NAME" :value="item.BIANMA"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="5">
            <el-form-item label="负责部门：" prop="DEPARTMENT_ID">
              <SelectTree
                v-if="treeData.length !== 0 && FIRE_CHECK_ID != ''"
                ref="deptTree_Select"
                :clearable="false"
                :options="treeData"
                :props="defaultProps"
                :key="form.UPDATE_DEPARTMENT_ID"
                v-model="form.UPDATE_DEPARTMENT_ID"
                placeholder="请选择部门"
                @change="updateDeptChange"/>
              <Treeselect
                v-if="updateValue && FIRE_CHECK_ID == ''"
                :multiple="true"
                :flat="true"
                :default-expand-level="1"
                :options="treeData"
                :normalizer="normalizer"
                :disable="true"
                :clearable="false"
                v-model="form.DEPARTMENT_ID"
                placeholder="请选择部门"
                no-options-text="暂无数据"
                no-children-text="暂无数据"
                style="width: 100%;"
                @select="handleSelect"
                @deselect="handleRemove"
              />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="检查人：" prop="USER_ID">
              <el-select
                v-if="FIRE_CHECK_ID == ''"
                ref="userTree_Select"
                :key="userListChangeValue"
                v-model="form.USER_ID"
                multiple
                clearable
                placeholder="请选择人员"
                class="filter-item">
                <el-option v-for="item in peopleList" :key="item.USER_ID" :label="item.NAME" :value="item.USER_ID"/>
              </el-select>
              <el-select
                v-if="FIRE_CHECK_ID != ''"
                ref="userTree_Select"
                :key="form.UPDATE_USER_ID"
                v-model="form.UPDATE_USER_ID"
                clearable
                placeholder="请选择人员"
                class="filter-item">
                <el-option v-for="item in peopleList" :key="item.USER_ID" :label="item.NAME" :value="item.USER_ID"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="level-line"/>
    <div class="level-title">
      <h1>检查器材</h1>
      <!--      <div class="level-btns">-->
      <!--        <el-button type="danger" size="mini" @click="removeSelectHandel">-->
      <!--          批量移除消防设备-->
      <!--        </el-button>-->
      <!--        <el-button type="primary" size="mini" @click="showRisk">-->
      <!--          添加消防设备-->
      <!--        </el-button>-->
      <!--      </div>-->
    </div>
    <div>
      <div class="rightCont">
        <el-form label-width="100px">
          <el-row :gutter="20">
            <el-col :span="4">
              <el-form-item label="消防区域" label-width="110px">
                <el-select v-model="deviceCriteria.FIRE_REGION_NAME" placeholder="请选择">
                  <el-option v-for="item in FILTER_LIST_REGION" :key="item" :label="item" :value="item" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="消防点位" label-width="110px">
                <el-select v-model="deviceCriteria.FIRE_POINT_NAME" placeholder="请选择">
                  <el-option v-for="item in FILTER_LIST_POINT" :key="item" :label="item" :value="item" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item>
                <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="searchCheckList">
                  搜索
                </el-button>
                <el-button v-waves class="filter-item" type="success" icon="el-icon-refresh" @click="resetCheckSearch">
                  重置
                </el-button>
                <el-button v-waves class="filter-item" type="danger" icon="el-icon-delete-solid" @click="removeSelectHandel">
                  批量移除消防设备
                </el-button>
                <el-button v-waves class="filter-item" type="primary" icon="el-icon-circle-plus" @click="showRisk">
                  添加其他消防设备
                </el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div>
          <el-table ref="multipleTable" :data="checkList" border style="width: 100%">
            <el-table-column
              type="selection"
              width="55"/>
            <el-table-column type="index" label="序号" width="55" align="center"/>
            <el-table-column prop="FIRE_REGION_NAME" label="消防区域"/>
            <el-table-column prop="FIRE_POINT_NAME" label="消防点位"/>
            <el-table-column prop="FIRE_POINT_CODE" label="点位编号"/>
            <el-table-column prop="FIRE_DEVICE_CODE" label="设备编号"/>
            <el-table-column prop="FIRE_DEVICE_TYPE_NAME" label="设备类型"/>
            <el-table-column prop="DEPARTMENTNAME" label="点位负责部门"/>
            <el-table-column prop="POSTNAME" label="岗位"/>
            <el-table-column prop="USERNAME" label="点位责任人"/>
            <el-table-column label="操作" width="350">
              <template slot-scope="{row}">
                <el-button type="success" icon="el-icon-success" @click="handleInfo(row)">查看检查项</el-button>
                <el-button type="danger" icon="el-icon-success" @click="handleDelete(row)">移除此设备</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
    </div>
    <div class="ui-height"/>
    <div class="ui-foot">
      <el-button v-if="FIRE_CHECK_ID == ''" type="success" @click="save">保 存</el-button>
      <el-button v-else type="success" @click="edit">修 改</el-button>
      <el-button plain type="info" @click="back">返 回</el-button>
    </div>
    <!--检查项数组-->
    <el-dialog :visible.sync="checkShow" title="检查项列表" width="80%">
      <div>
        <el-table
          ref="multipleTableRisk"
          :data="csList"
          border
          tooltip-effect="dark"
          style="width: 100%;">
          <el-table-column type="index" label="序号" width="55" align="center"/>
          <el-table-column prop="FIRE_CHECK_STANDARD_ITEM" label="检查项"/>
        </el-table>
      </div>
      <div class="page-btn-group">
        <div>
          <el-button plain type="info" @click="closAddOtherPoint">返 回</el-button>
        </div>
      </div>
    </el-dialog>
    <!--选择设备与检查标准-->
    <el-dialog :visible.sync="dialogTableVisible" title="添加消防设备" width="80%">
      <div class="filter-container">
        <el-form label-width="10px">
          <el-row>
            <el-col :span="4">
              <el-form-item label="消防区域" label-width="80px">
                <el-select
                  v-model="waitFor.FIRE_REGION_ID"
                  placeholder="请选择"
                  class="filter-item"
                  @change="changeRegionName">
                  <el-option
                    v-for="item in varRegionList"
                    :key="item.FIRE_REGION_ID"
                    :label="item.FIRE_REGION_NAME"
                    :value="item.FIRE_REGION_ID"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="消防点位" label-width="80px">
                <el-select
                  v-model="waitFor.FIRE_POINT_ID"
                  placeholder="请选择点位"
                  class="filter-item"
                  @change="changePointName">
                  <el-option
                    v-for="item in pointList"
                    :key="item.FIRE_POINT_ID"
                    :label="item.FIRE_POINT_NAME"
                    :value="item.FIRE_POINT_ID"/>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="设备编号" label-width="80px">
                <el-input
                  v-model="waitFor.FIRE_DEVICE_CODE"
                  placeholder="请输入"
                />
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="消防设备类型" label-width="100px">
                <el-select
                  v-model="waitFor.FIRE_DEVICE_TYPE_ID"
                  placeholder="消防设备类型"
                  class="filter-item"
                  @change="changeDeviceCode">
                  <el-option
                    v-for="item in deviceTypeList"
                    :key="item.NAME"
                    :label="item.NAME"
                    :value="item.BIANMA"/>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="4" label-width="80px">
              <el-form-item>
                <el-button v-waves class="filter-item" type="success" icon="el-icon-refresh" @click="getDevice">
                  查询设备
                </el-button>
                <el-button v-waves class="filter-item" type="success" icon="el-icon-refresh" @click="getReset">
                  重置
                </el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <el-table
        ref="multipleTableRisk"
        :data="tempList"
        border
        tooltip-effect="dark"
        style="width: 100%;">
        <el-table-column :reserve-selection="true" type="selection" width="55" align="center"/>
        <el-table-column type="index" label="序号" width="55" align="center"/>
        <el-table-column prop="FIRE_REGION_NAME" label="消防区域"/>
        <el-table-column prop="FIRE_POINT_NAME" label="消防点位"/>
        <el-table-column prop="FIRE_POINT_CODE" label="点位编号"/>
        <el-table-column prop="FIRE_DEVICE_CODE" label="设备编号"/>
        <el-table-column prop="DEPARTMENTNAME" label="部门"/>
        <el-table-column prop="POSTNAME" label="岗位"/>
        <el-table-column prop="USERNAME" label="责任人"/>
      </el-table>
      <div class="page-btn-group">
        <div/>
        <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getDevice"/>
      </div>
      <template slot="footer">
        <el-button type="success" @click="checkItem">保 存</el-button>
        <el-button plain type="info" @click="closAddOtherPoint">返 回</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination' // 通过 el-pagination二次打包
import { requestFN } from '@/utils/request'
import SelectTree from '@/components/SelectTree'
import waves from '@/directive/waves' // waves directive
export default {

  components: { Pagination, SelectTree },
  directives: { waves },
  data() {
    return {
      FILTER_LIST_REGION: [],
      FILTER_LIST_POINT: [],
      deviceCriteria: {
        FIRE_REGION_NAME: '',
        FIRE_POINT_NAME: ''
      },
      FIRE_CHECK_ID: '',
      dialogFormVisible: false,
      dialogTableVisible: false,
      riskForm: {
        RISKPOINT_ID: '',
        DEPARTMENT_ID: '',
        USER_ID: '',
        RISK_UNIT_ID: '',
        IDENTIFICATION_ID: '',
        RISK_DESCR: '',
        LEVELID: '',
        MEASURES: '',
        ACCIDENTS: '',
        ACCIDENTS_NAME: '',
        CHECK_CONTENT: '',
        EME_MEASURES: ''
      },
      riskRules: {
        DEPARTMENT_ID: [{ required: true, message: '部门不能为空', trigger: 'blur' }]
      },
      unitList: [],
      LISTMANAGER_ID: '',	// 主键ID
      KEYWORDS: '',
      msg: 'add',
      periodList: [],
      typeList: [],
      postList: [],
      userList: [],
      screenTypeList: [],
      defaultNodes: {},
      DEPARTMENT_ID: '',
      varList: [],
      otherIds: [],
      listItem: [],
      START_DATE: '',
      END_DATE: '',
      USER_IDS: '', // 负责人合集
      checked: false, // 全选框
      checkList: [], // 检查项
      waitfordel: [], // 待删除的检查项id
      waitfordelDeviceIds: [], // 待删除的检查项id
      checkListBackUp: [], // 检查项备份数组
      otherItemList: [], // 选中的其他存在风险集合
      treeData: [],
      peopleList: [],
      partsList: [],
      dates: [], // 时间
      defaultProps: {
        value: 'id',
        children: 'nodes',
        label: 'name'
      },
      formLabelWidth: '120px',
      OTHERLEVELID: '',
      OTHERDEPARTMENT_ID: '',
      OTHERKEYWORDS: '',
      levelList: [],
      riskList: [],
      // myself------------------
      checkShow: false,
      // 检查表类型 0日检1月检
      checkType: [
        { value: '0', label: '日检' },
        { value: '1', label: '月检' }
      ],
      form: {
        FIRE_CHECK_STANDARD_NAME: '',
        FIRE_CHECK_TYPE: '',
        POST_ID: [],
        FIRE_CHECK_TYPE_NAME: '',
        DEPARTMENT_ID: [],
        UPDATE_DEPARTMENT_ID: '',
        USER_ID: [],
        UPDATE_USER_ID: '',
        DATA: ''
      },
      rules: {
        FIRE_CHECK_STANDARD_NAME: [{ required: true, message: '清单名称不能为空', trigger: 'blur' }],
        DEPARTMENT_ID: [{ required: true, message: '部门不能为空', trigger: 'blur' }],
        POST_ID: [{ required: true, message: '岗位不能为空', trigger: 'blur' }],
        USER_ID: [{ required: true, message: '人员不能为空', trigger: 'blur' }],
        FIRE_CHECK_TYPE: [{ required: true, message: '清单类型不能为空', trigger: 'blur' }]
      },
      varRegionList: '',
      // 检查标准数组
      csList: [],
      // 检查项的id
      idListFcS: [],
      tableList: [],
      tempList: [],
      // 添加其他点位准备压入 展示页的数据
      waitList: [],
      tempRow: '',
      tempidListFcS: '',
      typeData: [],
      deviceTypeList: [],
      // 弹窗的点位等信息
      waitFor: {
        FIRE_REGION_ID: '',
        FIRE_REGION_NAME: '',
        FIRE_DEVICE_ID: '',
        FIRE_DEVICE_TYPE_ID: '',
        FIRE_DEVICE_CODE: '',
        FIRE_DEVICE_TYPE: '',
        FIRE_POINT_ID: '',
        FIRE_POINT_NAME: '',
        FIRE_POINT_CODE: '',
        DEPARTMENTNAME: '',
        POSTNAME: '',
        USERNAME: '',
        varList: [],
        idListFcS: []
      },
      pointList: [],
      pointCode: '',
      // ------------end
      total: 0,
      listQuery: {
        page: 1,
        limit: 20
      },
      multipleSelectionAll: [], // 所有选中的数据包含跨页数据
      multipleSelection: [], // 当前页选中的数据
      accidentList: [],
      updateValue: true,
      normalizer(node) {
        return {
          id: node.id,
          label: node.name,
          children: node.nodes,
          isDisabled: node.pId === '0'
        }
      },
      userListChangeValue: 0
    }
  },
  watch: {
    // 根据消防设备 查 检查列表
    'waitFor.FIRE_DEVICE_ID': {
      handler(newVal, oldVal) {
        if (newVal !== '') this.changeCheckItem(newVal)
      }, immediate: false
    },
    'form.USER_ID': {
      handler(newVal, oldVal) {

      }, immediate: false
    },
    'waitFor.FIRE_REGION_ID': {
      handler(newVal, oldVal) {
        if (newVal !== '') this.waitFor.FIRE_POINT_ID = ''
        if (newVal !== '') this.getPointByRegionId(newVal)
      }, immediate: false
    },
    'waitFor.FIRE_POINT_ID': {
      handler(newVal, oldVal) {
        if (newVal !== '') this.getDeviceByPointId(newVal)
      }, immediate: false
    },
    // 'form.POST_ID': {
    //   handler(newVal, oldVal) {
    //     this.peopleList = []
    //     this.form.USER_ID = []
    //     if (this.form.POST_ID != '') this.getUserList()
    //   }
    // },
    'form.DEPARTMENT_ID': {
      // 选择了负责部门 查询相关的点检
      handler(newVal, oldVal) {
        this.peopleList = []
        // if (newVal !== '') this.form.POST_ID = ''
        if (newVal.length !== 0) this.getList(newVal)
        if (newVal.length !== 0) this.getCheckList(newVal)
        // if (newVal !== '') this.getPostList(newVal)
        if (newVal.length !== 0) this.getUserList()
      },
      immediate: false
    },
    'form.UPDATE_DEPARTMENT_ID': {
      // 选择了负责部门 查询相关的点检
      handler(newVal, oldVal) {
        this.peopleList = []
        // if (newVal !== '') this.form.POST_ID = ''
        if (newVal.length !== 0) this.getList(newVal)
        if (newVal.length !== 0) this.getCheckList(newVal)
        // if (newVal !== '') this.getPostList(newVal)
        if (newVal.length !== 0) this.getUserList()
      },
      immediate: false
    }
  },
  async created() {
    this.getDict()
    await this.getTreeData()
    this.getFireRegion()
    if (this.$parent.FIRE_CHECK_ID) {
      // 修改的
      this.FIRE_CHECK_ID = this.$parent.FIRE_CHECK_ID
      this.getData(this.$parent.FIRE_CHECK_ID)
    }
  },
  methods: {
    getUserList() {
      let DEPARTMENT_ID = ''
      if (this.FIRE_CHECK_ID != '') {
        DEPARTMENT_ID = this.form.UPDATE_DEPARTMENT_ID
      } else {
        DEPARTMENT_ID = this.form.DEPARTMENT_ID.toString()
      }
      requestFN(
        '/user/listAll',
        {
          DEPARTMENT_ID: DEPARTMENT_ID
          // DEPARTMENT_ID: this.form.DEPARTMENT_ID,
          // POST_ID: this.form.POST_ID
        }
      ).then((data) => {
        this.peopleList = data.userList
      }).catch((e) => {
      })
    },
    // 岗位
    getPostList() {
      requestFN(
        '/post/listAll',
        { DEPARTMENT_ID: this.form.DEPARTMENT_ID }
      ).then((data) => {
        this.postList = data.postList
      }).catch((e) => {
      })
    },
    getDevice() {
      requestFN(
        '/fireDevice/list?showCount=' + this.listQuery.limit + '&currentPage=' + this.listQuery.page,
        {
          FIRE_DEVICE_TYPE_ID: this.waitFor.FIRE_DEVICE_TYPE_ID,
          FIRE_POINT_ID: this.waitFor.FIRE_POINT_ID,
          FIRE_DEVICE_CODE: this.waitFor.FIRE_DEVICE_CODE,
          FIRE_REGION_ID: this.waitFor.FIRE_REGION_ID
        }
      ).then((data) => {
        this.listLoading = false
        this.tempList = data.varList
        this.total = data.page.totalResult
      }).catch((e) => {
        this.listLoading = false
      })
    },
    getCheckTypeName(value) {
      this.periodList.find((item) => {
        if (item.DICTIONARIES_ID === value) {
          this.form.FIRE_CHECK_TYPE_NAME = item.NAME
        }
      })
    },
    // 根据设备id 获取 检查项
    getDeviceCheckItem(FIRE_DEVICE_ID, FIRE_DEVICE_TYPE_ID) {
      requestFN(
        '/fireCheckStandard/getStandardListByDeviceId',
        {
          FIRE_DEVICE_ID: FIRE_DEVICE_ID,
          FIRE_DEVICE_TYPE_ID: FIRE_DEVICE_TYPE_ID
        }
      ).then((data) => {
        this.csList = data.varList
        this.total = data.page.totalResult
      }).catch((e) => {
      })
    },
    // 点击添加其他的 器材
    changeCheckItem(newVal) {
      requestFN(
        '/fireCheckStandard/list',
        {
          FIRE_DEVICE_ID: newVal
        }
      ).then((data) => {
        this.tempList = data.varList
        this.waitList = data.waitList
        this.total = data.page.totalResult
      }).catch((e) => {
      })
    },
    // 部门列表树
    async getTreeData() {
      return new Promise((resolve, reject) => {
        requestFN(
          '/department/listTree',
          {}
        ).then((data) => {
          this.treeData = JSON.parse(data.zTreeNodes)
          this.treeData.forEach((item) => {
            item.isDisabled = true
          })
          resolve()
        }).catch((e) => {
        })
      })
    },
    getList(DEPARTMENT_ID) {
      // debugger
      this.listLoading = true
      if (DEPARTMENT_ID) {
        this.DEPARTMENT_ID = DEPARTMENT_ID
      }
      requestFN(
        '/user/getDepartmentId',
        {
          KEYWORDS: this.KEYWORDS,
          DEPARTMENT_ID: this.DEPARTMENT_ID.toString()
        }
      ).then((data) => {
        this.listLoading = false
        // this.peopleList = data.userList
        this.total = data.page.totalResult
      }).catch((e) => {
        this.listLoading = false
      })
    },
    // 删除单个检查项 未用
    handleCheckDelete(row) {
      this.tempRow.varList = this.tempList.filter(o => o.FIRE_CHECK_STANDARD_ID !== row.FIRE_CHECK_STANDARD_ID)
      this.tempList = this.tempList.filter(o => o.FIRE_CHECK_STANDARD_ID !== row.FIRE_CHECK_STANDARD_ID)
      this.tempRow.idListFcS = this.tempidListFcS.filter(o => o !== row.FIRE_CHECK_STANDARD_ID)
      this.tempidListFcS = this.tempidListFcS.filter(o => o !== row.FIRE_CHECK_STANDARD_ID)
    },
    // 获取消防区域名字
    changeRegionName(value) {
      this.varRegionList.find((item) => {
        if (item.FIRE_REGION_ID === value) {
          this.waitFor.FIRE_REGION_NAME = item.FIRE_REGION_NAME
        }
      })
    },
    // 获取点位名字
    changePointName(value) {
      this.pointList.find((item) => {
        if (item.FIRE_POINT_ID === value) {
          this.waitFor.FIRE_POINT_NAME = item.FIRE_POINT_NAME
          this.waitFor.FIRE_POINT_ID = item.FIRE_POINT_ID
          this.waitFor.FIRE_POINT_CODE = this.pointCode
        }
      })
    },
    // 获取消防设备
    changeDeviceCode(value) {
      this.pointList.find((item) => {
        if (item.FIRE_DEVICE_ID === value) {
          this.waitFor.FIRE_DEVICE_ID = item.FIRE_DEVICE_ID
          this.waitFor.FIRE_DEVICE_CODE = item.FIRE_DEVICE_CODE
        }
      })
    },
    // 查看当前的检查项
    handleInfo(row) {
      this.checkShow = true
      this.getDeviceCheckItem(row.FIRE_DEVICE_ID, row.FIRE_DEVICE_TYPE_ID)
    },
    // 删除单个行
    handleDelete(row) {
      this.$confirm('确定要移除吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        if (row.FIRE_PREPARED_ID) this.waitfordel.push(row.FIRE_PREPARED_ID)
        if (row.FIRE_DEVICE_ID) this.waitfordelDeviceIds.push(row.FIRE_DEVICE_ID)
        this.checkList = this.checkList.filter(o => o.FIRE_DEVICE_ID !== row.FIRE_DEVICE_ID)
        this.checkListBackUp = this.checkListBackUp.filter(o => o.FIRE_DEVICE_ID !== row.FIRE_DEVICE_ID)
        this.$message.success('移除成功')
        this.listLoading = false
      })
    },
    // 获取所有的检查标准
    /*
    getCheckStandard() {
      requestFN(
        '/fireCheckStandard/list?showCount=' + this.listQuery.limit + '&currentPage=' + this.listQuery.page,
        {}
      ).then((data) => {
        this.listLoading = false
        this.csList = data.varList
        this.total = data.page.totalResult
        this.hasButton()
      }).catch((e) => {
        this.listLoading = false
      })
    },*/
    // 获取点位下所有设备
    getDeviceByPointId(POINT_ID) {
      requestFN(
        '/fireDevice/getDeviceByPointId',
        { FIRE_POINT_ID: POINT_ID }
      ).then((data) => {
        this.pointList = data.varList
        this.pointCode = data.pointCode
      }).catch((e) => {
      })
    },
    getCheckList(DEPARTMENT_ID) {
      if (!this.FIRE_CHECK_ID) {
        requestFN(
          '/fireCheckList/getCheckList',
          { DEPARTMENT_ID: DEPARTMENT_ID.toString() }
        ).then((data) => {
          this.checkList = data.varList
          this.checkListBackUp = data.varList
          var r = new Set()
          var p = new Set()
          data.varList.forEach(item => {
            if (!r.has(item.FIRE_REGION_NAME)) {
              r.add(item.FIRE_REGION_NAME)
            }
            if (!p.has(item.FIRE_POINT_NAME)) {
              p.add(item.FIRE_POINT_NAME)
            }
          })
          this.FILTER_LIST_REGION = r
          this.FILTER_LIST_POINT = p
        }).catch((e) => {

        })
      }
    },
    // 获取所有区域下的点位
    getPointByRegionId(REGION_ID) {
      requestFN(
        '/fireregion/getAllPointName',
        { FIRE_REGION_ID: REGION_ID }
      ).then((data) => {
        this.pointList = data.varList
      }).catch((e) => {
      })
    },
    save() {
      if (this.checkList == null || this.checkList.length === 0) {
        this.$message({
          message: '请选择检查项...',
          type: 'error'
        })
        return false
      }
      this.$refs.form.validate(valid => {
        // debugger
        // 库里已有数据 所以对改数据修改
        if (valid) {
          this.listLoading = true
          this.form.DATA = this.checkList
          requestFN(
            '/fireCheckList/saveInfo',
            {
              from: JSON.stringify(this.form)
            }
          ).then((data) => {
            this.listLoading = false
            this.dialogFormEdit = false
            this.$parent.activeName = 'List'
          }).catch((e) => {
            this.listLoading = false
          })
        } else {
          this.form.DATA = this.tableList
          return false
        }
      })
    },
    edit() {
      this.$confirm('确定要修改吗?当前排查的清单将会变更，但已排查的记录不会变更。', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        if (this.checkList == null || this.checkList.length === 0) {
          this.$message({
            message: '请选择检查项...',
            type: 'error'
          })
          return false
        }
        this.$refs.form.validate(valid => {
          // debugger
          // 库里已有数据 所以对改数据修改
          if (valid) {
            this.listLoading = true
            this.form.DATA = this.checkList
            requestFN(
              // 修改人 会添加很多表
              // '/fireCheckList/updById',
              // {
              //   DATA_IDS: this.FIRE_CHECK_ID,
              //   from: JSON.stringify(this.form)
              // }
              '/fireCheckList/upd4PcById',
              {
                FIRE_CHECK_ID: this.FIRE_CHECK_ID,
                from: JSON.stringify(this.form),
                DEVICE_IDS: this.waitfordelDeviceIds.join(','),
                DEVICE_PREPARED_IDS: this.waitfordel.join(',')
              }
            ).then((data) => {
              this.listLoading = false
              this.dialogFormEdit = false
              this.$parent.activeName = 'List'
              this.$message.success('修改成功')
            }).catch((e) => {
              this.listLoading = false
            })
          } else {
            this.form.DATA = this.tableList
            return false
          }
        })
      })
    },
    // 获取消防区域列表
    getFireRegion() {
      requestFN(
        '/fireregion/getAllRegionName',
        {}
      ).then((data) => {
        this.varRegionList = data.varList
      }).catch((e) => {
      })
    },
    // 风险项搜索条件重置
    getReset() {
      this.waitFor.FIRE_REGION_ID = ''
      this.waitFor.FIRE_POINT_ID = ''
      this.waitFor.FIRE_CHECK_TYPE = ''
      this.waitFor.FIRE_CHECK_TYPE_ID = ''
      this.waitFor.FIRE_DEVICE_TYPE_ID = ''
      this.waitFor.FIRE_DEVICE_ID = ''
      this.waitFor.FIRE_DEVICE_CODE = ''
      this.waitFor.FIRE_POINT_NAME = ''
      this.pointList = []
      this.getList()
      this.getDevice()
    },
    // 选择检查项
    checkItem() {
      // 跨页保存
      const _selectData = this.$refs.multipleTableRisk.selection
      if (_selectData == null || _selectData.length == 0) {
        this.$message({
          message: '请选中要保存的项...',
          type: 'error'
        })
        return false
      }
      _selectData.map((item, index) => {
        var flag = true
        this.checkList.forEach(cl => {
          if (cl.FIRE_DEVICE_ID === item.FIRE_DEVICE_ID) {
            flag = false
          }
        })
        if (flag) {
          this.checkList.push(item)
          var p = new Set(this.FILTER_LIST_POINT)
          var r = new Set(this.FILTER_LIST_REGION)

          if (!p.has(item.FIRE_POINT_NAME)) {
            p.add(item.FIRE_POINT_NAME)
            this.FILTER_LIST_POINT = p
          }
          if (!r.has(item.FIRE_REGION_NAME)) {
            r.add(item.FIRE_REGION_NAME)
            this.FILTER_LIST_REGION = r
          }
          // this.checkListBackUp.push(item)
        }
        // this.checkList = this.checkList.filter(cl => cl.FIRE_DEVICE_ID === item.FIRE_DEVICE_ID)
      })
      // 清除选中项 this.$refs.multipleTable.clearSelection()
      // 校验等待添加的 集合不为null
      // if (this.waitFor.FIRE_DEVICE_ID !== '') {
      //   this.checkList = this.checkList.filter(cl => cl.FIRE_DEVICE_ID !== this.waitList[0].FIRE_DEVICE_ID)
      //   this.checkList.push(this.waitList[0])
      // } else {
      //   this.$message({ message: '请选择设备', type: 'error' })
      //   return
      // }
      // this.$message({ message: '添加成功', type: 'success' })
      this.resetWaitFor()
      this.closAddOtherPoint()
    },
    // 清除缓存
    resetWaitFor() {
      this.waitFor.FIRE_REGION_ID = ''
      this.waitFor.FIRE_REGION_NAME = ''
      this.waitFor.FIRE_CHECK_STANDARD_NAME = ''
      this.waitFor.FIRE_DEVICE_ID = ''
      this.waitFor.FIRE_DEVICE_CODE = ''
      this.waitFor.FIRE_CHECK_TYPE = ''
      this.waitFor.FIRE_POINT_ID = ''
      this.waitFor.FIRE_POINT_NAME = ''
      this.waitFor.FIRE_POINT_CODE = ''
      this.waitList = []
      this.waitFor.varList = []
      this.waitFor.idListFcS = []
    },
    searchCheckList() {
      if (this.deviceCriteria.FIRE_DEVICE_CODE) {
        this.checkList = this.checkList.filter(item => item.FIRE_DEVICE_CODE.search(this.deviceCriteria.FIRE_DEVICE_CODE) !== -1)
      }
      if (this.deviceCriteria.FIRE_POINT_CODE) {
        this.checkList = this.checkList.filter(item => item.FIRE_POINT_CODE.search(this.deviceCriteria.FIRE_POINT_CODE) !== -1)
      }
      if (this.deviceCriteria.FIRE_POINT_NAME) {
        this.checkList = this.checkList.filter(item => item.FIRE_POINT_NAME === this.deviceCriteria.FIRE_POINT_NAME)
      }
      if (this.deviceCriteria.FIRE_REGION_NAME) {
        this.checkList = this.checkList.filter(item => item.FIRE_REGION_NAME.search(this.deviceCriteria.FIRE_REGION_NAME) !== -1)
      }
    },
    resetCheckSearch() {
      this.checkList = this.checkListBackUp
      this.deviceCriteria = {}
    },
    // 批量移除检查项
    removeSelectHandel() {
      this.$confirm('确定要移除吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const selectData = this.$refs.multipleTable.selection
        if (!selectData.length > 0) {
          this.$message.warning('请选择要移除的设备')
          return
        }
        selectData.forEach(item => {
          if (item.FIRE_PREPARED_ID) {
            this.waitfordel.push(item.FIRE_PREPARED_ID)
          }
          this.waitfordelDeviceIds.push(item.FIRE_DEVICE_ID)
        })
        var set1 = new Set(this.checkList)
        var set2 = new Set(selectData)
        var set3 = new Set(this.checkListBackUp)
        var subset = []
        var subsetBack = []
        for (const item of set1) {
          if (!set2.has(item)) {
            subset.push(item)
          }
        }
        for (const item of set3) {
          if (!set2.has(item)) {
            subsetBack.push(item)
          }
        }
        this.checkList = subset
        this.checkListBackUp = subsetBack
        this.$message.success('移除成功')
      })
    },
    // 添加消防设备与检查内容
    showRisk() {
      if (this.form.USER_ID === '') {
        this.$message({ message: '请填写表头', type: 'error' })
        return
      }
      this.resetWaitFor()
      this.getDevice()
      this.$refs.multipleTableRisk && this.$refs.multipleTableRisk.clearSelection()
      this.dialogTableVisible = true
      // 重置表单
    },
    // 关闭选择其他风险
    closAddOtherPoint() {
      this.dialogTableVisible = false
      this.checkShow = false
    },
    // 返回列表
    back() {
      this.$parent.activeName = 'List'
    },
    getData(FIRE_CHECK_ID) {
      requestFN(
        '/fireCheckList/getInfo4Edit',
        {
          FIRE_CHECK_ID: FIRE_CHECK_ID
        }
      ).then((data) => {
        this.form.FIRE_CHECK_STANDARD_NAME = data.checkListInfo.FIRE_CHECK_STANDARD_NAME
        this.form.FIRE_CHECK_TYPE = data.checkListInfo.FIRE_CHECK_TYPE
        // this.form.DEPARTMENT_ID = data.checkListInfo.DEPARTMENT_ID
        this.form.DEPARTMENT_ID.push(data.checkListInfo.DEPARTMENT_ID)
        this.form.UPDATE_DEPARTMENT_ID = data.checkListInfo.DEPARTMENT_ID
        this.$nextTick(() => {
          // this.form.POST_ID = data.checkListInfo.POST_ID
          this.userList = []
          this.getUserList()
          const u = []
          u.push(data.checkListInfo.USER_ID)
          this.form.UPDATE_USER_ID = data.checkListInfo.USER_ID
          this.$nextTick(() => {
            this.form.USER_ID = u
            this.checkListBackUp = data.checkStandardList
            this.checkList = data.checkStandardList
            var r = new Set()
            var p = new Set()
            data.checkStandardList.forEach(item => {
              if (!r.has(item.FIRE_REGION_NAME)) {
                r.add(item.FIRE_REGION_NAME)
              }
              if (!p.has(item.FIRE_POINT_NAME)) {
                p.add(item.FIRE_POINT_NAME)
              }
            })
            this.FILTER_LIST_REGION = r
            this.FILTER_LIST_POINT = p
          })
        })
      })
    },
    // 数据字典
    getDict() {
      requestFN(
        '/dictionaries/getLevels',
        {
          DICTIONARIES_ID: 'f60cf0e8315b4993b6d6049dd29f2ba5'
        }
      ).then((data) => {
        this.periodList = data.list
      })
      requestFN(
        'dictionaries/getLevels',
        {
          DICTIONARIES_ID: '249151f04fd64132a949fdd430a7b9c5'
        }
      ).then((data) => {
        this.deviceTypeList = data.list
      })
    },
    onClick() {
      this.updateValue = false
      this.$nextTick(() => {
        this.updateValue = true
      })
    },
    // 选择项时触发的事件
    handleSelect(selectedItems) {
      // 选择父节点时同时勾选全部子节点
      if (!this.form.DEPARTMENT_ID.includes(selectedItems.id)) {
        this.form.DEPARTMENT_ID.push(selectedItems.id)
      }
      this.pullSelectedNodes(selectedItems)
      this.onClick()
    },
    // 将树节点的子节点全部添加到数组中
    pullSelectedNodes(selectedItems) {
      if (selectedItems.nodes && selectedItems.nodes.length > 0) {
        for (let i = 0; i < selectedItems.nodes.length; i++) {
          if (!this.form.DEPARTMENT_ID.includes(selectedItems.nodes[i].id)) {
            this.form.DEPARTMENT_ID.push(selectedItems.nodes[i].id)
          }
          this.pullSelectedNodes(selectedItems.nodes[i])
        }
      }
    },
    // 移除选中项时触发的事件
    handleRemove(selectedItems) {
      this.form.USER_ID = []
      // 移除选中项时同时移除全部子节点
      if (this.form.INVOLVING_CORPS_DEPART_ID.includes(selectedItems.id)) {
        this.form.INVOLVING_CORPS_DEPART_ID.splice(this.form.INVOLVING_CORPS_DEPART_ID.indexOf(selectedItems.id), 1)
      }
      this.removeSelectedNodes(selectedItems)
      this.onClick()
    },
    // 将选中项的子节点全部移除
    removeSelectedNodes(selectedItems) {
      if (selectedItems.nodes && selectedItems.nodes.length > 0) {
        for (let i = 0; i < selectedItems.nodes.length; i++) {
          if (this.form.INVOLVING_CORPS_DEPART_ID.includes(selectedItems.nodes[i].id)) {
            this.form.INVOLVING_CORPS_DEPART_ID.splice(this.form.INVOLVING_CORPS_DEPART_ID.indexOf(selectedItems.nodes[i].id), 1)
          }
          this.removeSelectedNodes(selectedItems.nodes[i])
        }
      }
    },
    updateDeptChange() {
      this.form.UPDATE_USER_ID = ''
    }
  }
}
</script>

<style scoped lang="scss">
.level-search {
  align-items: center;
}
</style>
