<template>
  <div>
    <el-card>
      <div slot="header" class="clearfix">
        <span>资产模型基本信息</span>
      </div>
      <el-descriptions border :column="2">
        <el-descriptions-item label="表名称">{{tableName}}</el-descriptions-item>
        <el-descriptions-item label="表备注">{{tableNameAnnotation}}</el-descriptions-item>
        <el-descriptions-item label="数据量">{{tableDataCount}}</el-descriptions-item>
        <el-descriptions-item label="是否核心">
          <el-tag size="small">是</el-tag>
        </el-descriptions-item>
      </el-descriptions>
    </el-card>
    <el-card style=" margin-top: 20px;">
      <div slot="header" class="clearfix">
        <span>资产模型详细信息</span>
      </div>
      <el-table
        :data="assetTableDetailsList"
        style="width: 100%;">
        <el-table-column prop="assetStructureTableId" label="资产表id" v-if="false"/>
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="annotation" label="注释" />
        <el-table-column prop="primaryOrNot" label="是否主键" >
          <template slot-scope="scope">
            <el-tag size="small" :type="scope.row.primaryOrNot === 'Y' ? 'success' : 'danger'">
              {{scope.row.primaryOrNot}}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="type" label="类型" />
        <el-table-column prop="mappingType" label="映射类型" />
        <el-table-column prop="length" label="长度" />
        <el-table-column prop="decimalPoint" label="小数位" />
        <el-table-column prop="nullOrNot" label="是否为空"  >
          <template slot-scope="scope">
            <el-tag size="small" :type="scope.row.nullOrNot === 'Y' ? 'success' : 'danger'" >
              {{scope.row.nullOrNot}}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="defaultValue" label="默认值" />
        <el-table-column prop="yesNoDictionary" label="是否字典" >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.yesNoDictionary === 'Y'" size="small" type="success">
              {{scope.row.yesNoDictionary}}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="mappingDictionary" label="映射字典" >
          <template slot-scope="scope">
            <el-popover
              v-if="scope.row.yesNoDictionary === 'Y'"
              placement="left"
              width="200"
              trigger="hover">
              <el-table v-for="item in dictionaryList" v-if="(item.name + '(' + item.type + ')') === scope.row.mappingDictionary" :data="item.dataDictionaryTypeList">
                <el-table-column property="label" label="字典标签"/>
                <el-table-column property="value" label="字典值"/>
              </el-table>
              <el-tag slot="reference">{{scope.row.mappingDictionary}}</el-tag>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column prop="id" label="操作" >
          <template slot-scope="scope">
            <el-button type="text" @click="update(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog title="资产结构修改" width="80%" :visible.sync="formStatus">
      <el-form :model="form" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="名称">
              <el-input v-model="form.name" readonly autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="注释">
              <el-input v-model="form.annotation" readonly autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否主键">
              <el-tag size="small" :type="form.primaryOrNot === 'Y' ? 'success' : 'danger'">
                {{form.primaryOrNot}}
              </el-tag>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="数据类型">
              <el-input v-model="form.type" readonly autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="数据长度">
              <el-input v-model="form.length" readonly autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="小数位">
              <el-input v-model="form.decimalPoint" readonly autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否为空">
              <el-tag size="small" :type="form.nullOrNot === 'Y' ? 'success' : 'danger'">
                {{form.nullOrNot}}
              </el-tag>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="默认值">
              <el-input v-model="form.defaultValue" readonly autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="映射字段">
              <el-input v-model="form.mappingType" readonly autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否字典">
              <el-switch
                v-model="form.yesNoDictionary"
                active-value="Y"
                inactive-value="N"
                active-color="#13ce66"
                inactive-color="#ff4949">
              </el-switch>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="form.yesNoDictionary === 'Y'">
          <el-col :span="24">
            <el-form-item label="字典">
              <el-select v-model="form.mappingDictionary">
                <el-option v-for="item in dictionaryList" :key="item.name" :label="item.name" :value="item.name + '(' + item.type + ')'"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24" v-for="item in dictionaryList" v-if="form.mappingDictionary === item.name + '(' + item.type + ')'">
            <el-form-item>
              <el-table :data="item.dataDictionaryTypeList" striped border>
                <el-table-column property="label" label="字典标签"/>
                <el-table-column property="value" label="字典值"/>
              </el-table>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="formStatus = false">取 消</el-button>
        <el-button type="primary" @click="detailsUpdate">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {editAssetTableDetails, findByAssetStructureTableIdlist} from "@/api/data/structure";

export default {
  props: {
    tableName: null,
    tableNameAnnotation: null,
    tableDataCount: null,
    assetTableDetailsList: [],
    dictionaryList: []
  },
  name: 'OverallAssetStructure',
  data() {
    return {
      form: {},
      formStatus: false,
    }
  },
  methods: {
    update(row) {
      console.log(row)
      this.form = row;
      this.formStatus = true;
    },
    detailsUpdate() {
      editAssetTableDetails(this.form).then(response => {
        findByAssetStructureTableIdlist(this.form.assetStructureTableId).then(res => {
          console.log(res.data)
          this.assetTableDetailsList = res.data;
          this.formStatus = false;
        })
      })
    }
  }
}
</script>
<style scoped lang="scss">

</style>
