<template>
  <el-row :gutter="40" class="panel-group">
    <div class="title-header">
      {{dataSourceObj.dataSource.fromSystem}} - 资产结构概述
    </div>
    <el-col :sm="12" :xs="12" class="card-panel-col">
      <div class="card-panel" @click="handleSetLineChartData('messages')">
        <div class="card-panel-icon-wrapper icon-message">
          <svg-icon class-name="card-panel-icon" icon-class="table"/>
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">
            资产模型
          </div>
          <count-to :duration="3000" :end-val="dataSourceObj.dataSource.tableCount" :start-val="0" class="card-panel-num"/>
        </div>
      </div>
    </el-col>
    <el-col :sm="12" :xs="12" class="card-panel-col">
      <div class="card-panel" @click="handleSetLineChartData('purchases')">
        <div class="card-panel-icon-wrapper icon-money">
          <svg-icon class-name="card-panel-icon" icon-class="field"/>
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">
            数据模型
          </div>
          <count-to :duration="3200" :end-val="dataSourceObj.dataSource.dataModelCount" :start-val="0" class="card-panel-num"/>
        </div>
      </div>
    </el-col>
    <el-col :span="24">
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span>基础字典</span>
          <el-popover
            placement="top-start"
            title="基础字典"
            width="200"
            trigger="hover"
            content="这个字典是数据资产项目当中的字典内容，而不是本项目当中的字典内容。主要作用为数据清洗过程中数据字典映射作用">
            <i class="el-icon-question" slot="reference"></i>
          </el-popover>
          <el-popover
            placement="right"
            width="400"
            trigger="click">
            <el-row :gutter="20">
              <el-col :span="18">
                <el-input v-model="dictionary.dictionaryName"></el-input>
              </el-col>
              <el-col :span="6">
                <el-button @click="addDict">确定</el-button>
              </el-col>
            </el-row>
            <el-button style="float: right; padding: 3px 0" type="text" slot="reference">新增字典</el-button>
          </el-popover>
        </div>
        <el-row :gutter="20">
          <el-col v-for="dictionaryInfoResp in dictionaryInfoRespList" :md="8" :sm="24" :xs="12">
            <el-card class="box-card" style="height: 300px">
              <div slot="header" class="clearfix">
                <span>{{dictionaryInfoResp.dictionary.dictionaryName}}</span>
                <el-button style="float: right; padding: 3px 0"
                           type="text"
                           @click="dictionaryInfoResp.dictionaryInfoList.push({dictionaryInfoTag: null, dictionaryInfoValue: null, isEdit: true })"
                >新增</el-button>
              </div>
              <el-table :data="dictionaryInfoResp.dictionaryInfoList" style="width: 100%" height="280px">
                <el-table-column label="标签">
                  <template slot-scope="scope">
                    <span v-if="!scope.row.isEdit">{{scope.row.dictionaryInfoTag}}</span>
                    <el-input v-if="scope.row.isEdit" v-model="scope.row.dictionaryInfoTag" size="mini"></el-input>
                  </template>
                </el-table-column>
                <el-table-column label="值">
                  <template slot-scope="scope">
                    <span v-if="!scope.row.isEdit">{{scope.row.dictionaryInfoValue}}</span>
                    <el-input v-if="scope.row.isEdit" v-model="scope.row.dictionaryInfoValue" size="mini"></el-input>
                  </template>
                </el-table-column>
                <el-table-column label="操作">
                  <template slot-scope="scope">
                    <el-button
                      v-if="!scope.row.isEdit"
                      size="mini"
                      type="text"
                      icon="el-icon-edit"
                      @click="$set(scope.row,'isEdit',true)"
                    >修改</el-button>
                    <el-button
                      v-if="scope.row.isEdit"
                      @click="editConfirm(scope.row,dictionaryInfoResp.dictionary.id)"
                      size="mini"
                      type="text"
                      icon="el-icon-finished"
                    >确定</el-button>
                  </template>
                </el-table-column>
              </el-table>
            </el-card>
          </el-col>
        </el-row>
      </el-card>

    </el-col>
    <el-col :span="24">
      <el-tabs v-model="activeName" type="border-card" >
        <el-tab-pane :name="assetsModelResp.dataTable.tableName" :label="assetsModelResp.dataTable.tableName" :key="assetsModelResp.dataTable.id" v-for="assetsModelResp in assetsModelRespArrayList">
          <overall-asset-structure :tableInfo="assetsModelResp.dataTable" :assetsModelList="assetsModelResp.assetsModelList" />
        </el-tab-pane>
      </el-tabs>
    </el-col>
  </el-row>

</template>

<script>
import CountTo from 'vue-count-to'
import OverallAssetStructure from './OverallAssetStructure.vue'
import {getDictionaryRespByDataSourceId, insertDictionary, insertDictionaryInfo} from "@/api/dataSource/dictionary";

