<style>
.eagle-scheme {
  padding: 0px;
}
.eagle-scheme__card {
  border: 1px solid #F5F5F5;
  border-radius: 2px;
  background-color: #fff;
  padding: 10px;
  transition: all .3s ease;
  margin-bottom: 10px;
}
.eagle-scheme__card  .eagle-search {
  padding-top: 20px;
  padding-bottom: 10px;
}
.eagle-scheme__action {
  padding-bottom: 10px;
}
.eagle-scheme__table .eagle-scheme__table-btn:not(:first-child) {
  padding-left: 10px;
  margin-left: 0px;
}
.eagle-scheme__pagination {
  text-align: right;
  padding-top: 10px;
}
</style>

<template>
  <div class="eagle-scheme">
    <div class="eagle-scheme__card">
      <eagle-search :list="_searchList"></eagle-search>
    </div>
    <div class="eagle-scheme__action">
      <el-button type="primary" size="small" @click="showDialog">新增</el-button>
    </div>
    <div class="eagle-scheme__table">
      <eagle-table :list="_tableList" :tableProps="tableProps" :value="tableData">
        <template v-if="$scopedSlots['table-append'] || $slots['table-append']">
          <slot name="table-append" slot="append"></slot>
        </template>
        <template v-else>
          <el-table-column slot="append" prop="$operation" label="操作" min-width="140">
            <slot v-if="$scopedSlots['table-append-btn'] || $slots['table-append-btn']" name="table-append-btn"></slot>
            <el-button class="eagle-scheme__table-btn" type="text" icon="el-icon-edit" title="编辑" @click="showDialog"></el-button>
            <eagle-confirm class="eagle-scheme__table-btn" title="是否删除？">
              <el-button type="text" icon="el-icon-delete" title="删除"></el-button>
            </eagle-confirm>
          </el-table-column>
        </template>
      </eagle-table>
      <div class="eagle-scheme__pagination">
        <el-pagination size="small" @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
        :page-size="100" :total="400" v-bind="paginationProps"></el-pagination>
      </div>
    </div>
    <el-dialog :title="dialogProps.title || '标题'" :visible.sync="dialogVisible" v-bind="dialogProps">
      <eagle-form :list="_formList" :formProps="formProps" :span="12" @cancel="hideDialog"></eagle-form>
    </el-dialog>
  </div>
</template>

<script>
import { format, objExclude, generateListSpace } from './parser';

export default {
  name: 'Scheme',
  props: {
    // 配置列表
    list: Array,
    // 搜索表单配置
    searchList: Array,
    // 表单配置
    formList: Array,
    // 表格配置
    tableList: Array,
    // 表格参数
    tableProps: {
      type: Object,
      default() {
        return { size: 'small', border: true }
      }
    },
    // 表单参数
    formProps: {
      type: Object,
      default() {
        return { size: 'small', 'label-width': '90px' }
      }
    },
    // 分页参数
    paginationProps: {
      type: Object,
      default() {
        return { 'page-sizes': [10, 20, 50], layout: 'total, sizes, prev, pager, next, jumper' }
      }
    },
    // 弹出框参数
    dialogProps: {
      type: Object,
      default() {
        return { width: '65%' }
      }
    }
  },
  data() {
    return {
      // 搜索表单配置
      _searchList: [],
      // 表单配置
      _formList: [],
      // 表格配置
      _tableList: [],
      // 当前页
      currentPage: 1,
      // 弹出框状态
      dialogVisible: false,
      tableData: [
        { name: '赵伯', code: 'U00001', type: 'admin', sort: 0, status: 'active' },
        { name: '钱仲', code: 'U00002', type: 'user', sort: 1, status: 'active' },
        { name: '孙叔', code: 'U00003', type: 'user', sort: 2, status: 'active' },
        { name: '李季', code: 'U00004', type: 'user', sort: 3, status: 'active' },
      ]
    };
  },
  created() {
    if (this.list instanceof Array) {
      const { search = [], form = [], table = [] } = generateListSpace(this.list);
      this._searchList = search;
      this._formList = form;
      this._tableList = table;
    } else {
      this._searchList = this.searchList || [];
      this._formList = this.formList || [];
      this._tableList = this.tableList || [];
    }
  },
  methods: {
    showDialog() {
      this.dialogVisible = true;
    },
    hideDialog() {
      this.dialogVisible = false;
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
    }
  }
};
</script>
