<template>
  <div class="app-container">
    <!-- 组合按钮 -->
    <el-button-group class="m-b-20 m-r-20">
      <el-button type="primary" icon="el-icon-plus" @click="$router.replace('/sku/add')">添加</el-button>
      <el-button type="danger" icon="el-icon-delete" @click="deleteSku(countSelIdArr($refs.myTable.selection))">删除
      </el-button>
      <el-button type="warning" icon="el-icon-edit" @click="toEditPage()">编辑</el-button>
    </el-button-group>
    <!-- 用户select选项 -->
    <el-button-group class="m-b-20">
      <SelectionShopId v-model="form.shop_id" :allSel="true" />
    </el-button-group>
    <!-- 商品sku表格 -->
    <el-table class="w-100" ref="myTable"
      :data="skuListArr.slice((currentPage - 1) * pageSize, currentPage * pageSize)" border tooltip-effect="dark">
      <el-table-column align="center" type="selection" width="40">
      </el-table-column>
      <el-table-column align="center" prop="id" label="id" width="50">
      </el-table-column>
      <el-table-column prop="name" label="名称" width="150" min-width="150">
      </el-table-column>
      <el-table-column prop="sku_number" label="编号" width="120">
      </el-table-column>
      <el-table-column prop="purchase_channel" label="采购渠道" min-width="120" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="price" label="单价" width="60" min-width="60">
      </el-table-column>
      <el-table-column prop="unit" label="单位" width="60" min-width="60">
      </el-table-column>
      <el-table-column label="图片" width="120" min-width="120">
        <template v-if="scope.row.photo != []" slot-scope="scope">
          <el-image :src="scope.row.photo[0]"
            :preview-src-list="scope.row.photo">
            <div slot="error" class="image-slot">
              <i class="el-icon-picture-outline"></i>
            </div>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column prop="controler" label="操作" width="280" min-width="280">
        <template slot-scope="scope">
          <el-button-group>
            <el-button type="warning" icon="el-icon-edit"
              @click="$router.replace(`/sku/edit/${scope.row.id}`)">编辑</el-button>
            <el-button type="danger" icon="el-icon-delete" @click="deleteSku([scope.row.id])">删除</el-button>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination class="m-t-20" layout="prev, pager, next" :current-page.sync="currentPage" :page-size="pageSize"
      :total="skuListArr.length">
    </el-pagination>
  </div>
</template>

<script>
import { countSelIdArr } from '@/utils'
import SelectionShopId from '@/components/SelectionShopId'

export default {
  name: 'Sku',
  data () {
    return {
      pageSize: 30, // 每页显示记录条数
      currentPage: 1, // 当前页
      form: {
        shop_id: ''
      }
    }
  },
  components: {
    SelectionShopId
  },
  computed: {
    // 获取商品sku列表
    skuList () {
      return this.$store.state.skuList
    },
    /**
     * @description: 按照shop_id 条件过滤 商品sku列表
     * @return: 商品sku列表
     */
    skuListArr () {
      if (this.form.shop_id !== '') {
        return this.skuList.filter((item) => item.shop_id === this.form.shop_id)
      } else {
        return this.skuList
      }
    }
  },
  methods: {
    countSelIdArr,
    /**
     * @description: 跳转到编辑页面
     */
    toEditPage () {
      const selId = this.countSelIdArr(this.$refs.myTable.selection)
      switch (selId.length) {
        case 0:
          this.$message.error('请选择一条需要编辑的记录')
          break
        case 1:
          this.$router.push('/sku/edit/' + selId['0'])
          break
        default:
          this.$message.error('只能选择一条记录')
      }
    },
    /**
     * @description: 删除sku
     */
    deleteSku (idArr) {
      this.$store.dispatch('fetchDelSku', idArr)
    }
  },
  watch: {
  },
  created () {
  },
  filters: {
    countSelIdArr
  }
}
</script>

<style lang="scss" scoped>
@import "@/styles/theme.scss";

i {
  font-size: 18px !important;
  margin-right: 12px;
  vertical-align: middle;
}
</style>
