<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="addGoodsSpec">新增商品规格</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="SpecName"
          label="规格名">
        </el-table-column>
        <el-table-column
          label="所属案场">
          <template slot-scope="scope">
            <label>{{getCaseName(scope.row.CaseId)}}</label>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <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"
      :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: {
    getCaseName (caseid) {
      return (this.cases.filter(x => x.CaseId === caseid)[0] || {}).CaseName
    },
    search () { // 搜索
      this.postData.page = 1
      this.currentList = []
      this.getList()
    },
    getList () { // 获取列表
      this.$ajax(this.$api.goodsManager.getGoodsSpecList.url, {
        method: this.$api.goodsManager.getGoodsSpecList.method,
        queryData: { ...this.postData, caseid: this.CaseId === 'all' ? '' : this.CaseId }
      }).then(res => {
        this.currentList = res.list
        this.postData.page = res.page
        this.total = res.pagenum
      })
    },
    handleCurrentChange (val) { // 跳转到分页
      this.getList()
    },
    handleEdit (index, row) { // 编辑
      this.$router.push({ name: 'editGoodsSpec', query: { id: row.SpecId } })
    },
    handleDelete (index, row) { // 删除
      let name = '确认删除规格“' + row.SpecName + '”?'
      this.$confirm(name, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$ajax(this.$api.goodsManager.deleteGoodsSpec.url, {
          method: this.$api.goodsManager.deleteGoodsSpec.method,
          urlData: { id: row.SpecId }
        }).then(res => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.search()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    addGoodsSpec () {
      this.$router.push({ name: 'addGoodsSpec' })
    }
  }
}
</script>

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