<template>
  <!-- 实例节点的详细信息 -->
  <div class="node-box">
    <div class="title">实例属性</div>
    <el-form ref="form" label-width="80px" :model="form" :rules="rules" style="height: 600px;overflow-y: auto">
      <el-form-item label="所属概念" prop="labels">
        <el-select v-model="form.labels" :disabled="editFlag" @change="handleLabelChange">
          <el-option v-for="label in labelList" :key="label.Name" :label="label.comment"
                     :value="label.Name"></el-option>
        </el-select>
      </el-form-item>
      <div class="loading-box" v-show="loading">
        <i class="el-icon-loading"></i>
        <label>查询中</label>
      </div>
      <el-form-item :label="item.comment" v-for="item in infoList" :key="item.field">
        <el-input v-model="item.value"></el-input>
      </el-form-item>
    </el-form>
    <div class="btn-box">
      <el-button type="danger" @click="removeItem" v-show="editFlag">删 除</el-button>
      <el-button type="primary" @click="submitUpdate">保 存</el-button>
    </div>
  </div>
</template>

<script>
import request from '@/utils/request';
import {getFirstStringProperty} from "@/utils/common";

var vm;
export default {
  data() {
    return {
      space: '',
      editFlag: false,
      labelList: [],
      selectObj: {},
      loading: false,
      form: {
        labels: '',
      },
      infoList: [],
      rules: {
        labels: [
          {required: true, message: '请选择所属概念'}
        ],
      },
      tag:""
    }
  },
  created() {
    vm = this;
  },
  mounted() {

  },
  methods: {
    queryTagList() { // 查询tag列表
      vm.propsInfoVisible = false;
      request({
        url: `/nebula_operate/showtag/${vm.space}`,
        method: 'get',
        data: {}
      }).then(res => {
        vm.labelList = res.data;
      });
    },
    createItem(space) {
      vm.editFlag = false;
      vm.space = space;
      vm.form.labels = "";
      vm.infoList = [];
      vm.selectObj.vid = null;
      vm.queryTagList();
    },
    queryDetail(space, {tag, vid}) {
      vm.editFlag = true;
      vm.space = space;
      vm.tag = tag;
      vm.queryTagList();
      vm.queryNodeDetail(tag, vid);
    },
    queryNodeDetail(tag, vid) {
      vm.selectObj = {
        vid: vid
      };
      request({
        url: `/nebula_operate/desctag/${vm.space}/${tag}`,
        method: 'get',
        data: {}
      }).then(res0 => {
        let map = {};
        res0.data.forEach(item => {
          map[item.field] = item.comment;
        });
        request({
          url: `/nebula_operate/findnodebyid/${vm.space}/${tag}/${vid}`,
          method: 'get',
          data: {}
        }).then(res => {
          vm.form.labels = res.data.labels;
          vm.resolveProperties(res.data.properties, map);
        });
      });
    },
    handleLabelChange() {
      vm.loading = true;
      request({
        url: `/nebula_operate/desctag/${vm.space}/${vm.form.labels}`,
        method: 'get',
        data: {}
      }).then(res => {
        vm.loading = false;
        vm.infoList = res.data;
      });
    },
    resolveProperties(properties, map) { // 处理节点和边的属性
      let _infoList = [];
      Reflect.ownKeys(properties).forEach(key => {
        _infoList.push({
          comment: map[key],
          field: key,
          value: properties[key]
        });
      });
      vm.infoList = _infoList;
    },
    removeItem() {
      this.$confirm('此操作将永久删除选中节点, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        request({
          url: `/nebula_operate/deletevertex/${vm.selectObj.vid}`,
          method: 'get',
          data: {}
        }).then(res => {
          vm.$message.success("删除成功");
          vm.$parent.querySpaceGraphData();
        });
      }).catch(() => {
      });
    },
    submitUpdate() { // 更新
      this.$refs.form.validate((valid) => {
        if (!valid) {
          return;
        }
        let _ob = {};
        vm.infoList.forEach(info => {
          _ob[info.field] = info.value;
        });
        // 添加节点
        request({
          url: `/nebula_operate/insertvertex/${vm.space}/${vm.form.labels}`,
          method: 'post',
          data: {
            vid: vm.selectObj.vid,
            ob: _ob
          }
        }).then(res => {
          vm.$parent.findNodeById(vm.tag,res.data.vid);
        });
      });
    },
  }
}

</script>

<style scoped>

.form-box {
  height: 900px;
  overflow-y: auto;

}

.el-input__inner {
  height: 40px !important;
}


.btn-box {
  text-align: right;
  margin-top: 25px;
}

.node-box {
  overflow: hidden;
}

.node-box .title {
  font-size: 18px;
  margin-bottom: 35px;
}

.form-title {
  font-size: 16px;
  margin: 15px 0;

}

.add-attribute-box {
  margin-top: 10px;
  color: #409EFF;
  cursor: pointer;
  text-align: right;
  float: right;
}

.info-content > div {
  margin-bottom: 5px;
  line-height: 20px;
}

.loading-box {
  text-align: center;
}


</style>
