<template>
  <div>
    <div class="search-bar">
      <Form @submit.native.prevent inline ref="searchForm" :model="searchForm" class="inline">
         <Form-item label="名称" prop="name">
            <Input v-model="searchForm.name" class="comWidth" />
        </Form-item>
        <Form-item label="作者" prop="author">
            <Input v-model="searchForm.author" class="comWidth" />
        </Form-item>
        <FormItem class="btnGroupStyle">
          <div>
            <Button class="btnStyle" @click="reset()">重置</Button>
            <Button type="primary" class="btnStyle" @click="filter">查询</Button>
          </div>
        </FormItem>
      </Form>
    </div>
    <div class="tableGroupStyle">
      <Table
        :columns="columns"
        :data="tableData"
        class="tableStyle"
      />
      <Page
        :total="total"
        v-if="total > 0"
        show-elevator
        show-sizer
        class="pageStyle"
        :current="searchForm.pageNo"
        :pageSize="searchForm.pageSize"
        @on-change="changePageNo"
      />
      <!-- @on-change="changePageNo"
        @on-page-size-change="changePageSize" -->
    </div>

  </div>
</template>
<script>
import navigatorApi from '@api/navigator.api'
import expandRow from './expandRow.vue';
import config from '@/config/index';
export default {
  props: {
    navId: {
      type: [Number, String],
      default: ''
    }
  },
  components: {
    expandRow
  },
  data() {
    return {
      total: 0,
      searchForm: {
        name: '',
        author: '',
        pageSize: 10,
        pageNo: 1
      },
      tableData: [],
      columns: [
        {
          align: 'center',
          title: '',
          width: 40,
          type: 'expand',
          render: (h, params) => {
            return h(expandRow, {
              props: {
                row: params.row,
                index: params.index
              },
              on: {
                select: (data) => {
                  this.$emit('selectPage', data);
                }
              }
            })
          }
        },
        {
          align: 'center',
          title: 'ID',
          key: 'id',
          width: 80,
        },
        {
          align: 'center',
          title: '名称',
          width: 160,
          key: 'name'
        },
        {
          align: 'center',
          title: '页面地址',
          width: 250
        },
        {
          align: 'center',
          title: '作者',
          key: 'author',
          width: 90
        },
        {
          align: 'center',
          title: '更新时间',
          key: 'updatedAt',
          width: 170
        },
        {
          align: 'center',
          title: '操作',
          key: 'operation'
        }
      ]
    }
  },
  created() {
    this.query();
  },
  methods: {
    filter() {
      this.searchForm.pageNo = 1;
      this.searchForm.pageSize = 10;
      this.query();
    },
    reset() {
      this.searchForm = {
        name: '',
        author: '',
        pageSize: 10,
        pageNo: 1
      }
      this.query();
    },
    async query() {
      const queryParams = Object.assign({}, this.searchForm);
      if (this.navId) {
        queryParams.id = this.navId;
      }
      const res = await navigatorApi.getListWithRelation(queryParams);
      this.total = res.total || 0;
      if (res.data.length) {
        for (let i = 0; i < res.data.length; i++) {
          res.data[i]['_expanded'] = true;
        }
      }
      this.tableData = res.data || []
    },
    changePageNo(pageNo) {
      this.searchForm.pageNo = pageNo;
      this.query();
    },
  }
}
</script>
<style scoped lang="less">
.comWidth {
  width: 200px;
}
.search-bar{
  width: 100%;
  height: 50px;
  .btnGroupStyle{
    button{
      margin-right: 6px;
    }
  }
  /deep/ .ivu-form-item{
    margin-bottom: 0;
  }
  /deep/ .ivu-form-item-label {
    font-weight: bold;
    display: inline-block;
  }
  /deep/.ivu-form-item-content {
    display: inline-block;
  }
}
.tableGroupStyle {
    background: #fff;
    .toolBarStyle {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
    .pageStyle {
      margin-top: 20px;
      width: 100%;
      text-align: right;
    }
}
/deep/ .ivu-table-expanded-cell{
  padding: 0;
}
</style>