<template>
  <el-col>
    <!-- group -->
    <el-col v-if="!sceneMode">
      <el-row class="title-css">
        <span class="title-text">
          {{ $t('dataset.datalist') }}
        </span>
      </el-row>
      <el-divider />

      <!--      <el-row>-->
      <!--        <el-form>-->
      <!--          <el-form-item class="form-item">-->
      <!--            <el-input-->
      <!--              v-model="search"-->
      <!--              size="mini"-->
      <!--              :placeholder="$t('dataset.search')"-->
      <!--              prefix-icon="el-icon-search"-->
      <!--              clearable-->
      <!--            />-->
      <!--          </el-form-item>-->
      <!--        </el-form>-->
      <!--      </el-row>-->

      <el-col class="custom-tree-container">
        <div class="block">
          <el-tree
            :default-expanded-keys="expandedArray"
            :data="data"
            node-key="id"
            :expand-on-click-node="true"
            @node-click="nodeClick"
          >
            <span slot-scope="{ node, data }" class="custom-tree-node">
              <span style="display: flex;flex: 1;width: 0;">
                <span v-if="data.type === 'scene'">
                  <!--                  <el-button-->
                  <!--                    icon="el-icon-folder-opened"-->
                  <!--                    type="text"-->
                  <!--                    size="mini"-->
                  <!--                  />-->
                  <svg-icon icon-class="scene" class="ds-icon-scene" />
                </span>
                <span style="margin-left: 6px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;">{{ data.name }}</span>
              </span>
            </span>
          </el-tree>
        </div>
      </el-col>
    </el-col>

    <!--scene-->
    <el-col v-if="sceneMode">
      <el-row class="title-css">
        <span class="title-text">
          {{ currGroup.name }}
        </span>
        <el-button icon="el-icon-back" size="mini" style="float: right" circle @click="back">
          <!--          {{ $t('dataset.back') }}-->
        </el-button>
      </el-row>
      <el-divider />
      <el-row>
        <el-form>
          <el-form-item class="form-item">
            <el-input
              v-model="search"
              size="mini"
              :placeholder="$t('dataset.search')"
              prefix-icon="el-icon-search"
              clearable
            />
          </el-form-item>
        </el-form>
      </el-row>
      <el-tree
        :data="tableData"
        node-key="id"
        :expand-on-click-node="true"
        class="tree-list"
        highlight-current
        @node-click="sceneClick"
      >
        <span slot-scope="{ node, data }" class="custom-tree-node-list">
          <span style="display: flex;flex: 1;width: 0;">
            <span>
              <svg-icon v-if="data.type === 'db'" icon-class="ds-db" class="ds-icon-db" />
              <svg-icon v-if="data.type === 'sql'" icon-class="ds-sql" class="ds-icon-sql" />
              <svg-icon v-if="data.type === 'excel'" icon-class="ds-excel" class="ds-icon-excel" />
              <svg-icon v-if="data.type === 'custom'" icon-class="ds-custom" class="ds-icon-custom" />
            </span>
            <span v-if="data.type === 'db' || data.type === 'sql'">
              <span v-if="data.mode === 0" style="margin-left: 6px"><i class="el-icon-s-operation" /></span>
              <span v-if="data.mode === 1" style="margin-left: 6px"><i class="el-icon-alarm-clock" /></span>
            </span>
            <span style="margin-left: 6px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;">{{ data.name }}</span>
          </span>
        </span>
      </el-tree>
    </el-col>
  </el-col>
</template>

<script>
import {isKettleRunning, post} from '@/api/dataset/dataset'

export default {
  name: 'DatasetGroupSelector',
  data() {
    return {
      isKettleRunning: false,
      sceneMode: false,
      search: '',
      data: [],
      tableData: [],
      tables: [],
      currGroup: null,
      expandedArray: [],
      groupForm: {
        name: '',
        pid: null,
        level: 0,
        type: '',
        children: [],
        sort: 'type desc,name asc'
      },
      tableForm: {
        name: '',
        sort: 'type asc,create_time desc,name asc'
      }
    }
  },
  computed: {},
  watch: {
    search(val) {
      if (val && val !== '') {
        this.tableData = JSON.parse(JSON.stringify(this.tables.filter(ele => { return ele.name.includes(val) })))
      } else {
        this.tableData = JSON.parse(JSON.stringify(this.tables))
      }
    }
  },
  mounted() {
    this.tree(this.groupForm)
    this.tableTree()
  },
  activated() {
    this.tree(this.groupForm)
    this.tableTree()
  },
  created(){
    this.kettleRunning()
  },
  methods: {
    kettleRunning(){
      isKettleRunning().then(res => {
        this.isKettleRunning = res.data
      })
    },
    close() {
      this.editGroup = false
      this.groupForm = {
        name: '',
        pid: null,
        level: 0,
        type: '',
        children: [],
        sort: 'type desc,name asc'
      }
    },

    closeTable() {
      this.editTable = false
      this.tableForm = {
        name: ''
      }
    },

    tree(group) {
      post('/dataset/group/tree', group).then(response => {
        this.data = response.data
      })
    },

    tableTree() {
      this.tableData = []
      if (this.currGroup) {
        post('/dataset/table/list', {
          sort: 'type asc,create_time desc,name asc',
          sceneId: this.currGroup.id
        }).then(response => {
          this.tables = response.data
          for (let i = 0; i < this.tables.length; i++) {
            if(this.tables[i].mode===1 && this.isKettleRunning === false){
              this.$set(this.tables[i],"disabled",true)
            }
          }
          this.tableData = JSON.parse(JSON.stringify(this.tables))
        })
      }
    },

    nodeClick(data, node) {
      if (data.type === 'scene') {
        this.sceneMode = true
        this.currGroup = data
        this.tableTree()
      }
      if (node.expanded) {
        this.expandedArray.push(data.id)
      } else {
        const index = this.expandedArray.indexOf(data.id)
        if (index > -1) {
          this.expandedArray.splice(index, 1)
        }
      }
      // console.log(this.expandedArray);
    },

    back() {
      this.sceneMode = false
    },

    sceneClick(data, node) {
      if(data.disabled){
        this.$message({
          type: 'warning',
          message: this.$t('dataset.invalid_dataset'),
          showClose: true
        })
        return
      }
      this.$emit('getTable', data)
    }
  }
}
</script>

<style scoped>
  .el-divider--horizontal {
    margin: 12px 0
  }

  .search-input {
    padding: 12px 0;
  }

  .tree-list>>>.el-tree-node__expand-icon.is-leaf{
    display: none;
  }

  .custom-tree-node {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 14px;
    padding-right: 8px;
  }

  .custom-tree-node-list {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 14px;
    padding:0 8px;
  }

  .custom-position {
    flex: 1;
    display: flex;
    align-items: center;
    font-size: 14px;
    flex-flow: row nowrap;
  }

  .form-item {
    margin-bottom: 0;
  }

  .title-css {
    height: 26px;
  }

  .title-text {
    line-height: 26px;
  }
</style>
