<template>
  <v-form ref="form" v-model="valid" class="login-form" @submit.native.prevent>
      <div>
          <v-row dense no-gutters class="justify-space-between">
            <template v-for="(item, index) in items.options">
              <slot :name="item.prevSlot"></slot>
              <v-col :key="item.key" v-if="!item.hide" :cols="item.col || '12'" class="position">
                <template v-if="item.slotTitle">
                  <div :class="item.class" :style="item.slotTitleStyle">{{ item.slotTitle }}</div>
                </template>
                <div :class="item.flexStyle || 'flex-row'" class="px-2 d-flex">
                  <v-text-field
                      v-if="['text', 'password', 'number'].includes(item.type)"
                      :type="item.type"
                      v-model="item.value"
                      :rules="item.rules"
                      :hide-details="item.hideDetails ?? false"
                      :disabled="item.disabled"
                      :dense="item.dense"
                      :style="{width: item.width}"
                      :color="item.color"
                      :label="item.label"
                      :placeholder="item.placeholder || item.label"
                      :outlined="item.outlined"
                      :autofocus="item.autofocus"
                      :required="item.required"
                      :class="item.class"
                      :suffix="item.suffix"
                      :append-icon="item.appendIcon"
                      :clearable="item.clearable"
                      :readonly="item.readonly"
                      :prepend-inner-icon="item.prependInnerIcon"
                      hide-spin-buttons
                      @wheel.native="$event => handleWheel($event, item)"
                      @keyup.enter.native="item.keyupEnterNative && item.keyupEnterNative(index)"
                      @click="item.click && item.click(index)"
                      @click:append="item.clickAppend && item.clickAppend(index)"
                      @change="handleChange(item)"
                      @input="item.onInput && item.onInput(item)"
                  />
                  <v-autocomplete
                      v-if="item.type === 'autocomplete'"
                      :rules="item.rules"
                      v-model="item.value"
                      :attach="!item.noAttach"
                      :loading="item.loading"
                      :label="item.label"
                      :placeholder="item.placeholder || item.label"
                      :items="item.items"
                      :item-text="item.itemText || 'label'"
                      :item-value="item.itemValue || 'value'"
                      :outlined="item.outlined"
                      :dense="item.dense"
                      :disabled="item.disabled"
                      :multiple="item.multiple"
                      :clearable="item.clearable"
                      :search-input.sync="item.searchInput"
                      :readonly="item.readonly"
                      :hide-no-data="item.hideNoData"
                      :hide-details="item.hideDetails ?? false"
                      :no-data-text="item.noDataText || 'No data available'"
                      :hide-selected="item.hideSelected"
                      :return-object="item.returnObject"
                      @change="handleChange(item)"
                  >
                    <template v-if="item.slotAppendItem" v-slot:append-item>
                      <slot :name="item.slotAppendItem" :item="item"></slot>
                    </template>
                    <template v-if="item.prependItem" #prepend-item>
                      <slot :name="item.prependItem" :item="item"></slot>
                    </template>
                  </v-autocomplete>
                  <!-- autocomplete2 多选纸片样式 -->
                  <v-autocomplete
                      v-if="item.type === 'autocomplete2'"
                      v-model="item.value"
                      :rules="item.rules"
                      :attach="!item.noAttach"
                      :loading="item.loading"
                      :label="item.label"
                      :placeholder="item.placeholder || item.label"
                      :items="item.canCreate ? [inputUpdateValue, ...item.items].filter(Boolean) : item.items"
                      :item-text="item.itemText || 'label'"
                      :item-value="item.itemValue || 'value'"
                      :outlined="item.outlined"
                      :dense="item.dense"
                      :multiple="item.multiple"
                      :clearable="item.clearable"
                      :search-input.sync="item.searchInput"
                      :hide-no-data="item.hideNoData"
                      :hide-selected="item.hideSelected"
                      :readonly="item.readonly"
                      @change="handleChange(item)"
                      @update:search-input="$event => item.canCreate ? inputUpdateValue = $event : inputUpdateAutocomplete($event)"
                      :hide-details="item.hideDetails ?? false"
                      deletable-chips
                      cache-items
                      small-chips
                    ></v-autocomplete>
                  <v-combobox
                      v-if="item.type === 'combobox'"
                      :rules="item.rules"
                      v-model="item.value"
                      :attach="true"
                      :label="item.label"
                      :placeholder="item.placeholder || item.label"
                      :items="item.items"
                      :item-text="item.itemText || 'label'"
                      :item-value="item.itemValue || 'value'"
                      :outlined="item.outlined"
                      :dense="item.dense"
                      :clearable="item.clearable"
                      :disabled="item.disabled"
                      @change="handleChange(item)"
                  >
                    <template v-if="item.hasIcon" v-slot:selection="data">
                      <v-icon color="blue darken-2">{{ data.item.label }}</v-icon>
                    </template>
                    <template v-if="item.hasIcon" v-slot:item="data">
                      <v-list-item-avatar>
                        <v-icon>{{ data.item.label }}</v-icon>
                      </v-list-item-avatar>
                      <v-list-item-content>
                        {{ data.item.label }}
                      </v-list-item-content>
                    </template>
                  </v-combobox>
                  <v-textarea
                    v-if="item.type === 'textarea'"
                    :rules="item.rules"
                    v-model="item.value"
                    :label="item.label"
                    :placeholder="item.placeholder || item.label"
                    :no-resize="!item.resize"
                    :outlined="item.outlined"
                    :readonly="item.readonly"
                    :dense="item.dense"
                    :rows="item.rows || 3"
                    :disabled="item.disabled"
                    @change="handleChange(item)"
                  ></v-textarea>
                  <v-radio-group
                    v-if="item.type === 'ifRadio'"
                    v-model="item.value"
                    :disabled="item.disabled"
                    :style="item.selfStyle"
                    :class="item.selfClass"
                    mandatory
                    row
                    @change="handleChange(item)"
                  >
                    <template v-if="item.label" v-slot:label>
                      <div :style="`width: ${item.width || 120}px;`">{{ item.label }}</div>
                    </template>
                    <v-radio
                      v-for="radio in item.items"
                      :key="`${item.key}_radio_${radio.label}`"
                      :readonly="radio.readonly"
                      :label="radio.label"
                      :value="radio.value"
                      class="mr-8"
                    ></v-radio>
                  </v-radio-group>
                  <template v-if="item.type === 'checkbox'">
                    <div style="width: 120px;" class="mt-4 label text-left">{{ item.label }}</div>
                    <div :style="item.style">
                      <v-checkbox
                        v-model="item.value"
                        v-for="k in item.items"
                        :key="k.key"
                        :label="k.label"
                        :color="item.color"
                        :value="k.value"
                        :readonly="k.readonly"
                        hide-details
                        :multiple="true"
                        class="mr-3"
                      ></v-checkbox>
                    </div>
                  </template>
                  <v-file-input
                    v-if="item.type === 'upload'"
                    :prepend-icon="item.prependIcon || ''"
                    :append-icon="item.appendIcon"
                    :append-outer-icon="item.appendOuterIcon"
                    :show-size="item.showSize"
                    :outlined="item.outlined"
                    :dense="item.dense"
                    v-model="item.value"
                    :placeholder="item.placeholder || item.label"
                    :hint="item.hint"
                    :rules="item.rules"
                    :label="item.label"
                    :persistent-hint="item.persistentHint"
                    :loading= "item.loading"
                    :disabled="item.disabled"
                    :multiple="item.multiple"
                    :success="item.success"
                    :error="item.error"
                    :accept="item.accept || '.xlsx, .xls, .csv, .pdf, .txt, .doc'"
                    @change="handleChange(item)"
                  >
                    <template v-if="item.selfAppend" #append>
                      <slot :name="item.selfAppend" :data="item.value"></slot>
                    </template>
                  </v-file-input>
                  <v-color-picker
                    v-if="item.type === 'colorPicker'"
                    class="mb-5"
                    v-model="item.value"
                    :elevation="item.elevation || 5"
                    :dot-size="item.dotSize || 25"
                    :show-swatches="item.showSwatches || false"
                    swatches-max-height="200"
                    :mode="item.mode || 'hexa'"
                    :hide-mode-switch="true"
                    @input="item.change"
                  />
                  <template v-if="item.type === 'switch'">
                    <span v-if="item.describe"> {{ item.describe }} </span>
                    <span class="ml-2" v-if="item.trueLabel"> {{ item.trueLabel }}</span>
                    <v-switch
                      dense hide-details class="mt-0 ml-2 pa-0"
                      v-model="item.value"
                      :label="item.label"
                      :disabled="item.disabled || false"
                      :color="item.color || 'primary'"
                      :true-value="(item.trueValue !== undefined) ? item.trueValue : true"
                      :false-value="(item.falseValue !== undefined) ? item.falseValue : false"
                    ></v-switch>
                    <span v-if="item.falseLabel"> {{ item.falseLabel }} </span>
                  </template>
                  <template v-if="item.type === 'date'">
                    <div class="d-flex" style="margin-bottom: 22px;flex: 1;">
                      <span v-if="item.width !== -1" class="label d-flex align-center" :style="`width: ${item.width || 120}px;`">{{ item.label }}</span>
                      <date-picker
                        :is-valid="isValid"
                        :option="{ ...item.option, disabled: item.disabled }"
                        v-model="item.value"
                        :style="item.style"
                        @change="item.value = $event; handleChange(item); handleCheck(item)"></date-picker>
                    </div>
                  </template>
                  <template v-if="item.type === 'dataPicker'">
                    <m-data-picker
                      v-model="item.value"
                      style="flex: 1"
                      :label="item.label"
                      :placeholder="item.placeholder || item.label"
                      :multiple="item.multiple"
                      :collapse-tags="item.collapseTags"
                      :max-collapse-tags="item.maxCollapseTags"
                      :item-children="item.itemChildren ?? 'children'"
                      :item-label="item.itemLabel ?? 'label'"
                      :item-value="item.itemValue ?? 'value'"
                      :items="item.items"
                      :rules="item.rules"
                      @change="handleChange(item)"
                    ></m-data-picker>
                  </template>
                  <template v-if="item.slotName">
                      <slot :name="item.slotName" :item="item"></slot>
                  </template>
                </div>
              </v-col>
              <slot :name="item.nextSlot"></slot>
            </template>
          </v-row>
      </div>
      <slot></slot>
  </v-form>
