<template>
  <div>

    <div>
      <div ref="lxHeader">
        <lx-header icon="md-apps" :text="$route.meta.title" style="margin-bottom: 10px; border: 0px; margin-top: 15px">
          <div slot="content">
            <div class="searchwrap" style="display: flex;align-items: center;">
              <div>
                <el-input v-model="select.name" placeholder="请输入名称"></el-input>
              </div>
              <div>
                <el-select v-model="select.position" placeholder="请选择显示位置" clearable>
                  <el-option v-for="item in position_options" :key="item.id" :label="item.value" :value="item.id">
                  </el-option>
                </el-select>
              </div>
              <div>
                <el-button type="primary" size="small" @click="select.page=1,getList()">查询</el-button>
              </div>
              <div>
                <el-button type="primary" size="small" @click="editBanner('add')">新增</el-button>
              </div>
            </div>
          </div>

        </lx-header>
      </div>
    </div>

    <div>
      <xy-table :list="list" :total="total" @pageIndexChange="pageIndexChange" @pageSizeChange="pageSizeChange" :table-item="table_item">
        <template v-slot:image_id>
          <el-table-column align='center' label="图片" width="100" header-align="center">
            <template slot-scope="scope">
              <el-image style="width: 70px; height: 40px" :src="scope.row.image.url" :preview-src-list="[scope.row.image.url]">
              </el-image>
            </template>
          </el-table-column>
        </template>
        <template v-slot:jump_type>
          <el-table-column align='center' label="跳转类型" width="120" header-align="center">
            <template slot-scope="scope">
              <div v-for="item in type_options">
                <div v-if="scope.row.jump_type===item.id">{{item.value}}</div>
              </div>
            </template>
          </el-table-column>
        </template>
        <template v-slot:position>
          <el-table-column align='center' label="显示位置" width="240" header-align="center">
            <template slot-scope="scope">
              <div v-for="item in position_options">
                <div v-if="scope.row.position===item.id">{{item.value}}</div>
              </div>
            </template>
          </el-table-column>
        </template>
        <template v-slot:btns>
          <el-table-column align='center' label="操作" width="180" header-align="center">
            <template slot-scope="scope">
              <el-button type="primary" size="small" @click="editBanner('editor',scope.row.id)">编辑</el-button>
              <el-popconfirm style="margin:0 10px" @confirm="deleteList(scope.row.id)" title="确定删除吗？">
                <el-button type="danger" size="small" slot="reference">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </template>
      </xy-table>
    </div>

    <add-banner ref="addBanner" @refresh="getList"></add-banner>
  </div>
</template>

<script>
  import addBanner from './components/addBanner.vue';
  import {
    index,
    destroy
  } from "@/api/info/banners.js"
  export default {
    components: {
      addBanner
    },
    data() {
      return {
        select: {
          name: '',
          page: 1,
          page_size: 10,
          position: ''
        },
        position_options: [{
          id: 1,
          value: '首页'
        }, {
          id: 2,
          value: '课程中心、我的课程、报名状态'
        }
        // , {
        //   id: 3,
        //   value: '公告'
        // },
        ],
        type_options: [{
          id: 1,
          value: '小程序'
        }, {
          id: 2,
          value: 'h5'
        }],
        list: [],
        total: 0,
        table_item: [{
          prop: 'name',
          label: '名称',
          align: 'left'
        }, {
          prop: 'image_id',
          label: '图片',
          align: 'center',
          width: 80,
          slot: 'imageshow'
        }, {
          prop: 'position',
          label: '显示位置',
          align: 'center',
          width: 240,
        }, {
          prop: 'jump_type',
          label: '跳转类型',
          align: 'center',
          width: 180,
        }, {
          prop: 'jump_url',
          label: '跳转链接',
          align: 'left',
          width: 220,
        }, {
          prop: 'sort',
          label: '排序',
          align: 'center',
          width: 80,
        }]

      }
    },
    created() {
      this.getList()
    },
    methods: {
      pageIndexChange(e){
        this.select.page = e
        this.getList()
      },
      pageSizeChange(e){
        this.select.page_size = e
        this.select.page = 1
        this.getList()
      },
      async getList() {
        const res = await index({
          page: this.select.page,
          page_size: this.select.page_size,
          filter: [{
            key: 'name',
            op: 'like',
            value: this.select.name
          }, {
            key: 'position',
            op: 'eq',
            value: this.select.position
          }],
          show_relation: ['image']
        })
        this.list = res.data
        this.total = res.total

      },
      editBanner(type, id) {
        if (id) {
          this.$refs.addBanner.id = id
        }
        this.$refs.addBanner.setOptions(this.position_options, this.type_options)
        this.$refs.addBanner.type = type
        this.$refs.addBanner.isShow = true
      },
      deleteList(id) {
        var that = this;
        destroy({
          id: id,
        }).then(response => {
          this.$Message.success('删除成功');
          this.getList()
        }).catch(error => {
          console.log(error)
          reject(error)
        })
      },
    }
  }
</script>

<style lang="scss" scoped>
  .searchwrap {
    display: flex;
    align-items: center;

    &>div {
      display: flex;
      align-items: center;
      margin-right: 10px;

      span {
        min-width: 70px;
      }
    }
  }
</style>
