<!--
 * @Author: 一日看尽长安花
 * @since: 2019-10-12 15:43:18
 * @LastEditTime : 2020-01-08 22:51:466
 * @LastEditors  : 一日看尽长安花安花
 * @Description: 后台管理页面的自动生成，
 * 主要暴露了分页方法、数据表格搜索方法、条件查询方法、增删改方法
 * 以及 查询面板插槽，功能按钮组插槽，编辑对话框插槽
 -->
<template>
  <div>
    <search-pane
      :metadata="metadata"
      @filter-search="filterSearch"
      @handle-create="handleCreate"
    >
      <template #filter-condition="{filterData:filterData}">
        <slot name="filter-condition" :filter-data="filterData"> </slot>
      </template>
      <template #operation-btn-group>
        <slot name="operation-btn-group"> </slot>
      </template>
    </search-pane>
    <data-table
      :loading="loading"
      :metadata="metadata"
      :tabledata="tabledata"
      :search-method="searchMethod"
      @update:tabledata="$emit('update:tabledata', $event)"
      @pagination="pagination"
      @handle-edit="handleEdit"
      @delete-data="$emit('delete-data')"
    ></data-table>
    <edit-dialog
      :metadata="metadata"
      :dialog-visible.sync="dialogVisible"
      :dialog-title="dialogTitle"
      :operation-type="operationType"
      :dialog-data="dialogData"
      @create-data="$emit('create-data')"
      @update-data="$emit('update-data')"
    >
      <!-- #dialog-form-item等价v-slot:dialog-form-item 语法
      详情参照 解构插槽 Prop 章节 -->
      <template #dialog-form-item="{dialogData:dialogData}">
        <slot :dialog-data="dialogData" name="dialog-form-item"></slot>
      </template>
    </edit-dialog>
  </div>
</template>

<script>
import SearchPane from './SearchPane';
import DataTable from './DataTable';
import EditDialog from './EditDialog';

export default {
  name: 'TableViews',
  components: { SearchPane, DataTable, EditDialog },
  props: {
    // 表格元数据
    metadata: {
      type: Object,
      default() {
        return {};
      }
    },
    // 表格数据
    tabledata: {
      type: Object,
      default() {
        return {
          data: [],
          total: 0
        };
      }
    },
    // 加载中的遮罩层
    loading: {
      type: Boolean,
      default: true
    },
    searchMethod: {
      type: Function,
      default() {
        return this.table.data;
      }
    }
  },
  data() {
    return {
      // 查询参数：包括搜索和分页参数
      queryParams: {},
      dialogData: {},
      dialogVisible: false,
      // 对话框标题
      dialogTitle: '创建',
      // 对话框操作类型
      operationType: 'create'
    };
  },
  methods: {
    pagination(queryParams) {
      this.queryParams = Object.assign({}, queryParams);
      this.$emit('pagination', this.queryParams);
    },
    filterSearch(filterData) {
      this.queryParams = Object.assign(this.queryParams, filterData);
      this.$emit('filter-search', Object.assign({}, this.queryParams));
    },
    handleCreate() {
      this.operationType = 'create';
      this.dialogTitle = '创建';
      this.dialogVisible = true;
      this.dialogData = {};
    },
    handleEdit(index, row) {
      this.operationType = 'update';
      this.dialogTitle = '修改';
      this.dialogVisible = true;
      this.dialogData = Object.assign({}, row);
    }
  }
};
</script>
<style>
.filter-container {
  margin-top: 1rem;
}

.filter-item-container {
  display: inline-block;
  margin: 0.15em;
}

.filter-item-container .el-cascader {
  top: -4px;
}

.filter-btn-group {
  margin: 0.15em;
}
</style>