</template>

<script>
import DatePicker from '@/components/Form/datePicker.vue'
import MDataPicker from '@/components/MDataPicker'
export default {
  name: 'form-list',
  components: { DatePicker, MDataPicker },
  props: {
    items: {
      type: Object,
      default: () => {},
      required: true
    }
  },
  data () {
    return {
      inputUpdateValue: '',
      valid: false,
      isValid: true
    }
  },
  methods: {
    handleWheel (event, item) {
      if (item.type !== 'number') return
      event.preventDefault()
      if (event.deltaY > 0) {
        item.value--
      } else {
        item.value++
      }
      this.handleChange(item)
    },
    handleCheck (e) {
      if (e.type !== 'date' || e.hide || !e.rules) return
      const rules = e.rules[0]
      const check = rules(e.value)
      if (typeof check === 'string') {
        e.option.error = true
        e.option.errorMsg = check
        return
      }
      e.option.error = false
      e.option.errorMsg = null
    },
    validateTime () {
      this.isValid = true
      this.items.options.forEach((e, index) => {
        if (e.type !== 'date' || e.hide || !e.rules) return
        const rules = e.rules[0]
        const check = rules(e.value)
        if (typeof check === 'string') {
          this.isValid = false
          e.option.error = true
          e.option.errorMsg = check
        } else {
          e.option.error = false
          e.option.errorMsg = null
        }
      })
      return this.isValid
    },
    validate () {
      const form = this.$refs.form.validate()
      const time = this.validateTime()
      return form && time
    },
    inputUpdateAutocomplete (val) {
      this.$emit('inputUpdateAutocomplete', val)
    },
    resetValidation () {
      this.$refs.form.resetValidation()
    },
    reset () {
      this.$refs.form.reset()
    },
    handleChange (item) {
      if (item.type === 'date' && item.value) item.option.validate = false
      if (item?.change) item.change(item.value, item)
      this.$emit('change', false)
    }
  }
}
</script>

<style lang="scss" scoped>
.position {
  position: relative;
}
.label {
  font-size: 14px;
  color: rgba(0, 0, 0, .6);
}
</style>
