<template>
  <el-card class="box-card" shadow="always">
    <div slot="header" class="clearfix">
      <span>{{ title }}</span>
      <el-button-group style="float: right;">
        <el-button size="mini" icon="el-icon-back" round @click="showCard">返回</el-button>
      </el-button-group>
    </div>
    <div class="body-wrapper">
      <el-form ref="form" :model="form" label-width="80px" disabled>
        <el-form-item label="规则名称">
          <el-input v-model="form.ruleName" />
        </el-form-item>
        <el-form-item label="规则级别">
          <el-radio-group v-model="form.ruleLevel">
            <el-radio
              v-for="dict in ruleLevelOptions"
              :key="dict.id"
              :label="dict.itemText"
            >{{ dict.itemValue }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="数据源">
          <el-select v-model="form.ruleSourceId">
            <el-option
              v-for="source in sourceOptions"
              :key="source.id"
              :label="source.sourceName"
              :value="source.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="数据表">
          <el-select v-model="form.ruleTableId">
            <el-option
              v-for="table in tableOptions"
              :key="table.id"
              :label="table.tableName"
              :value="table.id">
              <span style="float: left">{{ table.tableName + '(' + table.tableComment + ')' }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="核查字段">
          <el-select v-model="form.ruleColumnId">
            <el-option
              v-for="column in columnOptions"
              :key="column.id"
              :label="column.columnName"
              :value="column.id">
              <span style="float: left">{{ column.columnName + '(' + column.columnComment + ')' }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="核查脚本" prop="ruleSql">
          <el-input v-model="form.ruleSql" type="textarea" />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.id"
              :label="dict.itemText"
            >{{ dict.itemValue }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.remark" type="textarea" />
        </el-form-item>
      </el-form>
    </div>
  </el-card>
</template>

<script>
import { getCheckRule } from '@/api/quality/checkrule'
import { listDataSource } from '@/api/metadata/datasource'
import { listDataTable } from '@/api/metadata/datatable'
import { listDataColumn } from '@/api/metadata/datacolumn'

export default {
  name: 'CheckRuleDetail',
  props: {
    data: {
      type: Object,
      default: function() {
        return {}
      }
    }
  },
  data() {
    return {
      title: '核查规则详情',
      // 展示切换
      showOptions: {
        data: {},
        showList: true,
        showAdd: false,
        showEdit: false,
        showDetail: false
      },
      // 表单参数
      form: {},
      // 状态数据字典
      statusOptions: [],
      // 规则级别数据字典
      ruleLevelOptions: [],
      sourceOptions: [],
      tableOptions: [],
      columnOptions: []
    }
  },
  created() {
    console.log('id:' + this.data.id)
    this.getDicts('sys_common_status').then(response => {
      if (response.success) {
        this.statusOptions = response.data
      }
    })
    this.getDicts('data_check_rule_level').then(response => {
      if (response.success) {
        this.ruleLevelOptions = response.data
      }
    })
    this.getDataSourceList()
  },
  mounted() {
    this.getCheckRule(this.data.id)
  },
  methods: {
    showCard() {
      this.$emit('showCard', this.showOptions)
    },
    /** 获取详情 */
    async getCheckRule(id) {
      this.form = await getCheckRule(id).then(response => {
        if (response.success) {
          return response.data
        }
      })
      this.tableOptions = await listDataTable({ sourceId: this.form.ruleSourceId }).then(response => {
        if (response.success) {
          return response.data
        }
      }) || []
      this.columnOptions = await listDataColumn({ sourceId: this.form.ruleSourceId, tableId: this.form.ruleTableId }).then(response => {
        if (response.success) {
          return response.data
        }
      }) || []
    },
    getDataSourceList() {
      listDataSource().then(response => {
        if (response.success) {
          this.sourceOptions = response.data
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card ::v-deep .el-card__body {
  height: calc(100vh - 230px);
  overflow-y: auto;
}
</style>
