<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.title" placeholder="请输入标题" clearable />
              </div>
              <div>
                <el-button type="primary" size="small" @click="select.page=1,getList()">查询</el-button>
              </div>
              <div>
                <el-button type="primary" size="small" @click="editTimeEvent('add')">新增</el-button>
              </div>
            </div>
          </div>
        </lx-header>
      </div>
    </div>

    <div>
      <xy-table :list="list" :total="total" :table-item="table_item" @pageIndexChange="pageIndexChange" @pageSizeChange="pageSizeChange">
        <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="editTimeEvent('editor',scope.row.id)">编辑</el-button>
              <el-popconfirm title="确定删除吗？" style="margin:0 10px" @confirm="deleteList(scope.row.id)">
                <el-button slot="reference" type="danger" size="small">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </template>
      </xy-table>
    </div>

    <add-time-event ref="addTimeEvent" @refresh="getList" />
  </div>
</template>

<script>
import addTimeEvent from './components/addTimeEvent.vue'
import { index, destroy } from '@/api/timeEvent/index.js'

export default {
  components: {
    addTimeEvent
  },
  data() {
    return {
      select: {
        title: '',
        page: 1,
        page_size: 10
      },
      list: [],
      total: 0,
      table_item: [{
        prop: 'title',
        label: '标题',
        align: 'left',
        minWidth: 200
      }, {
        prop: 'sort',
        label: '排序',
        align: 'center',
        width: 100
      }, {
        prop: 'created_at',
        label: '创建时间',
        align: 'center',
        width: 180
      }]
    }
  },
  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()
    },
    editTimeEvent(type, id) {
      if (type === 'editor') {
        this.$refs.addTimeEvent.id = id
      }
      this.$refs.addTimeEvent.type = type
      this.$refs.addTimeEvent.isShow = true
    },
    async getList() {
      const params = {
        page_size: this.select.page_size,
        page: this.select.page,
        sort_name: 'sort',
        sort_type: 'ASC'
      }
      if (this.select.title) {
        params.filter = [{
          key: 'title',
          op: 'like',
          value: this.select.title
        }]
      }
      const res = await index(params, false)
      this.list = res.data
      this.total = res.total
    },
    deleteList(id) {    
      destroy({
        id: id
      }).then(response => {
        this.$message.success('删除成功')
        this.getList()
      }).catch(error => {
        console.log(error)
        this.$message.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>

