<template>
  <el-row :gutter="40" class="panel-group">
    <div class="title-header">
      {{title.dataSourceName+'('+title.databaseName+'-'+title.systemName+')'}} - 资产结构概述
    </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="assetModel" :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="table"/>
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">
            数据模型
          </div>
          <count-to :duration="3200" :end-val="dataModel" :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="200px"
            trigger="click">

                <el-form :inline="true" :model="dictInfo" class="demo-form-inline">
                  <el-form-item label="字典名称:">
                    <el-input v-model="dictInfo.dictionaryName" style="width: 150px"></el-input>
                  </el-form-item>
                  <el-form-item label="字典Key:">
                    <el-input v-model="dictInfo.dictionaryKey" style="width: 150px"></el-input>
                  </el-form-item>
                  <el-form-item>
                    <el-button @click="addDict">确定</el-button>
                  </el-form-item>
                </el-form>

            <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="dict in dictMap" :md="8" :sm="24" :xs="12">
            <el-card class="box-card" style="height: 300px">
              <div slot="header" class="clearfix">
                <span>{{dict.dictionaryName+'('+dict.dictionaryKey+')'}}</span>
                <el-button style="float: right; padding: 3px 0"
                           type="text"
                           @click="deleteDictionary(dict.id)"
                >删除</el-button>
                <el-button style="float: right; padding: 3px 0;margin-right: 8px"
                           type="text"
                           @click="dict.dictionaryDataList.push({ label: null, val: null, edit: true })"
                >新增</el-button>
              </div>
              <el-table :data="dict.dictionaryDataList" style="width: 100%" height="280px">
                <el-table-column prop="label" label="标签">
                  <template slot-scope="scope">
                    <span v-if="!scope.row.edit">{{scope.row.label}}</span>
                    <el-input v-if="scope.row.edit" v-model="scope.row.label" size="mini"></el-input>
                  </template>
                </el-table-column>
                <el-table-column prop="val" label="值">
                  <template slot-scope="scope">
                    <span v-if="!scope.row.edit">{{scope.row.val}}</span>
                    <el-input v-if="scope.row.edit" v-model="scope.row.val" size="mini"></el-input>
                  </template>
                </el-table-column>
                <el-table-column prop="val" label="操作">
                  <template slot-scope="scope">
                    <el-button
                      v-if="!scope.row.edit"
                      size="mini"
                      type="text"
                      icon="el-icon-edit"
                      @click="scope.row.edit = true"
                    >修改</el-button>
                    <el-button
                      v-if="scope.row.edit"
                      @click="editConfirm(dict,scope.row)"
                      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 v-for="tableAsset in title.tableList" :label="tableAsset.tableName+'('+tableAsset.tableComment+')'" :name="tableAsset.tableName">
          <overall-asset-structure ref="assetStru" v-if="activeName == tableAsset.tableName" :title="tableAsset"/>
        </el-tab-pane>
      </el-tabs>
    </el-col>
  </el-row>

</template>

<script>
import CountTo from 'vue-count-to'
import OverallAssetStructure from './OverallAssetStructure.vue'
import {addDictionary, deleteDictionary, getDictionaryList} from "@/api/etl/dictionary";
import {addData, updateData} from "@/api/etl/data";

export default {
  watch:{
    'title':{
      handler(val){
        this.getDictionaryList()
        this.activeName = val.tableList[0].tableName
        this.assetModel = 0
        this.dataModel = 0
        val.tableList.forEach(table => {
          if (table.tableCount!=null){
            this.dataModel = parseInt(this.dataModel) + parseInt(table.tableCount);
          }
          if (table.fields!=null){
            this.assetModel = parseInt(this.assetModel) + parseInt(table.fields);
          }
        })

      }
    }
  },
  props: {
    title:{}
  },
  data() {
    return {
      dictInfo: {
        dictionaryName: null,
        dictionaryKey: null,
        dataSourceId: null
      },
      dictMap: {},
      activeName: '',
      assetModel: 0,
      dataModel: 0
    }
  },
  components: {
    OverallAssetStructure,
    CountTo
  },
  methods: {
    deleteDictionary(id) {
      deleteDictionary(id).then(res => {
        this.getDictionaryList()
        // this.$nextTick(() => {
        //   this.$refs.assetStru.assetModelList()
        // })
      })

    },
    getDictionaryList() {
      getDictionaryList(this.title.id).then(res => {
        this.dictMap = res.data
      })
    },
    editConfirm(dict,row){
      if (!row.label || !row.val) {
        this.$message.error('字典标签或字典值，不可为空');
        return;
      }
      console.log(row)
      if (row.id) {
        console.log('修改')
        updateData(row).then(res => {
          row.edit = false;
          this.getDictionaryList()
          // this.$nextTick(() => {
          //   this.$refs.assetStru.assetModelList()
          // })
        })
      }else{
        console.log('添加')
        row.dictionaryId=dict.id
        addData(row).then(res => {
          row.edit = false;
          this.getDictionaryList()
          // this.$nextTick(() => {
          //   this.$refs.assetStru.assetModelList()
          // })
        })
      }

    },
    addDict(){
      if (!this.dictInfo.dictionaryName){
        this.$message.error('字典名称，不可为空');
        return;
      }
      if (!this.dictInfo.dictionaryKey){
        this.$message.error('字典Key，不可为空');
        return;
      }
      this.dictInfo.dataSourceId = this.title.id
      addDictionary(this.dictInfo).then(res => {
        this.dictInfo = {
          dictionaryName: null,
          dictionaryKey: null,
          dataSourceId: null
        }
        this.getDictionaryList()
      })

    },
    handleSetLineChartData(type) {
      this.$emit('handleSetLineChartData', type)
    }
  }
}
</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>
