<template>
  <!-- 边类型信息修改 -->
  <div class="node-box">
    <div class="title">{{ editFlag ? '编辑' : '新建' }}</div>
    <el-form ref="relationForm" label-width="100px" label-position="left">
      <el-form-item label="*边类型名称">
        <el-input v-model="modifyCmd.name" :disabled="editFlag"></el-input>
      </el-form-item>
      <el-form-item label="描述">
        <el-input v-model="modifyCmd.comment"></el-input>
      </el-form-item>
    </el-form>
    <div class="form-title">定义属性</div>
    <el-table :data="fields"
              border
              style="width: 100%">
      <el-table-column prop="field" label="属性名称"></el-table-column>
      <el-table-column prop="type" label="数据类型"></el-table-column>
      <!--      <el-table-column prop="Null" label="允许空值"></el-table-column>-->
      <!--      <el-table-column prop="Default" label="默认值"></el-table-column>-->
      <!--      <el-table-column prop="Comment" label="描述"></el-table-column>-->
      <el-table-column label="操作" width="120">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="modifyField(scope.row, scope.$index)">编辑</el-button>
          <el-button type="text" size="small" @click="removeField(scope.row, scope.$index)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="add-attribute-box" @click="showAddFieldDialog('source')">添加字段</div>
    <br/>
    <br/>
    <div class="btn-box">
      <el-button type="danger" @click="removeEdge" v-show="editFlag">删 除</el-button>
      <el-button type="primary" @click="submitUpdateNode" >保 存</el-button>
    </div>
    <el-dialog
        :title="dialogName"
        :append-to-body="true"
        :visible.sync="fieldDialogVisible"
        width="40%">
      <el-form ref="fieldCmd" label-width="80px">
        <el-form-item label="属性名称">
          <el-input v-model="fieldCmd.field" :disabled="dialogName == '编辑属性'"></el-input>
        </el-form-item>
        <el-form-item label="数据类型">
          <el-select v-model="fieldCmd.type" style="width: 100%">
            <el-option :label="item" :value="item" v-for="item in dataType"
                       :key="item"></el-option>
          </el-select>
        </el-form-item>
        <!--        <el-form-item label="允许空值">-->
        <!--          <el-switch-->
        <!--              v-model="fieldCmd.Null"-->
        <!--              active-color="#13ce66"-->
        <!--              inactive-color="#ff4949"-->
        <!--              active-value="YES"-->
        <!--              inactive-value="NO">-->
        <!--          </el-switch>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="默认值">-->
        <!--          <el-input v-model="fieldCmd.Default"></el-input>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="描述">-->
        <!--          <el-input v-model="fieldCmd.Comment" type="textarea"></el-input>-->
        <!--        </el-form-item>-->
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="fieldDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitAddField">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import request from '@/utils/request';

var vm;
export default {
  data() {
    return {
      space: '',
      modifyCmd: {
        name: '',
        fields: []
      },
      editFlag: false,
      fields: [],
      fieldDialogVisible: false,
      fieldCmd: {
        field: '',
        type: ''
      },
      dialogName: '添加属性',
      selectObj: {
        type: '',
        value: {}
      },
      dataType: ["INT64", "INT32", "INT16", "INT8", "FLOAT", "DOUBLE", "BOOL", "STRING", "DATE", "TIME", "DATETIME", "FIXED_STRING"],
    }
  },
  created() {
    vm = this;
  },
  mounted() {

  },
  methods: {
    updateValue(space, name) {
      vm.space = space;
      vm.modifyCmd.name = name;
      if (name.length > 0) {
        vm.queryEdgeDescribe();
        request({
          url: `/nebula_operate/showcreateedge/${vm.space}/${vm.modifyCmd.name}`,
          method: 'get',
          data: {}
        }).then(res => {
          vm.modifyCmd.comment = res.data.comment;
          vm.modifyCmd = JSON.parse(JSON.stringify(vm.modifyCmd))
        });
        vm.editFlag = true;
      } else {
        vm.editFlag = false;
        vm.fields = [];
      }
    },
    queryEdgeDescribe() {
      request({
        url: `/nebula_operate/descedge/${vm.space}/${vm.modifyCmd.name}`,
        method: 'get',
        data: {}
      }).then(res => {
        res.data.forEach(item => {
          item.type = item.type.toUpperCase();
        });
        vm.fields = res.data;
      });
    },
    modifyField(item, index) {
      vm.dialogName = '编辑属性';
      vm.fieldCmd = JSON.parse(JSON.stringify(item));
      vm.fieldCmd.index = index;
      vm.fieldDialogVisible = true;
    },
    removeField(item, index) {
      vm.fields.splice(index, 1);
      if (vm.editFlag) { // 编辑tag，需要提交请求
        vm.modifyCmd.fields = [item];
        request({
          url: `/nebula_operate/alterdropedge/${vm.space}`,
          method: 'post',
          data: vm.modifyCmd
        }).then(res => {
          vm.fieldDialogVisible = false;
        });
      }
    },
    showAddFieldDialog(type) {
      vm.fieldCmd = {
        field: '',
        type: '',
        null: 'YES',
      };
      vm.dialogName = '添加属性';
      vm.fieldDialogVisible = true;
    },
    submitAddField() {
      if (!vm.editFlag) { // 新建的tag,不需要即时提交
        if (vm.dialogName == '添加属性') {
          vm.fields.push(vm.fieldCmd);
        } else if (vm.dialogName == '编辑属性') {
          vm.fields[vm.fieldCmd.index] = vm.fieldCmd;
        }
        vm.fieldDialogVisible = false;
      } else {
        vm.modifyCmd.fields = [vm.fieldCmd];
        if (vm.dialogName == '添加属性') {
          request({
            url: `/nebula_operate/alteraddedge/${vm.space}`,
            method: 'post',
            data: vm.modifyCmd
          }).then(res => {
            vm.fields.push(vm.fieldCmd);
            vm.fieldDialogVisible = false;
          });
        } else if (vm.dialogName == '编辑属性') {
          request({
            url: `/nebula_operate/alterchangeedge/${vm.space}`,
            method: 'post',
            data: vm.modifyCmd
          }).then(res => {
            vm.fields[vm.fieldCmd.index] = vm.fieldCmd;
            vm.fieldDialogVisible = false;
          });
        }
      }
    },
    submitUpdateNode() { // 更新
      if(vm.modifyCmd.name.length === 0) {
        vm.$message.warning("边类型名称不能为空");
        return false;
      }
      vm.modifyCmd.fields = vm.fields;
      if (!vm.editFlag) { // 新建
        request({
          url: `/nebula_operate/createedge/${vm.space}`,
          method: 'post',
          data: vm.modifyCmd
        }).then(res => {
          vm.$parent.queryEdgeList();
        });
      } else { // 编辑
        vm.$parent.queryEdgeList();
      }
    },
    removeEdge() {
      vm.$confirm('此操作将永久删除记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        request({
          url: `/nebula_operate/dropedge/${vm.space}/${vm.modifyCmd.name}`,
          method: 'get',
          data: {}
        }).then(res => {
          vm.$message.success("删除成功");
          vm.$parent.queryEdgeList();
        });
      }).catch(() => {
      });
    },
  }
}

</script>

<style>

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

.node-box {
  overflow: hidden;
}

.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;
}


</style>