export default {
  watch:{
    assetsModelRespArrayList:{
      // 此处监听variable变量，当期有变化时执行
      handler(item1,item2){
        this.activeName=item1[0].dataTable.tableName
      }
    }
  },
  props: {
    assetsModelRespArrayList: {
      type: Array
    },
    dataSourceObj: {
      type: Object
    },
    dictionaryInfoRespList: {
      type: Array
    }
  },
  data() {
    return {
      flagDictionaryId:null,
      nowTime: null,
      activeName: '',
      dictionary: {},
      giveObj: []
    }
  },
  components: {
    OverallAssetStructure,
    CountTo
  },
  methods: {

    editConfirm(row,dictId){
      console.log("获得的dictID",dictId)
      row.dictionaryId = dictId
      console.log("字典信息",row)
      if (row.dictionaryInfoTag.length == 0  || row.dictionaryInfoValue.length == 0) {
        this.$message.error('字典标签或字典值，不可为空');
        return;
      }
      row.isEdit = false;
      insertDictionaryInfo(row).then(
        res => {
          console.log("添加字典详情:",res.data)
        }
      )
      this.getDictionaryResp()
    },
    getDictionaryResp() {
      getDictionaryRespByDataSourceId(this.dataSourceObj.dataSource.id).then(
        res => {
          console.log("字典表响应对象:",res.data)
          this.giveObj = res.data
          this.giveObj.forEach(dictionaryInfoResp => {
            dictionaryInfoResp.dictionaryInfoList.forEach(dictionaryInfo => {
              this.$set(dictionaryInfo,'isEdit',false)
            })
          })
          this.handleSetLineChartData(this.giveObj)
        }
      )
    },
    handleSetLineChartData(type) {
      this.$emit('handleSetLineChartData', type)
    },
    addDict(){
      if (!this.dictionary.dictionaryName){
        this.$message.error('数据字典，不可为空');
        return;
      }
      let dictionaryInfoArrayList = {}
      this.dictionary.dataSourceId = this.dataSourceObj.dataSource.id
      dictionaryInfoArrayList.dictionary = this.dictionary
      dictionaryInfoArrayList.dictionaryInfoList = []

      insertDictionary(this.dictionary).then(
        res => {
          console.log("让我看看",res)
          dictionaryInfoArrayList.dictionary.id = res.data
          this.dictionaryInfoRespList.push(dictionaryInfoArrayList)
          this.dictionary = {}
          this.getDictionaryResp()
          if (res.code == 200){
            this.$message.success("添加成功")
          }
        }
      )
    },
  }
}
</script>

<style lang="scss" scoped>
.panel-group {
  margin-top: 18px;

  .card-panel-col {
    margin-bottom: 32px;
  }

  .card-panel {
    height: 108px;
    cursor: pointer;
    font-size: 12px;
    position: relative;
    overflow: hidden;
    color: #666;
    background: #fff;
    box-shadow: 4px 4px 40px rgba(0, 0, 0, .05);
    border-color: rgba(0, 0, 0, .05);

    &:hover {
      .card-panel-icon-wrapper {
        color: #fff;
      }

      .icon-people {
        background: #40c9c6;
      }

      .icon-message {
        background: #36a3f7;
      }

      .icon-money {
        background: #f4516c;
      }

      .icon-shopping {
        background: #34bfa3
      }
    }

    .icon-people {
      color: #40c9c6;
    }

    .icon-message {
      color: #36a3f7;
    }

    .icon-money {
      color: #f4516c;
    }

    .icon-shopping {
      color: #34bfa3
    }

    .card-panel-icon-wrapper {
      float: left;
      margin: 14px 0 0 14px;
      padding: 16px;
      transition: all 0.38s ease-out;
      border-radius: 6px;
    }

    .card-panel-icon {
      float: left;
      font-size: 48px;
    }

    .card-panel-description {
      float: right;
      font-weight: bold;
      margin: 26px;
      margin-left: 0px;

      .card-panel-text {
        line-height: 18px;
        color: rgba(0, 0, 0, 0.45);
        font-size: 16px;
        margin-bottom: 12px;
      }

      .card-panel-num {
        font-size: 20px;
      }
    }
  }
}

@media (max-width: 550px) {
  .card-panel-description {
    display: none;
  }

  .card-panel-icon-wrapper {
    float: none !important;
    width: 100%;
    height: 100%;
    margin: 0 !important;

    .svg-icon {
      display: block;
      margin: 14px auto !important;
      float: none !important;
    }
  }
}
.title-header{
  text-align: center;
  width: 100%;
  font-size: 46px;
  font-weight: 600;
  line-height: 100px;
}
</style>
