<template>
  <div class="autoIndex">
    <div class="pageTitle">
      网关路由
      <span>
        查询结果：共查到{{num}}条满足条件的数据
      </span>
    </div>
    <div class="searchArea">
      <el-input v-model="search.blurNamepace"
                size="small"
                style="width:200px"
                placeholder="命名空间"></el-input>
      <el-input v-model="search.blurServiceName"
                size="small"
                style="width:200px"
                placeholder="网关服务名称"></el-input>
      <el-button type="primary"
                 size="small"
                 @click="queryData"
                 icon="el-icon-search">查询</el-button>
      <!-- <el-button class="floatR"
                 type="primary"
                 size="small"
                 @click="addRow"
                 icon="el-icon-plus">新增</el-button> -->
    </div>
    <div class="tableArea">
      <el-table v-loading="tableLoading"
                ref="singleTable"
                :data="tableData"
                :height="tableHeight"
                border
                drag-column
                class="yt-table-no-srcoll"
                highlight-current-row
                style="width: 100%">
        <el-table-column prop="namespace"
                         label="命名空间"
                         min-width="120"></el-table-column>
        <el-table-column prop="serviceName"
                         label="服务名"
                         min-width="120"></el-table-column>
        <el-table-column prop="serviceStatus"
                         label="状态"
                         min-width="120"></el-table-column>
        <el-table-column label="操作"
                         align="center"
                         width="220">
          <template slot-scope="scope">
            <el-button type="text"
                       size="small"
                       @click="checkRow(scope.row,'show')">
              查看
            </el-button>
            <el-button type="text"
                       size="small"
                       @click="checkRow(scope.row,'edit')">编辑</el-button>
            <el-button type="text"
                       size="small"
                       @click="deleteRow(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog :close-on-click-modal="closeFlag"
               v-if="dialogFormVisible"
               width="80%"
               :title="title"
               :visible.sync="dialogFormVisible">
      <el-form label-position="left"
               :model="form"
               :rules="rules"
               ref="form"
               :label-width="formLabelWidth">
        <el-form-item label="网关服务名"
                      prop="serviceName">
          <el-input v-model="form.serviceName"
                    :readonly="!isEdit"></el-input>
        </el-form-item>
        <el-form-item label="访问权重"
                      prop="accessWeight">
          <el-input v-model="form.accessWeight"
                    :readonly="!isEdit"></el-input>
        </el-form-item>
        <el-form-item label="描述"
                      prop="routeDesc">
          <el-input v-model="form.routeDesc"
                    :readonly="!isEdit"></el-input>
        </el-form-item>
        <el-form-item label="路由规则"
                      prop="rules">
          <monaco-editor :language="'text'"
                         :codes="form.routeRule"
                         @onMounted="textOnMounted"
                         @onCodeChange="textOnCodeChange" />
        </el-form-item>
      </el-form>
      <div slot="footer"
           class="dialog-footer"
           v-if="isEdit">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="setForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import MonacoEditor from '@/components/monacoEditor'
