<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-form inline size="mini">
      <el-form-item label="推荐位置：">
        <el-select v-model="form.position" placeholder="请选择推荐位置" clearable>
          <el-option v-for="item in locationOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="当前状态：">
        <el-select v-model="form.status" placeholder="请选择当前状态" clearable>
          <el-option v-for="item in currentOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="商品名称：">
        <el-input v-model="form.name" placeholder="请输入商品名称" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="getSearch">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="getRefresh">重置</el-button>
        <el-button type="primary" icon="el-icon-plus" plain @click="getDetail()"
          v-hasPermi="['goods:list:add']">新增</el-button>
      </el-form-item>
    </el-form>
    <!-- 列表 -->
    <el-table :data="tableData" v-loading="loading">
      <el-table-column label="推荐位置" prop="position" align="center" :formatter="positionFormatter" />
      <el-table-column label="商品名称" prop="name" align="center" show-overflow-tooltip />
      <el-table-column label="商品简介" prop="description" align="center" show-overflow-tooltip />
      <el-table-column label="商品价格/元" prop="price" align="center" />
      <el-table-column label="商品图片" align="center" width="100px">
        <template slot-scope="scope">
          <el-image :src="scope.row.pic" :previewSrcList="[scope.row.pic]" />
        </template>
      </el-table-column>
      <el-table-column label="当前状态" prop="status" align="center" :formatter="statusFormatter" />
      <el-table-column label="权重" prop="sort" align="center" />
      <el-table-column label="有效期" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ scope.row.startTime }} 至 {{ scope.row.endTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="getDetail(scope.row.id)" v-hasPermi="['goods:list:edit']">
            编辑
          </el-button>
          <el-button type="delete" @click="getDelete(scope.row)" v-hasPermi="['goods:list:delete']">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="form.pageNum" :limit.sync="form.pageSize"
      @pagination="getList" />
  </div>
</template>

<script>
import { list, remove } from '@/api/goods/list'
import { locMixin } from './mixin'
import { currentMixin } from '@/mixin/index'
export default {
  mixins: [locMixin, currentMixin],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 表单
      form: {
        pageNum: 1,
        pageSize: 10
      },
      // 列表
      tableData: [],
      total: 0
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    // 列表
    getList() {
      this.loading = true
      list(this.form).then(res => {
        if (res.code === 0) {
          this.tableData = res.data.records
          this.total = res.data.total
          this.loading = false
        }
      })
    },
    // 搜索
    getSearch() {
      this.form.pageNum = 1
      this.getList()
    },
    // 重置
    getRefresh() {
      this.form = {
        pageNum: 1,
        pageSize: 10
      }
      this.getList()
    },
    // 新增 编辑
    getDetail(id) {
      this.$router.push({
        path: `/goods/goodsList/detail`,
        query: {
          id: id
        }
      })
    },

    // 删除
    getDelete(row) {
      this.$confirm(`是否删除${row.name}商品？`, '提醒', {
        'confirmButtonText': '确定',
        'cancelButtonText': '取消',
        type: 'warning'
      }).then(() => {
        remove({
          id: row.id
        }).then(res => {
          if (res.code === 0) {
            this.$message.success('删除成功！')
            this.getList()
          }
        })
      })
    },

    // 字典翻译
    positionFormatter(row) {
      return this.selectDictLabel(this.locationOptions, row.position)
    },

    statusFormatter(row) {
      return this.selectDictLabel(this.currentOptions, row.status)
    }
  }
}
</script>