<template>
  <el-row :gutter="40" class="panel-group">
    <div class="title-header">
      {{title}} - 资产结构概述
    </div>
    <el-form :model="totalNum">
      <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="totalNum.assetsNum" :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="totalNum.fieldsNum" :start-val="0" class="card-panel-num"/>
          </div>
        </div>
      </el-col>
    </el-form>
    <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="dictAddName"></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="(val,key) in dictMap" :md="8" :sm="24" :xs="12">
            <el-card class="box-card" style="height: 300px">
              <div slot="header" class="clearfix">
                <span>{{val.dictType}}</span>
                <el-button style="float: right; padding: 3px 0"
                           type="text"
                           @click="val.dictDataList.push({ dictCode:0 , dictType:val.dictType, dictLabel: null, dictValue: null, isEdit: true })"
                >新增</el-button>
                <el-button style="float: right; padding: 3px 0"
                           @click="delType(val.dictType)">删除字典</el-button>
              </div>
              <el-table :data="val.dictDataList" style="width: 100%" height="280px">
                <el-table-column prop="label" label="标签">
                  <template slot-scope="scope">
                    <span v-if="!scope.row.isEdit">{{scope.row.dictLabel}}</span>
                    <el-input v-if="scope.row.isEdit" v-model="scope.row.dictLabel" size="mini"></el-input>
                  </template>
                </el-table-column>
                <el-table-column prop="val" label="值">
                  <template slot-scope="scope">
                    <span v-if="!scope.row.isEdit">{{scope.row.dictValue}}</span>
                    <el-input v-if="scope.row.isEdit" v-model="scope.row.dictValue" size="mini"></el-input>
                  </template>
                </el-table-column>
                <el-table-column prop="val" label="操作">
                  <template slot-scope="scope">
                    <el-button
                      v-if="!scope.row.isEdit"
                      size="mini"
                      type="text"
                      icon="el-icon-edit"
                      @click="updData(scope.row)"
                    >修改</el-button>
                    <el-button
                      v-if="!scope.row.isEdit"
                      size="mini"
                      type="text"
                      icon="el-icon-edit"
                      @click="delData(scope.row)"
                    >删除</el-button>
                    <el-button
                      v-if="scope.row.isEdit"
                      @click="editConfirm(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" style="margin-top: 20px">
      <el-tabs v-model="activeName" type="border-card" @tab-click="checkTableName">
        <el-tab-pane v-for="table in tableList" :label="table.tableName+'('+table.asas+')'"
                     :name="table.tableName">
          <overall-asset-structure v-if="table.tableName === tableParams.tableName" :table-params="tableParams"/>
        </el-tab-pane>
      </el-tabs>
    </el-col>
  </el-row>

</template>

<script>
import CountTo from 'vue-count-to'
import OverallAssetStructure from './OverallAssetStructure.vue'
import { listDictType, indexDictType, indexDictData, deleteDictData, selectDictData, deleteDictType } from "@/api/system/dicts"
import val from "quill";
import { selTotal } from "@/api/system/accredit";
export default {
  props: {
    title: {
      type: String,
      default: "-"
    },
    overallName: {
        type: Array
    }
  },
  watch: {
    overallName: {
      handler(val) {
        if (val != null){
          this.init()
        }
      },
      deep: true,
      immediate: true
    }
  },
  data() {
    return {
      activeName: null,
      dictAddName: null,
      dictMap: [],
      tableList: [],
      listStatus: [],
      tableParams: {
        tableName: '',
        tableAsName: ''
      },
      stats: null,
      dictData: {
        dictCode: '',
        dictType: '',
        dictLabel: '',
        dictValue: '',
        isEdit: false,
      },
      totalNum: {
        assetsNum: 0,
        fieldsNum: 0
      }
    }
  },
  components: {
    OverallAssetStructure,
    CountTo
  },
  methods: {
    val() {
      return val
    },
    checkTableName(checkTab){
      console.log(checkTab)
      this.tableParams = this.tableList.findLast(item => item.tableName === checkTab.name)
      console.log(this.tableParams)
    },
    updData(row) {
      selectDictData(row).then(res => {
        this.dictData = res.data
      })
      row.isEdit = true
    },
    editConfirm(row) {
      if (!row.dictLabel || !row.dictValue) {
        this.$message.error('字典标签或字典值，不可为空');
        return;
      }
      indexDictData(row).then(res => {
        this.$message.success(res.data)
      })
      row.isEdit = false;
    },
    delType(row) {
      deleteDictType(row).then(res => {
        this.$message.success(res.data)
      })
      this.init()
    },
    delData(row) {
      deleteDictData(row).then(res => {
        this.$message.success(res.data)
      })
      return;
    },
    addDict() {
      if (!this.dictAddName){
        this.$message.error('数据字典，不可为空');
        return;
      }
      indexDictType(this.dictAddName).then(res => {
        this.$message.success(res.data)
      })
      this.dictAddName = null
    },
    handleSetLineChartData(type) {
      this.$emit('handleSetLineChartData', type)
    },
    init() {
      // console.log(this.overallName.at(0).databaseName)
      selTotal(this.overallName.at(0).databaseName).then(res => {
        this.totalNum = res.data
      })
      listDictType().then(res => {
        this.dictMap = res.data
      })
      this.tableList = this.overallName;
      this.activeName = this.tableList[0].tableName;
      this.tableParams = {
        tableName: this.tableList[0].tableName,
        databaseName: this.tableList[0].databaseName
      }
      console.log(this.tableParams)
    }
  },
  // 生命周期 - 创建完成（可以访问当前this实例）",
  created() {
    this.init()
  }
}
</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>
