<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-button type="primary" size="small" @click="select.page=1,getList()">查询</el-button>
              </div> -->
              <div>
                <el-button type="primary" size="small" @click="editAppointmentType('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:dateRange>
          <el-table-column align='center' label="可预约时间段" width="120" header-align="center">
            <template slot-scope="scope">
              <div v-if="scope.row.start_time">
                {{scope.row.start_time}}~{{scope.row.end_time}}
              </div>
              <div v-else></div>
            </template>
          </el-table-column>
        </template>
        <template v-slot:btns>
          <el-table-column align='center' fixed="right" label="操作" width="180" header-align="center">
            <template slot-scope="scope">
              <el-button type="primary" size="small" @click="editAppointmentType('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>

    <addType ref="addType" @refresh="getList"></addType>
  </div>
</template>

<script>
  import myMixins from "@/mixin/selectMixin.js";
  import addType from './components/addType.vue';
  import {
    index,
    destroy
  } from "@/api/book/type.js"
  export default {
    mixins: [myMixins],
    components: {
      addType
    },
    data() {
      return {
        select: {
          name: '',
          page: 1,
          page_size: 10,
        },
        list: [],
        total: 0,
        table_item: [{
          prop: 'name',
          label: '类型名称',
          align: 'left',
          width: 120,
        }, {
          prop: 'dateRange',
          label: '可预约时间段',
          align: 'center',
          width: 120,
        }, {
          prop: 'introduce',
          label: '简介',
          align: 'left'
        }]

      }
    },
    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,
          sort_name:'sort',
          sort_type:'ASC'
        })
        this.list = res.data
        this.total = res.total
      },

      editAppointmentType(type, id) {
        if (id) {
          this.$refs.addType.id = id
        }

        this.$refs.addType.type = type
        this.$refs.addType.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>
