<template>
  <el-dialog :visible.sync="crud.tempShow.dmSystem" 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="getDmSystem()">搜索</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="dmSystems"
          tooltip-effect="dark"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            type="selection"
            width="55"
          />
          <el-table-column :show-overflow-tooltip="true" prop="systemName" label="用户名" />
          <el-table-column :show-overflow-tooltip="true" prop="createTime" width="135" 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="getDmSystem()"
          @current-change="getDmSystem()"
        />
      </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 crudDmSystem from '@/api/dm/system/dmSystem'
import { mapGetters } from 'vuex'
import CRUD, { crud } from '@crud/crud'

export default {
  mixins: [crud()],
  cruds() {
    return CRUD({ title: '子系统', url: 'api/dmSystem', tempBtnShow: {
      dmSystem: true
    }, crudMethod: { ...crudDmSystem }})
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      dmSystems: [],
      multipleSelection: crud.selectData || [],
      checkInfo: '',
      code: '',
      size: 0,
      page: 0,
      total: 0

    }
  },
  computed: {
    ...mapGetters([
      'dmSystem'
    ])
  },
  watch: {
    'crud.selectData': {
      handler: function(datainfo) {
        this.$nextTick(function() {
          this.toggleSelection()
          this.dmSystems.forEach((item, i) => {
            datainfo.forEach(row => {
              if (this.dmSystems[i].id === row * 1) {
                this.$refs.multipleTable.toggleRowSelection(this.dmSystems[i])
                this.setCheckInfo()
              }
            })
          })
        })
      },
      deep: true
    },
    multipleSelection: {
      handler: function() {
        this.setCheckInfo()
      }
    }
  },
  created() {
    crudDmSystem.getDmSystem().then(res => {
      this.total = res.totalElements
      this.dmSystems = res.content.map(function(obj) {
        return obj
      })
    })
  },
  methods: {
    getDmSystem() {
      crudDmSystem.getDmSystem({ blurry: this.code, page: this.page, size: this.size }).then(res => {
        this.total = res.totalElements
        this.dmSystems = res.content.map(function(obj) {
          return obj
        })
      })
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row)
        })
      } else {
        this.$refs.multipleTable.clearSelection()
      }
      this.crud.doCloseDmSystem()
    },
    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', this.$refs.multipleTable.selection, this.crud.openId)
      this.$refs.multipleTable.clearSelection()
      this.crud.doCloseDmSystem()
    },
    setCheckInfo() {
      var checkedDmSystems = this.$refs.multipleTable.selection
      var info = ''
      checkedDmSystems.forEach(row => {
        info += row.systemName + ','
      })
      this.checkInfo = info.substring(0, info.length - 1)
    }
  }
}
</script>
