<template>
  <div class="List">
    <ai-list>
      <template slot="title">
        <ai-title title="轮播图片" :isShowBottomBorder="false" :isShowArea="false"></ai-title>
      </template>
      <template slot="content">
        <ai-search-bar>
          <template slot="left">
            <el-button type="primary" icon="iconfont iconAdd" size="small" @click="add">添加</el-button>
          </template>
          <template slot="right">
            <el-input v-model="search.title" size="small" placeholder="搜索标题" clearable v-throttle="() => {page.current = 1, getList()}"
              @clear=";(page.current = 1), (search.title = ''), getList()" suffix-icon="iconfont iconSearch"/>
          </template>
        </ai-search-bar>
        <ai-table :tableData="tableData" :colConfigs="colConfigs" :total="page.total" :current.sync="page.current"
          :size.sync="page.size" @getList="getList" class="ai-table">
          <!-- 首页封面 -->
          <el-table-column label="首页封面" align="left" width="150" slot="imgUrl">
            <template slot-scope="{ row }">
              <img :src="row.imgUrl" alt="" class="banner-img" v-if="row.imgUrl"/>
            </template>
          </el-table-column>
          <!-- 操作 -->
          <el-table-column label="操作" align="center" width="300" slot="option">
            <template slot-scope="{ row }">
              <el-button type="text" @click="release(row)" v-if="row.status == 1">取消发布</el-button>
              <el-button type="text" @click="release(row)" v-else>发布</el-button>
              <el-button type="text" @click="detail(row.id)">详情</el-button>
              <el-button type="text" @click="edit(row.id)">编辑</el-button>
              <el-button type="text" @click="remove(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </ai-table>
      </template>
    </ai-list>
  </div>
</template>

<script>
import {mapState} from 'vuex'

export default {
  name: 'List',
  props: {
    instance: Function,
    dict: Object,
    permissions: Function,
  },
  data() {
    return {
      search: {
        title: '',
      },
      tableData: [],
      page: {
        size: 10,
        current: 1,
        total: 0,
      },
      colConfigs: [
        {prop: 'imgUrl', label: '首页封面', slot: 'imgUrl'},
        {prop: 'title', label: '标题', 'show-overflow-tooltip': true,},
        {prop: 'type', label: '链接类型',
          render: (h, {row}) => {return h('span', null, this.dict.getLabel('bannerType', row.type))},
        },
        {prop: 'status', label: '发布状态', width: 400,
          render: (h, {row}) => {return h('span', null, this.dict.getLabel('bannerSstatus', row.status))},
        },
        {slot: 'option', label: '操作', width: 280},
      ],
    }
  },
  // 计算
  computed: {
    ...mapState(['user']),
  },
  // 监听
  watch: {},
  // 实例创建后
  created() {
    this.dict.load('bannerType', 'bannerSstatus').then(() => {
      this.getList()
    })
  },
  // 实例渲染后
  mounted() {
  },
  // 方法
  methods: {
    getList() {
      this.instance.post(`/app/appbanner/list`, null, {
        params: {
          ...this.search,
          ...this.page,
        },
      }).then((res) => {
        if (res.code == 0) {
          this.tableData = res.data.records
          this.page.total = res.data.total
        }
      })
    },
    add() {
      this.$emit('change', {
        type: 'Add',
        params: {id: ''}
      })
    },
    // 发布/取消发布
    release(row) {
      this.$confirm('确定此操作？').then(() => {
        var status = row.status == 1 ? '0' : '1'
        this.instance.post(`/app/appbanner/setStatus?id=${row.id}&status=${status}`)
        .then((res) => {
          if (res?.code == 0) {
            this.getList()
          }
        })
      })
    },
    detail(id) {
      this.$emit('change', {
        type: 'Detail',
        params: {
          id
        }
      })
    },
    edit(id) {
      this.$emit('change', {
        type: 'Add',
        params: {
          id
        }
      })
    },
    // 删除
    remove(id) {
      this.$confirm('删除后不可恢复，是否要删除该事项？', {
        type: 'error',
      }).then(() => {
        this.instance.post(`/app/appbanner/delete?ids=${id}`).then((res) => {
          if (res.code == 0) {
            this.$message.success('删除成功！')
            this.getList()
          }
        })
      })
    },
  },
}
</script>

<style lang="scss">
.List {
  height: 100%;
  // padding: 15px 10px;
  .banner-img {
    width: 50px;
    height: 50px;
  }
}
</style>
