<template>
  <div class="app-container">
    <el-dialog :title="title" :visible.sync="open" fullscreen @close="cancel">
      <a-steps :current="current">
        <a-step title="第一步" />
        <a-step title="第二步" />
      </a-steps>
      <div class="steps-content" v-show="current == 0">
        <el-form
          ref="form"
          :model="form"
          :rules="rules"
          style="margin: 10px"
          label-width="120px"
        >
          <el-row>
            <el-col :span="24">
              <el-form-item label="获取方式" prop="dataAcquisitionMode">
                <el-select
                  v-model="form.dataAcquisitionMode"
                  placeholder="请选择获取方式"
                  clearable
                  size="small"
                >
                  <el-option
                    v-for="dict in dataAcquisitionMode"
                    :key="dict.dictValue"
                    :label="dict.dictLabel"
                    :value="dict.dictValue"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="16">
              <el-form-item label="条件">
                <el-input
                  type="textarea"
                  :rows="10"
                  placeholder="请输入执行语句/表名/共享ip+端口"
                  v-model="form.sentence"
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="16">
              <ul class="ml30 mv10" style="color: red; margin-left: 100px">
                <li>获取方式为mysql或oracle时，条件为sql语句。</li>
                <li>获取方式为influxdb时，条件为Flux函数</li>
                <li>获取方式为redis时，条件为redis中key值</li>
                <li>获取方式为共享服务端时，条件为共享服务端ip+端口</li>
              </ul>
            </el-col>
          </el-row>
        </el-form>
        <el-row>
          <el-col :span="12"> </el-col>
        </el-row>
      </div>
      <div class="steps-content" v-show="current == 1">
        <el-table
          :height="'62vh'"
          border
          v-loading="loading"
          :data="handleList"
        >
          <el-table-column type="index" align="center" />
          <el-table-column
            label="tag标识"
            align="center"
            prop="tag"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="类型"
            align="center"
            prop="type"
            :formatter="typeFormat"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
            <!-- slot-scope="scope" -->
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-caret-right"
                @click="configIt(scope.row)"
                >配置</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div slot="footer" class="steps-action">
        <a-button v-if="current < 1" type="primary" @click="next">
          下一步
        </a-button>
        <a-button v-if="current > 0" @click="prev"> 上一步 </a-button>
        <a-button
          v-if="current == 1"
          style="margin-left: 8px"
          type="primary"
          @click="complete"
        >
          完成
        </a-button>
      </div>
    </el-dialog>
    <tag-config ref="tagconfig"></tag-config>
  </div>
</template>
<script>
import TagConfig from "../modules/TagConfig.vue";
import { save } from "@/api/topology.js";
export default {
  components: {
    TagConfig,
  },
  data() {
    return {
      topologyData: {},
      loading: false,
      //切换下一步
      current: 0,
      // 表单校验
      rules: {},
      dataAcquisitionMode:this.topologyDict.dataAcquisitionMode,
      tagType:this.topologyDict.tagType,
      // 表单参数
      form: {
        dataAcquisitionMode: "",
        sentence: "",
      },
      //文件中的数据
      fileData: {},
      //处理的list
      handleList: [],
      open: false,
      title: "",
    };
  },
  methods: {
    update(row) {
      this.open = true;
      this.title = "设置参数";
      this.topologyData = Object.assign({}, row);
      this.fileData = Object.assign({}, JSON.parse(row.data));
      this.handleList = [];
      if (this.fileData.topologyHandle) {
        if (!!this.fileData.topologyHandle.dataAcquisitionMode) {
          this.form.dataAcquisitionMode =
            this.fileData.topologyHandle.dataAcquisitionMode;
        }
        if (!!this.fileData.topologyHandle.sentence) {
          this.form.sentence = this.fileData.topologyHandle.sentence;
        }
        if (!!this.fileData.topologyHandle.change) {
          for (let s of this.fileData.pens) {
            let judge = true;
            let param = {};
            if (s.tags.length > 0) {
              for (let s1 of this.fileData.topologyHandle.change) {
                if (s.tags[0] === s1.tag) {
                  param.tag = s1.tag;
                  param.type = s1.type;
                  param.handle = s1.handle;
                  if (s.name === "echarts") {
                    param.echarts = s.data.echarts;
                  }
                  judge = false;
                }
              }
              if (judge) {
                param.tag = s.tags[0];
                param.type = s.type;
                param.handle = [];
                if (s.name === "echarts") {
                  param.echarts = s.data.echarts;
                }
              }
              this.handleList.push(param);
            }
          }
        } else {
          for (let s of this.fileData.pens) {
            if (s.tags.length > 0) {
              let param = {};
              param.tag = s.tags[0];
              param.type = s.type;
              param.handle = [];
              if (s.name === "echarts") {
                param.echarts = s.data.echarts;
              }
              this.handleList.push(param);
            }
          }
        }
      } else {
        for (let s of this.fileData.pens) {
          if (s.tags.length > 0) {
            let param = {};
            param.tag = s.tags[0];
            param.type = s.type;
            if (s.name === "echarts") {
              param.echarts = s.data.echarts;
            }
            param.handle = [];
            this.handleList.push(param);
          }
        }
      }
    },
    paramsList() {},
    cancel() {
      this.open = false;
      Object.assign(this.$data, this.$options.data());
    },
    async complete() {
      let topologyHandle = {};
      topologyHandle.dataAcquisitionMode = this.form.dataAcquisitionMode;
      topologyHandle.sentence = this.form.sentence;
      topologyHandle.change = this.handleList;
      this.fileData.topologyHandle = topologyHandle;
      this.topologyData.data = Object.assign({}, this.fileData);
      await save(Object.assign({}, this.topologyData)).then((res) => {
        if (res.code === 200) {
          this.$message.success("配置成功");
        }
      });
      this.cancel();
      this.$emit("ok");
    },
    configIt(row) {
      this.$refs.tagconfig.init(row, Object.assign({}, this.form));
    },
    // 公告状态字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(this.tagType, row.type);
    },
    // 回显数据字典
    selectDictLabel(datas, value) {
      var actions = [];
      Object.keys(datas).some((key) => {
        if (datas[key].dictValue == "" + value) {
          actions.push(datas[key].dictLabel);
          return true;
        }
      });
      return actions.join("");
    },
    next() {
      if (!this.form.dataAcquisitionMode) {
        this.$message.warning("请选择获取方式");
        return;
      }
      if (!this.form.sentence) {
        this.$message.warning("请填写执行语句/表名");
        return;
      }
      this.current++;
    },
    prev() {
      this.current--;
    },
    tagConfigHandle(row) {},
  },
};
</script>
<style>
.steps-content {
  margin-top: 16px;
  border: 1px dashed #e9e9e9;
  border-radius: 6px;
  background-color: #fafafa;
  min-height: 62vh;
  /* padding-top: 5vh; */
  /* overflow-y: scroll; */
}

.steps-action {
  margin-top: 24px;
}
</style>