<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="handleEdit">新建错误码</el-button>
        </div>
        <div class="table-wrap">
          <el-table :data="tableData" style="width: 100%">
            <!-- <el-table-column prop="interfaceName" label="接口名称" show-overflow-tooltip> </el-table-column>
            <el-table-column label="项目/分组" show-overflow-tooltip>
              <template slot-scope="scope">
                {{ scope.row.projectAndGroup }}
              </template>
            </el-table-column> -->
            <el-table-column label="错误码" show-overflow-tooltip>
              <template slot-scope="scope">
                {{ scope.row.errorCode }}
              </template>
            </el-table-column>
            <el-table-column label="错误消息" show-overflow-tooltip>
              <template slot-scope="scope">
                {{ scope.row.errorInfo }}
              </template>
            </el-table-column>
            <el-table-column label="操作" show-overflow-tooltip>
              <template slot-scope="scope">
                <el-button @click="handleEdit(scope.$index, scope.row)" type="text">编辑</el-button>
                <!-- <el-button @click="handleDel(scope.$index, scope.row)" type="text">删除</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>
    <error-code-edit :editRow="editRow" :showDialog="showDialog" @hideDialog="hideDialog"> </error-code-edit>
  </div>
</template>
<script>
import navBread from '@/components/nav/nav-bread.vue';
import errorCodeEdit from '@/components/error/error-code-edit.vue';
import { postRequest } from '@/api/api';
import { _debounce } from '@/assets/js/public';
import errMsg from '@/assets/js/error';
import showMsg from '@/assets/js/showmsg';
export default {
  name: 'errorCode',
  components: {
    navBread,
    errorCodeEdit
  },
  data() {
    return {
      // 面包屑参数
      navPath: [
        {
          name: '错误码',
          path: ''
        }
      ],
      searchValue: '',
      tableData: [
        {
          errorSettingId: '1',
          interfaceName: '测试',
          projectAndGroup: '会员',
          errorCode: '404',
          errorInfo: '页面不存在'
        }
      ],
      pageSize: 20,
      currentPage: 1,
      totalCount: 0,
      showDialog: false,
      editIndex: '',
      editRow: {}
    };
  },
  methods: {
    /**
     *  搜索
     */
    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();
    },
    /**
     * 编辑
     */
    handleEdit(index, row) {
      const that = this;
      that.editIndex = index;
      that.editRow = row;
      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();
    },
    /**
     * 删除
     */
    handleDel(index, row) {
      const that = this;
      that
        .$confirm(`确认删除吗？`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        .then(() => {
          that.postDel(index, row);
        })
        .catch(() => {});
    },
    postDel(index, row) {
      const that = this;
      let para = {
        errorSettingId: row.errorSettingId
      };
      postRequest('/gateway-manage/delete-error-setting', para)
        .then(res => {
          let resData = res.data;
          if (resData.code == '0000') {
            showMsg.showmsg('删除成功', 'success');
            that.getData();
            return;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },
    /**
     *  获取数据
     */
    getData() {
      const that = this;
      let para = {
        search: that.searchValue,
        pageNum: that.currentPage,
        pageSize: that.pageSize
      };
      postRequest('/gateway-manage/page-error-setting', 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', 6);
    that.getData();
  }
};
</script>

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