<template>
  <div class="components">
    <div class="header flex">
      <img class="close" @click="cancel" src="/img/home/nav/close.png" alt="极度证券">
      <div class="tab" :class="{active: type == 1}" @click="type = 1">
        <!-- 按月 -->
        {{ $t('account.l66') }}
      </div>
      <div class="tab" :class="{active: type == 2}" @click="type = 2">
        <!-- 时间段 -->
        {{ $t('account.l67') }}
      </div>
    </div>
    <van-picker v-if="type == 1" ref="picker" :defaultIndex="defaultIndex" item-height="36px" visible-item-count="3" :columns="columns" />
    <div v-if="type == 2">
      <div class="ul">
        <div class="li" :class="{active: rangeType == 1}" @click="changeRangeType(1)">
          <!-- 近1月 -->
          {{ $t('account.l68').replace('{p}', 1) }}
        </div>
        <div class="li" :class="{active: rangeType == 3}" @click="changeRangeType(3)">
          <!-- 近3月 -->
          {{ $t('account.l68').replace('{p}', 3) }}
        </div>
        <div class="li" :class="{active: rangeType == 6}" @click="changeRangeType(6)">
          <!-- 近6月 -->
          {{ $t('account.l68').replace('{p}', 6) }}
        </div>
        <div class="li" :class="{active: rangeType == 12}" @click="changeRangeType(12)">
          <!-- 近12月 -->
          {{ $t('account.l68').replace('{p}', 12) }}
        </div>
      </div>
      <div class="box flex">
        <div class="input-box" @click="dateShowClick('startDate')">
          <div class="place" v-if="!startDate">
            <!-- 开始时间 -->
            {{ $t('account.l69') }}
          </div>
          <div v-else>{{ startDate }}</div>
        </div>
        <div>—</div>
        <div class="input-box" @click="dateShowClick('endDate')">
          <div class="place" v-if="!endDate">
            <!-- 结束时间 -->
            {{ $t('account.l70') }}
          </div>
          <div v-else>{{ endDate }}</div>
        </div>
      </div>
    </div>
    <div class="btn-box">
      <div class="btn" @click="submit">
        <!-- 完成 -->
        {{ $t('common.l10') }}
      </div>
    </div>
    <van-calendar v-model="dateShow" :default-date="defaultDate" :min-date="minDate" :max-date="maxDate" @confirm="onConfirm" />
  </div>
</template>
<script>
export default {
  data () {
    return {
      type: 1,
      rangeType: 1,
      columns: [],
      startDate: '',
      endDate: '',
      defaultDate: new Date(),
      defaultIndex: 0,
      dateType: 'startDate',
      dateShow: false,
      minDate: new Date(1),
      maxDate: new Date(),
    }
  },
  created () {
    this.changeRangeType(this.rangeType)
    this.getcolumns()
  },
  methods: {
    submit() {
      if (this.type == 1) {
        var val = this.$refs.picker.getValues()
        var y = val[0].text.replace(this.$t('account.l129'), '')
        var m = Number(val[1].text.replace(this.$t('common.l31'), ''))
        if (m < 10) {
          m = ('0' + m)
        }
        this.$emit('timeBack', `${y}-${m}`)
      }
      if (this.type == 2) {
        this.$emit('timeBack', this.startDate, this.endDate)
      }
    },
    dateShowClick(type) {
      this.dateType = type
      this.defaultDate = new Date(this[this.dateType].replace(new RegExp(/-/gm), "/"))
      this.dateShow = true
    },
    changeRangeType(v) {
      this.rangeType = v
      var now = new Date()
      var m = now.getMonth() + 1
      if (m < 10) {
        m = ('0' + m)
      }
      this.endDate = `${now.getFullYear()}-${m}`
      now.setMonth(now.getMonth() - v)
      m = now.getMonth() + 1
      if (m < 10) {
        m = ('0' + m)
      }
      this.startDate = `${now.getFullYear()}-${m}`
    },
    onConfirm(date) {
      this[this.dateType] = this.$parseTime(date, '{y}-{m}-{d}')
      this.dateShow = false
    },
    cancel() {
      this.$emit('cancel')
    },
    getcolumns() {
      var now = new Date()
      var columns = []
      var col = {
        text: now.getFullYear() + this.$t('account.l129'),
        children: []
      }
      var m = now.getMonth() + 1
      for(var i = 1; i<= m;i++) {
        // 月
        col.children.push({
          text: i + this.$t('common.l31')
        })
      }
      columns.push(col)
      for(var i = 0; i< 11;i++) {
        now.setFullYear(now.getFullYear() - 1)
        var col = {
          text: now.getFullYear() + this.$t('account.l129'),
          children: []
        }
        for(var a = 1; a<= 12;a++) {
          col.children.push({
            text: a + this.$t('common.l31')
          })
        }
        columns.unshift(col)
      }
      this.columns = columns
      this.defaultIndex = this.columns.length - 1
    }
  }
}
</script>
<style lang="less" scoped>
.components {
  padding: 27px 17px;
  color: #000;
}
.header {
  position: relative;
  font-size: 16px;
  font-weight: 500;
  margin-bottom: 27px;
  justify-content: center;
  column-gap: 18px;
  .close {
    position: absolute;
    top: 0;
    left: 0;
    width: 15px;
    cursor: pointer;
  }
  .tab {
    cursor: pointer;
    color: #A5A5A5;
    padding-bottom: 11px;
    border-bottom: 2px solid transparent;
    &.active {
      color: var(--color-);
      border-bottom-color: var(--color-);
    }
  }
}
.ul {
  display: flex;
  flex-wrap: wrap;
  column-gap: 8px;
  row-gap: 9px;
  padding-bottom: 17px;
  .li {
    height: 36px;
    // line-height: 36px;
    width: 79px;
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
    border-radius: 2px;
    background-color: #F7F7F7;
    cursor: pointer;
    color: #0D0D0D;
    font-size: 13px;
    &.active {
      background-color: #FFF0EA;
      color: var(--color-);
    }
  }
}
.box {
  column-gap: 4px;
  font-size: 13px;
  color: #8A8A8A;
  .input-box {
    flex: 1;
    min-width: 10%;
    max-width: 50%;
    background-color: #F7F7F7;
    border-radius: 2px;
    line-height: 36px;
    text-align: center;
    color: var(--color-1-);
  }
  .place {
    color: #BBBBBB;
  }
}
.btn-box {
  padding-top: 40px;
  .btn {
    cursor: pointer;
    border-radius: 6px;
    text-align: center;
    line-height: 44px;
    font-size: 16px;
    color: #fff;
    background-color: var(--color-);
  }
}
</style>