const Months = {
  template: `
      <a-select
        :placeholder="placeholder"
        :id="id"
        allowClear
        :value="defaultVal"
        :showSearch="true"
        :mode="mode"
        :disabled="disabled"
        :maxTagCount="2"
        @change="handleChange"
        option-filter-prop="children"
        :filter-option="filterOption">
        <a-select-option v-for="item in monthList" :key="item.val" :value="item.val" :disabled="item.disabled">{{ item.name }}</a-select-option>
      </a-select>
    `,
  props: {
    value: {
      type: [String,Array],
      defatut: ''
    },
    id: {
      type: String,
      default: ''
    },
    mode: {
      type: String,
      default: 'default'
    },
    disabled: {
      type: Boolean,
      default: false
    },
    placeholder:{
      type: String,
      default: '请选择月份'
    }
  },
  data() {
    return {
      defaultVal: this.value,
      monthList: []
    };
  },
  mounted() {
    const year = new Date().getFullYear()
    const curMonth = new Date().getMonth()
    const m = ['一','二','三','四','五','六','七','八','九','十','十一','十二']
    for(let i=1;i<=12;i++){
      this.monthList.push({
        val: year +"-" + (i<10?'0'+i:i),
        name: year + '年' + (i<10?'0'+i:i) + '月',
        disabled: (i-1)>curMonth
      })
    }
  },
  watch: {
    value(newValue, oldValue) {
      this.defaultVal = newValue
    },
  },
  methods: {
    filterOption (input, option) {
      return (
        option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      )
    },
    handleChange(value) {
      this.defaultVal = value;
      const row = this.monthList.find(item => item.val == value)
      this.$emit('input', value);
      this.$emit('change', value, this.id, row);
    },
  },
};

export default Months