<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>
            <!-- <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>
          <li>
            <el-input
              placeholder="请输入商品名称"
              v-model="postData.name"
            >
            </el-input>
          </li>
          <li>
            <span>商品类型：</span>
            <el-select v-model="postData.type" placeholder="请选择">
              <el-option
                v-for="item in types.list"
                :key="item.TypeId"
                :label="item.TypeName"
                :value="item.TypeId">
              </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="goods.list" stripe style="width: 100%">
        <el-table-column prop="GoodsName" label="商品名称">
        </el-table-column>
        <el-table-column label="图片" width="300">
          <template slot-scope="scope">
            <img :src="((scope.row.Images || [])[0] || {}).ImgUrl" style="width:200px;" />
          </template>
        </el-table-column>
        <el-table-column prop="Price" label="价格">
        </el-table-column>
        <el-table-column label="类型">
          <template slot-scope="scope">
            <label>{{getTypeName(scope.row.TypeId)}}</label>
          </template>
        </el-table-column>
        <el-table-column label="规格">
          <template slot-scope="scope">
            <label>{{getSpecNames(scope.row.Specs)}}</label>
          </template>
        </el-table-column>
        <el-table-column label="案场">
          <template slot-scope="scope">
            <label>{{getCaseName(scope.row.CaseId)}}</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="postData.pagesize"
    layout="prev, pager, next, jumper"
    :total="goods.pagenum">
    </el-pagination>
  </div>
</template>

<script>
import { createNamespacedHelpers, mapState } from 'vuex'
import tableSearch from '@/components/tableSearch/index'

const { mapState: mapGoodsState, mapActions: mapGoodsActions } = createNamespacedHelpers('goods')

export default {
  name: '',
  data () {
    return {
      currentPage: 0, // 当前页码
      key: '',
      postData: {
        caseid: '',
        name: '',
        type: '',
        page: 1,
        pagesize: 10,
      },
    }
  },
  computed: {
    ...mapState({
      cases: x => x.app.cases.list,
      defaultCaseId: x => x.app.cases.default
    }),
    ...mapGoodsState({
      goods: x => x.goodsList,
      types: x => x.goodsTypes,
    }),
    CaseId: {
      get () {
        return this.postData.caseid || this.defaultCaseId
      },
      set (val) {
        this.postData.caseid = val
        this.GetGoodTypes({ pagesize: 1000, caseid: this.CaseId === 'all' ? '' : this.CaseId })
      }
    }
  },
  components: {
    tableSearch
  },
  methods: {
    ...mapGoodsActions([
      'GetGoodsList',
      'GetGoodTypes',
      'DelGoods',
      'SetNull',
    ]),
    getTypeName (typeid) {
      return (this.types.list.filter(x => x.TypeId === typeid)[0] || {}).TypeName
    },
    getSpecNames (specs) {
      return (specs || []).map(x => x.SpecName).join(',')
    },
    getCaseName (caseid) {
      return (this.cases.filter(x => x.CaseId === caseid)[0] || {}).CaseName
    },
    search () { // 搜索
      this.postData.page = 1
      this.currentList = []
      this.getList()
    },
    getList () {
      this.GetGoodsList({ ...this.postData, caseid: this.CaseId === 'all' ? '' : this.CaseId })
    },
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange (val) {
      this.postData.page = this.currentPage
      this.getList()
    },
    handleEdit (index, row) {
      // 编辑
      this.SetNull()
      this.$router.push({ name: 'editGoods', query: { id: row.GoodsId } })
    },
    handleDelete (index, row) {
      // 删除
      this.$confirm('确认删除此商品?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.DelGoods({ id: row.GoodsId, callback: this.delCallBack })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    delCallBack () {
      this.$message({
        type: 'success',
        message: '删除成功!'
      })
      this.getList()
    },
    addRole () {
      this.SetNull()
      this.$router.push({ name: 'addGoods' })
    },
  },
  created () {
    this.GetGoodTypes({ pagesize: 1000, caseid: this.CaseId })
    this.$nextTick(function () {
      this.getList()
    })
  },
}
</script>

<style lang="scss" scoped>
</style>
