
<template>
  <div class="bannerManage app-container">
    <div class="search">
      <div>
        <el-input
          v-model="query.bannerName"
          size="small"
          placeholder="请输入名称"
          class="item-width-200 "
        ></el-input>
        <el-select
          v-model="query.menuId"
          size="small"
          clearable
          class="item-width-200 ml10"
          placeholder="请选择栏目"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
         <el-select
          v-model="platformType"
          multiple
          size="small"
          clearable
          class="item-width-200 ml10"
          placeholder="请选择所属平台"
        >
          <el-option
            v-for="item in masterShopOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
        <el-button
        class="ml10"
        type="primary"
        size="small"
        icon="el-icon-search"
        @click="handleSearch"
      >
        搜索
      </el-button>
      </div>
      <el-button class="add-btn" type="primary" size="small" @click="handleAdd"
        >添加</el-button
      >
    </div>

    <el-table
      :data="tableData"
      tooltip-effect="dark"
      border
      v-loading="loading"
      style="width: 100%"
    >
     
      <el-table-column
        prop="bannerName"
        align="center"
        label="名称"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        prop="createTime"
        align="center"
        label="banner图"
        show-overflow-tooltip
      >
      <template slot-scope="scope">
          <img class="icon" :src="scope.row.bannerImg" alt="">
        </template>
      </el-table-column>
      <el-table-column
        prop="menuId"
        align="center"
        label="栏目"
        show-overflow-tooltip
      >
       <template slot-scope="scope">
          <span>{{scope.row.menuId |filterMenu(that)}}</span>
        </template>
      </el-table-column>
       <el-table-column
        prop="accountChildType"
        align="center"
        label="所属平台"
        fit
      >
      <template slot-scope="scope">
        {{filterMasterName(scope.row.platformType )}}
      </template>
      </el-table-column>
      <el-table-column
        prop="jumpUrl"
        align="center"
        label="跳转链接"
        show-overflow-tooltip
      >
      <template slot-scope="scope">
          <span>{{scope.row.jumpUrl || '-'}}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="sort"
        align="center"
        label="权重"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column prop="address" align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="text" size="small" @click="handleDel(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="page-box">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
        :current-page="query.currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="query.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>
  
  <script>
import { debounce } from "@/utils/index";
import {bannerList,delBanner,getMenuName } from '@/api/appConfig'
export default {
  name: "bannerManage",
  data() {
    return {
     that:this,
      tableData: [],
      options: [],
      loading: false,
      platformType:"",
      query: {
        platformType:null,
        menuId: "",
        bannerName: "",
        currentPage: 1,
        pageSize: 10,
      },
      title: "",
      total: 0,
    };
  },
  watch: {
    platformType(val) {
        if(val.length){
          this.query.platformType = val.join(',')
        }else{
          this.query.platformType = null
        }
      }
  },
  filters: {
    filterMenu: function(value,that) {
      let msg
      that.options.map(item=>{
          if(item.value == value){
            msg = item.label
          }
      })
      return msg;
    }
  },
  methods: {
    handleSearch(){
      this.query.currentPage = 1
      this.bannerList();

    },
    handleEdit(row){
      sessionStorage.setItem('bannerItem',JSON.stringify(row))
      // this.$router.push('/operationManage/bannerManage/editBanner')
      this.$router.push({
        path:'/operationManage/bannerManage/addBanner',
        query:{
          title:'编辑banner',
        }
      })
    },
    handleAdd(){
      this.$router.push({
        path:'/operationManage/bannerManage/addBanner',
      })
    },
    // 删除
     handleDel(id) {
       this.$confirm(`此操作将删除该数据, 是否继续?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delBanner({bannerId:id}).then(res=>{
            if(res.state == 'Success'){
               this.$notify({
                title: '成功',
                message: '操作成功',
                type: 'success'
              });
              this.bannerList()
            }
          })
        })
    },
    handleSizeChange(val) {
      this.query.currentPage = 1;
      this.query.pageSize = val;
    this.bannerList()
    },
    handleCurrentChange(val) {
      this.query.currentPage = val;
    this.bannerList()
    },
    bannerList(){
      this.loading = true
      bannerList(this.query).then(res=>{
        this.loading = false
        if(res.state == 'Success'){
          this.tableData = res.content.records
          this.total = res.content.total
        }
      })
    },
    // 获取分类列表
    getMenuName(){
      getMenuName().then(res=>{
        if(res.state == 'Success'){
            for (const key in res.content) {
              if (res.content.hasOwnProperty.call(res.content, key)) {
                  this.options.push({
                    label:res.content[key],
                    value:key
                  })              
              }
            }
        }
      })
    }
  },
  created() {
    this.getMenuName()
    this.bannerList()
  },
};
</script>
  
<style lang="scss" scoped>
.bannerManage {
  .search{
    justify-content: space-between;
  }
  .icon{
    width: 120px;
    height: 80px;
  }
}
</style>
  