import routerUserMenu from '@/mixins/router-user-menu'
import breadcrumbComponent from '@/components/breadcrumb.vue'
import multipleSelectTree from '@/components/multiple-select-tree.vue'
import {editStatusOptions, enableStatusOptions} from '@/utils/public-options'
import {toTree} from '@/utils/util'
import {editStatusForCompute, editStatusForInput} from '../../../../utils/public-options'

/**
 * KPI批量创建
 *
 *  version = "1.0.0";
 *  author = "liucui";
 *  email = "liu.cui@data-u.com";
 * */
export default {
  name: 'KPIBatch',
  components: {
    breadcrumbComponent, multipleSelectTree
  },
  mixins: [routerUserMenu],
  data () {
    return {
      selectedAll: false,
      checkModel: [],
      checkList: [],
      // 主题树
      tree: [],
      // KPI类型
      typeOptions: [],
      // 规则类型
      // ruleTypeOptions: [],
      // 定义类型的规则选项
      defineRuleTypeOptions: [],
      // 输入类型的规则选项
      inputRuleTypeOptions: [],
      // 数据库
      databaseOptions: [],
      // 表设置数据表
      tableOptions: [],
      // 定义字段
      columnOptions: [],
      // 编辑状态、启用状态的对应关系
      formatterOptions: {
        'editStatusShow': editStatusOptions,
        'editStatusForCompute': editStatusForCompute,
        'editStatusForInput': editStatusForInput,
        'enableStatusShow': enableStatusOptions
      },
      searchForm: {
        topicCodes: [],
        databaseId: '',
        tableId: ''
      },
      formInfo: {
        contents: []/* contents: [{
          // 唯一标识
          id: '',
          // KPI名称
          name: '',
          // KPI类型ID
          type: '',
          // 主题编码
          topicCodes: [],
          // KPI规则/数据类型ID
          ruleTypeId: '',
          // KPI编码
          kpiCode: '',
          // 单位
          unit: '',
          // KPI描述
          description: '',
          // 启用状态
          enableStatus: '',
          // 编辑状态
          editStatus: '',
          // 数据库ID
          databaseId: '',
          // 定义/输入类型对象
          definedTypeInfo: {
            // 表ID
            tableId: '',
            // 字段ID
            columnId: ''
          }
        }] */
      }
    }
  },
  computed: {
    /* 转换数据格式 */
    treeData: {
      get: function () {
        let tree = this.tree
        // 线性数组转成树后渲染
        let defaultProps = {
          id: 'code',
          parent_id: 'pcode',
          label: 'name'
        }
        let newTree = toTree(tree, '0', defaultProps)
        return newTree
      }
    }
  },
  mounted () {
    // 获取KPI类型
    this.getKpiTypeOptions()
    // 规则类型
    this.getRuleTypeOptions()
    // 数据库
    this.getDatabaseOptions()
    // 获得主题树
    this.getTree()
    // 加载table
    this.getTableOptions()
    // 加载字段
    this.getColumnOptions()
  },
  watch: {
  },
  methods: {
    /* 获取整个树 */
    getTree () {
      this.$api.kpiTopic.topicTtree().then(res => {
        if (res.code == 1) {
          this.tree = res.data
        }
      })
    },
    /* 获取KPI类型列表 */
    getKpiTypeOptions () {
      this.$api.kpiCommon.listTypes().then(
        res => {
          if (res.code == 1) {
            this.typeOptions = res.data
          }
        })
    },
    /* 获取规则类型列表 */
    getRuleTypeOptions () {
      // 定义规则查询赋值
      this.$api.kpiCommon.listRuleTypesByKpiType({ kpiType: 1 }).then(
        res => {
          if (res.code == 1) {
            this.defineRuleTypeOptions = res.data
          }
        })
      // 输入类型规则赋值
      this.$api.kpiCommon.listRuleTypesByKpiType({ kpiType: 3 }).then(
        res => {
          if (res.code == 1) {
            this.inputRuleTypeOptions = res.data
          }
        })
    },
    /* 获取数据库列表 */
    getDatabaseOptions () {
      this.$api.kpiConfig.listMdmDatabases().then(
        res => {
          if (res.code == 1) {
            this.databaseOptions = res.data
          }
        })
    },
    /* 获取表信息 */
    getTableOptions (obj) {
      let params = { databaseId: obj }
      this.$api.kpiConfig.listMdmTablesByDatabase(params).then(
        res => {
          if (res.code == 1) {
            this.tableOptions = res.data
          }
        }
      )
    },
    /* 获取字段信息 */
    getColumnOptions (obj) {
      let params = {tableId: obj}
      this.$api.kpiConfig.listMdmColumnsByTable(params).then(
        res => {
          if (res.code == 1) {
            // 清空model
            this.checkModel = []
            this.columnOptions = res.data
            // 循环数据
            this.formInfo.contents = []
            let length = this.columnOptions.length
            for (let i = 0; i < length; i++) {
              let column = this.columnOptions[i]
              let content = {
                id: '',
                // 默认1 定义类型
                type: 1,
                // 定义类型对应的规则类型
                ruleTypeOptions: this.defineRuleTypeOptions,
                // 根据字段类型和小数位数来判断
                ruleTypeId: '',
                kpiCode: '',
                unit: '',
                // 默认1启用
                enableStatus: 1,
                // 默认 1可编辑
                editStatus: 1,
                name: column.columnName,
                topicCodes: this.searchForm.topicCodes,
                description: column.columnComment,
                databaseId: this.searchForm.databaseId,
                definedTypeInfo: {tableId: this.searchForm.tableId, columnId: column.id}
              }

              let options = this.defineRuleTypeOptions
              let ruleFormat = column.ruleFormat
              for (let j = 0; j < options.length; j++) {
                if (ruleFormat === options[j].format) {
                  content.ruleTypeId = options[j].id
                }
              }
              this.formInfo.contents.push(content)
            }
          }
        }
      )
    },
    getRuleTypeId (column) {

    },

    /* 选择类型 */
    typeChange (obj, index) {
      let type = obj.type
      if (type === 1) {
        this.formInfo.contents[index].ruleTypeOptions = this.defineRuleTypeOptions
      }
      if (type === 3) {
        this.formInfo.contents[index].ruleTypeOptions = this.inputRuleTypeOptions
      }
    },
    /* 定义/计算类型->选择数据库 */
    databaseChange (obj) {
      this.tableOptions = []
      this.searchForm.tableId = ''
      this.formInfo.contents = []
      this.getTableOptions(obj.databaseId)
    },
    /* 定义类型选择表 */
    tableChange (obj) {
      this.formInfo.contents = []
      this.getColumnOptions(obj.tableId)
    },
    /* 确定 */
    add () {
      let length = this.checkModel.length
      let topicCodes = this.searchForm.topicCodes
      if (length === 0 || topicCodes.length === 0) {
        this.$message.error('请选择')
        return false
      }
      let params = []
      for (let i = 0; i < length; i++) {
        let index = this.checkModel[i]
        let content = this.formInfo.contents[index]
        // 重新赋值
        content.topicCodes = topicCodes
        params.push(content)
      }

      // 判断组件设置是否通过验证
      this.$refs.formInfo.validate((valid) => {
        if (valid) {
          this.$api.kpiConfig.batchSave(params).then(res => {
            if (res.code === '1') {
              this.$message.success(res.msg)
              this.goBack()
            }
          })
        } else {
          return false
        }
      })
    },
    checkAll () {
      this.checkModel = []
      let contents = this.formInfo.contents
      for (let i = 0; i < contents.length; i++) {
        this.checkModel.push(i)
      }
    },
    unCheckAll () {
      this.checkModel = []
    },
    checkChange () {
    },
    chooseTopic () {
      console.log(this.formInfo.topicCodes)
    },
    /* 返回 */
    goBack () {
      this.$router.go(-1) // 返回上一层
    }
  }
}
