<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: `ShaftID='${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.handleRadios(1)
      })
    },
    handleRadios(val) { //判断radio的值显示关键信息，基本信息，图片，文件
      let arr = []
      if (val == 1) { //需采集信息
        arr = this.pointData.filter(i => i.visible)
      } else if (val == 2) { //有值信息点
        let list = this.pointData
        list.map(j => {
          if (this.instance.hasOwnProperty(j.path)) {
            return {
              ...j,
              value: this.instance[j.path]
            }
          }
          arr = list.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 {
          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 '--'
      }
    },
    displayData(arr) {//对数据进行处理传给组件展示
      const result = {}
      arr.forEach(i => {
        if (this.instance.hasOwnProperty(i.Path) && this.currentRadio != 2) {
          i.value = this.instance[i.Path]
        }
        if (i.InputMode == 'C5') {
          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)
          //判断输入类型
          if (i.InputMode == 'D1L') {
            let d1l = tools.formatDataSource(i.DataSource)
            d1l.forEach(k => {
              if (k.Code == this.instance[i.Path]) {
                i.value = k.Name
              }
            })
          } else if (i.InputMode == 'D2' || i.InputMode == 'C6') {
            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.InputMode) {
          case "L":
          case "L1":
          case "L2":
          case "M":
            break;
          default:
            if (result[`${i.FirstName}${i.SecondName ? '/' + i.SecondName : ''}`]) {
              result[`${i.FirstName}${i.SecondName ? '/' + i.SecondName : ''}`].paths.push({
                Path: i.Path,
                InfoPointName: i.InfoPointName,
                InfoPointCode: i.InfoPointCode,
                Value: i.value,
                Visible: i.Visible,
                KeyWord: i.KeyWord,
                InputMode: i.InputMode
              })
            } else {
              result[`${i.FirstName}${i.SecondName ? '/' + i.SecondName : ''}`] = {
                paths: [{
                  Path: i.Path,
                  InfoPointName: i.InfoPointName,
                  InfoPointCode: i.InfoPointCode,
                  Value: i.value,
                  Visible: i.Visible,
                  KeyWord: i.KeyWord,
                  InputMode: i.InputMode
                }]
              }
            }

        }


      })
      this.exhibitionBaseInformation.information = result
    },
    // 删除业务空间
    deleteCenote() {
      this.$confirm("此操作将删除竖井，是否继续？", "提示", {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteCenoteTableData([{ ShaftID: 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>

