<template>
  <div>
    <SearchForm v-show="showSearch" :value.sync="searchForm" />
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" @click="handleAdd">新增</el-button>
        <el-button type="warning" icon="el-icon-download" @click="handleExport">导出</el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" :columns="columns" @queryTable="_getTableList" />
    </el-row>
    <el-table :data="tableList">
      <el-table-column type="selection" width="50" align="center" />
      <template v-for="item in columns">
        <el-table-column v-if="item.visible" :key="item.prop" :label="item.label" align="center" min-width="100" :prop="item.prop" />
      </template>
      <el-table-column label="操作" fixed="right" align="center" width="160">
        <template slot-scope="scope">
          <el-button type="text" @click="handleDetail(scope.row)">详情</el-button>
          <el-button type="text" @click="handleCheck(scope.row)">审核</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination :total="total" :page.sync="searchForm.pageNum" :limit.sync="searchForm.pageSize" @pagination="_getTableList" />

    <DialogDetail ref="DialogDetail" />
    <DialogAudit ref="DialogAudit" />
    <DialogAdd ref="DialogAdd" />
  </div>
</template>

<script>
import DialogDetail from './DialogDetail.vue';
import DialogAudit from './DialogAudit.vue';
import DialogAdd from './DialogAdd.vue';
import SearchForm from './SearchForm.vue';
import { defaultColumns } from './columns.js';
export default {
  components: {
    SearchForm, DialogDetail, DialogAudit, DialogAdd
  },
  props: {
    type: {
      type: String,
      default: '1'
    }
  },
  data() {
    return {
      searchForm: {
        expenseUser: undefined,
        expenseDate: [],
        pageNum: 1,
        pageSize: 20
      },
      showSearch: true,
      tableList: [],
      total: 0,
      columns: []
    };
  },
  watch: {
    type: {
      handler() {
        const str = localStorage.getItem(`${this.$route.name}-${this.type}-table-columns`);
        this.columns = str ? JSON.parse(str) : defaultColumns[this.type];
        this._getTableList();
      },
      immediate: true
    }
  },
  methods: {
    async _getTableList() {
      const params = { ...this.searchForm };
      // api.list(params)
      this.tableList = [];
      for (let i = 0; i < 20; i++) {
        this.tableList.push({ applyUser: `数据${i + 1}` });
      }
    },
    handleDetail(row) {
      this.$refs.DialogDetail && this.$refs.DialogDetail.init({ ...row, type: this.type });
    },
    handleCheck(row) {
      this.$refs.DialogAudit && this.$refs.DialogAudit.init({ ...row, type: this.type });
    },
    handleExport() {

    },
    handleAdd() {
      this.$refs.DialogAdd && this.$refs.DialogAdd.init({ type: this.type });
    }
  }
};
</script>

<style lang="scss" scoped></style>
