<!--
 * @Descripttion: 当前组件信息
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2019-07-22 19:22:46
 * @LastEditors: 无尘
 * @LastEditTime: 2019-09-30 15:38:56
 -->
<template>
  <div class="api-body-content border-box">
    <div class="common-nav">
      <nav-bread :navPath="navPath"></nav-bread>
    </div>
    <div class="api-body-main common-body-main border-box">
      <div class="api-main-content border-box">
        <div class="search-wrap m-b-20 flex flex-space-between">
          <el-input clearable class="w-320" placeholder="请输入分组名称" v-model="searchValue" @keyup.native="value => searchData(value)" @clear="clearInput">
            <i slot="prefix" class="el-input__icon el-icon-search"></i>
          </el-input>
          <el-button type="primary" @click="editProgram">新建分组</el-button>
        </div>
        <div class="table-wrap">
          <el-table :data="tableData" style="width: 100%">
            <el-table-column prop="groupName" label="分组名称" show-overflow-tooltip> </el-table-column>
            <el-table-column label="描述" show-overflow-tooltip>
              <template slot-scope="scope">
                {{ scope.row.remark }}
              </template>
            </el-table-column>
            <el-table-column label="创建时间" show-overflow-tooltip>
              <template slot-scope="scope">
                <div class="color-606266 line-18">{{ scope.row.createTime | timeStampToYmd }}</div>
                <div class="color-606266 line-18">{{ scope.row.createTime | timeStampToHms }}</div>
              </template>
            </el-table-column>
            <el-table-column label="操作" show-overflow-tooltip>
              <template slot-scope="scope">
                <el-button @click="toApiList(scope.$index, scope.row)" type="text">API列表</el-button>
                <el-button @click="handleEdit(scope.$index, scope.row)" type="text">编辑</el-button>
                <el-button @click="handleDel(scope.$index, scope.row)" type="text">删除</el-button>
                <el-button type="text" @click="createApi(scope.$index, scope.row)">新建API</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="pagination text-right m-t-14" v-if="!!totalCount">
            <el-pagination small :page-size="pageSize" :current-page="currentPage" layout="prev, pager, next" :total="totalCount" @size-change="handleSizeChange" @current-change="handleCurrentChange"> </el-pagination>
          </div>
        </div>
      </div>
    </div>
    <new-api-group :editRow="editRow" :showDialog="showDialog" @hideDialog="hideDialog"> </new-api-group>
  </div>
</template>
<script>
import navBread from '@/components/nav/nav-bread.vue';
import newApiGroup from '@/components/program/new-api-group.vue';
import { getRequest } from '@/api/api';
import { _debounce } from '@/assets/js/public';
import showMsg from '@/assets/js/showmsg';
import errMsg from '@/assets/js/error';
export default {
  name: 'apiProgramGroup',
  components: {
    navBread,
    newApiGroup
  },
  data() {
    return {
      // 面包屑参数
      navPath: [
        {
          name: 'API管理',
          path: '/apiProgram'
        },
        {
          name: `${this.$route.query.projectName}项目列表`,
          path: 'apiProgram'
        },
        {
          name: `分组列表`,
          path: ''
        }
      ],
      searchValue: '',
      tableData: [
        /* {
          interfaceGroupId: '1',
          groupName: '测试',
          remark: '234',
          createTime: 1564455643252
        } */
      ],
      pageSize: 20,
      currentPage: 1,
      totalCount: 0,
      showDialog: false,
      editIndex: '',
      editRow: {}
    };
  },
  methods: {
    /**
     * 创建 API
     */
    createApi(index, row) {
      const that = this;
      that.$router.push(`/createApi?projectId=${row.projectId}&interfaceGroupId=${row.interfaceGroupId}`);
    },
    /**
     * 编辑环境
     */
    editProgram() {
      const that = this;
      that.showDialog = true;
    },
    hideDialog(obj) {
      const that = this;
      if (!obj) {
        that.editIndex = '';
        that.editRow = {};
        that.showDialog = false;
        return false;
      }
      that.currentPage = 1;
      that.getData();
      // }

      that.editIndex = '';
      that.editRow = {};
      that.showDialog = false;
      that.$forceUpdate();
    },
    /**
     *  搜索
     */
    searchData: _debounce(function(value) {
      const that = this;
      that.currentPage == 1;
      that.getData();
    }, 500),

    /**
     *  搜索清除
     */
    clearInput() {
      this.currentPage = 1;
      this.getData();
    },
    /**
     *  分页
     */
    handleSizeChange(val) {
      this.pageSize = val;
      this.getData();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getData();
    },
    /**
     * api列表
     */
    toApiList(index, row) {
      const that = this;
      that.$router.push(`/groupApiList?projectId=${row.projectId}&projectName=${that.$route.query.projectName}&interfaceGroupId=${row.interfaceGroupId}&groupName=${row.groupName}`);
    },
    /**
     * 编辑
     */
    handleEdit(index, row) {
      const that = this;
      that.editIndex = index;
      that.editRow = row;
      that.showDialog = true;
    },

    /**
     * 删除
     */
    handleDel(index, row) {
      const that = this;
      that
        .$confirm(`【${row.groupName}】下的接口将全部被删除，确认删除吗？`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        .then(() => {
          that.postDelData(index, row);
        })
        .catch(() => {});
    },
    postDelData(index, row) {
      const that = this;
      let para = {
        interfaceGroupId: row.interfaceGroupId
      };
      getRequest('/gateway-manage/delete-api-group', para)
        .then(res => {
          let resData = res.data;
          if (resData.code == '0000') {
            showMsg.showmsg('删除成功', 'success');
            that.tableData.splice(index, 1);
            return;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },
    /**
     *  获取数据
     */
    getData() {
      const that = this;
      let para = {
        search: that.searchValue,
        pageSize: that.pageSize,
        pageNum: that.currentPage,
        projectId: that.$route.query.projectId
      };
      getRequest('/gateway-manage/page-api-group', para)
        .then(res => {
          let resData = res.data;
          if (resData.code == '0000') {
            that.tableData = resData.result.result;
            that.totalCount = resData.result.totalCount;
            return;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    }
  },
  watch: {
    $route: {
      handler: function(val, oldVal) {},
      // 深度观察监听
      deep: true
    }
  },

  /* 接收数据 */
  mounted() {
    const that = this;
    that.$emit('showTab', 21);
    that.getData();
  }
};
</script>

<style lang="less" scoped>
.w-320 {
  width: 320px;
}
.m-t-14 {
  margin-top: 14px;
}
</style>
