<template>
  <a-form-model-item
    v-if="lx === 'type'"
    ref="model"
    :rules="rules"
    :label="null"
    prop="empowerType"
  >
    <a-select v-model="model.empowerType" @change="changee">
      <a-select-option v-for="(key, i) in selectListOption" :key="i" :value="key.value">
        {{ key.label }}
      </a-select-option>
    </a-select>
  </a-form-model-item>
  <a-form-model-item
    v-else-if="lx === 'stime'"
    ref="model"
    :rules="rules"
    :label="null"
    prop="startDate"
  >
    <a-date-picker
      :key="modelKey"
      v-model="model.startDate"
      :disabled="disabled"
      :disabled-date="disabledStartDate"
      @change="changest"
    />
  </a-form-model-item>
  <a-form-model-item v-else ref="model" :label="null" :rules="rules" prop="endDate">
    <a-date-picker
      :key="modelKey"
      v-model="model.endDate"
      :disabled="disabled"
      :disabled-date="disabledEndDate"
      @change="changeet"
    />
  </a-form-model-item>
</template>

<script>
import moment from 'moment'

export default {
  name: 'AuditWarrantInput',
  metaInfo: {
    title: 'AuditWarrantInput',
  },
  props: {
    value: {
      type: Object,
      default: null,
    },
    field: {
      type: Object,
      default: () => {},
    },
    index: {
      type: Number,
      default: -1,
    },
    lx: {
      type: String,
      default: '',
    },
    data: {
      type: String,
      default: 'udata',
    },
  },
  data() {
    return {
      selectListOption: [
        // 期间 0 永久 1 近一年 2  近一个月 三个月 六个月
        { label: '期间', value: '0' },
        { label: '永久', value: '1' },
        { label: '近一年', value: '2' },
        { label: '近一个月', value: '3' },
        { label: '近三个月', value: '4' },
        { label: '近六个月', value: '5' },
      ],
      modelKey: 0,
      time: '',
      model: { empowerType: '', startDate: '', endDate: '' },
      disabled:
        this.value.empowerType === '1' ||
        this.value.empowerType === '2' ||
        this.empowerType === '2',
      rules: [{ required: true, trigger: ['change', 'blur'] }],
      empowerType: '',
      startDate: '',
      endDate: '',
    }
  },
  watch: {
    empowerType(val) {
      this.isLastDay()
      this.$emit('change', this.model.empowerType, this.field, this.index, this.lx, this.data)
      this.$refs.model.validate('blur')
    },
    startDate() {
      this.$emit('change', this.model.startDate, this.field, this.index, this.lx, this.data)
      this.$refs.model.validate('blur')
    },
    endDate() {
      this.$emit('change', this.model.endDate, this.field, this.index, this.lx, this.data)
      this.$refs.model.validate('blur')
    },
  },
  created() {
    if (this.value !== null) {
      if (this.lx === 'type') {
        if (this.value.empowerType) {
          this.model.empowerType = this.value.empowerType
        }
      } else if (this.lx === 'stime') {
        if (this.value.startDate) {
          this.model.startDate = this.value.startDate
          if (this.value.empowerType === '0') {
            this.disabled = false
          } else {
            this.disabled = true
          }
        }
      } else if (this.lx === 'etime') {
        if (this.value.endDate) {
          this.model.endDate = this.value.endDate
          if (this.value.empowerType === '0') {
            this.disabled = false
          } else {
            this.disabled = true
          }
        }
      }
    }
  },
  methods: {
    disabledStartDate(startDate) {
      const finishDate = this.time
      if (!startDate || !finishDate) {
        return false
      }
      return startDate.valueOf() > finishDate.valueOf()
    },
    disabledEndDate(finishDate) {
      const startDate = this.time
      if (!finishDate || !startDate) {
        return false
      }
      return startDate.valueOf() >= finishDate.valueOf()
    },
    changee(value) {
      this.empowerType = value
      this.isLastDay()
    },
    changest(value) {
      this.startDate = value
      this.isLastDay()
    },
    changeet(value) {
      this.endDate = value
      this.isLastDay()
    },
    isLastDay() {
      // if (this.empowerType === '2') {
      //   const now = moment()
      //   const end = moment().endOf('year')
      //   this.startDate = now
      //   this.endDate = end
      //   this.model.startDate = now
      //   this.model.endDate = end
      // }
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
</style>
