<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-form inline label-width="110px" size="mini">
      <el-form-item label="Banner名称：">
        <el-input v-model="form.name" placeholder="请输入名称" />
      </el-form-item>
      <el-form-item label="展示位置：">
        <el-select v-model="form.type" placeholder="请选择展示位置">
          <el-option v-for="item in dict.type.show_the_location" :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="请选择当前状态">
          <el-option v-for="item in dict.type.on_off_out" :key="item.value" :label="item.label"
            :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="上/下架：">
        <el-select v-model="form.shelfStatus" placeholder="请选择上/下架状态">
          <el-option v-for="item in dict.type.top_bottom_shelves" :key="item.value" :label="item.label"
            :value="item.value" />
        </el-select>
      </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="['content:banner:add']">新增</el-button>
      </el-form-item>
    </el-form>
    <!-- 列表 -->
    <el-table :data="tableData" v-loading="loading">
      <el-table-column label="Banner名称" prop="name" align="center" />
      <el-table-column label="Banner位置" prop="type" align="center" :formatter="typeFormatter" />
      <el-table-column label="轮播顺序" prop="sort" align="center" />
      <el-table-column label="图片" align="center" width="100px">
        <template slot-scope="scope">
          <el-image :src="scope.row.pic" />
        </template>
      </el-table-column>
      <el-table-column label="当前状态" prop="currentStatus" align="center" :formatter="currentFormatter" />
      <el-table-column label="上/下架" prop="status" align="center" :formatter="statusFormatter" />
      <el-table-column label="开始时间" prop="startTime" align="center" />
      <el-table-column label="结束时间" prop="endTime" align="center" />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button v-if="scope.row.status === 1" type="text" @click="getChange(scope.row, 0)"
            v-hasPermi="['content:banner:down']">下架</el-button>
          <div v-else>
            <el-button type="text" @click="getDetail(scope.row)" v-hasPermi="['content:banner:edit']">
              编辑
            </el-button>
            <el-button type="text" @click="getChange(scope.row, 1)" v-hasPermi="['content:banner:up']">
              上架
            </el-button>
            <el-button v-if="scope.row.status === 0" type="delete" @click="getDelete(scope.row)"
              v-hasPermi="['content:banner:delete']">
              删除
            </el-button>
          </div>
        </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 { page, changeStatus, remove } from '@/api/content/banner'
export default {
  dicts: ['on_off_out', 'top_bottom_shelves', 'show_the_location'],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 表单
      form: {
        pageNum: 1,
        pageSize: 10
      },
      total: 0,
      // 列表
      tableData: []
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    // 搜索
    getSearch() {
      this.form.pageNum = 1
      this.form.pageSize = 10
      this.getList()
    },

    // 重置
    getRefresh() {
      this.form = {
        pageNum: 1,
        pageSize: 10
      }
      this.getList()
    },

    // 分页
    getList() {
      this.loading = true
      page(this.form).then(res => {
        if (res.code === 0) {
          this.tableData = res.data.records
          this.total = res.data.total
          this.loading = false
        }
      })
    },

    // 上下架
    getChange(row, status) {
      let tips = status === 0 ? '下架' : '上架'
      changeStatus({
        id: row.id,
        status: status
      }).then(res => {
        if (res.code === 0) {
          this.$message.success(`${tips}成功！`)
          this.getList()
        }
      })
    },

    // 删除
    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()
          }
        })
      }).catch(() => {
        this.$message.info('取消删除！')
      })
    },

    // 新增 编辑
    getDetail(row) {
      this.$router.push({
        path: '/content/banner/detail',
        query: {
          id: row.id
        }
      })
    },

    // 字典翻译
    statusFormatter(row) {
      return this.selectDictLabel(this.dict.type.top_bottom_shelves, row.status)
    },
    typeFormatter(row) {
      return this.selectDictLabel(this.dict.type.show_the_location, row.type)
    },
    currentFormatter(row) {
      return this.selectDictLabel(this.dict.type.on_off_out, row.currentStatus)
    }
  }
}
</script>