<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='addCaseArea'>新增案场区域</el-button>
        </div>
        <ul>
          <li>
            <!-- <span>选择案场：</span> -->
            <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="currentList"
        stripe
        style="width: 100%">
        <el-table-column
          prop="AreaName"
          label="区域名">
        </el-table-column>
        <el-table-column
          prop="AreaIcon"
          label="图标">
          <template slot-scope="scope">
            <a class="tableImg">
              <img :src="scope.row.AreaIcon" class="centerLabel contain" alt="">
            </a>
          </template>
        </el-table-column>
        <el-table-column
          prop="AreaIconWhite"
          label="黑白图标">
          <template slot-scope="scope">
            <a class="tableImg">
              <img :src="scope.row.AreaIconWhite" class="centerLabel contain" alt="">
            </a>
          </template>
        </el-table-column>
        <el-table-column
          label="案场" >
          <template slot-scope="scope">
            <span>{{getCaseName(scope.row.CaseId)}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="warning"
              @click="editItem(scope.$index, scope.row)">编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="deleteItem(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page.sync="postData.page"
      :page-size="postData.pagesize"
      layout="prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
import { mapState } from 'vuex'

export default {
  name: '',
  data () {
    return {
      total: 0,
      postData: { // 表格搜索条件
        caseid: '', // 案场id
        page: 1, // 当前页码
        pagesize: 10, // 请求数据量
      },
      currentList: [],
    }
  },
  mounted () {
    this.$nextTick(function () {
      this.getList()
    })
  },
  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
      }
    },
  },
  methods: {
    search () { // 搜索
      this.postData.page = 1
      this.currentList = []
      this.getList()
    },
    getCaseName (caseid) {
      return (this.cases.filter(x => x.CaseId === caseid)[0] || {}).CaseName
    },
    getList () { // 获取列表
      this.$ajax(this.$api.caseManager.getCaseAreaList.url, {
        method: this.$api.caseManager.getCaseAreaList.method,
        queryData: { ...this.postData, caseid: this.CaseId === 'all' ? '' : this.CaseId }
      }).then(res => {
        // for (var n = 0; n < res.list.length; n++) {
        //   res.list[n].CaseName = this.cases.filter(x => x.CaseId === res.list[n].CaseId)[0].CaseName
        // }
        this.currentList = res.list
        this.postData.page = res.page
        this.total = res.pagenum
      })
    },
    handleCurrentChange (val) { // 跳转到分页
      this.getList()
    },
    editItem (index, row) { // 编辑
      this.$router.push({ name: 'editCaseArea', query: {id: row.AreaId} })
    },
    deleteItem (index, row) { // 删除
      this.$confirm('确认删除此数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$ajax(this.$api.caseManager.deleteCaseArea.url, {
          method: this.$api.caseManager.deleteCaseArea.method,
          urlData: { id: row.AreaId }
        }).then(res => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.search()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    addCaseArea () {
      this.$router.push({ name: 'addCaseArea' })
    }
  }
}
</script>

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