<template>
  <div style="height: 100%">
    <el-row style="height: 100%">
      <el-col :span="12" style="height: 100%;overflow: auto;padding-bottom: 12px;" class="leftCol">

        <div style="width: 96%;margin:0 auto">
          <el-collapse v-model="configSwitch" accordion>
            <el-collapse-item title="图表配置" name="1">
              <div style="height: 100%">
                <el-form ref="form" :model="defaultData" label-width="80px">
                  <el-form-item label="表头数据">
                    <el-select
                      v-model="defaultData.th"
                      multiple
                      filterable
                      allow-create
                      default-first-option
                      placeholder="请选择表头">
                      <el-option
                        v-for="item in defaultData.th"
                        :key="item"
                        :label="item"
                        :value="item">
                      </el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item label="列数据">
                    <el-select
                      v-model="defaultData.td"
                      multiple
                      filterable
                      allow-create
                      default-first-option
                      placeholder="请选择列数据">
                      <el-option
                        v-for="item in tdkeys"
                        :key="item"
                        :label="item"
                        :value="item">
                      </el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item label="数据集">
                    <el-select v-model="defaultData.tableDataKey" placeholder="请选择数据" @change="dataChange" clearable>
                      <el-option :label="i" :value="i"
                                 v-for="i in Object.keys(networkData)"></el-option>
                    </el-select>
                  </el-form-item>
                </el-form>

              </div>
            </el-collapse-item>
            <el-collapse-item title="数据配置" name="2">
              <div>
                <NetworkRequest ref="NetworkRequest" :requests="defaultData.requests" @GetFinalData="getData"/>
              </div>
            </el-collapse-item>
          </el-collapse>
          <div style="text-align: center;height: 60px;line-height: 60px">
            <el-button type="primary" @click="onSubmit">立即创建</el-button>
            <el-button>重置</el-button>
          </div>
        </div>
      </el-col>
      <el-col :span="12" style="height: 100%">
        <div class="chart">
          <ComplexScrollTable ref="ComplexScrollTable" :defaultData="defaultData"
                              :default-table-data="tableData"></ComplexScrollTable>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import {ComplexScrollTable} from '@/components/complex'
import Vue from "vue";
import {queryGlobalCfg} from "@/api/configuration/globalCfg";
import NetworkRequest from "@/components/networkRequest/NetworkRequest";
import {sy} from "@/utils/scrin";

export default {
  components: {
    ComplexScrollTable,
    NetworkRequest
  },
  props: ['defaultData', 'isDialog'],
  watch: {
    'defaultData.requests': {
      async handler() {
        setTimeout(async ()=>{
          await this.getData()
          this.dataChange()
        },1000)
        console.log('this.networkData1122')
      },
      deep: true
    },
  },
  data() {
    return {
      tableData: [],
      configSwitch: false,
      networkData: {},
      tdkeys: [],
    }
  },
  created() {
  },
  async mounted() {
    await this.getData()
    this.dataChange()
    setInterval(async () => {
      await this.getData()
    }, 1000 * 60)
  },
  methods: {
    async getData(e) {
      console.log('data1-1', data)
      let data = await this.$getFinalData(this.defaultData.requests)
      this.$set(this, 'networkData', data)
    },
    async onSubmit() {
      if (this.isDialog) {
        this.$emit('close')
      }
    },
    dataChange() {
      this.tableData = this.networkData[this.defaultData.tableDataKey]

      let keys = []
        this.tableData.forEach(ee=>{
          keys = [...new Set(keys.concat(Object.keys(ee)))]
        })
      this.tdkeys = keys
    }
  },
}
</script>
<style scoped lang="less">
.chart {
  width: 100%;
  height: 100%;
}

.notScroll {
  &::-webkit-scrollbar {
    display: none;
  }
}

.leftCol::-webkit-scrollbar {
  display: none;
}

.add {
  display: inline-block;
  width: 1vw;
  height: 1vw;
  padding: 0.5vw;
  background-color: #409EFF;
  border-radius: 50%;
  margin: 0 1vw;
  cursor: pointer;
}

.subtract {
  display: inline-block;
  width: 1vw;
  height: 1vw;
  padding: 0.5vw;
  background-color: #909399;
  border-radius: 50%;
  cursor: pointer;
}

.textArea {
  position: relative;

  .textAreaIcon {
    position: absolute;
    top: 12px;
    right: 8px;
  }
}
</style>
