<template>
  <div class="pomentWrap">
    <el-row class="topBar">
      <el-col>
        <el-form :inline="true" :model="searchData">
          <el-form-item label>
            <el-input v-model="searchData.resourceName" placeholder="请输入名称"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="searchFun()">
              <i class="el-icon-search"></i> 搜索
            </el-button>
            <el-button type="primary" @click="resetFun()">
              <i class="el-icon-refresh"></i> 重置
            </el-button>
          </el-form-item>
          <el-form-item class="fr">
            <el-button plain type="primary" @click="addFun()">
              <i class="el-icon-plus"></i> 新增
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <el-table
      :data="tableData"
      border
      style="width:100%;"
      v-loading="loading"
      :default-expand-all="true"
    >
      <el-table-column type="expand" class="sonTd">
        <template slot-scope="props">
          <el-table
            v-if="props.row.childList.length > 0"
            :data="props.row.childList"
            border
            style="width:100%;"
            class="sonTable"
            :show-header="false"
          >
            <el-table-column prop="resourceName" label="名称"></el-table-column>
            <el-table-column prop="url" label="接口路径"></el-table-column>
            <el-table-column prop="method" label="访问方式"></el-table-column>
            <el-table-column label="操作" width="270" align="center">
              <template slot-scope="scope">
                <el-button type="success" @click="editFun(scope.row)" size="small" plain>
                  <i class="el-icon-edit"></i> 编辑
                </el-button>
                <el-button type="danger" @click="delFun(scope.row)" size="small" plain>
                  <i class="el-icon-delete"></i> 删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </template>
      </el-table-column>
      <el-table-column prop="resourceName" label="名称"></el-table-column>
      <el-table-column prop="url" label="接口路径"></el-table-column>
      <el-table-column prop="method" label="访问方式"></el-table-column>
      <el-table-column label="操作" width="270" align="center">
        <template slot-scope="scope">
          <el-button type="success" @click="editFun(scope.row)" size="small" plain>
            <i class="el-icon-edit"></i> 编辑
          </el-button>
          <el-button type="danger" @click="delFun(scope.row)" size="small" plain>
            <i class="el-icon-delete"></i> 删除
          </el-button>
          <el-button type="primary" @click="addFun(1,scope.row)" size="small" plain>
            <i class="el-icon-circle-plus-outline"></i> 添加
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- <el-pagination
      background
      layout="total,sizes, prev, pager, next, jumper"
      :total="tableData.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="PAGE.page"
      :page-size="PAGE.size"
    ></el-pagination>-->

    <APIMgEdit
      v-if="dialogVisible"
      :dialogVisible="dialogVisible"
      :editForm="formData"
      :title="dialogTit"
      @dialogFun="closeDialog"
      @refreshTableData="getTableData"
      :key="zjKey"
    ></APIMgEdit>
  </div>
</template>
<style lang="scss">
.pomentWrap .sonTable table tr {
  background: #f3fafc;
  &:nth-child(1) td {
    border-top: 0;
  }
  &:last-child td {
    border-bottom: 0;
  }
}
.pomentWrap .el-table__expanded-cell {
  padding: 0 0 0 47px !important;
}
</style>
<script lang="ts">
import { Component, Vue, Provide } from "vue-property-decorator";
import APIMgEdit from "./APIMgEdit.vue";
import METHOD from '@/utils/methods'
@Component({
  components: { APIMgEdit }
})
export default class APIMg extends Vue {
  @Provide() tableData: any = [];
  @Provide() PAGE: any = { page: 1, size: 10 };
  @Provide() searchData: any = { resourceName: "" };
  @Provide() loading: Boolean = false;
  //编辑组件
  @Provide() dialogVisible: Boolean = false;
  @Provide() dialogTit: String = "新增";
  @Provide() formData: any = {};
  @Provide() zjKey: any = 0;
  getTableData() {
    let that = this,
      param = Object.assign({}, that.PAGE, that.searchData);
    that.loading = true;
    (this as any).$axios
      .get(
        `/resource/getResourcesList/REST-API?resourceName=${param.resourceName}&page=${param.page}&size=${param.size}`
      )
      .then((res: any) => {
        that.loading = false;
        if( res.code == 0 ) that.tableData = res.data;
      });
  }
  searchFun() {
    this.PAGE = { page: 1, size: 10 };
    this.getTableData();
  }
  resetFun() {
    this.PAGE = { page: 1, size: 10 };
    this.searchData.resourceName = "";
    this.getTableData();
  }
  addFun(w: number, row: any) {
    let that = this;
    if (w == 1) {
      //增加子级
      that.formData.parentId = row.id;
      that.formData.levels = row.levels + 1;
    } else {
      //增加顶级
      that.formData.parentId = 0;
      that.formData.levels = 0;
    }
    that.dialogTit = "新增";
    that.zjKey++;
    that.dialogVisible = true;
  }
  editFun(row: any) {
    this.dialogTit = "编辑";
    this.formData = Object.assign({}, row);
    this.zjKey++;
    this.dialogVisible = true;
  }
  delFun(row: any) {
    let that = this as any;
    METHOD.deleteFun(
      that,
      `/resource/delResourcesById/${row.id}`,
      function() {
        that.getTableData();
      }
    );
  }
  handleSizeChange(val: any) {
    this.PAGE.size = val;
    this.getTableData();
  }
  handleCurrentChange(val: any) {
    this.PAGE.page = val;
    this.getTableData();
  }
  closeDialog() {
    //关闭窗口
    this.dialogVisible = false;
    this.formData = {};
  }
  created() {
    this.getTableData();
  }
}
</script>
