<template>
  <folder-page type="api" @click="folderHandleSelected" ref="folder">
    <div v-if="queryParams.groupId && queryParams.groupId !== 0">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="接口名称" prop="name">
          <el-input v-model="queryParams.name" placeholder="请输入接口名称" clearable @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item label="请求类型" prop="method">
          <el-select v-model="queryParams.method" placeholder="请输入接口请求类型" clearable @keyup.enter.native="handleQuery">
            <el-option v-for="dict in dict.type.http_method" :key="dict.value" :label="dict.label" :value="dict.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="接口路径" prop="uri">
          <el-input v-model="queryParams.uri" placeholder="请输入接口路径" clearable @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleImport">导入</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
      <el-table v-loading="loading" :data="apiList">
        <el-table-column label="接口名称" align="center" prop="name"/>
        <el-table-column label="接口请求类型" align="center" prop="method"/>
        <el-table-column label="接口路径" align="center" prop="uri"/>
        <el-table-column label="创建时间" align="center" prop="createTime"/>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>
    </div>
    <el-empty v-else>
      <el-button type="primary" plain icon="el-icon-plus" @click="handleImport">导入</el-button>
    </el-empty>
    <el-dialog title="导入接口" :visible.sync="dialogVisible" :close-on-click-modal="false" destroy-on-close>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="swagger" prop="url">
          <el-input v-model="form.url" placeholder="请输入swagger文档链接"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </folder-page>
</template>

<script>
import {listApi, delApi, importApi} from "@/api/test/api";
import FolderPage from "@/components/FolderPage/index.vue";

export default {
  name: "Api",
  components: {FolderPage},
  dicts: ['http_method'],
  data() {
    return {
      loading: false,
      showSearch: true,
      total: 0,
      apiList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        groupId: null,
        name: null,
        method: null,
        uri: null,
      },
      dialogVisible: false,
      form: {},
      rules: {
        url: [{required: true, message: "swagger文档链接不能为空", trigger: "blur"}]
      }
    };
  },
  methods: {
    folderHandleSelected(id) {
      if (id) {
        this.queryParams.groupId = id;
        this.getList();
      } else {
        this.apiList = [];
      }
    },
    getList() {
      this.loading = true;
      listApi(this.queryParams).then(response => {
        this.apiList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleAdd() {
      this.$tab.openPage("添加接口", "/api/add", {groupId: this.queryParams.groupId});
    },
    handleImport() {
      this.dialogVisible = true;
    },
    handleUpdate(row) {
      this.$tab.openPage("修改接口", "/api/edit", {id: row.id});
    },
    handleDelete(row) {
      this.$modal.confirm('是否确认删除接口？').then(function () {
        return delApi(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      });
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          const loading = this.$loading({
            lock: true,
            text: 'Loading',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
          });
          importApi({url: this.form.url}).then((res) => {
            loading.close();
            this.$message.success("导入成功")
            this.$refs.folder.getGroup();
            this.cancel();
          })
        }
      })
    },
    cancel() {
      this.dialogVisible = false
    }
  }
};
</script>

<style scoped lang="scss">
.el-aside {
  padding: 20px 0 0;
}
</style>
