<template>
  <div class="subPage">
    <div class="system-table-search">
      <div class="flex-h">
        <div class="flex-item flex-h">
          <el-button size="mini" type="success" @click='addRole'>新增体检设备</el-button>
        </div>
      </div>
      <div class="moreFilter"></div>
    </div>
    <div class="system-table-box">
      <el-table :data="list.list" stripe style="width: 100%">
        <el-table-column label="案场名称">
          <template slot-scope="scope">
            <label>{{GetCaseName(scope.row.CaseId)}}</label>
          </template>
        </el-table-column>
        <el-table-column prop="Name" label="体检设备名称">
        </el-table-column>
        <el-table-column prop="EquipmentCode" label="设备Code">
        </el-table-column>
        <el-table-column label="创建时间" width="300">
          <template slot-scope="scope">
            <label>{{FormatDate(scope.row.CreateDate)}}</label>
          </template>
        </el-table-column>
        <el-table-column fixed='right' label="操作" width="300">
          <template slot-scope="scope">
            <el-button size="mini" type="warning" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
    @size-change="handleSizeChange"
    @current-change="handleCurrentChange"
    :current-page.sync="currentPage"
    :page-size="10"
    layout="prev, pager, next, jumper"
    :total="list.pagenum">
    </el-pagination>
  </div>
</template>

<script>
import { createNamespacedHelpers, mapState } from 'vuex'

const { mapState: mapDeviceState, mapActions: mapDeviceActions } = createNamespacedHelpers('device')

export default {
  name: '',
  data () {
    return {
      currentPage: 0, // 当前页码
    }
  },
  computed: {
    ...mapState({
      cases: x => x.app.cases.list,
    }),
    ...mapDeviceState({
      list: x => x.deviceList,
    }),
  },
  methods: {
    ...mapDeviceActions([
      'GetDeviceList',
      'DelDevice',
    ]),
    FormatDate (date) {
      return this.toolClass.dateFormat(date)
    },
    GetCaseName (caseid) {
      return ((this.cases.filter(x => x.CaseId === caseid) || [])[0] || {}).CaseName
    },
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange (val) {
      this.GetDeviceList({ page: val })
      console.log(`当前页: ${val}`)
    },
    handleEdit (index, row) {
      // 编辑
      this.$router.push({ name: 'editDevice', query: { id: row.EquipmentId } })
    },
    handleDelete (index, row) {
      // 删除
      console.log(index, row)
      this.$confirm('确认删除此设备?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.DelDevice({id: row.EquipmentId, callback: this.delCallBack})
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    delCallBack () {
      this.$message({
        type: 'success',
        message: '删除成功!'
      })
      this.GetDeviceList()
    },
    addRole () {
      this.$router.push({ name: 'editDevice' })
    },
  },
  beforeMount () {
    this.GetDeviceList()
  },
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" scoped>
</style>
