<template>
  <el-dialog :visible.sync="crud.dmDeviceTempShow.user" title="选择设备" width="60%">
    <!--用户数据-->
    <el-row>
      <el-col :xs="15" :sm="18" :md="19" :lg="20" :xl="20">
        <!--工具栏-->
        <div class="head-container">
          <!-- 搜索 -->
          <el-input
            v-model="blurry"
            type="text"
            size="small"
            placeholder="输入设备名称/铭牌/维护人/联系方式/备注搜索"
            style="width: 350px;"
            class="filter-item"
          />
          <el-input :value="roomId" style="display: none;" />
          <span>
            <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" @click="getDevices()">搜索</el-button>
            <el-button class="filter-item" size="mini" type="warning" icon="el-icon-refresh-left" @click="resetSearch()">重置</el-button>
          </span>
        </div>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="17">
        <el-table
          ref="multipleTable"
          v-loading="crud.loading"
          :data="devices"
          tooltip-effect="dark"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            type="selection"
            width="55"
          />
          <el-table-column :show-overflow-tooltip="true" prop="deviceId" label="主键" v-if="false" />
          <el-table-column prop="deviceName" label="设备名称" />
          <el-table-column prop="imgPath" label="设备图片" />
          <el-table-column prop="nameplate" label="设备铭牌" />
          <el-table-column prop="room.roomName" label="所属会议室" />
          <el-table-column prop="registerDate" label="登记时间" />
          <el-table-column prop="maintainer" label="维护人" />
          <el-table-column prop="contacts" label="联系方式" />
          <el-table-column prop="remark" label="备注" />
        </el-table>
        <!--分页组件-->
        <el-pagination
          :page-size.sync="size"
          :total="total"
          :current-page.sync="page"
          style="margin-top: 8px;"
          layout="total, prev, pager, next, sizes"
          @size-change="getDevices()"
          @current-change="getDevices()"
        />
      </el-col>
      <el-col :span="6" :push="1">
        <el-card style="height: 100%;">
          <div>您已选择:</div>
          <div>{{ checkInfo }}</div>
        </el-card>
      </el-col>
    </el-row>
    <div slot="footer">
      <el-row>
        <el-col :xs="10" :sm="10" :md="10" :lg="15" :xl="15">
          <el-button style="" class="filter-item" @click="toggleSelection()">取消选择</el-button>
          <el-button class="filter-item" style="margin-left:20% ;" @click="selected()">选择</el-button>
        </el-col>
      </el-row>

    </div>
  </el-dialog>
</template>
<script>
import crudDevice from '@/api/dm/meeting/dmDevice'
import { mapGetters } from 'vuex'
import CRUD, { crud } from '@crud/crud'

export default {
  mixins: [crud()],
  // 数据字典
  // dicts: ['user_status'],
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    roomId: {
      type: String,
      default: '',
      required: false
    }
  },
  cruds() {
    return CRUD({ title: '会议设备', url: 'api/dmDevice', dmDeviceTempBtnShow: {
      user: true
    }, crudMethod: { ...crudDevice }})
  },
  data() {
    return {
      // showIdColumn: false,
      devices: [],
      multipleSelection: crud.selectDataDmDevice || [],
      checkInfo: '',
      blurry: '',
      size: 10,
      page: 0,
      total: 0,
      selectSwitch: false
    }
  },
  computed: {
    ...mapGetters([
      'user'
    ])
  },
  watch: {
    /* 'crud.selectDataDmDevice': {
      handler: function(datainfo) {
        this.$nextTick(function() {
          console.info('监听 crud.selectDataDmDevice || this.devices ==>> ' + JSON.stringify(this.devices))
          console.info('监听 crud.selectDataDmDevice || datainfo ==>> ' + JSON.stringify(datainfo))
          this.toggleSelection()
          this.devices.forEach((item, i) => {
            datainfo.forEach(row => {
              if (this.devices[i].deviceId === row) {
                this.$refs.multipleTable.toggleRowSelection(this.devices[i])
                this.setCheckInfo()
              }
            })
          })
        })
      },
      deep: true
    }, */
    selectSwitch: {
      handler: function() {
        // console.log('selectSwitch || crud.selectDataDmDevice ==>> ' + this.crud.selectDataDmDevice)
        if (this.$refs.multipleTable) {
          this.$nextTick(function() {
            this.toggleSelection()
            this.devices.forEach((item, i) => {
              this.crud.selectDataDmDevice.forEach(row => {
                if (item.deviceId === row) {
                  this.$refs.multipleTable.toggleRowSelection(item)
                  this.setCheckInfo()
                }
              })
            })
          })
        }
      }
    },
    multipleSelection: {
      handler: function() {
        this.setCheckInfo()
      }
    },
    roomId() {
      this.getDevices()
    }
  },
  created() {
    // console.info('params', this.user)
    // console.info('crudDevice ==>> ' + JSON.stringify(crudDevice))
    this.getDevices()
  },
  methods: {
    getDevices() {
      crudDevice.getDevices({ blurry: this.blurry, roomId: this.roomId, page: this.page - 1, size: this.size }).then(res => {
        this.total = res.totalElements
        this.devices = res.content.map(function(obj) {
          return obj
        })
        this.selectSwitch = !this.selectSwitch
      })
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row)
        })
      } else {
        // console.log('this.$refs.multipleTable ==>> ' + this.$refs.multipleTable)
        this.$refs.multipleTable.clearSelection()
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    selected() {
      // console.log(this.$refs.multipleTable.selection)
      this.$emit('on-change', this.$refs.multipleTable.selection, this.crud.openId)
      this.$refs.multipleTable.clearSelection()
      this.crud.doCloseDmDevice()
    },
    setCheckInfo() {
      var checkedDevice = this.$refs.multipleTable.selection
      var info = ''
      checkedDevice.forEach(row => {
        info += row.deviceName + ','
      })
      this.checkInfo = info.substring(0, info.length - 1)
    },
    resetSearch() {
      this.blurry = ''
      this.getDevices()
    }
  }
}
</script>