export default {
  name: 'ConfigList',
  components: {
    MonacoEditor
  },
  data () {
    return {
      isEdit: false,
      num: 10,
      closeFlag: false,
      tableLoading: false, // 表格loading
      title: '新增',
      restaurants: [],
      tableHeight: '750px',
      tableData: [
        {
          NameSpace: '1111',
          ServerName: '32222',
          status: 'up'
        }
      ],
      userTag: '',
      search: {
        blurServiceName: '',
        blurNamepace: ''

      },
      dialogFormVisible: false,
      form: {
        scId: '',
        namespace: '',
        serviceStatus: '',
        serviceConfigKey: '',
        serviceName: '',
        accessWeight: '',
        routeDesc: '',
        routeRule: ''
      },
      formLabelWidth: '80px',
      rules: {
        // DataId: [
        //   { required: true, message: '请输入DataId', trigger: 'blur' }
        // ]
      },
      tagList: [{ tagname: '测试1' }, { tagname: '测试2' }, { tagname: 'test' }, { tagname: 'test2' }],
      TextCodes: 'Text',
      textEditor: null,
      pageSize: 20,
      pageIndex: 1
    }
  },
  mounted: function () {
    let h = document.body.clientHeight;
    this.tableHeight = h - 270;
    this.queryData();
    this.restaurants = this.loadAll();
  },
  watch: {
    dialogFormVisible: function (newV, oldV) {
      if (!newV) {
        this.cleraForm();
      }
    }
  },
  methods: {
    queryData: function () {
      let that = this,
        defaultParams = {
          pageSize: that.pageSize,
          currentPage: that.pageIndex,
          blurServiceName: that.search.blurServiceName || 'DIS',
          blurNamepace: that.search.blurNamepace || 'eureka保留空间'
        }
      that.tableLoading = true;
      that.axios.get('configCenter/zuulServiceConfig/getRecordsByPage', {
        params: defaultParams
      }
      ).then(function (response) {
        if (response) {
          let data = response.data.data;
          that.tableData = data;
          that.total = data.length;
          that.num = data.length;
        }
        that.tableLoading = false;
      }).catch(function () {
        that.tableLoading = false;
      });
    },
    addRow: function () {
      let that = this;
      that.title = '新增';
      that.isEdit = true;
      that.dialogFormVisible = true;
    },
    checkRow: function (row, status) {
      let that = this;
      that.title = '编辑';
      for (let i in that.form) {
        that.form[i] = row[i]
      }
      that.isEdit = status === 'edit';
      that.dialogFormVisible = true;
    },
    deleteRow: function (row) {
      let that = this;
      that.$confirm('确认刪除？')
        .then(_ => {
          that.axios.delete('configCenter/zuulServiceConfig/delete/config/' + row.scId
          ).then(function (response) {
            that.queryData();
          }).catch(function () {
          });
        })
        .catch(_ => { });
    },
    cleraForm: function () {
      let that = this;
      that.form = {
        scId: '',
        namespace: '',
        serviceStatus: '',
        serviceConfigKey: '',
        serviceName: '',
        accessWeight: '',
        routeDesc: '',
        routeRule: ''
      }
    },
    createFilter (queryString) {
      return (restaurant) => {
        return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
      };
    },
    handleSelect (item) {
      console.log(item);
    },
    setForm: function () {
      let that = this;
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (that.form.scId) {
            that.axios.post('configCenter/zuulServiceConfig/editToLoad',
              that.form
            ).then(function (response) {
              that.queryData();
              that.dialogFormVisible = false;
              that.cleraForm();
            }).catch(function () {
            });
          } else {
            // that.axios.post('pass/v1/console/namespaces/list',
            //   that.form
            // ).then(function (response) {
            //   that.queryData();
            //   that.dialogFormVisible = false;
            //   that.cleraForm();
            // }).catch(function () {
            // });
          }
        } else {
          return false;
        }
      });
    },
    loadAll () {
      let tagArr = this.tagList;
      for (let i = 0; i < tagArr.length; i++) {
        tagArr[i].value = tagArr[i].tagname;
      }
      return tagArr
    },
    textOnMounted (edit) {
      this.jsonEditor = edit;
    },
    textOnCodeChange (value, event) {
      this.form.routeRule = value;
      this.TextCodes = value;
    },
    handleCurrentChange (pageIndex) {
      this.pageIndex = pageIndex
      this.getTableData();
    },
    handleSizeChange (pageSize) {
      this.pageIndex = 1
      this.pageSize = pageSize;
      this.getTableData()
    }
  }
}
</script>
<style>
.autoIndex {
  padding: 10px;
}
.searchArea {
  padding: 0px 0px 10px 0px;
}
.floatR {
  float: right;
}
.classTab {
  width: 100%;
  padding: 10px;
  margin-bottom: 10px;
  background: #eee;
  border-bottom: solid 1px #cecccc;
  text-align: left;
}

.classTab div {
  display: inline-block;
  cursor: pointer;
  color: #666;
  font-size: 12px;
  margin-right: 10px;
}

.classTab div span {
  margin: 0 5px;
  color: #666;
}

.classTab div:hover {
  color: #00c1de;
}

.choiceItem {
  color: #00c1de !important;
}

.pageTitle {
  width: 100%;
  height: 40px;
  line-height: 40px;
  border-left: solid 2px #0099cc;
  padding: 0 5px;
  font-size: 16px;
  color: #000;
  margin-bottom: 10px;
}
.pageTitle span {
  font-size: 12px;
  color: #ccc;
  margin-left: 10px;
}
.advancedQuery {
  width: auto;
  height: 28px;
  display: inline-block;
  line-height: 28px;
  color: #0099cc;
  cursor: pointer;
  margin-left: 20px;
}
.advancedQueryMenu {
  width: 100%;
  height: 38px;
  margin-bottom: 10px;
}
</style>
