<script>
import formBuilder from '@/utils/formBuilder'
import moment from "moment/moment";
import {deepCopy} from "@/utils";
export default {
  props: {
    readable: {
      type: Array,
      default: () => [],
      required: true
    },
    writeable: {
      type: Array,
      default: () => [],
      required: true
    },
    originalForm: {
      type: Object,
      default: () => ({}),
      required: true
    },
    subForm: {
      type: Map,
      default: () => new Map()
    },
    device: {
      type: String,
      default: 'desktop',
      required: true
    },
    fields: {
      type: Array,
      default: () => [],
      required: true
    },
    fileList: {
      type: Object,
      default: () => ({}),
      required: true
    },
    scriptContent: String

  },
  data() {
    return {
      form: {},
      file: {
        ggg: []
      },
      showControl: {},
      vanCalendarOption: {
        isShow: false,
        forFormName: "",
        originalObj: ""
      },
      vanTimePickerOption: {
        isShow: false,
        forFormName: "",
        originalObj: ""
      },
      vanPopupOption: {
        isShow: false,
        forFormName: "",
        columns: [],
        originalObj: ""
      }
    }
  },
  methods: {},
  computed: {},
  watch: {
    info(newVal) {
      let keys = newVal.map(i => i.name)
      keys.forEach(key => {
        this.form[key] = ''
      })
    },
    originalForm(newVal) {
      this.form = deepCopy(newVal)
    },
    fileList(newVal) {
      this.file = deepCopy(newVal)
    },
    scriptContent(newVal) {
      if(newVal) {
        try {
          new Function(newVal).bind(this)()
        } catch (err) {
          console.error(err)
        }
      }
    }
  },
  render(h) {
    const authFields = this.fields.map(field => ({
      ...field,
      _readable: this.readable.indexOf(field.id) !== -1,
      _writeable: this.writeable.indexOf(field.id) !== -1
    }))
    return h('div',[
      h('van-form',{
        props: {
          'scroll-to-error': true
        }
      },authFields.map(field => formBuilder.bind(this)(this.device, field, h))),
      //calendar
      authFields.findIndex(i => i.type === 'date') !== -1 ?
        h('van-calendar',{
          ref: `vanCalendar`,
          props: {
            value: this.vanCalendarOption.isShow,
            'min-date': this.$moment().subtract('years',10).toDate(),
            'max-date': this.$moment().add('years',10).toDate(),
          },
          on: {
            input: e => {
              this.$set(this.vanCalendarOption,'isShow',e)
              this.$set(this.vanCalendarOption,'isShow',false)
            },
            confirm: date => {
              if(typeof this.vanCalendarOption.originalObj === 'object') {
                this.$set(this.vanCalendarOption.originalObj,this.vanCalendarOption.forFormName,moment(date).format('YYYY-MM-DD'))
              } else {
                this.$set(this.form,this.vanCalendarOption.forFormName,moment(date).format('YYYY-MM-DD'))
              }
              this.$set(this.vanCalendarOption,'isShow',false)
            }
          }
        }) : '',
      //datetimepicker
      authFields.findIndex(i => i.type === 'datetime') !== -1 ?
        h('van-popup',{
          props: {
            value: this.vanTimePickerOption.isShow,
            position: 'bottom',
          },
          on: {
            input: e => {
              this.$set(this.vanTimePickerOption,'isShow',e)
            },
          }
        },[
          h('van-datetime-picker',{
            props: {
              type: 'datetime'
            },
            on: {
              confirm: time => {
                if(typeof this.vanTimePickerOption.originalObj === 'object') {
                  console.log(this.vanTimePickerOption.originalObj)
                  this.$set(this.vanTimePickerOption.originalObj,this.vanTimePickerOption.forFormName,moment(time).format('YYYY-MM-DD HH:mm:ss'))
                } else {
                  this.$set(this.form,this.vanTimePickerOption.forFormName,moment(time).format('YYYY-MM-DD HH:mm:ss'))
                }
                this.$set(this.vanTimePickerOption,'isShow',false)
              },
              cancel: _ => {
                this.$set(this.vanTimePickerOption,'isShow',false)
              }
            }
          })
        ]) : '',
      //popup
      authFields.findIndex(i => i.type === 'select') !== -1 ?
        h('van-popup',{
          props: {
            value: this.vanPopupOption.isShow,
            position: 'bottom',
          },
          on: {
            input: e => {
              this.$set(this.vanPopupOption,'isShow',e)
            },
          }
        },[
          h('van-picker',{
            props: {
              'show-toolbar': true,
              columns: this.vanPopupOption.columns,
              'value-key': typeof this.vanPopupOption.columns[0] === 'object' ? 'name' : 'yext'
            },
            on: {
              confirm: value => {
                if(typeof this.vanPopupOption.originalObj === 'object') {
                  this.$set(this.vanPopupOption.originalObj,this.vanPopupOption.forFormName,typeof value === 'object' ? value['id'] : value)
                } else {
                  this.$set(this.form,this.vanPopupOption.forFormName,typeof value === 'object' ? value['id'] : value)
                }
                this.$set(this.vanPopupOption,'isShow',false)
              },
              cancel: _ => {
                this.$set(this.vanPopupOption,'isShow',false)
              }
            }
          })
        ]) : '',
    ])
  }
}
</script>

<style scoped lang="scss">
</style>
