<template>
  <el-dialog :visible.sync="crud.tempShow.device" 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="code"
            type="text"
            size="small"
            placeholder="输入设备编号"
            style="width: 200px;"
            class="filter-item"
          />
          <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="crud.resetQuery()">重置</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="id" label="id" />
          <el-table-column :show-overflow-tooltip="true" prop="code" label="设备编号" />
          <el-table-column :show-overflow-tooltip="true" prop="name" label="设备名称" />
          <el-table-column prop="floor.floorName" label="设备安装位置" />
          <el-table-column prop="unPrintingCount" 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/device/dmDevice'
import { mapGetters } from 'vuex'
import CRUD, { crud } from '@crud/crud'

export default {
  mixins: [crud()],
  // 数据字典
  dicts: ['user_status'],
  cruds() {
    return CRUD({ title: '设备', url: 'api/dmDevice', tempBtnShow: {
      device: true
    }, crudMethod: { ...crudDevice }})
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      devices: [],
      multipleSelection: crud.selectData || [],
      checkInfo: '',
      code: '',
      size: 0,
      page: 0,
      total: 0

    }
  },
  computed: {
    ...mapGetters([
      'device'
    ])
  },
  watch: {
    'crud.selectData': {
      handler: function(datainfo) {
        this.$nextTick(function() {
          console.info('crud.selectData', crud.selectData)
          this.toggleSelection()
          this.devices.forEach((item, i) => {
            datainfo.forEach(row => {
              if (this.devices[i].id === row) {
                this.$refs.multipleTable.toggleRowSelection(this.devices[i])
                this.setCheckInfo()
              }
            })
          })
        })
      },
      deep: true
    },
    multipleSelection: {
      handler: function() {
        this.setCheckInfo()
      }
    }
  },
  created() {
    crudDevice.selectList().then(res => {
      this.total = res.totalElements
      this.devices = res.content.map(function(obj) {
        return obj
      })
    })
  },
  methods: {
    getDevices() {
      console.info('this.page  ', this.page)
      console.info('this.size  ', this.size)
      crudDevice.selectList({ page: this.page, size: this.size }).then(res => {
        this.total = res.totalElements
        this.devices = res.content.map(function(obj) {
          return obj
        })
      })
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row)
        })
      } else {
        this.$refs.multipleTable.clearSelection()
      }
    },
    handleSelectionChange(val) {
      var selectOne = this.crud.selectOne
      if (selectOne === false) { // 不限制选项
        this.multipleSelection = val
      }
      if (selectOne === true) {
        this.multipleSelection = val
        this.multipleSelection = val[0]
        if (val.length > 1) {
          this.$refs.multipleTable.clearSelection()
          this.$refs.multipleTable.toggleRowSelection(val.pop())
        }
      }
    },
    selected() {
      this.$emit('on-change-device', this.$refs.multipleTable.selection, this.crud.openId)
      this.$refs.multipleTable.clearSelection()
      this.crud.doCloseDevice()
    },
    setCheckInfo() {
      var checkedDevices = this.$refs.multipleTable.selection
      var info = ''
      checkedDevices.forEach(row => {
        info += row.name + ','
      })
      this.checkInfo = info.substring(0, info.length - 1)
    }
  }
}
</script>
