<template>
  <div class="box">
    <div class="condition">
      <div class="header">
        <el-button style="float:left;" size="small" type="default" icon="el-icon-back" @click="goBack"></el-button>
        <span>{{ params.name || '' }}</span>
      </div>
      <el-row class="spaceTypes">
        <div class="types">
          <el-tabs v-model="activeTab" type='card'>
            <template v-for="(item,index) in tabsList">
              <el-tab-pane :name="item.Code" :key="index" :label="item.Name"></el-tab-pane>
            </template>
          </el-tabs>
        </div>
        <div class="deleBtn">
          <el-button size="small" @click="deleteCenote">删除竖井</el-button>
        </div>
      </el-row>
      <div v-if="activeTab=='detail'" class="content">
        <div class="content-left">
          <div class="content-showType">
            <selectRadio
              :selectRadio="selectRadio"
              @changeRadio="changeRadio"
            />
          </div>
          <div class="content-point">
            <div class="content-point-left">
              <el-scrollbar style="height:100%;">
                <div>
                  <exhibitionBaseInformation :exhibitionBaseInformation="exhibitionBaseInformation"/>

                </div>
              </el-scrollbar>
            </div>
          </div>
        </div>
      </div>
      <!-- 竖井内的设备 -->
      <device-table v-if="activeTab=='device'" :params="params" :type="activeTab"></device-table>
      <!-- 连通的其他竖井 -->
      <cenote-table v-else-if="activeTab=='cenote'" :params="params" :type="activeTab"></cenote-table>
      <!-- 竖井内系统 -->
      <system-table v-else-if="activeTab=='system'" :params="params" :type="activeTab"></system-table>
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex"
import { deleteCenoteTableData, queryCenoteTableData } from '@/api/scan/request'
import { getDataDictionary } from "@/api/dict";
import cenoteTable from '@/components/ledger/cenote/table/cenoteTable'
import deviceTable from '@/components/ledger/cenote/table/deviceTable'
import systemTable from '@/components/ledger/cenote/table/systemTable'
import exhibitionBaseInformation from "@/components/ledger/details/detail/exhibitionBaseInformation";
import selectRadio from "@/components/ledger/details/detail/selectRadio";
import tools from "@/utils/scan/tools"

