<template>
  <div>
    <Form ref="generateForm" :model="models" :rules="rules" :label-position="data.config.labelPosition" :label-width="data.config.labelWidth">
      <template v-for="item in data.list">

        <template v-if="item.type == 'grid'">
          <Row
            :key="item.key"
            type="flex"
            :gutter="item.options.gutter ? item.options.gutter : 0"
            :justify="item.options.justify"
            :align="item.options.align">
            <i-col v-for="(col, colIndex) in item.columns" :key="colIndex" :span="col.span">
                <template v-for="citem in col.list" >
                  <FormItem v-if="citem.type=='blank'" :label="citem.name" :prop="citem.model" :key="citem.key">
                    <slot :name="citem.model" :model="models"></slot>
                  </FormItem>
                  <genetate-form-item v-else :key="citem.key" :models.sync="models" :remote="remote" :rules="rules" :widget="citem"></genetate-form-item>
                </template>
            </i-col>
          </Row>
        </template>

        <template v-else-if="item.type == 'blank'">
          <FormItem :label="item.name" :prop="item.model" :key="item.key">
            <slot :name="item.model" :model="models"></slot>
          </FormItem>
        </template>

        <template v-else>
          <genetate-form-item :key="item.key" :models.sync="models" :rules="rules" :widget="item" :remote="remote"></genetate-form-item>
        </template>

      </template>
    </Form>
  </div>
</template>

<script>
import GenetateFormItem from './GenerateFormItem'

export default {
  name: 'fm-generate-form',
  components: {
    GenetateFormItem
  },
  props: ['data', 'remote', 'value'],
  data () {
    return {
      models: {},
      rules: {}
    }
  },
  created () {
    console.log(this.data, '传递过来的data')
    this.generateModle(this.data.list)
  },
  methods: {
    generateModle (genList) {
      for (let i = 0; i < genList.length; i++) {
        if (genList[i].type === 'grid') {
          genList[i].columns.forEach(item => {
            this.generateModle(item.list)
          })
        } else {
          if (Object.keys(this.value).indexOf(genList[i].model) >= 0) {
            this.models[genList[i].model] = this.value[genList[i].model]
          } else {
            if (genList[i].type === 'blank') {
              this.models[genList[i].model] = genList[i].options.defaultType === 'String' ? '' : (genList[i].options.defaultType === 'Object' ? {} : [])
            } else {
              this.models[genList[i].model] = genList[i].options.defaultValue
            }
          }

          if (this.rules[genList[i].model]) {
            this.rules[genList[i].model] = [...this.rules[genList[i].model], ...genList[i].rules]
          } else {
            this.rules[genList[i].model] = [...genList[i].rules]
          }
        }
      }
    },
    getData () {
      console.log(this.rules, '验证的规则')
      return new Promise((resolve, reject) => {
        this.$refs.generateForm.validate(valid => {
          if (valid) {
            resolve(this.models)
          } else {
            reject(new Error('表单数据校验失败').message)
          }
        })
      })
    },
    refresh () {

    }
  },
  watch: {
    value: {
      deep: true,
      handler (val) {
        console.log(JSON.stringify(val))
        this.models = { ...this.models, ...val }
      }
    }
  }
}
</script>
