<template>
  <div>
    <el-form ref="generateForm" 
      label-suffix=""
      :size="data.config.size"
      :model="models" :rules="rules" :label-position="data.config.labelPosition" :label-width="data.config.labelWidth + 'px'">
      <template v-for="item in data.list">

        <template v-if="item.type == 'grid'">
          <el-row
            :key="item.key"
            type="flex"
            :class="{'form-grid-container':item.options.customClass=='form-grid'?true:false}"
            :gutter="item.options.gutter ? item.options.gutter : 0"
            :justify="item.options.justify"
            :align="item.options.align"
          >
            <el-col v-for="(col, colIndex) in item.columns" :key="colIndex" :span="col.span"  :class="{'form-grid':item.options.customClass=='form-grid'?true:false}">

              <template v-for="citem in col.list" >
                <el-form-item v-if="citem.type=='blank'" :label="citem.name" :prop="citem.model" :key="citem.key">
                  <slot :name="citem.model" :model="models"></slot>
                </el-form-item>
                <genetate-form-item v-else 
                  :key="citem.key" 
                  :models.sync="models" 
                  :remote="remote" 
                  :rules="rules" 
                  :widget="citem"
                  @input-change="onInputChange">
                </genetate-form-item>
              </template>
            </el-col>
          </el-row>
        </template>

        <template v-else-if="item.type == 'blank'" >
          <el-form-item v-if="item.options.hidden==true?false:true" :label="item.name" :prop="item.model" :key="item.key">
            <slot :name="item.model" :model="models"></slot>
          </el-form-item>
        </template>

        <template v-else>
          <genetate-form-item 
            :key="item.key" 
            :models.sync="models" 
            :rules="rules" 
            :widget="item" 
            @input-change="onInputChange"
            :remote="remote">
          </genetate-form-item>
        </template>
        
      </template>
    </el-form>
  </div>
</template>

<script>
import GenetateFormItem from './GenerateFormItem'
import {loadJs} from '../util/index.js'

export default {
  name: 'fm-generate-form',
  components: {
    GenetateFormItem
  },
  props: ['data', 'remote', 'value', 'insite'],
  data () {
    return {
      models: {},
      rules: {}
    }
  },
  created () {
    this.generateModle(this.data.list)
  },
  mounted () {
  },
  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)
          })
          console.log("genList[i].type if 33")
        } else {
          if (this.value && 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.$set(this.models, genList[i].model, genList[i].options.defaultType === 'String' ? '' : (genList[i].options.defaultType === 'Object' ? {} : []))
            } else {
                // if (this.value && (genList[i].type != 'text')&& (genList[i].type != 'divider')) {
                  this.models[genList[i].model] = genList[i].options.defaultValue
                  // console.log("--- genList[i].options.defaultValue")
                  // console.log(genList[i].options.defaultValue)
                // }
            }      
          }
          
          if (this.rules[genList[i].model]) {
            
            this.rules[genList[i].model] = [...this.rules[genList[i].model], ...genList[i].rules.map(item => {
              if (item.pattern) {
                return {...item, pattern: eval(item.pattern)}
              } else {
                return {...item}
              }
            })]
          } else {
            
            this.rules[genList[i].model] = [...genList[i].rules.map(item => {
              if (item.pattern) {
                return {...item, pattern: eval(item.pattern)}
              } else {
                return {...item}
              }
            })]
          }      
        }
      }
    },
    getData () {
      return new Promise((resolve, reject) => {
        this.$refs.generateForm.validate(valid => {
          if (valid) {
            console.log("get this.models")
            console.log(this.models)
            resolve(this.models)
          } else {
            reject(new Error('表单数据校验失败').message)
          }
        })
      })
    },
    reset () {
      this.$refs.generateForm.resetFields()
    },
    onInputChange (value, field) {
      this.$emit('on-change', field, value, this.models)
    },
    refresh () {
      
    }
  },
  watch: {
    data: {
      deep: true,
      handler (val) {
        this.generateModle(val.list)
      }
    },
    value: {
      deep: true,
      handler (val) {
        console.log(JSON.stringify(val))
        this.models = {...this.models, ...val}
      }
    }
  }
}
</script>

<style>

  .form-grid{
      border:  1px solid #333333;
  }
  .form-grid:nth-child(2n+1){
    border-right: 0px;
    /* border-bottom: 1px solid #333333; */
    border-bottom: 0px;
    /* margin-top: 1.0px; */
  }
  
  .form-grid:nth-child(2n+2){
    border-right: 0px;
    border-bottom: 0px;
    /* border-bottom: 1px solid #333333; */
    /* margin-top: 1px; */

  }
  .form-grid:last-child{
    border-right: 1px solid #333333;
  }
  .form-grid .el-input__inner, .form-grid .el-textarea__inner {
      border: 0 !important;
  }

  .form-grid-container:last-child{
     border-bottom: 1px solid #333333;
  }

</style>

<style lang="scss" scoped>
  .form-grid .el-form-item--mini.el-form-item,.form-grid .el-form-item--small.el-form-item {
      margin-bottom: 0px; 
  }
</style>