export default {
  data() {
    return {
      params: {},
      tabsList: [
        { Code: 'detail', Name: '竖井详情' },
        { Code: 'device', Name: '竖井内的设备' },
        { Code: 'cenote', Name: '连通的其它竖井' },
        { Code: 'system', Name: '竖井内系统' },
      ], //tab页
      activeTab: 'detail', //当前选中的tab
      pointData: [], //信息点数据
      currentRadio: '1',//当前选中的select
      exampleData: {}, //实例数据
      exhibitionBaseInformation: {
        information: {}
      },
      selectRadio: {
        information: '设备信息点',
        initRadio: '1',
        radioList: [
          {
            value: '显示需采集信息点',
            label: '1'
          },
          {
            value: '显示有值信息点',
            label: '2'
          },
          {
            value: '显示全部信息点',
            label: '3'
          }
        ]
      },
    }
  },
  computed: {
    ...mapGetters("layout", ["projectId", "userId", "secret"])
  },
  components: {
    cenoteTable,
    deviceTable,
    systemTable,
    exhibitionBaseInformation,
    selectRadio
  },
  created() {
    this.params = this.$route.query;
    this.getData();
  },
  mounted() {
  },
  watch: {
    projectId() {
    }
  },
  methods: {
    deepCopy(obj) {
      return JSON.parse(JSON.stringify(obj))
    },
    // 返回
    goBack() {
      this.$router.push({ name: "cenotelist" });
    },
    getData() {
      let params1 = {
        orders: "sort asc",
        pageNumber: 1,
        pageSize: 1000,
        type: 'shaft'
      }, params2 = {
        filters: `id='${this.params.ShaftID}'`
      };
      let promise1 = new Promise((resolve, reject) => {
        getDataDictionary(params1, res => {
          resolve(res)
        })
      })
      let promise2 = new Promise((resolve, reject) => {
        queryCenoteTableData(params2, res => {
          resolve(res)
        })
      })
      Promise.all([promise1, promise2]).then(values => {
        this.pointData = values[0].content;
        this.exampleData = values[1].content[0];
        let obj = this.deepCopy(values[1].content[0]);
        obj = tools.flattenKeys(obj);
        for (let key in obj) {
          if (Array.isArray(obj[key]) && !obj[key].length) {
            delete obj[key]
          }
        }
        this.instance = obj;
        this.pointData.forEach(item => {
            if (this.instance.hasOwnProperty(item.path)) {
                item.value = this.instance[item.path]
            }
        })
        this.handleRadios(1)
      })
    },
    handleRadios(val) { //判断radio的值显示关键信息，基本信息，图片，文件
      let arr = []
      if (val == 1) { //需采集信息
        arr = this.pointData.filter(i => i.visible)
      } else if (val == 2) { //有值信息点
        arr = this.pointData.filter(k => k.value)
      } else { //全部信息点
        arr = this.pointData
      }
      this.displayData(arr)
    },
    changeRadio(val) { //1,需采集，2，有值，3，全部
      this.handleRadios(val)
      this.currentRadio = val
    },
    formatDate(str) {
      if (str) {
        if (str.includes('-')) {
          return str
        } else {
          if (str.length > 8) {
            return str.substr(0, 4) + "-" + str.substr(4, 2) + "-" + str.substr(6, 2) + " " + str.substr(8, 2) + ":" + str.substr(10, 2) + ":" + str.substr(12, 2)
          } else {
            return str.substr(0, 4) + "-" + str.substr(4, 2) + "-" + str.substr(6, 2) + " " + str.substr(8, 2)
          }
        }
      } else {
        return '--'
      }
    },
    displayData(arr) {//对数据进行处理传给组件展示
      const result = {}
      arr.forEach(i => {
        if (this.instance.hasOwnProperty(i.path) && this.currentRadio != 2) {
          i.value = this.instance[i.path]
        }
        if (i.dataType == 'DATETIME') {
          if (this.instance.hasOwnProperty(i.path)) {
            let time = this.instance[i.path]
            i.value = this.formatDate(time)
          }
        }
        if (i.dataSource && i.dataSource.length) {
          // let source = JSON.parse(i.dataSource)
          let source = i.dataSource
          //判断输入类型
          if (i.dataType == 'ENUM') {
            let d1l = tools.formatDataSource(i.dataSource)
            d1l.forEach(k => {
              if (k.code == this.instance[i.path]) {
                i.value = k.name
              }
            })
          } else if (i.dataType == 'MENUM' || i.dataType == 'DATETIME'){
            if (this.instance.hasOwnProperty(i.path)) {
              if (!Array.isArray(this.instance[i.path])) {
                let transArray = this.instance[i.path].split(',')
                i.value = source.filter(item => transArray.includes(item.code)).map(item => item.name).join(',')
              }
            }
          }
          source.forEach(j => {
            if (j.code == this.instance[i.path]) {
              i.value = j.name
            }
          })
        }
        switch (i.category) {
          case "STATIC":
            if (result[`${i.firstName}${i.secondName ? '/' + i.secondName : ''}`]) {
              result[`${i.firstName}${i.secondName ? '/' + i.secondName : ''}`].paths.push({
                Path: i.path,
                InfoPointName: i.name,
                InfoPointCode: i.code,
                Value: i.value,
                Visible: i.visible,
                KeyWord: i.keyWord,
                dataType: i.dataType
              })
            } else {
              result[`${i.firstName}${i.secondName ? '/' + i.secondName : ''}`] = {
                paths: [{
                  Path: i.path,
                  InfoPointName: i.name,
                  InfoPointCode: i.code,
                  Value: i.value,
                  Visible: i.visible,
                  KeyWord: i.keyWord,
                  dataType: i.dataType
                }]
              }
            }
            break
        }
      })
      this.exhibitionBaseInformation.information = result
    },
    // 删除业务空间
    deleteCenote() {
      this.$confirm("此操作将删除竖井，是否继续？", "提示", {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteCenoteTableData([{ id: this.params.ShaftID }], res => {
          this.$message.success('删除成功')
          this.goBack()
        })
      }).catch(() => {
        this.$message("取消删除")
      })
    }
  }
};
</script>

<style scoped lang='less'>
.box {
  .condition {
    padding: 10px;
    display: flex;
    height: 100%;
    flex-direction: column;
    border: 1px solid #dfe6ec;
    box-sizing: border-box;
    background: #fff;

    .header {
      padding-bottom: 10px;

      span {
        line-height: 33px;
        margin-left: 15px;
      }
    }

    .spaceTypes {
      .types {
        float: left;
        width: calc(100% - 200px);

        /deep/ .el-tabs__item.is-top {
          border-top: 2px solid transparent;

          &.is-active {
            border-top: 2px solid #409eff;
          }
        }
      }

      .deleBtn {
        float: left;
        width: 200px;
        text-align: right;
        height: 40px;
        border-bottom: 1px solid #e4e7ed;
      }
    }

    .content {
      display: flex;
      flex-direction: row;
      box-sizing: border-box;
      height: calc(100% - 99px);

      .content-left {
        display: flex;
        flex-direction: column;
        flex: 1;

        .content-showType {
          height: 50px;
          border: 1px solid #ccc;
          border-bottom: none;
          box-sizing: border-box;
        }

        .content-point {
          display: flex;
          flex-direction: row;
          height: calc(100% - 50px);
          border: 1px solid #ccc;
          box-sizing: border-box;

          .content-point-left {
            flex: 1;
          }
        }
      }
    }

    .main {
      margin-top: 10px;
      height: calc(100% - 96px);
    }

    .footer {
      margin-bottom: 10px;
    }

    /deep/ .el-scrollbar__wrap {
      overflow-x: hidden;
    }
  }
}
</style>

<style lang="less">
.el-table th {
  background-color: #d9d9d9;
  color: #000;
}
</style>

