<template>
<div class="list">
        <div class="system-table-search">
      <div class="moreFilter"></div>
    </div>
  <el-table
    :data="list || []"
    style="width: 100%">
    <el-table-column
      prop="ruleName"
      label="类型">
    </el-table-column>
    <el-table-column
      prop="pointsAmount"
      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
      label="操作时间">
      <template slot-scope="scope">
          <span>{{formateDate(scope.row.updateDate)}}</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";
import dayjs from 'dayjs'

const { mapActions: mapPointsActions } = createNamespacedHelpers("points");

export default {
  name: "goods-list",
  data() {
    return {
      filterData: {
        goodsName: ""
      },
      list: [],
      pageNavi: {
        current: 1,
        size: 20,
        total: 0
      }
    };
  },
  computed: {},
  methods: {
    ...mapPointsActions(["getPointsRules", "changePointsRulesStatus"]),
    getList() {
      const pageNumber = this.pageNavi.current || 1;
      const pageSize = this.pageNavi.size;

      this.getPointsRules({
        ...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);
        });
    },
    toDetail(row) {
      this.$router.push({
        name: "points.edit",
        params: { id: row.ruleId }
      });
    },
    changeStatus(row) {
      this.showLoadding("保存中...");
      this.changePointsRulesStatus(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 } });
    },
    formateDate(dt) {
    return !dt ? '' : dayjs(dt).format('YYYY-MM-DD HH:mm')
  },
  },
  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>
