<template>
  <div class="view-table-content">
    <div style="width: 100%">
      <div class="view-dataType-title">
        <div class="view-dataType-title-btn">
          <el-button type="success" @click="openDialog()">新增</el-button>
          <el-button type="warning" @click="reset()">重置</el-button>
        </div>
        <div class="view-dataType-title-search">
          <el-input placeholder="请输入实体名称" v-model="keyWordData" class="input1">
            <el-button slot="append" icon="el-icon-search" @click="searchClick"></el-button>
          </el-input>
        </div>
      </div>
      <div class="view-dataType-table">
        <LTable ref="table" @selection-change="selection" :defaultFetch="false" :fetch="fetch" :columns="columns" :dataSource="tableData" :options="options"></LTable>
      </div>
      <!-- 添加或修改模型信息对话框 -->
      <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="600px" :before-close="handleClose">
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="实体名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入实体名称" />
          </el-form-item>
          <el-form-item label="实体类型" prop="category">
            <el-select v-model="form.category" placeholder="请选择实体类型">
              <el-option v-for="(item, index) in categoryList" :key="index" :label="item.name" :value="item.name" />
            </el-select>
          </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="submit">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getEntityList, getAllEntityClass, addEntity, updateEntity, deleteEntity, checkEntity } from '@/api/als/entityManage'
import { deepClone, debounce } from '@/utils/index'

export default {
  name: 'AtlasFile',
  components: {},
  data() {
    // 这里存放数据
    return {
      dialogTitle: '新增',
      dialogVisible: false,
      keyWordData: '',
      searchValue: '',
      columns: [
        {
          prop: 'name',
          label: '实体名称'
        },
        {
          prop: 'category',
          label: '实体类型'
        },
        {
          button: true,
          label: '操作',
          width: '240px',
          group: [
            {
              name: '编辑',
              type: 'text',
              round: false,
              plain: false,
              onClick: (row, index, scope) => {
                this.handUpdate(row)
              }
            },
            {
              name: '删除',
              type: 'text',
              round: false,
              plain: false,
              onClick: (row, index, scope) => {
                this.remove([row])
              }
            }
          ]
        }
      ],
      options: {
        stripe: true, // 斑马纹
        mutiSelect: true, // 多选框
        index: false, // 显示序号， 多选则 mutiSelect
        loading: false, // 表格动画
        initTable: false, // 是否一挂载就加载数据
        border: true,
        height: 'calc(100vh - 300px)'
      },
      tableCheckItems: [],
      tableData: [],
      tableRequset: {
        total: 0,
        pageIndex: 1,
        pageSize: 10,
        searchValue: ''
      },
      form: {
        id: '',
        name: '',
        category: ''
      },
      debounceFn: debounce(this.fetch, 500),
      categoryList: []
    }
  },
  watch: {
    keyWord() {
      this.tableRequset.pageIndex = 1
      this.debounceFn()
    }
  },
  mounted() {
    this.getEntityAPI()
    this.getEntityClassAPI()
  },
  methods: {
    async deleteEntityAPI(params) {
      try {
        const deleteData = {
          node_id: params[0]
        }
        const { code } = await deleteEntity(deleteData)
        if (code === 200) {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          this.getEntityAPI()
          this.handleClose()
        }
      } catch (error) {}
    },
    reset() {
      this.getEntityAPI()
      this.keyWordData = ''
    },
    async getEntityAPI(params) {
      try {
        if (this.$refs.table) this.$refs.table.clearSelection()
        const {
          data: { dataList, total }
        } = await getEntityList()
        this.tableData = dataList
        this.tableRequset.total = total
      } catch (error) {}
    },

    async getEntityClassAPI(params) {
      try {
        const {
          data: { dataList }
        } = await getAllEntityClass()
        this.categoryList = dataList
      } catch (error) {}
    },

    fetch() {
      this.getEntityAPI()
    },

    async searchClick() {
      try {
        if (this.$refs.table) this.$refs.table.clearSelection()
        const {
          data: { dataList, total }
        } = await checkEntity({ node_name: this.keyWordData })
        this.tableData = dataList
        this.tableRequset.total = total
      } catch (error) {}
    },

    async addEntityAPI() {
      try {
        const addData = {
          node_name: this.form.name,
          node_label: this.form.category
        }
        const { code } = await addEntity(addData)
        if (code === 200) {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          this.handleClose()
          this.getEntityAPI()
        }
      } catch (error) {}
    },

    async updateEntityAPI() {
      try {
        const updateData = {
          node_id: this.form.id,
          new_node_name: this.form.name,
          new_node_label: this.form.category
        }
        const { code } = await updateEntity(updateData)
        if (code === 200) {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          this.handleClose()
          this.getEntityAPI()
        }
      } catch (error) {}
    },

    openDialog() {
      this.dialogTitle = '新增'
      this.dialogVisible = true
    },

    handleClose() {
      this.dialogVisible = false
      this.form = {
        id: '',
        name: '',
        category: ''
      }
    },

    handUpdate(row) {
      this.dialogTitle = '编辑'
      this.form = deepClone(row)
      this.dialogVisible = true
    },

    submit() {
      switch (this.dialogTitle) {
        case '编辑':
          this.updateEntityAPI()
          break
        case '新增':
          this.addEntityAPI()
          break
      }
    },

    selection(val) {
      this.tableCheckItems = val
    },

    remove(row) {
      this.$confirm('是否删除该数据', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.deleteEntityAPI(row.map((e) => e.id))
        })
        .catch(() => {})
    }
  }
}
</script>

<style lang="scss" scoped>
@import '../../index.scss';
</style>
