<template>
<div class="list">
        <div class="system-table-search">
      <div class="flex-h">
        <el-form :inline="true" :model="form" class="demo-form-inline">
          <el-form-item label="">
            <el-input v-model="filterData.goodsName" placeholder="商品名称"></el-input>
          </el-form-item>
          <el-form-item label="">
            <el-select v-model="filterData.status" clearable placeholder="状态">
              <el-option
                      v-for="item in options"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="">
            <select-building v-model="filterData.buildingId"></select-building>
          </el-form-item>
          <el-form-item label="">
            <el-input v-model="filterData.priceLesser" placeholder="价格小" style="width: 100px;"/>
          </el-form-item>
          <el-form-item label="">
            <el-input v-model="filterData.priceGreater" placeholder="价格大" style="width: 100px;"/>
          </el-form-item>
          <el-form-item label="">
            <el-button
                    size="mini"
                    type="primary" @click="search">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="flex-item flex-h">
        <el-button size="mini" type="success" @click='addGoods'>新增</el-button>
      </div>

      <div class="moreFilter"></div>
    </div>
  <el-table
    :data="list || []"
    style="width: 100%">
    <el-table-column
      label="商品图片">
      <template slot-scope="scope">
          <div class="header">
            <img :src="scope.row.imgUrl" alt="" />
          </div>
        </template>
    </el-table-column>
    <el-table-column
      prop="goodsName"
      label="商品名称">
    </el-table-column>
    <el-table-column
      prop="pointPrice"
      label="所需积分">
    </el-table-column>
    <el-table-column
      prop="totalNum"
      label="总数量">
    </el-table-column>
    <el-table-column
      label="已经兑换数量">
      <template slot-scope="scope">
        <span  >{{scope.row.totalNum -scope.row.inventory }}</span>
      </template>
    </el-table-column>
    <el-table-column
      prop="inventory"
      label="剩余数量">
    </el-table-column>
    <el-table-column   label="状态">
      <template slot-scope="scope">
        <span :style="{color:(scope.row.status == 1?'':'red')}" >{{scope.row.status == 1 ? '已上架' : '未上架'}}</span>
      </template>
    </el-table-column>
    <el-table-column
      fixed="right"
      label="操作">
      <template slot-scope="scope">
        <el-button type="text" @click="changeStatus(scope.row)" size="small">{{scope.row.status == 1?'下架':'上架'}}</el-button>
        <el-button type="text" @click="toDetail(scope.row)" size="small">编辑</el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-pagination
    small
    style="margin-top:10px;"
    layout="prev, pager, next"
    :current-page.sync="pageNavi.current"
    :pageSize="pageNavi.size"
    :total="pageNavi.total || 0"
    @current-change="getList"
  >
  </el-pagination>
</div>
</template>

<script>
import { createNamespacedHelpers } from "vuex";

const { mapActions: mapGoodsActions } = createNamespacedHelpers("goods");

export default {
  name: "goods-list",
  data() {
    return {
      filterData: {
        goodsName: "",
        status: '',
        buildingId: '',
        priceLesser: '',
        priceGreater: ''
      },
      list: [],
      pageNavi: {
        current: 1,
        size: 20,
        total: 0
      },
      options: [{
        value: 1,
        label: '已上架'
      }, {
        value: 0,
        label: '已下架'
      }],
    };
  },
  computed: {},
  methods: {
    ...mapGoodsActions(["getGoods", "changeGoodsStatus"]),
    getList() {
      const pageNumber = this.pageNavi.current || 1;
      const pageSize = this.pageNavi.size;

      this.getGoods({
        ...this.filterData,
        pageNumber,
        pageSize
      })
        .then(res => {
          const { records, ...pageNavi } = res;

          this.list = records;
          this.pageNavi = pageNavi;
        })
        .catch(err => {
          this.$notify.error(err.msg || err.message);
        });
    },
    addGoods() {
      this.$router.push({ name: "goods.edit" });
    },
    toDetail(row) {
      this.$router.push({
        name: "goods.edit",
        params: { id: row.goodsId }
      });
    },
    changeStatus(row) {
      this.showLoadding("保存中...");
      this.changeGoodsStatus(row)
        .then(res => {
          this.hideLoadding();
          this.$notify.info("保存成功");
          this.search();
        })
        .catch(err => {
          this.hideLoadding();
          this.$notify.error(err.message);
        });
    },
    showLoadding(text) {
      this.loading = this.$loading({
        text,
        lock: true,
        spinner: "el-icon-loading",
        background: "rgba(255, 255, 255, 0.7)"
      });
    },
    hideLoadding() {
      if (this.loading) this.loading.close();
    },
    search() {
      this.pageNavi.current = 1;
      this.getList();
    },
    newPage(page) {
      this.$router.replace({ name: "goods.list", query: { page } });
    }
  },
  created() {
    this.pageNavi.current = this.$route.query.page || 1;

    this.getList();
  }
};
</script>

<style lang="scss" scoped>
.list {
  .header {
    width: 50px;
    height: 50px;
    border-radius: 50%;
  }
  img {
      width: 100%;
      height: 100%;
    }
}
.system-table-search {
  width: calc(100% - 40px);
  margin: 20px auto 0;
}

.system-table-search li {
  margin-right: 20px;
}

.system-table-search ul {
  font-size: 0;
  white-space: nowrap;
}

.system-table-search ul > li {
  display: inline-block;
}

.flex-h {
  display: flex;
  align-items: center;
}

.flex-item {
  flex: 1;
  -webkit-flex: 1;
  position: relative;
  overflow: hidden;
}
</style>
