import { getByKey, getById } from '@/api/platform/data/dataTemplate'
import { getFormDataByFormKey } from '@/api/platform/form/formDef'
import FormOptions from '@/business/platform/form/constants/formOptions'
import Utils from '@/utils/util'
import { Dialog } from 'vant'

var TemplateUtils = {
  getById(id, params, resolve, reject) {
    getById({
      dataTemplateId: id
    }).then(response => {
      const dataTemplate = Utils.parseData(response.data)
      this.buildData({
        dataTemplate: dataTemplate,
        fields: this.datasets2Fields(dataTemplate.datasets)
      }, resolve, reject)
    }).catch((e) => {
      // TODO 异常
      console.error(e)
      reject(e)
    })
  },
  getByKey(key, params, resolve, reject) {
    getByKey({
      dataTemplateKey: key
    }).then(response => {
      const dataTemplate = Utils.parseData(response.data)
      this.buildData({
        dataTemplate: dataTemplate,
        fields: this.datasets2Fields(dataTemplate.datasets)
      }, resolve, reject)
    }).catch((e) => {
      // TODO 异常
      console.error(e)
      reject(e)
    })
  },
  /**
 * 数据源数据，转字段数据
 * @param {*} datasets
 */
  datasets2Fields(datasets) {
    if (Utils.isEmpty(datasets)) { return [] }
    const fields = []
    let dataset = {}
    for (var _i = 0, _len = datasets.length; _i < _len; _i++) {
      dataset = datasets[_i]
      if (dataset.attrType === 'column') {
        fields.push(dataset)
      }
    }
    return fields
  },
  buildData({ dataTemplate, fields }, resolve, reject) {
    if (dataTemplate.templates) {
      let template = {}
      if (dataTemplate.showType === 'compose') { // 如果是组合
        var tpl = {}
        if (dataTemplate.composeType === 'treeList') {
          tpl = dataTemplate.templates[1]
        } else if (dataTemplate.composeType === 'listTree') {
          tpl = dataTemplate.templates[0]
        }
        if (!tpl) {
          Dialog.alert('未设置模版！')
          return
        }

        if (tpl.dataTemplate['bind_template'] === 'Y') {
          this.getByKey(tpl.attrs['bind_template_key'])
        } else {
          template = tpl
        }
      } else { // 不是组合
        template = dataTemplate.templates[0]
        this.buildTemplateData(
          {
            dataTemplate: dataTemplate,
            key: dataTemplate.type + '_' + dataTemplate.showType,
            template: template,
            fields: fields
          }, resolve
        )

        // conditions = template.filter_conditions

        // this.buildKey(dataTemplate, template)

        // var dynamicParams = this.buildDynamicParams(conditions)

        // var data = {
        //   dynamicParams: dynamicParams
        // }
        // this.params = params
      }
    }
  },
  buildTemplateData({
    dataTemplate,
    key,
    template,
    fields
  }, resolve) {
    const conditions = template.filter_conditions

    const dynamicParams = this.buildDynamicParams(conditions)
    const data = { ...dataTemplate }
    data['templates'] = null

    data.dynamicParams = dynamicParams
    data[key] = template
    data['fields'] = fields
    this.initFelds(data, resolve)
  },
  initFelds(data, resolve) {
    if (data.attrs && Utils.isNotEmpty(data.attrs.form_key)) {
      getFormDataByFormKey({
        formKey: data.attrs.form_key
      }).then(response => {
        const formData = Utils.parseData(response.data)
        const fields = this.buildFelds(formData.fields, data.fields)
        data.fields = fields
        resolve(data)
      }).catch(() => {
      })
    } else {
      resolve(data)
    }
  },
  buildFelds(fields, datasets) {
    const fieldMap = {}
    const columns = this.getColumns(fields)

    columns.forEach(field => {
      if (Utils.isNotEmpty(field.field_name)) {
        fieldMap[field.field_name.toLowerCase()] = field
      }
    })
    const rtn = []
    datasets.forEach(dataset => {
      let field = fieldMap[dataset.name]
      if (Utils.isNotEmpty(dataset.name)) {
        field = fieldMap[dataset.name.toLowerCase()]
      }
      if (Utils.isNotEmpty(field)) {
        dataset.field_name = Utils.isNotEmpty(field.name) ? field.name.toLowerCase() : field.name
        dataset.form_field_name = field.name
        dataset.field_type = field.field_type
        dataset.field_options = field.field_options
      }
      rtn.push(dataset)
    })
    return rtn
  },
  /**
   * 获取所有字段
   * @param {*} columns
   */
  getColumns(columns) {
    const formColumns = []
    const traverse = (fields) => {
      fields.forEach((field) => {
        if (FormOptions.t.NESTED_FIELD_TYPES.includes(field.field_type)) {
          const childColumns = field.field_options.columns
          childColumns.forEach(child => {
            traverse(child.fields)
          })
        } else {
          formColumns.push(field)
        }
      })
    }
    traverse(columns)
    return formColumns
  },
  buildDynamicParams(conditions) {
    const dynamicParams = {}
    if (Utils.isNotEmpty(conditions)) { return dynamicParams }
    const filterConditions = Utils.isNotEmpty(conditions) ? conditions[0] : {}
    const	filter = filterConditions.filter
    if (filter) { this._createDynamicParams(dynamicParams, filter) }
    return dynamicParams
  },
  _createDynamicParams(dynamicParams, filter) {

  }
}

/**
 * 通过模版key 获取数据模版
 *
 */
export function loadDataTemplateByKey(key, params) {
  return new Promise((resolve, reject) => {
    TemplateUtils.getByKey(key, params, resolve, reject)
  })
}

export function loadDataTemplateById(id, params) {
  return new Promise((resolve, reject) => {
    TemplateUtils.getById(id, params, resolve, reject)
  })
}

export function getDataTemplateListTemplate(dataTemplate) {
  if (dataTemplate.showType === 'compose') { // 如果是组合
  } else { //
    const type = dataTemplate.type
    const showType = dataTemplate.showType
    return dataTemplate[type + '_' + showType]
  }
}

/**
 * 构建数据模版字段
 */
export function buildDataTemplateFields(fields, key = 'name') {
  if (Utils.isEmpty(fields)) {
    return {}
  }
  const res = {}
  fields.forEach(field => {
    res[field[key]] = field
  })
  return res
}

