<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>
        <ul>
          <li>
            <el-select v-model="CaseId" placeholder="请选择">
              <el-option
                key="all"
                label="所有案场"
                value="all">
              </el-option>
              <el-option
                v-for="item in cases"
                :key="item.CaseId"
                :label="item.CaseName"
                :value="item.CaseId">
              </el-option>
            </el-select>
          </li>
        </ul>
        <el-button
          size="mini"
          type="primary" @click="search">搜索</el-button>
      </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
      @current-change="handleCurrentChange"
      :page-size="10"
      layout="prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

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

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

export default {
  name: '',
  data () {
    return {
      currentPage: 0, // 当前页码
      total: 0,
      postData: { // 表格搜索条件
        caseid: '', // 案场id
        page: 1, // 当前页码
        pagesize: 10, // 请求数据量
      },
    }
  },
  computed: {
    ...mapState({
      cases: x => x.app.cases.list,
      defaultCaseId: x => x.app.cases.default
    }),
    CaseId: {
      get () {
        return this.postData.caseid || this.defaultCaseId
      },
      set (val) {
        this.postData.caseid = val
      }
    },
    ...mapDeviceState({
      list: x => x.deviceList,
    }),
  },
  mounted () {
    this.$nextTick(function () {
      this.getList()
    })
  },
  methods: {
    ...mapDeviceActions([
      'GetDeviceList',
      'DelDevice',
    ]),
    FormatDate (date) {
      return this.toolClass.dateFormat(date)
    },
    GetCaseName (caseid) {
      return ((this.cases.filter(x => x.CaseId === caseid) || [])[0] || {}).CaseName
    },
    handleCurrentChange (val) {
      this.postData.page = val
      this.getList()
    },
    handleEdit (index, row) {
      // 编辑
      this.$router.push({ name: 'editDevice', query: { id: row.EquipmentId } })
    },
    handleDelete (index, row) {
      // 删除
      this.$confirm('确认删除此设备?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.DelDevice({id: row.EquipmentId, callback: this.delCallBack})
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    search () { // 搜索
      this.postData.page = 1
      this.currentList = []
      this.getList()
    },
    getList () { // 获取列表
      this.GetDeviceList({ ...this.postData, caseid: this.CaseId === 'all' ? '' : this.CaseId }).then((res) => {
        this.postData.page = res.page
        this.total = res.pagenum
      })
    },
    delCallBack () {
      this.$message({
        type: 'success',
        message: '删除成功!'
      })
      this.getList()
    },
    addRole () {
      this.$router.push({ name: 'addDevice' })
    },
  },
}
</script>

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