<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>
      <el-form-item label="头实例">
        <el-input v-model="form.start" disabled></el-input>
      </el-form-item>
      <el-form-item label="尾实例">
        <el-input v-model="form.end" disabled></el-input>
      </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: '',
        start: '',
        end: '',
      },
      infoList: [],
      rules: {
        labels: [
          {required: true, message: '请选择类型'}
        ],
      }
    }
  },
  created() {
    vm = this;
  },
  mounted() {
  },
  methods: {
    queryEdgeList() {
      vm.propsInfoVisible = false;
      request({
        url: `/nebula_operate/showedge/${vm.space}`,
        method: 'get',
        data: {}
      }).then(res => {
        vm.labelList = res.data;
      });
    },
    createItem(space, srcId, dstId) {
      vm.editFlag = false;
      vm.space = space;
      vm.form.labels = "";
      vm.infoList = [];
      vm.selectObj.vid = null;
      vm.selectObj.srcId = srcId;
      vm.selectObj.dstId = dstId;
      vm.queryNodeForEdge(srcId, 'start');
      vm.queryNodeForEdge(dstId, 'end');
      vm.queryEdgeList();
    },
    queryDetail(space, {edgeType, srcVid, dstVid}) {
      vm.editFlag = true;
      vm.space = space;
      vm.queryEdgeList();
      vm.queryEdgeDetail(edgeType, srcVid, dstVid);
    },
    queryNodeForEdge(vid, comment) {
      request({
        url: `/nebula_operate/findnodebyid/${vm.space}/${vid}`,
        method: 'get',
        data: {}
      }).then(res => {
        vm.form[comment] = getFirstStringProperty(res.data.properties);
      });
    },
    queryEdgeDetail(edgeType, srcVid, dstVid) {
      vm.queryNodeForEdge(srcVid, 'start');
      vm.queryNodeForEdge(dstVid, 'end');
      vm.selectObj = {
        srcId: srcVid,
        dstId: dstVid
      };
      request({
        url: `/nebula_operate/descedge/${vm.space}/${edgeType}`,
        method: 'get',
        data: {}
      }).then(res0 => {
        let map = {};
        res0.data.forEach(item => {
          map[item.field] = item.comment;
        });
        request({
          url: `/nebula_operate/findrelationbyid/${vm.space}/${edgeType}/${srcVid}/${dstVid}`,
          method: 'get',
          data: {}
        }).then(res => {
          vm.form.labels = res.data.edgeName;
          vm.resolveProperties(res.data.properties, map);
        });
      });

    },
    handleLabelChange() {
      vm.loading = true;
      request({
        url: `/nebula_operate/descedge/${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/deleteedgeline/${vm.space}/${vm.form.labels}`,
          method: 'post',
          data: {
            srcId: vm.selectObj.srcId,
            dstId: vm.selectObj.dstId,
          }
        }).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/insertedgeline/${vm.space}/${vm.form.labels}`,
          method: 'post',
          data: {
            srcId: vm.selectObj.srcId,
            dstId: vm.selectObj.dstId,
            ob: _ob
          }
        }).then(res => {
          vm.$parent.querySpaceGraphData();
        });
      });
    },
  }
}

</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>
